Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Mpp
MLUQ
Commits
1061a80c
Commit
1061a80c
authored
Jul 10, 2021
by
niklas.baumgarten
Browse files
worked on TestSparseGridGenerator, hpp might be deleted
parent
4902bc6c
Changes
2
Hide whitespace changes
Inline
Side-by-side
mluq/tests/generators/TestSparseGridGenerator.cpp
View file @
1061a80c
#include
"TestEnvironment.hpp"
#include
"MeshesCreator.hpp"
#include
"TestSparseGridGenerator.hpp"
#include
"SparseGridGenerator.hpp"
#include
"MeshesCreator.hpp"
#include
"TestEnvironment.hpp"
const
double
TEST_TOLERANCE
=
1e-10
;
const
expr
double
TEST_TOLERANCE
=
1e-10
;
class
TestSparseGridGenerator
:
public
Test
{
struct
TestParams
{
int
dimension
;
int
level
;
double
exact
;
};
Logging
&
operator
<<
(
Logging
&
s
,
const
TestParams
&
testParams
)
{
return
s
<<
"Dimension: "
<<
testParams
.
dimension
<<
endl
<<
"Level: "
<<
testParams
.
level
<<
endl
<<
"Exact solution: "
<<
testParams
.
exact
<<
endl
;
}
class
TestSparseGridGenerator
:
public
TestWithParam
<
TestParams
>
{
protected:
int
level
;
int
outputs
;
int
dimension
;
double
exact
;
std
::
unique_ptr
<
Meshes
>
meshes
;
SparseGridGenerator
generator
;
TestSparseGridGenerator
(
int
dimension
,
int
outputs
)
:
TestSparseGridGenerator
(
TasGrid
::
TypeDepth
depth
,
TasGrid
::
TypeOneDRule
rule
)
:
level
(
GetParam
().
level
),
dimension
(
GetParam
().
dimension
),
exact
(
GetParam
().
exact
),
outputs
(
0
),
meshes
(
MeshesCreator
(
"Interval"
).
CreateUnique
()),
generator
(
SparseGridGenerator
(
*
meshes
,
dimension
,
outputs
))
{
int
level
=
6
;
double
exact
=
2.513723354063905e+00
;
generator
.
CreateGlobalGrid
(
level
);
generator
(
SparseGridGenerator
(
*
meshes
,
dimension
,
outputs
,
level
,
depth
,
rule
))
{
}
void
TearDown
()
override
{}
};
class
Test1DSparseGridGenerator
:
public
TestSparseGridGenerator
{
public:
Test1DSparseGridGenerator
()
:
TestSparseGridGenerator
(
1
,
0
)
{}
};
class
Test2DSparseGridGenerator
:
public
TestSparseGridGenerator
{
class
TestClenshawCurtis
:
public
TestSparseGridGenerator
{
public:
Test2DSparseGridGenerator
()
:
TestSparseGridGenerator
(
2
,
0
)
{}
TestClenshawCurtis
()
:
TestSparseGridGenerator
(
TasGrid
::
type_level
,
TasGrid
::
rule_clenshawcurtis
)
{}
};
TEST_F
(
Test
1D
SparseGridGenerator
,
Test
NumOfGridPoints
)
{
EXPECT_EQ
(
generator
.
GetNumPoints
(),
generator
.
GetWeights
().
size
());
EXPECT_EQ
(
generator
.
GetNumPoints
(),
generator
.
GetPoints
().
size
());
}
INSTANTIATE_TEST_SUITE_P
(
TestSparseGridGenerator
,
Test
ClenshawCurtis
,
Values
(
TestParams
{
1
,
6
,
1.0
},
TestParams
{
2
,
6
,
1.0
}
));
TEST_
F
(
Test
2DSparseGridGenerator
,
TestNumOfGridPoints
)
{
TEST_
P
(
Test
ClenshawCurtis
,
TestNumOfGridPoints
)
{
EXPECT_EQ
(
generator
.
GetNumPoints
(),
generator
.
GetWeights
().
size
());
//
EXPECT_EQ(generator.GetNumPoints(), generator.GetPoints().size());
EXPECT_EQ
(
generator
.
GetNumPoints
()
*
dimension
,
generator
.
GetPoints
().
size
());
}
TEST_
F
(
Test
1DSparseGridGenerator
,
TestSumOfWeights
)
{
TEST_
P
(
Test
ClenshawCurtis
,
TestSumOfWeights
)
{
double
sum
=
0.0
;
for
(
auto
&
weight
:
generator
.
GetWeights
())
sum
+=
weight
;
EXPECT_NEAR
(
sum
,
2.0
,
TEST_TOLERANCE
);
EXPECT_NEAR
(
sum
,
2.0
*
dimension
,
TEST_TOLERANCE
);
}
TEST_F
(
Test2DSparseGridGenerator
,
TestSumOfWeights
)
{
double
sum
=
0.0
;
for
(
auto
&
weight
:
generator
.
GetWeights
())
sum
+=
weight
;
EXPECT_NEAR
(
sum
,
4.0
,
TEST_TOLERANCE
);
}
TEST_F
(
Test1DSparseGridGenerator
,
TestQuadrature
)
{
std
::
cout
<<
std
::
scientific
;
std
::
cout
.
precision
(
17
);
std
::
cout
<<
"Example 1: integrate f(x,y) = exp(-x^2) * cos(y),
\n
"
<<
" using clenshaw-curtis nodes and grid of type level
\n
"
;
std
::
cout
<<
"Example is 2D"
<<
endl
;
}
TEST_F
(
Test2DSparseGridGenerator
,
TestQuadrature
)
{
std
::
cout
<<
std
::
scientific
;
std
::
cout
.
precision
(
17
);
std
::
cout
<<
"Example 1: integrate f(x,y) = exp(-x^2) * cos(y),
\n
"
<<
" using clenshaw-curtis nodes and grid of type level
\n
"
;
std
::
cout
<<
"Example is 2D"
<<
endl
;
double
I
=
generator
.
Quadrature
([](
double
x
,
double
y
)
{
return
std
::
exp
(
-
x
*
x
)
*
std
::
cos
(
y
);
});
std
::
cout
<<
I
<<
endl
;
TEST_P
(
TestClenshawCurtis
,
TestQuadrature
)
{
// double I = 0.0; // should leave and be implemented in wrapper around Tasmanian
// for (int i = 0; i < num_points; i++) {
// double x = points[i * dimension];
// I += weights[i] * TestFunction(x);
// }
//
// double exact = 2.0;
// double error = std::abs(exact - I);
// EXPECT_NEAR(error, 0.0, TEST_TOLERANCE);
// std::cout << " at level: " << level
// << "\n the grid has: " << num_points
// << "\n integral: " << I
// << "\n error: " << error << "\n\n";
}
int
main
(
int
argc
,
char
**
argv
)
{
return
MppTest
(
MppTestBuilder
(
argc
,
argv
).
...
...
mluq/tests/generators/TestSparseGridGenerator.hpp
0 → 100644
View file @
1061a80c
#ifndef TESTSPARSEGRIDGENERATOR_HPP
#define TESTSPARSEGRIDGENERATOR_HPP
#endif //TESTSPARSEGRIDGENERATOR_HPP
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment