https://mooseframework.inl.gov
Loading...
Searching...
No Matches
DerivativeMaterialInterface.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12#include "AuxiliarySystem.h"
13#include "BlockRestrictable.h"
16#include "KernelBase.h"
17#include "BoundaryCondition.h"
18#include "Material.h"
19#include "MooseVariableFE.h"
20#include "NonlinearSystem.h"
21
22// Forward declarations
23class FEProblemBase;
24template <typename>
26
31template <class T>
33{
34public:
36
38
43 template <typename U, bool is_ad = false>
45
47 template <typename U, bool is_ad = false>
49 getDefaultMaterialPropertyByName(const std::string & name);
50
52
58 template <typename U, bool is_ad = false>
60 declarePropertyDerivative(const std::string & base, const std::vector<VariableName> & c);
61
62 template <typename U, bool is_ad = false>
64 const std::vector<SymbolName> & c);
65
66 template <typename U, bool is_ad = false>
68 const SymbolName & c1,
69 const SymbolName & c2 = "",
70 const SymbolName & c3 = "");
72
74
80 template <typename U, bool is_ad = false>
82 getMaterialPropertyDerivative(const std::string & base, const std::vector<VariableName> & c);
83
84 template <typename U, bool is_ad = false>
86 getMaterialPropertyDerivative(const std::string & base, const std::vector<SymbolName> & c);
87
88 template <typename U, bool is_ad = false>
90 getMaterialPropertyDerivative(const std::string & base,
91 const SymbolName & c1,
92 const SymbolName & c2 = "",
93 const SymbolName & c3 = "");
95
101 template <typename U, bool is_ad = false>
103 getMaterialPropertyDerivative(const std::string & base,
104 const SymbolName & c1,
105 unsigned int v2,
106 unsigned int v3 = libMesh::invalid_uint);
107
108 template <typename U, bool is_ad = false>
110 getMaterialPropertyDerivative(const std::string & base,
111 unsigned int v1,
112 unsigned int v2 = libMesh::invalid_uint,
113 unsigned int v3 = libMesh::invalid_uint);
115
117
123 template <typename U, bool is_ad = false>
125 getMaterialPropertyDerivativeByName(const MaterialPropertyName & base,
126 const std::vector<VariableName> & c);
127
128 template <typename U, bool is_ad = false>
130 getMaterialPropertyDerivativeByName(const MaterialPropertyName & base,
131 const std::vector<SymbolName> & c);
132
133 template <typename U, bool is_ad = false>
135 getMaterialPropertyDerivativeByName(const MaterialPropertyName & base,
136 const SymbolName & c1,
137 const SymbolName & c2 = "",
138 const SymbolName & c3 = "");
140
141#ifdef MOOSE_KOKKOS_SCOPE
143
153 template <typename U, unsigned int dimension = 0>
155 getKokkosMaterialPropertyDerivative(const std::string & base, const std::vector<SymbolName> & c);
156
157 template <typename U, unsigned int dimension = 0>
159 getKokkosMaterialPropertyDerivative(const std::string & base, const SymbolName & c1)
160 {
161 return getKokkosMaterialPropertyDerivative<U, dimension>(base, std::vector<SymbolName>{c1});
162 }
163
164 template <typename U, unsigned int dimension = 0>
166 getKokkosMaterialPropertyDerivativeByName(const MaterialPropertyName & base,
167 const std::vector<SymbolName> & c);
169
178 template <typename U, unsigned int dimension = 0>
180 declareKokkosPropertyDerivative(const std::string & base, const std::vector<SymbolName> & c)
181 {
182 return this->template declareKokkosPropertyByName<U, dimension>(
183 derivativePropertyName(base, c));
184 }
185#endif
186
188
192 template <typename U, bool is_ad = false>
193 void validateCoupling(const MaterialPropertyName & base,
194 const std::vector<VariableName> & c,
195 bool validate_aux = true);
196
197 template <typename U, bool is_ad = false>
198 void validateCoupling(const MaterialPropertyName & base,
199 const VariableName & c1 = "",
200 const VariableName & c2 = "",
201 const VariableName & c3 = "");
202
203 template <typename U, bool is_ad = false>
204 void validateNonlinearCoupling(const MaterialPropertyName & base,
205 const VariableName & c1 = "",
206 const VariableName & c2 = "",
207 const VariableName & c3 = "");
209
216 template <typename U, bool is_ad = false>
217 void validateDerivativeMaterialPropertyBase(const std::string & base);
218
219private:
221 template <typename U, bool is_ad = false>
222 bool haveMaterialProperty(const std::string & prop_name);
223
225 std::vector<VariableName>
226 buildVariableVector(const VariableName & c1, const VariableName & c2, const VariableName & c3);
227
229 template <typename U, bool is_ad = false>
230 void validateCouplingHelper(const MaterialPropertyName & base,
231 const std::vector<VariableName> & c,
232 const System & system,
233 std::vector<VariableName> & missing);
234
235 // check if the specified variable name is not the variable this kernel is acting on (always true
236 // for any other type of object)
237 bool isNotObjectVariable(const VariableName & name);
238
241};
242
243template <class T>
245 : T(parameters),
246 _dmi_fe_problem(*parameters.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base"))
247{
248}
249
250template <>
251template <typename U, bool is_ad>
252bool
254{
255 return ((this->boundaryRestricted() &&
256 this->template hasBoundaryMaterialProperty<U, is_ad>(prop_name)) ||
257 (this->template hasBlockMaterialProperty<U, is_ad>(prop_name)));
258}
259
260template <class T>
261template <typename U, bool is_ad>
262bool
264{
265 // Call the correct method to test for material property declarations
266 BlockRestrictable * blk = dynamic_cast<BlockRestrictable *>(this);
267 BoundaryRestrictable * bnd = dynamic_cast<BoundaryRestrictable *>(this);
268 return ((bnd && bnd->boundaryRestricted() &&
269 bnd->template hasBoundaryMaterialProperty<U, is_ad>(prop_name)) ||
270 (blk && blk->template hasBlockMaterialProperty<U, is_ad>(prop_name)) ||
271 (this->template hasGenericMaterialProperty<U, is_ad>(prop_name)));
272}
273
274template <class T>
275template <typename U, bool is_ad>
278{
279 // get the base property name
280 std::string prop_name = this->getMaterialPropertyName(name);
281
282 // Check if it's just a constant
283 const auto * default_property =
284 this->template defaultGenericMaterialProperty<U, is_ad>(prop_name);
285 if (default_property)
286 return *default_property;
287
288 // if found return the requested property
289 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(prop_name);
290}
291
292template <class T>
293template <typename U, bool is_ad>
296{
297 // TODO: deprecate this
298 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(prop_name);
299}
300
301template <class T>
302template <typename U, bool is_ad>
305 const std::vector<VariableName> & c)
306{
307 std::vector<SymbolName> symbol_vector(c.begin(), c.end());
308 return declarePropertyDerivative<U, is_ad>(base, symbol_vector);
309}
310
311template <class T>
312template <typename U, bool is_ad>
315 const std::vector<SymbolName> & c)
316{
317 return this->template declareGenericProperty<U, is_ad>(derivativePropertyName(base, c));
318}
319
320template <class T>
321template <typename U, bool is_ad>
324 const SymbolName & c1,
325 const SymbolName & c2,
326 const SymbolName & c3)
327{
328 if (c3 != "")
329 return this->template declareGenericProperty<U, is_ad>(
330 derivativePropertyNameThird(base, c1, c2, c3));
331 if (c2 != "")
332 return this->template declareGenericProperty<U, is_ad>(
333 derivativePropertyNameSecond(base, c1, c2));
334 return this->template declareGenericProperty<U, is_ad>(derivativePropertyNameFirst(base, c1));
335}
336
337template <class T>
338template <typename U, bool is_ad>
341 const std::vector<VariableName> & c)
342{
343 std::vector<SymbolName> symbol_vector(c.begin(), c.end());
344 return getMaterialPropertyDerivative(base, symbol_vector);
345}
346
347template <class T>
348template <typename U, bool is_ad>
351 const std::vector<SymbolName> & c)
352{
353 // get the base property name
354 std::string prop_name = this->getMaterialPropertyName(base);
355
360 if (this->template defaultGenericMaterialProperty<U, is_ad>(prop_name))
361 return this->template getGenericZeroMaterialProperty<U, is_ad>();
362
363 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(
364 derivativePropertyName(prop_name, c));
365}
366
367template <class T>
368template <typename U, bool is_ad>
371 const SymbolName & c1,
372 const SymbolName & c2,
373 const SymbolName & c3)
374{
375 // get the base property name
376 std::string prop_name = this->getMaterialPropertyName(base);
377
382 if (this->template defaultGenericMaterialProperty<Real, is_ad>(prop_name))
383 return this->template getGenericZeroMaterialProperty<U, is_ad>();
384
385 if (c3 != "")
386 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(
387 derivativePropertyNameThird(prop_name, c1, c2, c3));
388 if (c2 != "")
389 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(
390 derivativePropertyNameSecond(prop_name, c1, c2));
391 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(
392 derivativePropertyNameFirst(prop_name, c1));
393}
394
395template <class T>
396template <typename U, bool is_ad>
399 const SymbolName & c1,
400 unsigned int v2,
401 unsigned int v3)
402{
403 return getMaterialPropertyDerivative<U, is_ad>(
404 base,
405 c1,
406 this->_coupled_standard_moose_vars[v2]->name(),
407 v3 == libMesh::invalid_uint ? "" : this->_coupled_standard_moose_vars[v3]->name());
408}
409
410template <class T>
411template <typename U, bool is_ad>
414 unsigned int v1,
415 unsigned int v2,
416 unsigned int v3)
417{
418 return getMaterialPropertyDerivative<U, is_ad>(
419 base,
420 this->_coupled_standard_moose_vars[v1]->name(),
421 v2 == libMesh::invalid_uint ? "" : this->_coupled_standard_moose_vars[v2]->name(),
422 v3 == libMesh::invalid_uint ? "" : this->_coupled_standard_moose_vars[v3]->name());
423}
424
425template <class T>
426template <typename U, bool is_ad>
429 const MaterialPropertyName & base, const std::vector<VariableName> & c)
430{
431 std::vector<SymbolName> symbol_vector(c.begin(), c.end());
432 return getMaterialPropertyDerivativeByName(base, symbol_vector);
433}
434
435template <class T>
436template <typename U, bool is_ad>
439 const MaterialPropertyName & base, const std::vector<SymbolName> & c)
440{
441 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(
442 derivativePropertyName(base, c));
443}
444
445#ifdef MOOSE_KOKKOS_SCOPE
446template <class T>
447template <typename U, unsigned int dimension>
450 const std::string & base, const std::vector<SymbolName> & c)
451{
452 const auto prop_name = this->getMaterialPropertyName(base);
453
454 // Derivatives of constants are zero
455 if (MooseUtils::parsesToReal(prop_name))
457
458 return getKokkosMaterialPropertyDerivativeByName<U, dimension>(prop_name, c);
459}
460
461template <class T>
462template <typename U, unsigned int dimension>
465 const MaterialPropertyName & base, const std::vector<SymbolName> & c)
466{
467 const auto prop_name = derivativePropertyName(base, c);
468
469 if (!this->template hasKokkosMaterialPropertyByName<U, dimension>(prop_name))
471
472 return this->template getKokkosMaterialPropertyByName<U, dimension>(prop_name);
473}
474#endif
475
476template <class T>
477template <typename U, bool is_ad>
480 const MaterialPropertyName & base,
481 const SymbolName & c1,
482 const SymbolName & c2,
483 const SymbolName & c3)
484{
485 if (c3 != "")
486 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(
487 derivativePropertyNameThird(base, c1, c2, c3));
488 if (c2 != "")
489 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(
490 derivativePropertyNameSecond(base, c1, c2));
491 return this->template getGenericZeroMaterialPropertyByName<U, is_ad>(
492 derivativePropertyNameFirst(base, c1));
493}
494
495template <class T>
496template <typename U, bool is_ad>
497void
499 const std::vector<VariableName> & c,
500 const System & system,
501 std::vector<VariableName> & missing)
502{
503 unsigned int ncoupled = this->_coupled_standard_moose_vars.size();
504
505 // iterate over all variables in the current system (in groups)
506 for (unsigned int i = 0; i < system.n_variable_groups(); ++i)
507 {
508 const libMesh::VariableGroup & vg = system.variable_group(i);
509 for (unsigned int j = 0; j < vg.n_variables(); ++j)
510 {
511 std::vector<SymbolName> cj(c.begin(), c.end());
512 SymbolName jname = vg.name(j);
513 cj.push_back(jname);
514
515 // if the derivative exists make sure the variable is coupled
516 if (haveMaterialProperty<U, is_ad>(derivativePropertyName(base, cj)))
517 {
518 // kernels and BCs to not have the variable they are acting on in coupled_moose_vars
519 bool is_missing = isNotObjectVariable(jname);
520
521 for (unsigned int k = 0; k < ncoupled; ++k)
522 if (this->_coupled_standard_moose_vars[k]->name() == jname)
523 {
524 is_missing = false;
525 break;
526 }
527
528 if (is_missing)
529 missing.push_back(jname);
530 }
531 }
532 }
533}
534
535template <class T>
536template <typename U, bool is_ad>
537void
538DerivativeMaterialInterface<T>::validateCoupling(const MaterialPropertyName & base,
539 const std::vector<VariableName> & c,
540 bool validate_aux)
541{
542 // get the base property name
543 std::string prop_name = this->getMaterialPropertyName(base);
544 // list of potentially missing coupled variables
545 std::vector<VariableName> missing;
546
547 // iterate over all variables in the both the non-linear and auxiliary system (optional)
548 validateCouplingHelper<U, is_ad>(
549 prop_name, c, _dmi_fe_problem.getNonlinearSystemBase(/*nl_sys=*/0).system(), missing);
550 if (validate_aux)
551 validateCouplingHelper<U, is_ad>(
552 prop_name, c, _dmi_fe_problem.getAuxiliarySystem().system(), missing);
553
554 if (missing.size() > 0)
555 {
556 // join list of missing variable names
557 std::string list = missing[0];
558 for (unsigned int i = 1; i < missing.size(); ++i)
559 list += ", " + missing[i];
560
561 mooseWarning("Missing coupled variables {",
562 list,
563 "} (add them to coupled_variables parameter of ",
564 this->name(),
565 ")");
566 }
567}
568
569template <class T>
570std::vector<VariableName>
572 const VariableName & c2,
573 const VariableName & c3)
574{
575 std::vector<VariableName> c;
576 if (c1 != "")
577 {
578 c.push_back(c1);
579 if (c2 != "")
580 {
581 c.push_back(c2);
582 if (c3 != "")
583 c.push_back(c3);
584 }
585 }
586 return c;
587}
588
589template <class T>
590template <typename U, bool is_ad>
591void
592DerivativeMaterialInterface<T>::validateCoupling(const MaterialPropertyName & base,
593 const VariableName & c1,
594 const VariableName & c2,
595 const VariableName & c3)
596{
597 validateCoupling<U, is_ad>(base, buildVariableVector(c1, c2, c3), true);
598}
599
600template <class T>
601template <typename U, bool is_ad>
602void
604 const VariableName & c1,
605 const VariableName & c2,
606 const VariableName & c3)
607{
608 validateCoupling<U, is_ad>(base, buildVariableVector(c1, c2, c3), false);
609}
610
611template <class T>
612template <typename U, bool is_ad>
613void
615{
616 // resolve the input parameter name base to the actual material property name
617 const MaterialPropertyName prop_name = this->template getParam<MaterialPropertyName>(base);
618
619 // check if the material property does not exist on the blocks of the current object,
620 // and check if it is not a plain number in the input file
621 if (!haveMaterialProperty<U, is_ad>(prop_name) &&
622 this->template defaultGenericMaterialProperty<U, is_ad>(prop_name) == 0)
623 mooseWarning("The material property '",
624 prop_name,
625 "' does not exist. The kernel '",
626 this->name(),
627 "' only needs its derivatives, but this may indicate a typo in the input file.");
628}
629
630template <class T>
631inline bool
633{
634 // try to cast this to a Kernel pointer
635 KernelBase * kernel_ptr = dynamic_cast<KernelBase *>(this);
636 if (kernel_ptr != nullptr)
637 return kernel_ptr->variable().name() != name;
638
639 // try to cast this to a BoundaryCondition pointer
640 BoundaryCondition * bc_ptr = dynamic_cast<BoundaryCondition *>(this);
641 if (bc_ptr != nullptr)
642 return bc_ptr->variable().name() != name;
643
644 // This interface is not templated on a class derived from either Kernel or BC
645 return true;
646}
typename GenericMaterialPropertyStruct< T, is_ad >::type GenericMaterialProperty
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
An interface that restricts an object to subdomains via the 'blocks' input parameter.
Base class for creating new types of boundary conditions.
/class BoundaryRestrictable /brief Provides functionality for limiting the object to certain boundary...
virtual bool boundaryRestricted() const
Returns true if this object has been restricted to a boundary.
Interface class ("Veneer") to provide generator methods for derivative material property names.
const GenericMaterialProperty< U, is_ad > & getMaterialPropertyDerivative(const std::string &base, const SymbolName &c1, const SymbolName &c2="", const SymbolName &c3="")
std::vector< VariableName > buildVariableVector(const VariableName &c1, const VariableName &c2, const VariableName &c3)
helper method to combine multiple VariableNames into a vector (if they are != "")
DerivativeMaterialPropertyNameInterface::SymbolName SymbolName
void validateCoupling(const MaterialPropertyName &base, const VariableName &c1="", const VariableName &c2="", const VariableName &c3="")
const GenericMaterialProperty< U, is_ad > & getMaterialPropertyDerivative(const std::string &base, const std::vector< SymbolName > &c)
void validateCouplingHelper(const MaterialPropertyName &base, const std::vector< VariableName > &c, const System &system, std::vector< VariableName > &missing)
helper method to compile list of missing coupled variables for a given system
FEProblemBase & _dmi_fe_problem
Reference to FEProblemBase.
Moose::Kokkos::MaterialProperty< U, dimension > declareKokkosPropertyDerivative(const std::string &base, const std::vector< SymbolName > &c)
Declare a derivative Kokkos material property.
const GenericMaterialProperty< U, is_ad > & getMaterialPropertyDerivativeByName(const MaterialPropertyName &base, const std::vector< SymbolName > &c)
const GenericMaterialProperty< U, is_ad > & getDefaultMaterialPropertyByName(const std::string &name)
Fetch a material property by name if it exists, otherwise return getZeroMaterialProperty.
const GenericMaterialProperty< U, is_ad > & getMaterialPropertyDerivative(const std::string &base, const SymbolName &c1, unsigned int v2, unsigned int v3=libMesh::invalid_uint)
Convenience methods for retrieving derivative material properties based on a mix of variable names c ...
Moose::Kokkos::MaterialProperty< U, dimension > getKokkosMaterialPropertyDerivativeByName(const MaterialPropertyName &base, const std::vector< SymbolName > &c)
const GenericMaterialProperty< U, is_ad > & getMaterialPropertyDerivative(const std::string &base, unsigned int v1, unsigned int v2=libMesh::invalid_uint, unsigned int v3=libMesh::invalid_uint)
GenericMaterialProperty< U, is_ad > & declarePropertyDerivative(const std::string &base, const SymbolName &c1, const SymbolName &c2="", const SymbolName &c3="")
const GenericMaterialProperty< U, is_ad > & getMaterialPropertyDerivativeByName(const MaterialPropertyName &base, const std::vector< VariableName > &c)
Methods for retrieving derivative material properties.
DerivativeMaterialInterface(const InputParameters &parameters)
void validateNonlinearCoupling(const MaterialPropertyName &base, const VariableName &c1="", const VariableName &c2="", const VariableName &c3="")
bool haveMaterialProperty(const std::string &prop_name)
Check if a material property is present with the applicable restrictions.
Moose::Kokkos::MaterialProperty< U, dimension > getKokkosMaterialPropertyDerivative(const std::string &base, const std::vector< SymbolName > &c)
Methods for retrieving derivative Kokkos material properties.
bool isNotObjectVariable(const VariableName &name)
const GenericMaterialProperty< U, is_ad > & getMaterialPropertyDerivative(const std::string &base, const std::vector< VariableName > &c)
Methods for retrieving derivative material properties.
Moose::Kokkos::MaterialProperty< U, dimension > getKokkosMaterialPropertyDerivative(const std::string &base, const SymbolName &c1)
GenericMaterialProperty< U, is_ad > & declarePropertyDerivative(const std::string &base, const std::vector< SymbolName > &c)
const GenericMaterialProperty< U, is_ad > & getMaterialPropertyDerivativeByName(const MaterialPropertyName &base, const SymbolName &c1, const SymbolName &c2="", const SymbolName &c3="")
void validateCoupling(const MaterialPropertyName &base, const std::vector< VariableName > &c, bool validate_aux=true)
check if derivatives of the passed in material property exist w.r.t a variable that is not coupled in...
const GenericMaterialProperty< U, is_ad > & getDefaultMaterialProperty(const std::string &name)
Fetch a material property if it exists, otherwise return getZeroMaterialProperty.
void validateDerivativeMaterialPropertyBase(const std::string &base)
Check if the material property base exists.
GenericMaterialProperty< U, is_ad > & declarePropertyDerivative(const std::string &base, const std::vector< VariableName > &c)
Methods for declaring derivative material properties.
const MaterialPropertyName derivativePropertyName(const MaterialPropertyName &base, const std::vector< SymbolName > &c) const
Helper functions to generate the material property names for the arbitrary derivatives.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
This is the common base class for the three main kernel types implemented in MOOSE,...
Definition KernelBase.h:29
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
The Kokkos material property class.
virtual const MooseVariableBase & variable() const =0
Returns the variable that this object operates on.
unsigned int n_variables() const
const std::string & name(unsigned int v) const
bool parsesToReal(const std::string &input, Real *parsed_real)
Definition MooseUtils.C:152
const unsigned int invalid_uint