Skip to content

Conversation

@gassmoeller
Copy link
Member

@gassmoeller gassmoeller commented Oct 23, 2025

In #2139 we had moved the anisotropic viscosity assemblers out of the main code and into the tests, since there was no realistic application for them at the time. #3066 then introduced a cookbook that duplicated the existing code from the test in a plugin. #6615 now needs the same code again, so I think it is time to move the code back into the main program and unify the code. The assemblers are separated from the normal Stokes assemblers, because they change things in the innermost loop of the Stokes assembler and are significantly more expensive than the isotropic viscosity ones (that was the motivation in #2139 to split them from the normal assemblers).

To make sure nothing was changed between the cookbook and the test assemblers, I first created a test for the cookbook (meanwhile fixing a bug in the cookbook prm), and then unified the assembler code. The results of the test remained unchanged.

The anisotropic viscosity assemblers are still not accessible from a simple parameter file change, they need a plugin to replace the isotropic assemblers with the anisotropic ones. But at least we now dont need to duplicate the code anymore.

I have now also unified the shear heating plugins into a new heating plugin.

@Wang-yijun this will simplify your work in #6615

@gassmoeller
Copy link
Member Author

/rebuild

@gassmoeller gassmoeller force-pushed the unify_anisotropic_viscosity_code branch from dbf80e1 to e544695 Compare November 3, 2025 14:08
@gassmoeller
Copy link
Member Author

/rebuild

subsection Function
set Variable names = x,z
set Function constants = pi=3.1415926;
set Function constants = pi=3.1415926
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did that yield an error? If not, our parsing is not robust enough...

Copy link
Contributor

@bangerth bangerth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have to look at the .cc file for the assembly, but here are a few comments already.

Comment on lines +34 to +35
* anisotropic viscosity tensor. If the material model provides a stress-
* strain director tensor, then the strain-rate is multiplied with this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* anisotropic viscosity tensor. If the material model provides a stress-
* strain director tensor, then the strain-rate is multiplied with this
* anisotropic viscosity tensor. If the material model provides a stress-strain
* director tensor, then the strain-rate is multiplied with this

Comment on lines +49 to +60
const std::shared_ptr<const MaterialModel::AnisotropicViscosity<dim>> anisotropic_viscosity =
material_model_outputs.template get_additional_output_object<MaterialModel::AnisotropicViscosity<dim>>();

for (unsigned int q=0; q<heating_model_outputs.heating_source_terms.size(); ++q)
{
// If there is an anisotropic viscosity, use it to compute the correct stress
const SymmetricTensor<2,dim> &directed_strain_rate = ((anisotropic_viscosity != nullptr)
?
anisotropic_viscosity->stress_strain_directors[q]
* material_model_inputs.strain_rate[q]
:
material_model_inputs.strain_rate[q]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to just always expect that a director is given when using this heating model?

Comment on lines +28 to +41
namespace aspect
{
namespace MaterialModel
{
/**
* Additional output fields for anisotropic viscosities to be added to
* the MaterialModel::MaterialModelOutputs structure and filled in the
* MaterialModel::Interface::evaluate() function.
*/
template <int dim>
class AnisotropicViscosity : public NamedAdditionalMaterialOutputs<dim>
{
public:
AnisotropicViscosity(const unsigned int n_points);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assemblers directory is a bit of a funny place to find the declaration of a material model (or named additional output, as it may be). Have you thought moving it into the respective directory?

Comment on lines +60 to +74
namespace
{
template <int dim>
std::vector<std::string> make_AnisotropicViscosity_additional_outputs_names()
{
std::vector<std::string> names;

for (unsigned int i = 0; i < Tensor<4,dim>::n_independent_components ; ++i)
{
TableIndices<4> indices(Tensor<4,dim>::unrolled_to_component_indices(i));
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]));
}
return names;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modules don't allow for anonymous namespaces in header files, and it's generally a bad idea anyway. I think that you're using this function only immediately below in one place, so it should be easy to move this code into its only caller.

template <int dim>
AnisotropicViscosity<dim>::AnisotropicViscosity (const unsigned int n_points)
:
NamedAdditionalMaterialOutputs<dim>(make_AnisotropicViscosity_additional_outputs_names<dim>()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just convert the function above into a lambda function that you can declare and call in-place here.

Comment on lines +87 to +97
template <int dim>
std::vector<double>
AnisotropicViscosity<dim>::get_nth_output(const unsigned int idx) const
{
std::vector<double> output(stress_strain_directors.size());
for (unsigned int i = 0; i < stress_strain_directors.size() ; ++i)
{
output[i]= stress_strain_directors[i][Tensor<4,dim>::unrolled_to_component_indices(idx)];
}
return output;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function could probably just as well live in a .cc file.

namespace Assemblers
{
/**
* A class containing the functions to assemble the Stokes preconditioner.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* A class containing the functions to assemble the Stokes preconditioner.
* A class containing the functions to assemble the Stokes preconditioner for the case of anisotropic viscosities.

Comment on lines +122 to +123
* A class containing the functions to assemble the compressible adjustment
* to the Stokes preconditioner.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* A class containing the functions to assemble the compressible adjustment
* to the Stokes preconditioner.
* A class containing the functions to assemble the compressible adjustment
* to the Stokes preconditioner for the case of anisotropic viscosities.

Comment on lines +136 to +137
* This class assembles the terms for the matrix and right-hand-side of the incompressible
* Stokes equation for the current cell.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, and perhaps also below

};

/**
* This class assembles the term that arises in the viscosity term of Stokes matrix for
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This class assembles the term that arises in the viscosity term of Stokes matrix for
* This class assembles the term that arises in the viscosity term of the Stokes matrix for

This is a copy-paste mistake -- the grammatically wrong code is already in line include/aspect/simulator/assemblers/stokes.h:81.

Copy link
Contributor

@bangerth bangerth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the rest.

Comment on lines +40 to +41
std::shared_ptr<const MaterialModel::AnisotropicViscosity<dim>> anisotropic_viscosity =
scratch.material_model_outputs.template get_additional_output_object<MaterialModel::AnisotropicViscosity<dim>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this variable be const?

const double eta = scratch.material_model_outputs.viscosities[q];
const double one_over_eta = 1. / eta;

const bool use_tensor = (anisotropic_viscosity != nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable should move out of the loop. But more generally, shouldn't this assemble only be called for the anisotropic case? We could assert that at the top, and then simplify a lot of the code below. (I recognize the history of the code as what we used to have, where we catered to both the anisotropic and isotropic case, but perhaps we don't need to carry this provenance around with us for forever.)

Comment on lines +131 to +135
template <int dim>
void
StokesCompressiblePreconditionerAnisotropicViscosity<dim>::
execute (internal::Assembly::Scratch::ScratchBase<dim> &scratch_base,
internal::Assembly::CopyData::CopyDataBase<dim> &data_base) const
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments for this function as for the one above.

Comment on lines +203 to +207
template <int dim>
void
StokesIncompressibleTermsAnisotropicViscosity<dim>::
execute (internal::Assembly::Scratch::ScratchBase<dim> &scratch_base,
internal::Assembly::CopyData::CopyDataBase<dim> &data_base) const
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and for this one too.

Comment on lines +324 to +328
template <int dim>
void
StokesCompressibleStrainRateViscosityTermAnisotropicViscosity<dim>::
execute (internal::Assembly::Scratch::ScratchBase<dim> &scratch_base,
internal::Assembly::CopyData::CopyDataBase<dim> &data_base) const
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and one more time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants