-
Notifications
You must be signed in to change notification settings - Fork 1.2k
MFEM Parsed function of non-linear variable #31134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nmnobre
merged 6 commits into
idaholab:next
from
karthichockalingam:ab_sr_kc/parsed-function
Nov 7, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac71d05
Add parsed function and coefficient classes for MFEM problems (#31134)
karthichockalingam 48d066e
Add documentation for parsed function functionality (#31134)
karthichockalingam 10dec02
Add test for parsed function functionality (#31134)
karthichockalingam b1d4cd5
Reframe MFEMParsedFunction as a Function not a UserObject (#31134)
nmnobre 3be1edc
Improve documentation for parsed function functionality (#31134)
nmnobre 9a927c6
Reduce parsed function test footprint w/ manufactured solution (#31134)
nmnobre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
framework/doc/content/source/mfem/functions/MFEMParsedFunction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # MFEMParsedFunction | ||
|
|
||
| !if! function=hasCapability('mfem') | ||
|
|
||
| ## Summary | ||
|
|
||
| !syntax description /Functions/MFEMParsedFunction | ||
|
|
||
| ## Overview | ||
|
|
||
| `MFEMParsedFunction` is responsible for parsing expressions that, in addition | ||
| to being scalar functions of position and time, can also depend on scalar | ||
| coefficients, including any scalar variables, postprocessors, material | ||
| properties or functions specified by the user. | ||
|
|
||
| The input parameters are those of [ParsedFunction](/MooseParsedFunction.md) | ||
| and provide the same flexibility. Note that, in the context of MFEM problems, | ||
| a scalar variable is a real-valued scalar field, not (necessarily) a constant | ||
| over the entire domain as elsewhere in MOOSE. | ||
|
|
||
| ## Example Input File Syntax | ||
|
|
||
| !listing test/tests/mfem/functions/parsed_function_source.i block=Functions | ||
|
|
||
| !syntax parameters /Functions/MFEMParsedFunction | ||
|
|
||
| !syntax inputs /Functions/MFEMParsedFunction | ||
|
|
||
| !syntax children /Functions/MFEMParsedFunction | ||
|
|
||
| !if-end! | ||
|
|
||
| !else | ||
| !include mfem/mfem_warning.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //* This file is part of the MOOSE framework | ||
| //* https://mooseframework.inl.gov | ||
| //* | ||
| //* All rights reserved, see COPYRIGHT for full restrictions | ||
| //* https://github.com/idaholab/moose/blob/master/COPYRIGHT | ||
| //* | ||
| //* Licensed under LGPL 2.1, please see LICENSE for details | ||
| //* https://www.gnu.org/licenses/lgpl-2.1.html | ||
|
|
||
| #ifdef MOOSE_MFEM_ENABLED | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "FunctionParserUtils.h" | ||
|
|
||
| /** | ||
| * Scalar coefficient that, given a set of scalar (possibly, but not necessarily, gridfunction) | ||
| * coefficients u, v, w, ..., and a scalar, parsed function f, returns f(u, v, w, ..., x, y, z, t) | ||
| */ | ||
| class MFEMParsedCoefficient : public mfem::Coefficient | ||
| { | ||
| private: | ||
| const std::vector<std::reference_wrapper<mfem::Coefficient>> & _coefficients; | ||
| const FunctionParserUtils<false>::SymFunctionPtr & _sym_function; | ||
| mfem::Array<mfem::real_t> _vals; | ||
| mfem::Vector _transip; | ||
|
|
||
| public: | ||
| MFEMParsedCoefficient(const unsigned & arity, | ||
| const std::vector<std::reference_wrapper<mfem::Coefficient>> & coefs, | ||
| const FunctionParserUtils<false>::SymFunctionPtr & sym_function); | ||
|
|
||
| mfem::real_t Eval(mfem::ElementTransformation & T, const mfem::IntegrationPoint & ip) override; | ||
| }; | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| //* This file is part of the MOOSE framework | ||
| //* https://mooseframework.inl.gov | ||
| //* | ||
| //* All rights reserved, see COPYRIGHT for full restrictions | ||
| //* https://github.com/idaholab/moose/blob/master/COPYRIGHT | ||
| //* | ||
| //* Licensed under LGPL 2.1, please see LICENSE for details | ||
| //* https://www.gnu.org/licenses/lgpl-2.1.html | ||
|
|
||
| #ifdef MOOSE_MFEM_ENABLED | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "MooseParsedFunction.h" | ||
| #include "FunctionParserUtils.h" | ||
| #include "MFEMProblem.h" | ||
|
|
||
| /** | ||
| * Scalar, parsed function of position, time, and any number of scalar problem coefficients, | ||
| * including any scalar variables, postprocessors, material properties or functions | ||
| */ | ||
| class MFEMParsedFunction : public MooseParsedFunction, public FunctionParserUtils<false> | ||
| { | ||
| public: | ||
| static InputParameters validParams(); | ||
|
|
||
| MFEMParsedFunction(const InputParameters & parameters); | ||
| virtual ~MFEMParsedFunction() = default; | ||
|
|
||
| void initialSetup() override; | ||
|
|
||
| protected: | ||
| /// reference to the MFEMProblem instance | ||
| MFEMProblem & _mfem_problem; | ||
| /// function parser object | ||
| SymFunctionPtr _sym_function; | ||
| /// coordinate and time variable names | ||
| const std::vector<std::string> _xyzt; | ||
| /// vector of references to the scalar coefficients used in the function | ||
| std::vector<std::reference_wrapper<mfem::Coefficient>> _coefficients; | ||
| }; | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| //* This file is part of the MOOSE framework | ||
| //* https://mooseframework.inl.gov | ||
| //* | ||
| //* All rights reserved, see COPYRIGHT for full restrictions | ||
| //* https://github.com/idaholab/moose/blob/master/COPYRIGHT | ||
| //* | ||
| //* Licensed under LGPL 2.1, please see LICENSE for details | ||
| //* https://www.gnu.org/licenses/lgpl-2.1.html | ||
|
|
||
| #ifdef MOOSE_MFEM_ENABLED | ||
|
|
||
| #include "MFEMParsedCoefficient.h" | ||
|
|
||
| MFEMParsedCoefficient::MFEMParsedCoefficient( | ||
| const unsigned & arity, | ||
| const std::vector<std::reference_wrapper<mfem::Coefficient>> & coefficients, | ||
| const FunctionParserUtils<false>::SymFunctionPtr & sym_function) | ||
| : _coefficients(coefficients), _sym_function(sym_function), _vals(arity), _transip(3) | ||
| { | ||
| } | ||
|
|
||
| mfem::real_t | ||
| MFEMParsedCoefficient::Eval(mfem::ElementTransformation & T, const mfem::IntegrationPoint & ip) | ||
| { | ||
| for (unsigned i = 0; i < _coefficients.size(); i++) | ||
| _vals[i] = _coefficients[i].get().Eval(T, ip); | ||
|
|
||
| T.Transform(ip, _transip); | ||
|
|
||
| for (int i = 0; i < 3; i++) | ||
| _vals[_coefficients.size() + i] = i < _transip.Size() ? _transip(i) : 0.; | ||
|
|
||
| _vals[_coefficients.size() + 3] = GetTime(); | ||
|
|
||
| return _sym_function->Eval(_vals.GetData()); | ||
| } | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //* This file is part of the MOOSE framework | ||
| //* https://mooseframework.inl.gov | ||
| //* | ||
| //* All rights reserved, see COPYRIGHT for full restrictions | ||
| //* https://github.com/idaholab/moose/blob/master/COPYRIGHT | ||
| //* | ||
| //* Licensed under LGPL 2.1, please see LICENSE for details | ||
| //* https://www.gnu.org/licenses/lgpl-2.1.html | ||
|
|
||
| #ifdef MOOSE_MFEM_ENABLED | ||
karthichockalingam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #include "MFEMParsedFunction.h" | ||
| #include "MFEMParsedCoefficient.h" | ||
|
|
||
| registerMooseObject("MooseApp", MFEMParsedFunction); | ||
|
|
||
| InputParameters | ||
| MFEMParsedFunction::validParams() | ||
| { | ||
| InputParameters params = MooseParsedFunction::validParams(); | ||
| params += FunctionParserUtils<false>::validParams(); | ||
| params.addClassDescription("Parses scalar function of position, time and scalar " | ||
| "problem coefficients (including scalar variables)."); | ||
| return params; | ||
| } | ||
|
|
||
| MFEMParsedFunction::MFEMParsedFunction(const InputParameters & parameters) | ||
| : MooseParsedFunction(parameters), | ||
| FunctionParserUtils(parameters), | ||
| _mfem_problem(static_cast<MFEMProblem &>(_pfb_feproblem)), | ||
| _sym_function(std::make_shared<SymFunction>()), | ||
| _xyzt({"x", "y", "z", "t"}) | ||
| { | ||
| // variable symbols the function depends on (including position and time) | ||
| std::string symbols = MooseUtils::stringJoin({_vars.begin(), _vars.end()}, ","); | ||
| symbols += (symbols.empty() ? "" : ",") + MooseUtils::stringJoin(_xyzt, ","); | ||
|
|
||
| // setup parsed function | ||
| parsedFunctionSetup(_sym_function, _value, symbols, {}, {}, comm()); | ||
|
|
||
| // create MFEMParsedCoefficient | ||
| _mfem_problem.getCoefficients().declareScalar<MFEMParsedCoefficient>( | ||
| name(), _vars.size() + _xyzt.size(), _coefficients, _sym_function); | ||
| } | ||
|
|
||
| void | ||
| MFEMParsedFunction::initialSetup() | ||
| { | ||
| for (const auto i : index_range(_vars)) | ||
| _coefficients.push_back(_mfem_problem.getCoefficients().getScalarCoefficient(_vals[i])); | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
test/tests/mfem/functions/gold/OutputData/ParsedFunctionSource.csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| time,error | ||
| 0,0 | ||
| 1,0.21586854778503 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| [Mesh] | ||
| type = MFEMMesh | ||
| file = ../mesh/hinomaru.e | ||
| [] | ||
|
|
||
| [Problem] | ||
| type = MFEMProblem | ||
| [] | ||
|
|
||
| [FESpaces] | ||
| [H1FESpace] | ||
| type = MFEMScalarFESpace | ||
| fec_type = H1 | ||
| fec_order = FIRST | ||
| [] | ||
| [] | ||
|
|
||
| [Variables] | ||
| [variable] | ||
| type = MFEMVariable | ||
| fespace = H1FESpace | ||
| [] | ||
| [] | ||
|
|
||
| [ICs] | ||
| [ic] | ||
| type = MFEMScalarIC | ||
| variable = variable | ||
| coefficient = material | ||
| [] | ||
| [] | ||
|
|
||
| [BCs] | ||
| [bc] | ||
| type = MFEMScalarDirichletBC | ||
| variable = variable | ||
| boundary = skin | ||
| [] | ||
| [] | ||
|
|
||
| [FunctorMaterials] | ||
| [material] | ||
| type = MFEMGenericFunctorMaterial | ||
| prop_names = material | ||
| prop_values = -100 | ||
| [] | ||
| [] | ||
|
|
||
| [Functions] | ||
| [r] | ||
| type = ParsedFunction | ||
| expression = hypot(x,y) | ||
| [] | ||
| [p] | ||
| type = ParsedFunction | ||
| expression = atan2(y,x) | ||
| [] | ||
| [source] | ||
| type = MFEMParsedFunction | ||
| expression = v*sin(w*p) | ||
| symbol_names = 'p w v' | ||
| symbol_values = 'p 4 variable' | ||
| [] | ||
| [solution] | ||
| type = MFEMParsedFunction | ||
| expression = if(r<=1,-c*sin(w*p)*(r^w-r^2)/(w^2-4),0) | ||
| symbol_names = 'r p w c' | ||
| symbol_values = 'r p 4 material' | ||
| [] | ||
| [] | ||
|
|
||
| [Kernels] | ||
| [diff] | ||
| type = MFEMDiffusionKernel | ||
| variable = variable | ||
| [] | ||
| [source] | ||
| type = MFEMDomainLFKernel | ||
| variable = variable | ||
| coefficient = source | ||
| block = wire | ||
| [] | ||
| [] | ||
|
|
||
| [Preconditioner] | ||
| [boomeramg] | ||
| type = MFEMHypreBoomerAMG | ||
| [] | ||
| [] | ||
|
|
||
| [Solver] | ||
| type = MFEMHyprePCG | ||
| preconditioner = boomeramg | ||
| l_tol = 1e-16 | ||
| [] | ||
|
|
||
| [Executioner] | ||
| type = MFEMSteady | ||
| device = cpu | ||
| [] | ||
|
|
||
| [Postprocessors] | ||
| [error] | ||
| type = MFEML2Error | ||
| variable = variable | ||
| function = solution | ||
| [] | ||
| [] | ||
|
|
||
| [Outputs] | ||
| csv = true | ||
| file_base = OutputData/ParsedFunctionSource | ||
| [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [Tests] | ||
| [MFEMParsedFunction] | ||
| type = CSVDiff | ||
| input = parsed_function_source.i | ||
| csvdiff = OutputData/ParsedFunctionSource.csv | ||
| design = 'MFEMParsedFunction.md' | ||
| issues = '#31145' | ||
| requirement = 'The system shall have the ability to parse non-linear functions of problem variables' | ||
| capabilities = 'mfem' | ||
| compute_devices = 'cpu cuda' | ||
| recover = false | ||
| [] | ||
| [] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.