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

initial commit

parents
Test-ListOps2.C
EXE = $(FOAM_USER_APPBIN)/Test-ListOps2
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-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/>.
Application
Test-ListOps2
Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "List.H"
#include "FixedList.H"
#include "DynamicList.H"
#include "SubList.H"
#include "ListOps.H"
#include "FlatOutput.H"
#include "UPtrList.H"
using namespace Foam;
// Proof-of-concept for sorted HashTable output
// .. but yet not really convincing
// Forward declarations
template<class T, class Key, class Hash> class HashSorter;
template<class T, class Key, class Hash>
Ostream& operator<<
(
Ostream& os,
const HashSorter<T, Key, Hash>& sorter
);
template<class T, class Key, class Hash=Foam::Hash<Key>>
class HashSorter
{
const HashTable<T, Key, Hash>& table;
public:
HashSorter(const HashTable<T, Key, Hash>& ht)
:
table(ht)
{}
friend Ostream& operator<<
(
Ostream& os,
const HashSorter<T, Key, Hash>& sorter
)
{
const auto& tbl = sorter.table;
const label len = tbl.size();
// Should actually be able to get the flat entries or iterators
// and sort that instead.
UPtrList<const Key> keys(len);
label count = 0;
for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
{
keys.set(count, &(iter.key()));
++count;
}
labelList order(identity(len));
std::sort
(
order.begin(),
order.end(),
ListOps::less<UPtrList<const Key>>(keys)
);
// Size and start list delimiter
os << nl << len << nl << token::BEGIN_LIST << nl;
// Contents
for (const label idx : order)
{
const auto& k = keys[idx];
os << k << token::SPACE << tbl[k] << nl;
}
os << token::END_LIST; // End list delimiter
return os;
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class ListType>
void testFind(const ListType& list)
{
Info<< nl << "list: " << flatOutput(list) << nl << endl;
for (auto val : { 20, 35, 6, 13, 12, -12})
{
Info<< "lookup " << val
<< " found " << list.found(val)
<< " index " << list.find(val) << nl;
}
}
template<class ListType>
void testMoving(ListType& list)
{
Info<< nl << "list: " << flatOutput(list) << nl << endl;
{
const label i = 3;
list.swapFirst(i);
Info<<"swapFirst: " << i << " = " << flatOutput(list) << nl;
}
{
const label i = 6;
list.moveFirst(i);
Info<<"moveFirst: " << i << " = " << flatOutput(list) << nl;
}
{
const label i = 6;
list.moveLast(i);
Info<<"moveLast: " << i << " = " << flatOutput(list) << nl;
}
{
const label i = 8;
list.swapLast(i);
Info<<"swapLast: " << i << " = " << flatOutput(list) << nl;
}
}
// Main program:
int main(int argc, char *argv[])
{
List<label> list1(identity(15));
shuffle(list1);
FixedList<label, 15> list2(list1);
inplaceReverseList(list2);
DynamicList<label> list3(list1);
inplaceReverseList(list3);
testFind(list1);
testFind(list2);
testFind(list3);
testMoving(list1);
testMoving(list2);
testMoving(list3);
// Test remove
{
auto& list = list3;
Info<< nl << "list: " << flatOutput(list) << nl << endl;
list.remove();
Info<<"remove = " << flatOutput(list) << nl;
{
const label i = 6;
list.remove(i);
Info<<"rmSwap: " << i << " = " << flatOutput(list) << nl;
}
{
const label i = 3;
list.remove(i);
Info<<"rmSwap: " << i << " = " << flatOutput(list) << nl;
}
{
const label i = 8;
list.remove(i);
Info<<"rmMove: " << i << " = " << flatOutput(list) << nl;
}
}
// Test remapping
{
Info<< nl << "Test inplaceMapValue" << nl << nl;
HashTable<label> input;
typedef HashSorter<label, label> Mapper;
typedef HashSorter<label, word> Sorter;
for (label i=0; i < 10; ++i)
{
input.insert(word::printf("word%d", i), i);
}
Map<label> mapper;
{
// A mapping that does some, but not all values
labelList rndList(identity(16)); // larger range
shuffle(rndList);
for (label i=0; i < 8; ++i) // smaller sample
{
mapper.insert(rndList[i], 100*i);
}
}
Info<< nl
<< "input: " << Sorter(input) << nl
<< "mapper: " << Mapper(mapper) << nl << nl;
inplaceMapValue(mapper, input);
Info<< nl << "output: " << Sorter(input) << nl;
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-Map.C
EXE = $(FOAM_USER_APPBIN)/Test-Map
/* 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 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/>.
Application
Test-Map
Description
\*---------------------------------------------------------------------------*/
#include "Map.H"
#include <map>
#include "IOstreams.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
Map<bool> map1
{
{1, true}, {2, false}, {3, true}, {4, false}, {5, true}
};
// Taking a const iterator from find does not work!
// Also, fails later on op==
Map<bool>::const_iterator map1Iter = map1.cfind(5);
// Same, but with non-const access
// Map<bool>::iterator map1Iter = map1.find(5);
if (!map1Iter.found()) // same as (map1Iter == map1.end())
{
Info<< "not found" << endl;
}
else
{
Info<< "5 is " << *map1Iter << endl;
}
// Repeat with std::map
Info<< "Same with STL" << endl;
std::map<label, bool> stdmap1
{
{1, true}, {2, false}, {3, true}, {4, false}, {5, true}
};
std::map<label, bool>::const_iterator stdmap1Iter = stdmap1.find(5);
if (stdmap1Iter == stdmap1.cend())
{
Info<< "not found" << endl;
}
else
{
Info<< "5 is " << stdmap1Iter->second << endl;
}
Info<<"test move construct" << nl;
Map<bool> map2(std::move(map1));
Map<bool> map3;
std::map<label, bool> stdmap2(std::move(stdmap1));
std::map<label, bool> stdmap3;
Info<<"map1: " << map1 << nl
<<"map2: " << map2 << nl;
Info
<<"stdmap1: " << stdmap1.size() << nl
<<"stdmap2: " << stdmap2.size() << nl;
Info<<"test move assign" << nl;
map3 = std::move(map2);
stdmap3 = std::move(stdmap2);
Info<<"map2: " << map2 << nl
<<"map3: " << map3 << nl;
Info
<<"stdmap2: " << stdmap2.size() << nl
<<"stdmap3: " << stdmap3.size() << nl;
Info<< nl << "End\n" << endl;
return 0;
}
// ************************************************************************* //
Test-MathFunctions.C
EXE = $(FOAM_USER_APPBIN)/Test-MathFunctions
/*---------------------------------------------------------------------------*\
========= |
\\ / 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/>.
Application
Test-MathFunctions
Description
Tests for \c Math namespace member functions
using \c doubleScalar base type.
\*---------------------------------------------------------------------------*/
#include "MathFunctions.H"
#include "mathematicalConstants.H"
#include "IOmanip.H"
#include "TestTools.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Execute each member functions of Foam::Math::, and print output
template<class Type>
void test_member_funcs(Type)
{
Random rndGen(1234);
const label numberOfTests = 10000;
const scalar tolerance = 1e-3;
for (label i = 0; i < numberOfTests; ++i)
{
Info<< "# Inverse error functions:" << endl;
const Type x1 = rndGen.sample01<Type>();
const Type x2 = rndGen.sample01<Type>();
Info<< " # Operands:" << nl
<< " x1 = " << x1 << nl
<< " x2 = " << x2 << endl;
cmp
(
" # erf(erfinv(x1)) = x1 = ",
x1,
Foam::erf(Foam::Math::erfInv(x1)),
tolerance
);
cmp
(
" # erf(erfinv(-x2)) = -x2 = ",
x2,
Foam::erf(Foam::Math::erfInv(x2)),
tolerance
);
Info<< "# Incomplete gamma functions:" << endl;
const Type a = rndGen.position(SMALL, Type(100));
const Type x = rndGen.position(Type(0), Type(100));
Info<< " # Operands:" << nl
<< " a = " << a << nl
<< " x = " << x << endl;
cmp
(
" # incGammaRatio_Q(a,x) + incGammaRatio_P(a,x) = 1 = ",
Foam::Math::incGammaRatio_Q(a,x) + Foam::Math::incGammaRatio_P(a,x),
scalar(1),
tolerance
);
cmp
(
" # incGamma_Q(1, x) = exp(-x) = ",
Foam::Math::incGamma_Q(1, x),
Foam::exp(-x),
tolerance
);
cmp
(
" # incGamma_Q(0.5, x) = sqrt(pi)*erfc(sqrt(x)) = ",
Foam::Math::incGamma_Q(0.5, x),
Foam::sqrt(Foam::constant::mathematical::pi)
*Foam::erfc(Foam::sqrt(x)),
tolerance
);
cmp
(
" # incGamma_P(1,x) = 1 - exp(x) = ",
Foam::Math::incGamma_P(1, x),
1 - Foam::exp(-x),
tolerance
);
cmp
(
" # incGamma_P(0.5, x) = sqrt(pi)*erf(sqrt(x)) = ",
Foam::Math::incGamma_P(0.5, x),
Foam::sqrt(Foam::constant::mathematical::pi)
*Foam::erf(Foam::sqrt(x)),
tolerance
);
}
}
// Do compile-time recursion over the given types
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type
run_tests(const std::tuple<Tp...>& types, const List<word>& typeID){}
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), void>::type
run_tests(const std::tuple<Tp...>& types, const List<word>& typeID)
{
Info<< nl << " ## Test member functions: "<< typeID[I] <<" ##" << nl;
test_member_funcs(std::get<I>(types));
run_tests<I + 1, Tp...>(types, typeID);
}
// * * * * * * * * * * * * * * * Main Program * * * * * * * * * * * * * * * //
int main()
{
Info<< setprecision(15);
const std::tuple<doubleScalar> types
(
std::make_tuple(Zero)
);
const List<word> typeID
({
"doubleScalar"
});
run_tests(types, typeID);
if (nFail_)
{
Info<< nl << " #### "
<< "Failed in " << nFail_ << " tests "
<< "out of total " << nTest_ << " tests "
<< "####\n" << endl;
return 1;
}
Info<< nl << " #### Passed all " << nTest_ <<" tests ####\n" << endl;
return 0;
}
// ************************************************************************* //
Test-NamedEnum.C
EXE = $(FOAM_USER_APPBIN)/Test-NamedEnum
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
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/>.
Description
Testing of NamedEnum.
The class is deprecated, but we may still need to support it for things
like swak4Foam etc.
\*---------------------------------------------------------------------------*/
#include "NamedEnum.H"
#include "FlatOutput.H"
// Expect compile-time warnings
using namespace Foam;
struct testing
{
enum class option { A, B, C, D };
static const Foam::NamedEnum<option, 4> option1Names;
};
// All names - we have no choice with NamedEnum
template<>
const char* Foam::NamedEnum<testing::option, 4>::names[] =
{
"a",
"b",
"c",
"d"
};
const Foam::NamedEnum<testing::option, 4> testing::option1Names;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "Enum 1" << nl
<< " names: " << testing::option1Names << nl
<< " values: " << flatOutput(testing::option1Names.values())
<< nl << nl;
Info<< nl
<< int(testing::option1Names["a"]) << nl
<< testing::option1Names[testing::option::A] << nl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-OCountStream.C
EXE = $(FOAM_USER_APPBIN)/Test-OCountStream
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
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
Test null and counting output streams
\*---------------------------------------------------------------------------*/
#include "OCountStream.H"
#include "StringStream.H"
#include "Fstream.H"
#include "IOstreams.H"
#include "argList.H"
using namespace Foam;
template<class OS>
void generateOutput(OS& os)
{
for (label i = 0; i < 50; ++i)
{
os << 1002 << " " << "abcd" << " "
<< "def" << " " << 3.14159 << ";\n";
}
}
void printInfo(OSstream& os)
{
Info<< "name: " << os.name() << " : " << os.stdStream().tellp() << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::addOption("write", "file", "test writing to file");
#include "setRootCase.H"
OCountStream cnt;
OStringStream str;
ocountstream plain;
generateOutput(str);
generateOutput(cnt);
generateOutput(plain);
cnt.print(Info);
Info<< "counter state: " << (cnt.stdStream().rdstate()) << nl
<< "via string-stream: " << str.str().size() << " chars" << nl
<< "via ocountstream: " << plain.size() << " chars" << endl;
fileName outputName;
args.readIfPresent("write", outputName);
if (outputName.size())
{
IOstreamOption streamOpt;
if (outputName.hasExt("gz"))
{
outputName.removeExt();
streamOpt.compression(IOstreamOption::COMPRESSED);
}
OFstream os1(outputName, streamOpt);
OFstream os2(nullptr); // A /dev/null equivalent
OFstream os3("/dev/null");
// Doubled output
generateOutput(os1); generateOutput(os1);
generateOutput(os2); generateOutput(os2);
generateOutput(os3); generateOutput(os3);
Info<< nl
<< "doubled output" << nl;
printInfo(os1);
printInfo(os2);
printInfo(os3);
// Rewind and test single output
os1.rewind(); generateOutput(os1);
os2.rewind(); generateOutput(os2);
os3.rewind(); generateOutput(os3);
Info<< nl
<< "single output" << nl;
printInfo(os1);
printInfo(os2);
printInfo(os3);
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-ODE.C
EXE = $(FOAM_USER_APPBIN)/Test-ODE
EXE_INC = /* -DFoam_no_besselFunc */ -I$(LIB_SRC)/ODE/lnInclude
EXE_LIBS = -lODE
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 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 "argList.H"
#include "IOmanip.H"
#include "ODESystem.H"
#include "ODESolver.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
class testODE
:
public ODESystem
{
public:
testODE()
{}
label nEqns() const
{
return 4;
}
void derivatives
(
const scalar x,
const scalarField& y,
scalarField& dydx
) const
{
dydx[0] = -y[1];
dydx[1] = y[0] - (1.0/x)*y[1];
dydx[2] = y[1] - (2.0/x)*y[2];
dydx[3] = y[2] - (3.0/x)*y[3];
}
void jacobian
(
const scalar x,
const scalarField& y,
scalarField& dfdx,
scalarSquareMatrix& dfdy
) const
{
dfdx[0] = 0.0;
dfdx[1] = (1.0/sqr(x))*y[1];
dfdx[2] = (2.0/sqr(x))*y[2];
dfdx[3] = (3.0/sqr(x))*y[3];
dfdy(0, 0) = 0.0;
dfdy(0, 1) = -1.0;
dfdy(0, 2) = 0.0;
dfdy(0, 3) = 0.0;
dfdy(1, 0) = 1.0;
dfdy(1, 1) = -1.0/x;
dfdy(1, 2) = 0.0;
dfdy(1, 3) = 0.0;
dfdy(2, 0) = 0.0;
dfdy(2, 1) = 1.0;
dfdy(2, 2) = -2.0/x;
dfdy(2, 3) = 0.0;
dfdy(3, 0) = 0.0;
dfdy(3, 1) = 0.0;
dfdy(3, 2) = 1.0;
dfdy(3, 3) = -3.0/x;
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::addArgument("ODESolver");
argList args(argc, argv);
// Create the ODE system
testODE ode;
dictionary dict;
dict.add("solver", args[1]);
// Create the selected ODE system solver
autoPtr<ODESolver> odeSolver = ODESolver::New(ode, dict);
// Initialise the ODE system fields
scalar xStart = 1.0;
scalarField yStart(ode.nEqns());
yStart[0] = ::Foam::j0(xStart);
yStart[1] = ::Foam::j1(xStart);
yStart[2] = ::Foam::jn(2, xStart);
yStart[3] = ::Foam::jn(3, xStart);
// Print the evolution of the solution and the time-step
scalarField dyStart(ode.nEqns());
ode.derivatives(xStart, yStart, dyStart);
Info<< setw(10) << "relTol" << setw(12) << "dxEst";
Info<< setw(13) << "dxDid" << setw(14) << "dxNext" << endl;
Info<< setprecision(6);
for (label i=0; i<15; i++)
{
scalar relTol = ::Foam::exp(-scalar(i + 1));
scalar x = xStart;
scalarField y(yStart);
scalar dxEst = 0.6;
scalar dxNext = dxEst;
odeSolver->relTol() = relTol;
odeSolver->solve(x, y, dxNext);
Info<< scientific << setw(13) << relTol;
Info<< fixed << setw(11) << dxEst;
Info<< setw(13) << x - xStart << setw(13) << dxNext
<< setw(13) << y[0] << setw(13) << y[1]
<< setw(13) << y[2] << setw(13) << y[3]
<< endl;
}
scalar x = xStart;
scalar xEnd = x + 1.0;
scalarField y(yStart);
scalarField yEnd(ode.nEqns());
yEnd[0] = ::Foam::j0(xEnd);
yEnd[1] = ::Foam::j1(xEnd);
yEnd[2] = ::Foam::jn(2, xEnd);
yEnd[3] = ::Foam::jn(3, xEnd);
scalar dxEst = 0.5;
odeSolver->relTol() = 1e-4;
odeSolver->solve(x, xEnd, y, dxEst);
Info<< nl << "Analytical: y(2.0) = " << yEnd << endl;
Info << "Numerical: y(2.0) = " << y << ", dxEst = " << dxEst << endl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-OListStream.C
EXE = $(FOAM_USER_APPBIN)/Test-OListStream
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