Commit 55e5a777 authored by shunbo's avatar shunbo
Browse files

initial commit

parents
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
\*---------------------------------------------------------------------------*/
#include "point.H"
#include "DynamicField.H"
#include "IOstreams.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
DynamicField<point> testField;
DynamicField<point> testField2;
testField.setSize(5);
testField2.setSize(5);
testField[0] = testField2[0] = vector(1.0, 4.5, 6.3);
testField[1] = testField2[1] = vector(5.2, 2.3, 3.5);
testField[2] = testField2[2] = vector(7.5, 4.7, 7.7);
testField[3] = testField2[3] = vector(2.8, 8.2, 2.3);
testField[4] = testField2[4] = vector(6.1, 1.7, 8.8);
Info << "testField:" << testField << endl;
testField.append(vector(0.5, 4.8, 6.2));
Info << "testField after appending:" << testField << endl;
testField.append(vector(2.7, 2.3, 6.1));
Info << "testField after appending:" << testField << endl;
vector elem = testField.remove();
Info << "removed element:" << elem << endl;
Info << "testField:" << testField << endl;
testField.append(vector(3.0, 1.3, 9.2));
Info << "testField:" << testField << endl;
testField.setSize(10, vector(1.5, 0.6, -1.0));
Info << "testField after setSize:" << testField << endl;
testField.append(testField2);
Info << "testField after appending testField2:" << testField << endl;
testField = testField2;
Info << "testField after assignment:" << testField << endl;
testField += testField2;
Info << "testField after field algebra:" << testField << endl;
testField.clear();
testField.append(vector(3.0, 1.3, 9.2));
Info << "testField after clear and append:" << testField << endl;
testField.clearStorage();
Info << "testField after clearStorage:" << testField << endl;
return 0;
}
// ************************************************************************* //
Test-DynamicList.C
EXE = $(FOAM_USER_APPBIN)/Test-DynamicList
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
\*---------------------------------------------------------------------------*/
#include "DynamicList.H"
#include "IOstreams.H"
#include "FlatOutput.H"
#include "ListOps.H"
#include "labelRange.H"
#include "labelIndList.H"
using namespace Foam;
template<class T>
void printInfo
(
const word& tag,
const UList<T>& lst,
const bool showSize = false
)
{
Info<< "<" << tag;
if (showSize)
{
Info<< " size=\"" << lst.size() << "\"";
if (lst.cdata())
{
Info<< " ptr=\"" << name(lst.cdata()) << "\"";
}
else
{
Info<< " ptr=\"nullptr\"";
}
}
Info<< ">" << nl << flatOutput(lst) << nl << "</" << tag << ">" << endl;
}
template<class T, int SizeMin>
void printInfo
(
const word& tag,
const DynamicList<T, SizeMin>& lst,
const bool showSize = false
)
{
Info<< "<" << tag;
if (showSize)
{
Info<< " size=\"" << lst.size()
<< "\" capacity=\"" << lst.capacity() << "\"";
if (lst.cdata())
{
Info<< " ptr=\"" << name(lst.cdata()) << "\"";
}
else
{
Info<< " ptr=\"nullptr\"";
}
}
Info<< ">" << nl << flatOutput(lst) << nl << "</" << tag << ">" << endl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
List<DynamicList<label>> ldl(2);
ldl[0](0) = 0;
ldl[0](2) = 2;
ldl[0](3) = 3;
ldl[0](1) = 1;
ldl[0].setCapacity(5); // increase allocated size
ldl[1].setCapacity(10); // increase allocated size
ldl[0].reserve(15); // should increase allocated size
ldl[1].reserve(5); // should not decrease allocated size
ldl[1](3) = 2; // allocates space and sets value
#ifndef FULLDEBUG
// Accessing an out-of-bounds address, but writing into allocated memory.
// No segfault, doesn't change the list size. Nonetheless not a good idea.
ldl[0][4] = 4;
#endif
ldl[1] = 3;
Info<< "<ldl>" << flatOutput(ldl) << "</ldl>" << nl << "sizes: ";
forAll(ldl, i)
{
Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
}
Info<< endl;
List<List<label>> ll(2);
ll[0].transfer(ldl[0]);
ll[1].transfer(ldl[1].shrink());
Info<< "<ldl>" << flatOutput(ldl) << "</ldl>" << nl << "sizes: ";
forAll(ldl, i)
{
Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
}
Info<< endl;
Info<< "<ll>" << ll << "</ll>" << nl << endl;
// test the transfer between DynamicLists
DynamicList<label> dlA
{
0, 1, 2, 3, 4
};
dlA.append({ 5, 6 });
dlA = { 1, 2, 4 };
DynamicList<label> dlB;
dlA.setCapacity(10);
Info<< "<dlA>" << flatOutput(dlA) << "</dlA>" << nl << "sizes: "
<< " " << dlA.size() << "/" << dlA.capacity() << endl;
dlB.transfer(dlA);
// provokes memory error if previous transfer did not maintain
// the correct allocated space
dlB[6] = 6;
Info<< "Transferred to dlB" << endl;
Info<< "<dlA>" << flatOutput(dlA) << "</dlA>" << nl << "sizes: "
<< " " << dlA.size() << "/" << dlA.capacity() << endl;
Info<< "<dlB>" << flatOutput(dlB) << "</dlB>" << nl << "sizes: "
<< " " << dlB.size() << "/" << dlB.capacity() << endl;
// try with a normal list:
List<label> lstA;
lstA.transfer(dlB);
Info<< "Transferred to normal list" << endl;
printInfo("lstA", lstA, true);
printInfo("dlB", dlB, true);
// Copy back and append a few time
for (label i=0; i < 3; i++)
{
dlB.append(lstA);
}
Info<< "appended list a few times" << endl;
printInfo("dlB", dlB, true);
// assign the list (should maintain allocated space)
dlB = lstA;
Info<< "assigned list" << endl;
printInfo("dlB", dlB, true);
// Copy back and append a few time
for (label i=0; i < 3; i++)
{
dlB.append(lstA);
}
// check allocation granularity
DynamicList<label> dlC;
printInfo("dlC", dlC, true);
dlC.reserve(dlB.size());
dlC = dlB;
printInfo("dlC", dlC, true);
List<label> lstB(std::move(dlC));
Info<< "Move construct to normal list" << endl;
printInfo("lstB", lstB, true);
printInfo("dlC", dlC, true);
DynamicList<label> dlD(std::move(lstB));
Info<< "Transfer construct from normal list" << endl;
printInfo("lstB", lstB, true);
printInfo("dlD", dlD, true);
DynamicList<label,10> dlE1(10);
DynamicList<label> dlE2(dlE1); // construct dissimilar
printInfo("dlE1", dlE1, true);
printInfo("dlE2", dlE2, true);
for (label elemI=0; elemI < 5; ++elemI)
{
dlE1.append(4 - elemI);
dlE2.append(elemI);
}
printInfo("dlE2", dlE2, true);
DynamicList<label> dlE3(dlE2); // construct identical
printInfo("dlE3", dlE3, true);
dlE3 = dlE1; // assign dissimilar
printInfo("dlE3", dlE3, true);
dlE3 = dlE2; // assign identical
printInfo("dlE3", dlE3, true);
DynamicList<label> dlE4(reorder(identity(dlE3.size()), dlE3));
printInfo("dlE4", dlE4, true);
printInfo("dlE3", dlE3, true);
{
DynamicList<label> addr(10);
addr.append(3);
addr.append(1);
addr.append(2);
forAll(dlE2, i)
{
dlE2[i] *= 10;
}
labelUIndList uil
(
dlE2, addr
);
Info<< "use UIndirectList " << uil << " remapped from " << dlE2 << endl;
dlE4 = uil;
printInfo("dlE4", dlE4, true);
}
{
Info<< nl << "Test moving:" << nl;
labelList input1 = identity(15);
labelList input2 = identity(15);
inplaceReverseList(input2);
DynamicList<label> list1(std::move(input1));
DynamicList<label> list2;
Info<< "move construct:" << nl
<< "input: " << flatOutput(input1) << nl
<< "list: " << flatOutput(list1) << endl;
list1 = std::move(input2);
Info<< "move assignment:" << nl
<< "input: " << flatOutput(input2) << nl
<< "list: " << flatOutput(list1) << endl;
list2 = std::move(list1);
Info<< "list in: " << flatOutput(list1) << nl
<< "list out: " << flatOutput(list2) << endl;
input2 = identity(15); // don't need std::move() on temporary object
list2 = std::move(input2);
Info<< "list in: " << flatOutput(input2) << nl
<< "list out: " << flatOutput(list2) << endl;
input1 = identity(15);
input2 = identity(15);
inplaceReverseList(input2);
Info<< "test move-append with "
<< flatOutput(input1) << " and " << flatOutput(input2) << endl;
list2.append(std::move(list1));
list2.append(std::move(input1));
list2.append(std::move(input2));
Info<< "result: " << flatOutput(list2) << nl
<< "inputs: " << flatOutput(list1) << " / "
<< flatOutput(input1) << " / "
<< flatOutput(input2) << nl;
Info<< "test move dissimilar sizing:" << nl;
list1 = list2;
list1.reserve(100);
// DynamicList<label,1000> list3; // (std::move(list1));
DynamicList<label,1000> list3(std::move(list1));
Info<< "orig: " << flatOutput(list1) << nl;
// list3.swap(list1);
// list3 = std::move(list1);
printInfo("input", list1, true);
printInfo("output", list3, true);
input1 = list2;
Info<< nl << "test subset/remove with "
<< flatOutput(input1) << endl;
for
(
const labelRange range :
{
labelRange(-10, 8), // invalid range
labelRange(40, 18), // trailing portion
labelRange(-5, 10), // leading portion
labelRange(10, 8), // mid-portion
labelRange(input1.size()), // everything
}
)
{
list1 = input1;
list2 = input1;
list1.remove(range);
list2.subset(range);
Info<< "input = " << flatOutput(input1) << nl
<< "remove " << range << " = " << flatOutput(list1) << nl
<< "subset " << range << " = " << flatOutput(list2) << nl;
}
}
Info<< "\nEnd\n";
return 0;
}
// ************************************************************************* //
Test-DynamicList2.C
EXE = $(FOAM_USER_APPBIN)/Test-DynamicList2
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Test allocation patterns when reading into an existing list.
\*---------------------------------------------------------------------------*/
#include "DynamicList.H"
#include "DynamicField.H"
#include "IOstreams.H"
#include "ITstream.H"
#include "OTstream.H"
#include "StringStream.H"
#include "FlatOutput.H"
#include "ListOps.H"
#include "labelRange.H"
#include "labelIndList.H"
using namespace Foam;
template<class T, int SizeMin>
void printInfo
(
const word& tag,
const DynamicList<T, SizeMin>& list,
const bool showSize = true
)
{
Info<< '<' << tag;
if (showSize)
{
Info<< " size=\"" << list.size()
<< "\" capacity=\"" << list.capacity() << "\"";
if (list.cdata())
{
Info<< " ptr=\"" << name(list.cdata()) << "\"";
}
else
{
Info<< " ptr=\"nullptr\"";
}
}
Info<< '>' << nl << flatOutput(list) << nl
<< "</" << tag << ">\n" << endl;
}
template<class T, int SizeMin>
void printInfo
(
const word& tag,
const DynamicField<T, SizeMin>& list,
const bool showSize = true
)
{
Info<< '<' << tag;
if (showSize)
{
Info<< " size=\"" << list.size()
<< "\" capacity=\"" << list.capacity() << "\"";
if (list.cdata())
{
Info<< " ptr=\"" << name(list.cdata()) << "\"";
}
else
{
Info<< " ptr=\"nullptr\"";
}
}
Info<< '>' << nl << flatOutput(list) << nl
<< "</" << tag << ">\n" << endl;
}
template<class T, int SizeMin>
void readList
(
DynamicList<T, SizeMin>& output,
const UList<T>& input
)
{
OTstream os;
os << input;
ITstream is("input", os.tokens());
is >> output;
}
template<class T, int SizeMin>
void readList
(
DynamicField<T, SizeMin>& output,
const UList<T>& input
)
{
OTstream os;
os << input;
ITstream is("input", os.tokens());
is >> output;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
//
{
DynamicList<label, 64> list1;
list1.resize(4);
ListOps::identity(list1);
list1.resize(3);
printInfo("", list1);
// list1.clear();
// printInfo("", list1);
list1.setCapacity(3);
printInfo("", list1);
}
Info<< "\nEnd\n";
return 0;
}
// ************************************************************************* //
Test-Enum.C
EXE = $(FOAM_USER_APPBIN)/Test-Enum
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Testing of Enum lookups.
\*---------------------------------------------------------------------------*/
#include "Enum.H"
#include "dictionary.H"
#include "FlatOutput.H"
#include "IOstreams.H" // For 'Sin'
#include <array>
using namespace Foam;
struct testing
{
enum class option { A, B, C, D };
static const Foam::Enum<option> option1Names;
static const Foam::Enum<option> option2Names;
};
// All names
const Foam::Enum<testing::option> testing::option1Names
({
{ testing::option::A, "a" },
{ testing::option::B, "b" },
{ testing::option::C, "c" },
{ testing::option::D, "d" },
});
// Subset of names
const Foam::Enum<testing::option> testing::option2Names
({
{ testing::option::C, "c" },
{ testing::option::D, "d" },
});
// Can use for integers as well, but not scalar etc.
const Foam::Enum<int> otherNames1
({
{ 0, "a" },
{ 2, "b" },
{ 3, "c" },
{ 3, "d" },
});
// Can use for integers as well, but not scalar etc.
Foam::Enum<int> otherNames2
({
{ 0, "a" },
{ 2, "b" },
{ 3, "c" },
{ 3, "asdasd" },
});
std::array<const char*, 2> myarray{ "false", "true" };
// Verify compile-time warnings
// #include "NamedEnum.H"
// const Foam::NamedEnum<testing::option, 2> bad_legacy;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<<"Enum 1" << nl
<< " names: " << testing::option1Names << nl
<< " values: " << flatOutput(testing::option1Names.values())
<< nl << nl;
Info<<"Enum 2" << nl
<< " names: " << testing::option2Names << nl
<< " values: " << flatOutput(testing::option2Names.values())
<< nl << nl;
Info<<"Other Enum" << nl
<< " names: " << otherNames2 << nl
<< " values: " << flatOutput(otherNames2.values())
<< nl << nl;
otherNames2.append
({
{ 15, "fifteen"},
{ 16, "sixteen"}
});
Info<<"Other Enum (appended)" << nl
<< " names: " << otherNames2 << nl
<< " values: " << flatOutput(otherNames2.values())
<< nl << nl;
std::cout
<<"stdout: "<< otherNames2
<< nl << nl;
Info<< "iterate:" << nl;
forAllConstIters(otherNames2, iter)
{
Info<< "key=" << iter.key() << " val=" << iter.val() << nl;
}
for (const word& k : otherNames2)
{
Info<< " " << k << " is " << otherNames2[k] << nl;
}
Info<< nl;
otherNames2.clear();
otherNames2.append
({
{ 1, "one"},
{ 2, "two"}
});
Info<<"After clear and append:" << nl
<< otherNames2 << nl
<< otherNames2.values() << nl
<< nl;
dictionary testDict;
testDict.add("lookup1", "c");
testDict.add("lookup2", "rubbish");
Info<< nl
<< int(testing::option1Names["a"]) << nl
<< testing::option1Names[testing::option::A] << nl;
Info<< "--- test dictionary lookup ---" << endl;
{
Info<< "dict: " << testDict << endl;
Info<< "lookupOrDefault(notFound) = "
<< int
(
testing::option1Names.lookupOrDefault
(
"notFound",
testDict,
testing::option::A
)
)
<< nl;
Info<< "lookupOrDefault(lookup1) = "
<< int
(
testing::option1Names.lookupOrDefault
(
"lookup1",
testDict,
testing::option::A
)
)
<< nl;
Info<< "lookupOrDefault(lookup1) = "
<< int
(
testing::option2Names.lookupOrDefault
(
"lookup1",
testDict,
testing::option::A
)
)
<< nl;
}
Info<< "--- test read ---" << endl;
testing::option dummy(testing::option1Names.read(Sin));
Info<< testing::option1Names[dummy] << endl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-FixedList.C
EXE = $(FOAM_USER_APPBIN)/Test-FixedList
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-FixedList
Description
Simple tests and examples for FixedList
See also
Foam::FixedList
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "FixedList.H"
#include "Fstream.H"
#include "List.H"
#include "IPstream.H"
#include "OPstream.H"
#include <numeric>
using namespace Foam;
template<class T, unsigned N>
Ostream& printInfo(const FixedList<List<T>, N>& list)
{
Info<< list << " addresses:";
for (unsigned i = 0; i < N; ++i)
{
Info<< ' ' << name(list[i].cdata());
}
Info<< nl;
return Info;
}
template<class T, unsigned N>
Ostream& printInfo
(
const FixedList<List<T>, N>& list1,
const FixedList<List<T>, N>& list2
)
{
Info<< "llist1:"; printInfo(list1);
Info<< "llist2:"; printInfo(list2);
return Info;
}
template<class T, unsigned N>
void compileInfo()
{
// Info<< typeid(decltype(FixedList<T, N>)).name() << nl;
// Info<< " holds: "
// << typeid(decltype(FixedList<T, N>::value_type())).name() << nl;
Info<< "max_size:"
<< FixedList<T, N>::max_size() << nl;
}
template<class FixedListType>
typename std::enable_if
<(FixedListType::max_size() == 2), bool>::type
is_pair()
{
return true;
}
template<class FixedListType>
typename std::enable_if<(FixedListType::max_size() != 2), std::string>::type
is_pair()
{
return "not really at all";
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noCheckProcessorDirectories();
argList::addBoolOption("assign");
argList::addBoolOption("iter");
argList::addBoolOption("swap");
argList::addBoolOption("default", "reinstate default tests");
argList::addNote("runs default tests or specified ones only");
#include "setRootCase.H"
// Run default tests, unless only specific tests are requested
const bool defaultTests =
args.found("default") || args.options().empty();
typedef FixedList<scalar,2> scalar2Type;
typedef FixedList<label,3> label3Type;
// Compile-time info
compileInfo<label, 5>();
Info<< "pair: " << is_pair<scalar2Type>() << nl;
Info<< "pair: " << is_pair<label3Type>() << nl;
Info<< "max_size:" << scalar2Type::max_size() << nl;
if (defaultTests || args.found("iter"))
{
Info<< nl
<< "Test iterators" << nl;
FixedList<label, 15> ident;
std::iota(ident.begin(), ident.end(), 0);
// auto iter = ident.begin();
//
// iter += 5;
// Info << *iter << "< " << nl;
// iter -= 2;
// Info << *iter << "< " << nl;
// Don't yet bother with making reverse iterators random access
// auto riter = ident.crbegin();
// riter += 5;
// Info << *riter << "< " << nl;
// riter += 2;
// Info << *riter << "< " << nl;
Info<<"Ident:";
forAllConstIters(ident, iter)
{
Info<<" " << *iter;
}
Info<< nl;
Info<<"reverse:";
forAllReverseIters(ident, iter)
{
Info<<" " << *iter;
}
Info<< nl;
Info<<"const reverse:";
forAllConstReverseIters(ident, iter)
{
Info<<" " << *iter;
}
Info<< nl;
}
if (defaultTests || args.found("swap"))
{
Info<< nl
<< "Test swap" << nl;
FixedList<label, 4> list1{2, 3, 4, 5};
Info<< "list1:" << list1
<< " hash:" << FixedList<label, 4>::hasher()(list1) << nl
<< " hash:" << Hash<FixedList<label, 4>>()(list1) << nl;
label a[4] = {0, 1, 2, 3};
FixedList<label, 4> list2(a);
Info<< "list2:" << list2
<< " hash:" << FixedList<label, 4>::hasher()(list2) << nl
<< " hash:" << Hash<FixedList<label, 4>>()(list2) << nl;
// Using FixedList for content too
{
List<FixedList<label, 4>> twolists{list1, list2};
Info<<"List of FixedList: " << flatOutput(twolists) << nl;
sort(twolists);
// outer-sort only
Info<<"sorted FixedList : " << flatOutput(twolists) << nl;
}
Info<< "====" << nl
<< "Test swap" << nl;
Info<< "list1: " << list1 << nl
<< "list2: " << list2 << nl;
// Addresses don't change with swap
Info<< "mem: "
<< name(list1.data()) << " " << name(list2.data()) << nl;
list1.swap(list2);
Info<< "The swap() method" << nl;
Info<< "list1: " << list1 << nl
<< "list2: " << list2 << nl;
Info<< "mem: "
<< name(list1.data()) << " " << name(list2.data()) << nl;
Swap(list1, list2);
Info<< "The Swap() function" << nl;
Info<< "list1: " << list1 << nl
<< "list2: " << list2 << nl;
Info<< "mem: "
<< name(list1.data()) << " " << name(list2.data()) << nl;
Info<< "====" << nl;
Info<< nl
<< "Test of swap with other container content" << nl;
FixedList<labelList, 4> llist1;
FixedList<labelList, 4> llist2;
{
label i = 1;
for (auto& item : llist1)
{
item = identity(1 + 1.5*i);
++i;
}
}
Info<< nl
<< "initial lists" << nl;
printInfo(llist1, llist2);
llist2.transfer(llist1);
Info<< nl
<< "After transfer" << nl;
printInfo(llist1, llist2);
llist2.swap(llist1);
Info<< nl
<< "After swap" << nl;
printInfo(llist1, llist2);
llist2 = llist1;
Info<< nl
<< "After copy assignment" << nl;
printInfo(llist1, llist2);
llist2 = std::move(llist1);
Info<< nl
<< "After move assignment" << nl;
printInfo(llist1, llist2);
}
Info<< nl
<< "Test construct and assignment" << nl;
List<label> list3{0, 1, 2, 3};
FixedList<label, 4> list4(list3);
Info<< "list3: " << list3 << nl
<< "list4: " << list4 << nl;
list4 = {1, 20, 3, 40};
Info<< "list4: " << list4 << nl;
FixedList<label, 5> list5{0, 1, 2, 3, 4};
Info<< "list5: " << list5 << nl;
{
const FixedList<label, 2> indices({3, 1});
FixedList<label, 2> list4b(list4, indices);
Info<< "subset " << list4 << " with " << indices << " -> "
<< list4b << nl;
}
List<FixedList<label, 2>> list6{{0, 1}, {2, 3}};
Info<< "list6: " << list6 << nl;
if (Pstream::parRun())
{
if (Pstream::master())
{
for (const int proci : Pstream::subProcs())
{
IPstream fromSlave(Pstream::commsTypes::blocking, proci);
FixedList<label, 2> list3(fromSlave);
Serr<< "Receiving from " << proci
<< " : " << list3 << endl;
}
}
else
{
Perr<< "Sending to master" << endl;
OPstream toMaster
(
Pstream::commsTypes::blocking,
Pstream::masterNo()
);
FixedList<label, 2> list3;
list3[0] = 0;
list3[1] = Pstream::myProcNo();
toMaster << list3;
}
}
return 0;
}
// ************************************************************************* //
Test-FixedList2.C
EXE = $(FOAM_USER_APPBIN)/Test-FixedList2
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-FixedList2
Description
Test speeds, usability of some List/FixedList operations
See also
Foam::FixedList
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "FixedList.H"
#include "labelList.H"
#include "vectorList.H"
#include "ListOps.H"
#include "IFstream.H"
#include "OFstream.H"
#include "cpuTime.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
using namespace Foam;
template<class ListType>
void runSwapTest
(
const label nLoops,
ListType& list1,
ListType& list2
)
{
cpuTime timer;
Info<<"Swapping fixed lists with " << list1.size() << " elements\n";
Info<< "input 1: " << list1.first() << nl;
Info<< "input 2: " << list2.first() << nl;
// Should be zero, since this is a compile-time value
Info<< "Perform " << nLoops << " swaps..." << nl;
for (label iLoop = 0; iLoop < nLoops; ++iLoop)
{
Swap(list1, list2);
}
Info<< "output 1: " << list1.first() << nl;
Info<< "output 2: " << list2.first() << nl;
Info<< "Operation took"
<< " " << timer.cpuTimeIncrement() << " s\n\n";
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::addBoolOption("label");
argList::addBoolOption("float");
argList::addBoolOption("vector");
argList::addBoolOption("labelList");
argList::addBoolOption("vectorList");
argList::addBoolOption("fixedLabel");
argList::addBoolOption("fixedLabelList");
argList args(argc, argv);
if (args.options().empty())
{
Info<< nl << "Specify an option! " << nl << endl;
}
if (args.found("label"))
{
FixedList<label, 100000> list1(1);
FixedList<label, 100000> list2(0);
runSwapTest(1000001, list1, list2);
}
if (args.found("float"))
{
FixedList<double, 100000> list1(1.0);
FixedList<double, 100000> list2(0.0);
runSwapTest(1000001, list1, list2);
}
if (args.found("vector"))
{
FixedList<vector, 100000> list1(vector::one);
FixedList<vector, 100000> list2(vector::zero);
runSwapTest(100001, list1, list2);
}
if (args.found("labelList"))
{
typedef labelList testType;
testType initVal(500);
initVal = 0;
FixedList<testType, 1000> list1(initVal);
initVal = 1;
FixedList<testType, 1000> list2(initVal);
runSwapTest(100001, list1, list2);
}
if (args.found("vectorList"))
{
typedef vectorList testType;
testType initVal(500);
initVal = vector::zero;
FixedList<testType, 1000> list1(initVal);
initVal = vector::one;
FixedList<testType, 1000> list2(initVal);
runSwapTest(100001, list1, list2);
}
if (args.found("fixedLabel"))
{
typedef FixedList<label,1000> testType;
testType initVal;
initVal = 0;
FixedList<testType, 1000> list1(initVal);
initVal = 1;
FixedList<testType, 1000> list2(initVal);
runSwapTest(100001, list1, list2);
}
if (args.found("fixedLabelList"))
{
typedef labelList testType;
typedef FixedList<testType,10> containerType;
testType tinitVal(500);
containerType initVal;
tinitVal = 0;
initVal = tinitVal;
FixedList<containerType, 1000> list1(initVal);
tinitVal = 1;
initVal = tinitVal;
FixedList<containerType, 1000> list2(initVal);
runSwapTest(10001, list1, list2);
}
Info<< nl << "Done" << nl << endl;
return 0;
}
// ************************************************************************* //
Test-Function1.C
EXE = $(FOAM_USER_APPBIN)/Test-Function1
EXE_INC = \
-DFULLDEBUG -g \
-I$(LIB_SRC)/meshTools/lnInclude
EXE_LIBS = \
-lmeshTools
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-Function1
Description
Tests Function1
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOstreams.H"
#include "Function1.H"
#include "scalarIndList.H"
#include "scalarField.H"
#include "IOdictionary.H"
#include "linearInterpolationWeights.H"
#include "splineInterpolationWeights.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::noParallel();
const word dictName("function1Properties");
argList::addBoolOption("all", "Test all functions in function1Properties");
argList::addArgument("function1");
argList::addArgument("...");
argList::addArgument("functionN");
argList::noMandatoryArgs();
#include "setRootCase.H"
#include "createTime.H"
{
scalarField samples({0, 1, 2, 3});
scalarField values(4, scalar(1));
linearInterpolationWeights interpolator
//splineInterpolationWeights interpolator
(
samples
);
labelList indices;
scalarField weights;
interpolator.integrationWeights(1.1, 1.2, indices, weights);
Pout<< "indices:" << indices << nl
<< "weights:" << weights << nl;
scalar baseSum = interpolator.weightedSum
(
weights,
scalarUIndList(values, indices)
);
Pout<< "baseSum=" << baseSum << nl << nl << endl;
// interpolator.integrationWeights(-0.01, 0, indices, weights);
// scalar partialSum = interpolator.weightedSum
// (
// weights,
// scalarUIndList(values, indices)
// );
// Pout<< "partialSum=" << partialSum << nl << nl << endl;
// interpolator.integrationWeights(-0.01, 1, indices, weights);
// //Pout<< "samples:" << samples << endl;
// //Pout<< "indices:" << indices << endl;
// //Pout<< "weights:" << weights << endl;
// scalar sum = interpolator.weightedSum
// (
// weights,
// scalarUIndList(values, indices)
// );
// Pout<< "integrand=" << sum << nl << nl << endl;
}
if (args.found("all") || args.size() > 1)
{
#include "setConstantRunTimeDictionaryIO.H"
IOdictionary propsDict(dictIO);
const scalarField xvals(propsDict.lookup("x"));
Info<< "Entries" << flatOutput(propsDict.toc()) << nl << nl;
Info<< "Inputs" << nl
<< " x = " << xvals << nl
<< endl;
DynamicList<word> functionNames;
auto nameFilter = [](const word& val)
{
return !(val == "x" || val.ends_with("Coeffs"));
};
if (args.found("all"))
{
for (const word& f : propsDict.toc())
{
if (nameFilter(f))
{
functionNames.append(f);
}
}
}
else
{
for (label argi=1; argi < args.size(); ++argi)
{
functionNames.append(args[argi]);
}
}
for (const word& funName : functionNames)
{
auto function1 = Function1<scalar>::New(funName, propsDict);
// Info<< "Data entry type: " << function1().type() << nl;
Info<< "////" << nl;
function1().writeData(Info);
Info<< nl;
Info<< "Values" << nl;
for (const scalar& x : xvals)
{
Info<< " f(" << x << ") = " << function1().value(x) << nl;
}
Info<< endl;
}
}
return 0;
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: v2112 |
| \\ / A nd | Website: www.openfoam.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
location "constant";
object function1Properties;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// The 'x' values for evaluation
x
(
0
0.25
0.5
0.75
1
);
constant1 constant 100;
table1 table
(
(0 0)(10 1)
);
table2
{
type table;
values
(
(0 0)(10 1)
);
}
table3
{
type table;
file "<constant>/table-values";
}
poly1 polynomial
(
(0 1)
(1 1)
);
poly2
{
type polynomial;
coeffs
(
(0 1)
(1 1)
);
}
function2
{
type expression;
expression #{ sqr(arg()) #};
}
function2b expression;
function2bCoeffs
{
type expression;
expression #{ sqr(arg()) #};
}
stepf1
{
type step;
start 0.24;
duration 0.5;
}
rampf1
{
type linearRamp;
start 0.24;
duration 0.5;
}
sine1
{
type sine;
frequency 0.1;
scale 1;
level 0;
}
sine2
{
type sine;
period 10;
scale 1;
level 0;
}
cosine1
{
type cosine;
period 10;
scale 1;
level 0;
}
sine6
{
type sine;
period 6;
t0 -1.5; // want cos
scale 1;
level 0;
}
cosine6
{
type cosine;
period 6;
scale 1;
level 0;
}
expr1
{
type expression;
functions<scalar>
{
func1 constant 21;
func2 constant 15;
sin constant -5;
}
functions<vector>
{
vfunc3 constant (1 2 3);
vfunc4 constant (3 2 1);
}
expression
#{
100 * fn:vfunc3() & fn:vfunc4() * (vector::z) .z()
* (fn:sin(arg()) + fn:func1(fn:func2()))
#};
}
expr2
{
type expression;
functions<scalar>
{
func1 constant 21;
func2 constant 15;
sin constant -5;
}
functions<vector>
{
vfunc3 constant (1 2 3);
vfunc4 constant (3 2 1);
}
expression
#{
(fn:vfunc3() & vector::z) * arg()
#};
}
// ************************************************************************* //
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment