Skip to content

Commit

Permalink
Pass MooseApp pointer to NEML2 (#28486)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Aug 28, 2024
1 parent 97af19c commit ebfb80d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ template <class T>
template <typename... P>
NEML2ModelInterface<T>::NEML2ModelInterface(const InputParameters & params, P &&... args)
: T(params, args...),
_model(neml2::get_model(params.get<std::string>("model"), params.get<bool>("enable_AD"))),
_model(NEML2Utils::get_model_moose(
params.get<std::string>("model"), this, params.get<bool>("enable_AD"))),
_device(params.get<std::string>("device"))
{
}
Expand Down
8 changes: 7 additions & 1 deletion modules/solid_mechanics/include/neml2/utils/NEML2Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "InputParameters.h"

class MooseObject;
class MooseBase;
class Action;

#ifdef NEML2_ENABLED
Expand All @@ -43,6 +43,12 @@ namespace NEML2Utils

#ifdef NEML2_ENABLED

/// special version of get_model that adds a MooseApp parameter
neml2::Model & get_model_moose(const std::string & mname,
MooseBase * moose_base,
bool enable_ad = true,
bool force_create = true);

/// Map a variable name onto the old_xxx sub-axis
neml2::VariableName getOldName(const neml2::VariableName & var);

Expand Down
2 changes: 1 addition & 1 deletion modules/solid_mechanics/src/neml2/actions/NEML2Action.C
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ NEML2Action::act()

if (_verbose)
{
auto & model = neml2::get_model(_mname, _enable_AD);
auto & model = NEML2Utils::get_model_moose(_mname, this, _enable_AD);

_console << COLOR_YELLOW << "*** BEGIN NEML2 INFO ***" << std::endl;
_console << model << std::endl;
Expand Down
16 changes: 16 additions & 0 deletions modules/solid_mechanics/src/neml2/utils/NEML2Utils.C
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifdef NEML2_ENABLED

#include "VariadicTable.h"
#include "MooseBase.h"

namespace neml2
{
Expand Down Expand Up @@ -54,6 +55,21 @@ namespace NEML2Utils

#ifdef NEML2_ENABLED

neml2::Model &
get_model_moose(const std::string & mname,
MooseBase * moose_base,
bool enable_ad,
bool force_create)
{
neml2::OptionSet extra_opts;
extra_opts.set<MooseApp *>("_MooseApp") = &moose_base->getMooseApp();
extra_opts.set<bool>("_enable_AD") = enable_ad;
auto & model =
neml2::Factory::get_object<neml2::Model>("Models", mname, extra_opts, force_create);
model.reinit();
return model;
}

neml2::VariableName
getOldName(const neml2::VariableName & var)
{
Expand Down
6 changes: 6 additions & 0 deletions modules/solid_mechanics/test/neml2/src/NEML2TestModel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "NEML2TestModel.h"
#include "MooseApp.h"

namespace neml2
{
Expand All @@ -25,6 +26,7 @@ NEML2TestModel::expected_options()
// Use AD
options.set<bool>("_use_AD_first_derivative") = true;
options.set<bool>("_use_AD_second_derivative") = true;
options.set<MooseApp *>("_MooseApp") = nullptr;
return options;
}

Expand All @@ -37,6 +39,10 @@ NEML2TestModel::NEML2TestModel(const OptionSet & options)
_sum(declare_output_variable<Scalar>("sum")),
_product(declare_output_variable<Scalar>("product"))
{
const auto moose_app = options.get<MooseApp *>("_MooseApp");
neml_assert(moose_app,
"The _MooseApp option was not set. The model should be build using the "
"NEML2Utils::get_model_moose method!");
}

void
Expand Down

0 comments on commit ebfb80d

Please sign in to comment.