Skip to content

Commit a266d9c

Browse files
committed
Improve anisotropic viscosity code
1 parent e544695 commit a266d9c

File tree

7 files changed

+57
-152
lines changed

7 files changed

+57
-152
lines changed

cookbooks/anisotropic_viscosity/av_material.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include <aspect/introspection.h>
2222
#include <aspect/material_model/interface.h>
23-
#include <aspect/plugins.h>
23+
#include <aspect/material_model/additional_outputs/anisotropic_viscosity.h>
2424
#include <aspect/simulator/assemblers/stokes_anisotropic_viscosity.h>
2525
#include <deal.II/base/parameter_handler.h>
2626
#include <deal.II/base/patterns.h>

include/aspect/heating_model/shear_heating_anisotropic_viscosity.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ namespace aspect
3131
{
3232
/**
3333
* A class that implements a standard model for shear heating extended for an
34-
* anisotropic viscosity tensor. If the material model provides a stress-
35-
* strain director tensor, then the strain-rate is multiplied with this
34+
* anisotropic viscosity tensor. If the material model provides a stress-strain
35+
* director tensor, then the strain-rate is multiplied with this
3636
* tensor to compute the stress that is used when computing the shear heating.
3737
*
3838
* @ingroup HeatingModels

include/aspect/simulator/assemblers/stokes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace aspect
7878
};
7979

8080
/**
81-
* This class assembles the term that arises in the viscosity term of Stokes matrix for
81+
* This class assembles the term that arises in the viscosity term of the Stokes matrix for
8282
* compressible models, because the divergence of the velocity is not longer zero.
8383
*/
8484
template <int dim>

include/aspect/simulator/assemblers/stokes_anisotropic_viscosity.h

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -27,80 +27,11 @@
2727

2828
namespace aspect
2929
{
30-
namespace MaterialModel
31-
{
32-
/**
33-
* Additional output fields for anisotropic viscosities to be added to
34-
* the MaterialModel::MaterialModelOutputs structure and filled in the
35-
* MaterialModel::Interface::evaluate() function.
36-
*/
37-
template <int dim>
38-
class AnisotropicViscosity : public NamedAdditionalMaterialOutputs<dim>
39-
{
40-
public:
41-
AnisotropicViscosity(const unsigned int n_points);
42-
43-
std::vector<double>
44-
get_nth_output(const unsigned int idx) const override;
45-
46-
/**
47-
* Stress-strain "director" tensors at the given positions. This
48-
* variable is used to implement anisotropic viscosity.
49-
*
50-
* @note The strain rate term in equation (1) of the manual will be
51-
* multiplied by this tensor *and* the viscosity scalar ($\eta$), as
52-
* described in the manual section titled "Constitutive laws". This
53-
* variable is assigned the rank-four identity tensor by default.
54-
* This leaves the isotropic constitutive law unchanged if the material
55-
* model does not explicitly assign a value.
56-
*/
57-
std::vector<SymmetricTensor<4,dim>> stress_strain_directors;
58-
};
59-
60-
namespace
61-
{
62-
template <int dim>
63-
std::vector<std::string> make_AnisotropicViscosity_additional_outputs_names()
64-
{
65-
std::vector<std::string> names;
66-
67-
for (unsigned int i = 0; i < Tensor<4,dim>::n_independent_components ; ++i)
68-
{
69-
TableIndices<4> indices(Tensor<4,dim>::unrolled_to_component_indices(i));
70-
names.push_back("anisotropic_viscosity"+std::to_string(indices[0])+std::to_string(indices[1])+std::to_string(indices[2])+std::to_string(indices[3]));
71-
}
72-
return names;
73-
}
74-
}
75-
76-
77-
78-
template <int dim>
79-
AnisotropicViscosity<dim>::AnisotropicViscosity (const unsigned int n_points)
80-
:
81-
NamedAdditionalMaterialOutputs<dim>(make_AnisotropicViscosity_additional_outputs_names<dim>()),
82-
stress_strain_directors(n_points, dealii::identity_tensor<dim> ())
83-
{}
84-
85-
86-
87-
template <int dim>
88-
std::vector<double>
89-
AnisotropicViscosity<dim>::get_nth_output(const unsigned int idx) const
90-
{
91-
std::vector<double> output(stress_strain_directors.size());
92-
for (unsigned int i = 0; i < stress_strain_directors.size() ; ++i)
93-
{
94-
output[i]= stress_strain_directors[i][Tensor<4,dim>::unrolled_to_component_indices(idx)];
95-
}
96-
return output;
97-
}
98-
}
99-
10030
namespace Assemblers
10131
{
10232
/**
103-
* A class containing the functions to assemble the Stokes preconditioner.
33+
* A class containing the functions to assemble the Stokes preconditioner for the
34+
* case of anisotropic viscosities.
10435
*/
10536
template <int dim>
10637
class StokesPreconditionerAnisotropicViscosity : public Assemblers::Interface<dim>,
@@ -120,7 +51,7 @@ namespace aspect
12051

12152
/**
12253
* A class containing the functions to assemble the compressible adjustment
123-
* to the Stokes preconditioner.
54+
* to the Stokes preconditioner for the case of anisotropic viscosities.
12455
*/
12556
template <int dim>
12657
class StokesCompressiblePreconditionerAnisotropicViscosity : public Assemblers::Interface<dim>,
@@ -134,7 +65,7 @@ namespace aspect
13465

13566
/**
13667
* This class assembles the terms for the matrix and right-hand-side of the incompressible
137-
* Stokes equation for the current cell.
68+
* Stokes equation for the case of anisotropic viscosities.
13869
*/
13970
template <int dim>
14071
class StokesIncompressibleTermsAnisotropicViscosity : public Assemblers::Interface<dim>,
@@ -153,8 +84,9 @@ namespace aspect
15384
};
15485

15586
/**
156-
* This class assembles the term that arises in the viscosity term of Stokes matrix for
157-
* compressible models, because the divergence of the velocity is not longer zero.
87+
* This class assembles the term that arises in the viscosity term of the Stokes matrix for
88+
* compressible models for the case of anisotropic viscosities, because the divergence
89+
* of the velocity is not longer zero.
15890
*/
15991
template <int dim>
16092
class StokesCompressibleStrainRateViscosityTermAnisotropicViscosity : public Assemblers::Interface<dim>,

source/heating_model/shear_heating_anisotropic_viscosity.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#include <aspect/heating_model/shear_heating_anisotropic_viscosity.h>
2222

2323
#include <aspect/material_model/interface.h>
24+
#include <aspect/material_model/additional_outputs/anisotropic_viscosity.h>
2425
#include <aspect/heating_model/shear_heating.h>
25-
#include <aspect/simulator/assemblers/stokes_anisotropic_viscosity.h>
2626

2727
#include <deal.II/base/symmetric_tensor.h>
2828
#include <deal.II/base/signaling_nan.h>
@@ -49,15 +49,15 @@ namespace aspect
4949
const std::shared_ptr<const MaterialModel::AnisotropicViscosity<dim>> anisotropic_viscosity =
5050
material_model_outputs.template get_additional_output_object<MaterialModel::AnisotropicViscosity<dim>>();
5151

52+
Assert(anisotropic_viscosity != nullptr,
53+
ExcMessage("This heating plugin should only be used with material models that provide "
54+
"an anisotropic viscosity tensor, but none was provided."));
55+
5256
for (unsigned int q=0; q<heating_model_outputs.heating_source_terms.size(); ++q)
5357
{
5458
// If there is an anisotropic viscosity, use it to compute the correct stress
55-
const SymmetricTensor<2,dim> &directed_strain_rate = ((anisotropic_viscosity != nullptr)
56-
?
57-
anisotropic_viscosity->stress_strain_directors[q]
58-
* material_model_inputs.strain_rate[q]
59-
:
60-
material_model_inputs.strain_rate[q]);
59+
const SymmetricTensor<2,dim> &directed_strain_rate = anisotropic_viscosity->stress_strain_directors[q]
60+
* material_model_inputs.strain_rate[q];
6161

6262
const SymmetricTensor<2,dim> stress =
6363
2 * material_model_outputs.viscosities[q] *

source/simulator/assemblers/stokes_anisotropic_viscosity.cc

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <aspect/simulator/assemblers/stokes_anisotropic_viscosity.h>
2222

2323
#include <aspect/gravity_model/interface.h>
24+
#include <aspect/material_model/additional_outputs/anisotropic_viscosity.h>
2425

2526
#include <deal.II/base/signaling_nan.h>
2627

@@ -37,9 +38,13 @@ namespace aspect
3738
internal::Assembly::Scratch::StokesPreconditioner<dim> &scratch = dynamic_cast<internal::Assembly::Scratch::StokesPreconditioner<dim>&> (scratch_base);
3839
internal::Assembly::CopyData::StokesPreconditioner<dim> &data = dynamic_cast<internal::Assembly::CopyData::StokesPreconditioner<dim>&> (data_base);
3940

40-
std::shared_ptr<const MaterialModel::AnisotropicViscosity<dim>> anisotropic_viscosity =
41+
const std::shared_ptr<const MaterialModel::AnisotropicViscosity<dim>> anisotropic_viscosity =
4142
scratch.material_model_outputs.template get_additional_output_object<MaterialModel::AnisotropicViscosity<dim>>();
4243

44+
Assert(anisotropic_viscosity != nullptr,
45+
ExcMessage("This assembler should only be used with material models that provide "
46+
"an anisotropic viscosity tensor, but none was provided."));
47+
4348
const Introspection<dim> &introspection = this->introspection();
4449
const FiniteElement<dim> &fe = this->get_fe();
4550
const unsigned int stokes_dofs_per_cell = data.local_dof_indices.size();
@@ -78,30 +83,16 @@ namespace aspect
7883

7984
const double eta = scratch.material_model_outputs.viscosities[q];
8085
const double one_over_eta = 1. / eta;
81-
82-
const bool use_tensor = (anisotropic_viscosity != nullptr);
83-
84-
const SymmetricTensor<4, dim> &stress_strain_director = (use_tensor)
85-
?
86-
anisotropic_viscosity->stress_strain_directors[q]
87-
:
88-
dealii::identity_tensor<dim>();
89-
90-
91-
86+
const SymmetricTensor<4, dim> &stress_strain_director = anisotropic_viscosity->stress_strain_directors[q];
9287
const double JxW = scratch.finite_element_values.JxW(q);
9388

9489
for (unsigned int i = 0; i < stokes_dofs_per_cell; ++i)
9590
for (unsigned int j = 0; j < stokes_dofs_per_cell; ++j)
9691
if (scratch.dof_component_indices[i] ==
9792
scratch.dof_component_indices[j])
98-
data.local_matrix(i, j) += ((
99-
use_tensor ?
100-
2.0 * eta * (scratch.grads_phi_u[i]
101-
* stress_strain_director
102-
* scratch.grads_phi_u[j]) :
103-
2.0 * eta * (scratch.grads_phi_u[i]
104-
* scratch.grads_phi_u[j]))
93+
data.local_matrix(i, j) += (2.0 * eta * (scratch.grads_phi_u[i]
94+
* stress_strain_director
95+
* scratch.grads_phi_u[j])
10596
+ one_over_eta * pressure_scaling
10697
* pressure_scaling
10798
* (scratch.phi_p[i]
@@ -140,6 +131,10 @@ namespace aspect
140131
const std::shared_ptr<const MaterialModel::AnisotropicViscosity<dim>> anisotropic_viscosity =
141132
scratch.material_model_outputs.template get_additional_output_object<MaterialModel::AnisotropicViscosity<dim>>();
142133

134+
Assert(anisotropic_viscosity != nullptr,
135+
ExcMessage("This assembler should only be used with material models that provide "
136+
"an anisotropic viscosity tensor, but none was provided."));
137+
143138
const Introspection<dim> &introspection = this->introspection();
144139
const FiniteElement<dim> &fe = this->get_fe();
145140
const unsigned int stokes_dofs_per_cell = data.local_dof_indices.size();
@@ -174,26 +169,15 @@ namespace aspect
174169
}
175170

176171
const double eta_two_thirds = scratch.material_model_outputs.viscosities[q] * 2.0 / 3.0;
177-
178-
const bool use_tensor = (anisotropic_viscosity != nullptr);
179-
180-
const SymmetricTensor<4, dim> &stress_strain_director = (use_tensor)
181-
?
182-
anisotropic_viscosity->stress_strain_directors[q]
183-
:
184-
dealii::identity_tensor<dim>();
185-
172+
const SymmetricTensor<4, dim> &stress_strain_director = anisotropic_viscosity->stress_strain_directors[q];
186173
const double JxW = scratch.finite_element_values.JxW(q);
187174

188175
for (unsigned int i = 0; i < stokes_dofs_per_cell; ++i)
189176
for (unsigned int j = 0; j < stokes_dofs_per_cell; ++j)
190177
if (scratch.dof_component_indices[i] ==
191178
scratch.dof_component_indices[j])
192-
data.local_matrix(i, j) += (- (use_tensor ?
193-
eta_two_thirds * (scratch.div_phi_u[i] * trace(stress_strain_director * scratch.grads_phi_u[j]))
194-
:
195-
eta_two_thirds * (scratch.div_phi_u[i] * scratch.div_phi_u[j])
196-
))
179+
data.local_matrix(i, j) += (- eta_two_thirds *
180+
(scratch.div_phi_u[i] * trace(stress_strain_director * scratch.grads_phi_u[j])))
197181
* JxW;
198182
}
199183
}
@@ -212,6 +196,10 @@ namespace aspect
212196
const std::shared_ptr<const MaterialModel::AnisotropicViscosity<dim>> anisotropic_viscosity =
213197
scratch.material_model_outputs.template get_additional_output_object<MaterialModel::AnisotropicViscosity<dim>>();
214198

199+
Assert(anisotropic_viscosity != nullptr,
200+
ExcMessage("This assembler should only be used with material models that provide "
201+
"an anisotropic viscosity tensor, but none was provided."));
202+
215203
const Introspection<dim> &introspection = this->introspection();
216204
const FiniteElement<dim> &fe = this->get_fe();
217205
const unsigned int stokes_dofs_per_cell = data.local_dof_indices.size();
@@ -247,13 +235,7 @@ namespace aspect
247235
:
248236
numbers::signaling_nan<double>());
249237

250-
const bool use_tensor = (anisotropic_viscosity != nullptr);
251-
252-
const SymmetricTensor<4, dim> &stress_strain_director = (use_tensor)
253-
?
254-
anisotropic_viscosity->stress_strain_directors[q]
255-
:
256-
dealii::identity_tensor<dim>();
238+
const SymmetricTensor<4, dim> &stress_strain_director = anisotropic_viscosity->stress_strain_directors[q];
257239

258240
const Tensor<1,dim>
259241
gravity = this->get_gravity_model().gravity_vector (scratch.finite_element_values.quadrature_point(q));
@@ -274,18 +256,15 @@ namespace aspect
274256
if (scratch.rebuild_stokes_matrix)
275257
for (unsigned int j=0; j<stokes_dofs_per_cell; ++j)
276258
{
277-
data.local_matrix(i,j) += ( (use_tensor ?
278-
eta * 2.0 * (scratch.grads_phi_u[i] * stress_strain_director * scratch.grads_phi_u[j])
279-
:
280-
eta * 2.0 * (scratch.grads_phi_u[i] * scratch.grads_phi_u[j]))
281-
// assemble \nabla p as -(p, div v):
282-
- (pressure_scaling *
283-
scratch.div_phi_u[i] * scratch.phi_p[j])
284-
// assemble the term -div(u) as -(div u, q).
285-
// Note the negative sign to make this
286-
// operator adjoint to the grad p term:
287-
- (pressure_scaling *
288-
scratch.phi_p[i] * scratch.div_phi_u[j]))
259+
data.local_matrix(i,j) += (eta * 2.0 * (scratch.grads_phi_u[i] * stress_strain_director * scratch.grads_phi_u[j])
260+
// assemble \nabla p as -(p, div v):
261+
- (pressure_scaling *
262+
scratch.div_phi_u[i] * scratch.phi_p[j])
263+
// assemble the term -div(u) as -(div u, q).
264+
// Note the negative sign to make this
265+
// operator adjoint to the grad p term:
266+
- (pressure_scaling *
267+
scratch.phi_p[i] * scratch.div_phi_u[j]))
289268
* JxW;
290269
}
291270
}
@@ -336,6 +315,10 @@ namespace aspect
336315
const std::shared_ptr<const MaterialModel::AnisotropicViscosity<dim>> anisotropic_viscosity =
337316
scratch.material_model_outputs.template get_additional_output_object<MaterialModel::AnisotropicViscosity<dim>>();
338317

318+
Assert(anisotropic_viscosity != nullptr,
319+
ExcMessage("This assembler should only be used with material models that provide "
320+
"an anisotropic viscosity tensor, but none was provided."));
321+
339322
const Introspection<dim> &introspection = this->introspection();
340323
const FiniteElement<dim> &fe = this->get_fe();
341324
const unsigned int stokes_dofs_per_cell = data.local_dof_indices.size();
@@ -357,25 +340,14 @@ namespace aspect
357340

358341
// Viscosity scalar
359342
const double eta_two_thirds = scratch.material_model_outputs.viscosities[q] * 2.0 / 3.0;
360-
361-
const bool use_tensor = (anisotropic_viscosity != nullptr);
362-
363-
const SymmetricTensor<4, dim> &stress_strain_director = (use_tensor)
364-
?
365-
anisotropic_viscosity->stress_strain_directors[q]
366-
:
367-
dealii::identity_tensor<dim>();
368-
343+
const SymmetricTensor<4, dim> &stress_strain_director = anisotropic_viscosity->stress_strain_directors[q];
369344
const double JxW = scratch.finite_element_values.JxW(q);
370345

371346
for (unsigned int i=0; i<stokes_dofs_per_cell; ++i)
372347
for (unsigned int j=0; j<stokes_dofs_per_cell; ++j)
373348
{
374-
data.local_matrix(i,j) += (- (use_tensor ?
375-
eta_two_thirds * (scratch.div_phi_u[i] * trace(stress_strain_director * scratch.grads_phi_u[j]))
376-
:
377-
eta_two_thirds * (scratch.div_phi_u[i] * scratch.div_phi_u[j])
378-
))
349+
data.local_matrix(i,j) += (-eta_two_thirds * (scratch.div_phi_u[i] * trace(stress_strain_director * scratch.grads_phi_u[j]))
350+
)
379351
* JxW;
380352
}
381353
}

tests/anisotropic_viscosity.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <aspect/simulator_access.h>
2222

2323
#include <aspect/material_model/simple.h>
24+
#include <aspect/material_model/additional_outputs/anisotropic_viscosity.h>
2425
#include <aspect/heating_model/shear_heating.h>
2526
#include <aspect/heating_model/interface.h>
2627
#include <aspect/gravity_model/interface.h>

0 commit comments

Comments
 (0)