Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
dumux
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dumux-repositories
dumux
Commits
916856c3
Commit
916856c3
authored
5 years ago
by
Timo Koch
Browse files
Options
Downloads
Patches
Plain Diff
[test] Add test for cubic spline
parent
a98dfaa3
No related branches found
No related tags found
1 merge request
!1600
Feature/simple cubic spline
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
test/common/spline/CMakeLists.txt
+2
-0
2 additions, 0 deletions
test/common/spline/CMakeLists.txt
test/common/spline/test_cubicspline.cc
+102
-0
102 additions, 0 deletions
test/common/spline/test_cubicspline.cc
with
104 additions
and
0 deletions
test/common/spline/CMakeLists.txt
+
2
−
0
View file @
916856c3
dune_add_test
(
SOURCES test_spline.cc
LABELS unit
)
dune_add_test
(
SOURCES test_cubicspline.cc
LABELS unit
)
This diff is collapsed.
Click to expand it.
test/common/spline/test_cubicspline.cc
0 → 100644
+
102
−
0
View file @
916856c3
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
* See the file COPYING for full copying permissions. *
* *
* This program 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. *
* *
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
/*!
* \file
* \brief Test the simple cubic spline implementation
*/
#include
<config.h>
#include
<cmath>
#include
<vector>
#include
<algorithm>
#include
<functional>
#include
<dune/common/exceptions.hh>
#include
<dune/common/parallel/mpihelper.hh>
#include
<dumux/common/cubicspline.hh>
#include
<dumux/io/gnuplotinterface.hh>
std
::
vector
<
double
>
linspace
(
const
double
begin
,
const
double
end
,
const
double
samples
)
{
const
double
delta
=
(
end
-
begin
)
/
static_cast
<
double
>
(
samples
-
1
);
std
::
vector
<
double
>
vec
(
samples
);
for
(
int
i
=
0
;
i
<
samples
;
++
i
)
vec
[
i
]
=
begin
+
i
*
delta
;
return
vec
;
}
template
<
class
Function
>
std
::
vector
<
double
>
eval
(
const
Function
&
f
,
const
std
::
vector
<
double
>&
x
)
{
auto
y
=
x
;
std
::
transform
(
x
.
begin
(),
x
.
end
(),
y
.
begin
(),
[
&
](
const
double
x
)
{
return
f
(
x
);
});
return
y
;
}
int
main
(
int
argc
,
char
**
argv
)
{
Dune
::
MPIHelper
::
instance
(
argc
,
argv
);
// we test the spline interpolation against a sample function
const
auto
f
=
[](
double
x
){
return
1.0
/
(
1.0
+
x
*
x
);
};
const
auto
df
=
[](
double
x
){
return
-
2.0
*
x
/
((
1.0
+
x
*
x
)
*
(
1.0
+
x
*
x
));
};
// create some test samples
const
auto
testPoints
=
linspace
(
-
4.0
,
4.0
,
1000
);
const
auto
ref
=
eval
(
f
,
testPoints
);
const
auto
refDeriv
=
eval
(
df
,
testPoints
);
// create the spline sample points
const
auto
samplePoints
=
linspace
(
-
4.0
,
4.0
,
15
);
const
auto
y
=
eval
(
f
,
samplePoints
);
// create the spline
Dumux
::
CubicSpline
spline
(
samplePoints
,
y
);
// evaluate spline and derivative
const
auto
result
=
eval
([
&
](
const
double
x
)
{
return
spline
.
eval
(
x
);
},
testPoints
);
const
auto
resultDeriv
=
eval
([
&
](
const
double
x
)
{
return
spline
.
evalDerivative
(
x
);
},
testPoints
);
// compute largest difference
auto
diff
=
result
;
auto
diffDeriv
=
result
;
std
::
transform
(
result
.
begin
(),
result
.
end
(),
ref
.
begin
(),
diff
.
begin
(),
[](
auto
a
,
auto
b
){
return
std
::
abs
(
a
-
b
);
});
std
::
transform
(
resultDeriv
.
begin
(),
resultDeriv
.
end
(),
refDeriv
.
begin
(),
diffDeriv
.
begin
(),
[](
auto
a
,
auto
b
){
return
std
::
abs
(
a
-
b
);
});
const
auto
maxNorm
=
std
::
accumulate
(
diff
.
begin
(),
diff
.
end
(),
diff
[
0
],
[](
auto
a
,
auto
b
){
return
std
::
max
(
a
,
b
);
});
const
auto
maxNormDeriv
=
std
::
accumulate
(
diffDeriv
.
begin
(),
diffDeriv
.
end
(),
diffDeriv
[
0
],
[](
auto
a
,
auto
b
){
return
std
::
max
(
a
,
b
);
});
std
::
cout
<<
"Maximum error: "
<<
maxNorm
<<
"
\n
"
;
std
::
cout
<<
"Maximum error in derivative: "
<<
maxNormDeriv
<<
"
\n
"
;
if
(
maxNorm
>
0.0038
||
maxNormDeriv
>
0.021
)
DUNE_THROW
(
Dune
::
Exception
,
"Maximum error in spline interpolation too large!"
);
// plot with Gnuplot (plot a bit more so we can see the linear extension)
const
auto
plotPoints
=
linspace
(
-
8.0
,
8.0
,
1000
);
const
auto
refPlot
=
eval
(
f
,
plotPoints
);
const
auto
refDerivPlot
=
eval
(
df
,
plotPoints
);
const
auto
resultPlot
=
eval
([
&
](
const
double
x
)
{
return
spline
.
eval
(
x
);
},
plotPoints
);
const
auto
resultDerivPlot
=
eval
([
&
](
const
double
x
)
{
return
spline
.
evalDerivative
(
x
);
},
plotPoints
);
Dumux
::
GnuplotInterface
<
double
>
gnuplot
(
false
);
gnuplot
.
addDataSetToPlot
(
plotPoints
,
refPlot
,
"reference"
);
gnuplot
.
addDataSetToPlot
(
plotPoints
,
refDerivPlot
,
"reference_derivative"
);
gnuplot
.
addDataSetToPlot
(
plotPoints
,
resultPlot
,
"spline"
);
gnuplot
.
addDataSetToPlot
(
plotPoints
,
resultDerivPlot
,
"spline_derivative"
);
gnuplot
.
plot
(
"spline"
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment