Skip to content

Commit 654046b

Browse files
gassmoellerMFraters
authored andcommitted
Simplify derivative test
1 parent 8fe1cc6 commit 654046b

File tree

4 files changed

+171
-161
lines changed

4 files changed

+171
-161
lines changed

source/material_model/visco_plastic.cc

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ namespace aspect
183183
// The first time this function is called (first iteration of first time step)
184184
// a specified "reference" strain rate is used as the returned value would
185185
// otherwise be zero.
186-
const double edot_ii = ( (&(this->get_simulator()) != nullptr && this->get_timestep_number() == 0 && strain_rate.norm() <= std::numeric_limits<double>::min())
186+
const double edot_ii = ( (this->get_timestep_number() == 0 && strain_rate.norm() <= std::numeric_limits<double>::min())
187187
?
188188
ref_strain_rate
189189
:
@@ -358,22 +358,11 @@ namespace aspect
358358
MaterialModel::MaterialModelDerivatives<dim> *derivatives;
359359
derivatives = out.template get_additional_output<MaterialModel::MaterialModelDerivatives<dim> >();
360360

361-
// When there is no simulator, we are in a test, so make sure we use the derivatives.
362-
double derivative_scaling_factor = 1;
363-
if (&(this->get_simulator()) != nullptr)
364-
{
365-
// If do not use use the Newton solver, get the derivative scaling factor,
366-
// otherwise set it to zero.
367-
if (this->get_parameters().nonlinear_solver == Parameters<dim>::NonlinearSolver::Newton_Stokes)
368-
{
369-
derivative_scaling_factor = this->get_newton_handler().get_newton_derivative_scaling_factor();
370-
}
371-
else
372-
{
373-
derivative_scaling_factor = 0;
374-
}
375-
}
376-
361+
// If we use the Newton solver, get the derivative scaling factor,
362+
// otherwise set it to zero.
363+
double derivative_scaling_factor = 0.0;
364+
if (this->get_parameters().nonlinear_solver == Parameters<dim>::NonlinearSolver::Newton_Stokes)
365+
derivative_scaling_factor = this->get_newton_handler().get_newton_derivative_scaling_factor();
377366

378367
// Loop through points
379368
for (unsigned int i=0; i < in.temperature.size(); ++i)
@@ -458,7 +447,7 @@ namespace aspect
458447
}
459448

460449
/**
461-
* Now compute the derivative of the viscoisty to the pressure
450+
* Now compute the derivative of the viscosity to the pressure
462451
*/
463452
double pressure_difference = in.pressure[i] + (std::fabs(in.pressure[i]) * finite_difference_accuracy);
464453

@@ -634,14 +623,6 @@ namespace aspect
634623
void
635624
ViscoPlastic<dim>::declare_parameters (ParameterHandler &prm)
636625
{
637-
prm.enter_subsection("Compositional fields");
638-
{
639-
prm.declare_entry ("Number of fields", "0",
640-
Patterns::Integer (0),
641-
"The number of fields that will be advected along with the flow field, excluding "
642-
"velocity, pressure and temperature.");
643-
}
644-
prm.leave_subsection();
645626
prm.enter_subsection("Material model");
646627
{
647628
prm.enter_subsection ("Visco Plastic");
@@ -827,13 +808,7 @@ namespace aspect
827808
ViscoPlastic<dim>::parse_parameters (ParameterHandler &prm)
828809
{
829810
// increment by one for background:
830-
unsigned int n_fields = 0;
831-
prm.enter_subsection("Compositional fields");
832-
{
833-
n_fields = prm.get_integer("Number of fields")+1;//this->n_compositional_fields() + 1;
834-
}
835-
prm.leave_subsection();
836-
811+
const unsigned int n_fields = this->n_compositional_fields() + 1;
837812

838813
// number of required compositional fields for full finite strain tensor
839814
const unsigned int s = Tensor<2,dim>::n_independent_components;
@@ -842,7 +817,6 @@ namespace aspect
842817
{
843818
prm.enter_subsection ("Visco Plastic");
844819
{
845-
846820
// Reference and minimum/maximum values
847821
reference_T = prm.get_double("Reference temperature");
848822
min_strain_rate = prm.get_double("Minimum strain rate");
@@ -873,6 +847,7 @@ namespace aspect
873847
if (use_strain_weakening)
874848
AssertThrow(this->n_compositional_fields() >= 1,
875849
ExcMessage("There must be at least one compositional field. "));
850+
876851
use_finite_strain_tensor = prm.get_bool ("Use finite strain tensor");
877852
if (use_finite_strain_tensor)
878853
AssertThrow(this->n_compositional_fields() >= s,

tests/visco_plastic_derivatives.cc

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
#include <aspect/simulator.h>
2-
#include <deal.II/grid/tria.h>
2+
#include <aspect/simulator_access.h>
3+
34
#include <aspect/material_model/interface.h>
45
#include <aspect/material_model/visco_plastic.h>
56
#include <aspect/simulator_access.h>
67
#include <aspect/newton.h>
78

9+
#include <deal.II/grid/tria.h>
10+
#include <deal.II/base/exceptions.h>
11+
#include <deal.II/base/std_cxx11/shared_ptr.h>
12+
813
#include <iostream>
914

10-
int f(double parameter)
15+
template <int dim>
16+
void f(const aspect::SimulatorAccess<dim> &simulator_access,
17+
aspect::Assemblers::Manager<dim> &)
1118
{
19+
// Prepare NewtonHandler for test
20+
const_cast<aspect::NewtonHandler<dim> &> (simulator_access.get_newton_handler()).set_newton_derivative_scaling_factor(1.0);
1221

13-
std::cout << std::endl << "Test for p = " << parameter << std::endl;
22+
std::cout << std::endl << "Testing ViscoPlastic derivatives against analytical derivatives" << std::endl;
1423

15-
const int dim=2;
1624
using namespace aspect::MaterialModel;
1725
MaterialModelInputs<dim> in_base(5,3);
1826
in_base.composition[0][0] = 0;
@@ -38,9 +46,9 @@ int f(double parameter)
3846
in_base.pressure[4] = 5e8;
3947

4048
/**
41-
* We can't take to small strain-rates, because then the difference in the
42-
* visocisty will be too small for the double accuracy which stores
43-
* the visocity solutions and the finite diference solution.
49+
* We can't take too small strain-rates, because then the difference in the
50+
* viscosity will be too small for the double accuracy which stores
51+
* the viscosity solutions and the finite difference solution.
4452
*/
4553
in_base.strain_rate[0] = SymmetricTensor<2,dim>();
4654
in_base.strain_rate[0][0][0] = 1e-12;
@@ -125,48 +133,18 @@ int f(double parameter)
125133
MaterialModelOutputs<dim> out_dviscositydstrainrate_oneone(5,3);
126134
MaterialModelOutputs<dim> out_dviscositydtemperature(5,3);
127135

128-
if (out_base.get_additional_output<MaterialModelDerivatives<dim> >() != NULL)
129-
throw "error";
130-
131-
out_base.additional_outputs.push_back(std::make_shared<MaterialModelDerivatives<dim> > (5));
132-
133-
ViscoPlastic<dim> mat;
134-
ParameterHandler prm;
135-
mat.declare_parameters(prm);
136-
137-
prm.enter_subsection("Compositional fields");
138-
{
139-
prm.set ("Number of fields", "3");
140-
}
141-
prm.leave_subsection();
142-
143-
prm.enter_subsection("Material model");
144-
{
145-
prm.enter_subsection ("Visco Plastic");
146-
{
147-
// prm.enter_subsection ("Viscosity");
148-
// {
149-
//prm.set ("Reference strain rate", "1e-20");
150-
prm.set ("Angles of internal friction", "30");
151-
// }
152-
// prm.leave_subsection();
153-
}
154-
prm.leave_subsection();
155-
}
156-
prm.leave_subsection();
157-
158-
mat.parse_parameters(prm);
136+
out_base.additional_outputs.push_back(std_cxx11::make_shared<MaterialModelDerivatives<dim> > (5));
159137

160-
mat.evaluate(in_base, out_base);
161-
mat.evaluate(in_dviscositydpressure, out_dviscositydpressure);
162-
mat.evaluate(in_dviscositydstrainrate_zerozero, out_dviscositydstrainrate_zerozero);
163-
mat.evaluate(in_dviscositydstrainrate_onezero, out_dviscositydstrainrate_onezero);
164-
mat.evaluate(in_dviscositydstrainrate_oneone, out_dviscositydstrainrate_oneone);
165-
mat.evaluate(in_dviscositydtemperature, out_dviscositydtemperature);
138+
simulator_access.get_material_model().evaluate(in_base, out_base);
139+
simulator_access.get_material_model().evaluate(in_dviscositydpressure, out_dviscositydpressure);
140+
simulator_access.get_material_model().evaluate(in_dviscositydstrainrate_zerozero, out_dviscositydstrainrate_zerozero);
141+
simulator_access.get_material_model().evaluate(in_dviscositydstrainrate_onezero, out_dviscositydstrainrate_onezero);
142+
simulator_access.get_material_model().evaluate(in_dviscositydstrainrate_oneone, out_dviscositydstrainrate_oneone);
143+
simulator_access.get_material_model().evaluate(in_dviscositydtemperature, out_dviscositydtemperature);
166144

167145
// set up additional output for the derivatives
168146
MaterialModelDerivatives<dim> *derivatives;
169-
derivatives = out_base.get_additional_output<MaterialModelDerivatives<dim> >();
147+
derivatives = out_base.template get_additional_output<MaterialModelDerivatives<dim> >();
170148

171149
double temp;
172150
for (unsigned int i = 0; i < 5; i++)
@@ -223,7 +201,7 @@ int f(double parameter)
223201
std::cout << "onezero at point " << i << ": Finite difference = " << temp << ". Analytical derivative = " << derivatives->viscosity_derivative_wrt_strain_rate[i][1][0] << std::endl;
224202
if (std::fabs(temp - derivatives->viscosity_derivative_wrt_strain_rate[i][1][0]) > 1e-3 * (std::fabs(temp) + std::fabs(derivatives->viscosity_derivative_wrt_strain_rate[i][1][0])) )
225203
{
226-
std::cout << " Error: The derivative of the viscosity to the strain rate is too different from the analitical value." << std::endl;
204+
std::cout << " Error: The derivative of the viscosity to the strain rate is too different from the analytical value." << std::endl;
227205
Error = true;
228206
}
229207
}
@@ -241,34 +219,40 @@ int f(double parameter)
241219
std::cout << "oneone at point " << i << ": Finite difference = " << temp << ". Analytical derivative = " << derivatives->viscosity_derivative_wrt_strain_rate[i][1][1] << std::endl;
242220
if (std::fabs(temp - derivatives->viscosity_derivative_wrt_strain_rate[i][1][1]) > 1e-3 * (std::fabs(temp) + std::fabs(derivatives->viscosity_derivative_wrt_strain_rate[i][1][1])) )
243221
{
244-
std::cout << " Error: The derivative of the viscosity to the strain rate is too different from the analitical value." << std::endl;
222+
std::cout << " Error: The derivative of the viscosity to the strain rate is too different from the analytical value." << std::endl;
245223
Error = true;
246224
}
247225

248226
}
249227

250228
if (Error)
251229
{
252-
std::cout << "Some parts of the test where not succesful." << std::endl;
230+
std::cout << "Some parts of the test where not successful." << std::endl;
253231
}
254232
else
255233
{
256234
std::cout << "OK" << std::endl;
257235
}
258236

259-
return 42;
237+
// Restore NewtonHandler state after test
238+
const_cast<aspect::NewtonHandler<dim> &> (simulator_access.get_newton_handler()).set_newton_derivative_scaling_factor(0.0);
239+
240+
}
241+
242+
template <>
243+
void f(const aspect::SimulatorAccess<3> &simulator_access,
244+
aspect::Assemblers::Manager<3> &)
245+
{
246+
AssertThrow(false,dealii::ExcInternalError());
260247
}
261248

262-
int exit_function()
249+
template <int dim>
250+
void signal_connector (aspect::SimulatorSignals<dim> &signals)
263251
{
264-
exit(0);
265-
return 42;
252+
std::cout << "* Connecting signals" << std::endl;
253+
signals.set_assemblers.connect (&f<dim>);
266254
}
267-
// run this function by initializing a global variable by it
268-
int ik = f(-1); // Testing harmonic mean
269-
int ji = f(0); // Testing geometric mean
270-
int jj = f(1); // Testing arithmetic mean
271-
int kj = f(1000); // Testing max function
272-
int kl = exit_function();
273255

256+
ASPECT_REGISTER_SIGNALS_CONNECTOR(signal_connector<2>,
257+
signal_connector<3>)
274258

tests/visco_plastic_derivatives.prm

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,106 @@
22
# material model are the same as the finite difference derivatives
33
# created by the drucker prager material model.
44
set Additional shared libraries = tests/libvisco_plastic_derivatives.so
5+
6+
set Dimension = 2
7+
set End time = 0
8+
set Use years in output instead of seconds = true
9+
set Nonlinear solver scheme = Newton Stokes
10+
set Max nonlinear iterations = 1
11+
set Number of cheap Stokes solver steps = 0
12+
13+
# Model geometry (100x100 km, 10 km spacing)
14+
subsection Geometry model
15+
set Model name = box
16+
subsection Box
17+
set X repetitions = 10
18+
set Y repetitions = 10
19+
set X extent = 100e3
20+
set Y extent = 100e3
21+
end
22+
end
23+
24+
# Mesh refinement specifications
25+
subsection Mesh refinement
26+
set Initial adaptive refinement = 0
27+
set Initial global refinement = 0
28+
set Time steps between mesh refinement = 0
29+
end
30+
31+
32+
# Boundary classifications (fixed T boundaries, prescribed velocity)
33+
subsection Model settings
34+
set Fixed temperature boundary indicators = bottom, top, left, right
35+
set Prescribed velocity boundary indicators = bottom y: function, top y: function, left x: function, right x: function
36+
end
37+
38+
# Velocity on boundaries characterized by functions
39+
subsection Boundary velocity model
40+
subsection Function
41+
set Variable names = x,y
42+
set Function constants = m=0.0005, year=1
43+
set Function expression = if (x<50e3 , -1*m/year, 1*m/year); if (y<50e3 , 1*m/year, -1*m/year);
44+
end
45+
end
46+
47+
# Temperature boundary and initial conditions
48+
subsection Boundary temperature model
49+
set List of model names = box
50+
subsection Box
51+
set Bottom temperature = 273
52+
set Left temperature = 273
53+
set Right temperature = 273
54+
set Top temperature = 273
55+
end
56+
end
57+
subsection Initial temperature model
58+
set Model name = function
59+
subsection Function
60+
set Function expression = 273
61+
end
62+
end
63+
64+
# Compositional fields used to track finite strain invariant
65+
subsection Compositional fields
66+
set Number of fields = 3
67+
end
68+
69+
# We prescribe some initial strain at the center of the domain
70+
subsection Initial composition model
71+
set Model name = function
72+
subsection Function
73+
set Variable names = x,y
74+
set Function expression = if(x>=45e3&x<=55e3&y>=45.3e3&y<=55.e3,0.2,0);0;0
75+
end
76+
end
77+
78+
# Boundary composition specification
79+
subsection Boundary composition model
80+
set Model name = initial composition
81+
end
82+
83+
# Material model (values for background material)
84+
subsection Material model
85+
set Model name = visco plastic
86+
subsection Visco Plastic
87+
set Angles of internal friction = 30.
88+
end
89+
end
90+
91+
# Gravity model
92+
subsection Gravity model
93+
set Model name = vertical
94+
subsection Vertical
95+
set Magnitude = 10.0
96+
end
97+
end
98+
99+
# Post processing
100+
# named additional outputs includes the weakened cohesions and friction angles
101+
subsection Postprocess
102+
set List of postprocessors = velocity statistics, mass flux statistics, visualization
103+
subsection Visualization
104+
set List of output variables = viscosity, strain rate, named additional outputs
105+
set Output format = gnuplot
106+
end
107+
end

0 commit comments

Comments
 (0)