From 65aec0c8d0db6ce7936054c57e7b0dc508f6e8ad Mon Sep 17 00:00:00 2001 From: Guillaume Giudicelli Date: Fri, 18 Feb 2022 23:31:24 -0700 Subject: [PATCH] Update modules' transfers Refs #19451 --- .../src/transfers/MultiAppFXTransfer.C | 30 ++++++++++--------- .../LevelSetMeshRefinementTransfer.C | 14 ++++----- .../src/transfers/PODResidualTransfer.C | 4 +-- .../transfers/PODSamplerSolutionTransfer.C | 26 +++++++++------- .../src/transfers/SamplerParameterTransfer.C | 12 ++++---- .../transfers/SamplerPostprocessorTransfer.C | 18 +++++------ .../src/transfers/SamplerReporterTransfer.C | 22 +++++++------- .../src/transfers/StochasticToolsTransfer.C | 18 ++++++----- 8 files changed, 76 insertions(+), 68 deletions(-) diff --git a/modules/functional_expansion_tools/src/transfers/MultiAppFXTransfer.C b/modules/functional_expansion_tools/src/transfers/MultiAppFXTransfer.C index d4d8f585fcdd..3788cd737ad9 100644 --- a/modules/functional_expansion_tools/src/transfers/MultiAppFXTransfer.C +++ b/modules/functional_expansion_tools/src/transfers/MultiAppFXTransfer.C @@ -50,23 +50,25 @@ MultiAppFXTransfer::MultiAppFXTransfer(const InputParameters & parameters) void MultiAppFXTransfer::initialSetup() { + const auto multi_app = _from_multi_app ? _from_multi_app : _to_multi_app; + // Search for the _this_app_object_name in the LocalApp getMultiAppObject = - scanProblemBaseForObject(_multi_app->problemBase(), _this_app_object_name, "MultiApp"); + scanProblemBaseForObject(multi_app->problemBase(), _this_app_object_name, "MultiApp"); if (getMultiAppObject == NULL) mooseError( "Transfer '", name(), "': Cannot find object '", _this_app_object_name, "' in MultiApp"); // Search for the _multi_app_object_name in each of the MultiApps - for (std::size_t i = 0; i < _multi_app->numGlobalApps(); ++i) - if (_multi_app->hasLocalApp(i)) + for (std::size_t i = 0; i < multi_app->numGlobalApps(); ++i) + if (multi_app->hasLocalApp(i)) { if (i == 0) // First time through, assign without checking against previous values getSubAppObject = scanProblemBaseForObject( - _multi_app->appProblemBase(i), _multi_app_object_name, _multi_app->name()); - else if (getSubAppObject != scanProblemBaseForObject(_multi_app->appProblemBase(i), + multi_app->appProblemBase(i), _multi_app_object_name, multi_app->name()); + else if (getSubAppObject != scanProblemBaseForObject(multi_app->appProblemBase(i), _multi_app_object_name, - _multi_app->name())) + multi_app->name())) mooseError("The name '", _multi_app_object_name, "' is assigned to two different object types. Please modify your input file and " @@ -164,16 +166,16 @@ MultiAppFXTransfer::execute() { // Get a reference to the object in the LocalApp const MutableCoefficientsInterface & from_object = - (this->*getMultiAppObject)(_multi_app->problemBase(), _this_app_object_name, 0); + (this->*getMultiAppObject)(_to_multi_app->problemBase(), _this_app_object_name, 0); - for (unsigned int i = 0; i < _multi_app->numGlobalApps(); ++i) + for (unsigned int i = 0; i < _to_multi_app->numGlobalApps(); ++i) { - if (_multi_app->hasLocalApp(i)) + if (_to_multi_app->hasLocalApp(i)) for (THREAD_ID t = 0; t < libMesh::n_threads(); ++t) { // Get a reference to the object in each MultiApp - MutableCoefficientsInterface & to_object = - (this->*getSubAppObject)(_multi_app->appProblemBase(i), _multi_app_object_name, t); + MutableCoefficientsInterface & to_object = (this->*getSubAppObject)( + _to_multi_app->appProblemBase(i), _multi_app_object_name, t); if (to_object.isCompatibleWith(from_object)) to_object.importCoefficients(from_object); @@ -196,17 +198,17 @@ MultiAppFXTransfer::execute() * among all instances, thus we only need to grab the set of coefficients from the first * SubApp. */ - if (_multi_app->hasLocalApp(0)) + if (_from_multi_app->hasLocalApp(0)) { // Get a reference to the first thread object in the first MultiApp const MutableCoefficientsInterface & from_object = - (this->*getSubAppObject)(_multi_app->appProblemBase(0), _multi_app_object_name, 0); + (this->*getSubAppObject)(_from_multi_app->appProblemBase(0), _multi_app_object_name, 0); for (THREAD_ID t = 0; t < libMesh::n_threads(); ++t) { // Get a reference to the object in each LocalApp instance MutableCoefficientsInterface & to_object = - (this->*getMultiAppObject)(_multi_app->problemBase(), _this_app_object_name, t); + (this->*getMultiAppObject)(_from_multi_app->problemBase(), _this_app_object_name, t); if (to_object.isCompatibleWith(from_object)) to_object.importCoefficients(from_object); diff --git a/modules/level_set/src/transfers/LevelSetMeshRefinementTransfer.C b/modules/level_set/src/transfers/LevelSetMeshRefinementTransfer.C index 74373f064df2..7dd8122dacc7 100644 --- a/modules/level_set/src/transfers/LevelSetMeshRefinementTransfer.C +++ b/modules/level_set/src/transfers/LevelSetMeshRefinementTransfer.C @@ -45,11 +45,11 @@ LevelSetMeshRefinementTransfer::LevelSetMeshRefinementTransfer(const InputParame void LevelSetMeshRefinementTransfer::initialSetup() { - FEProblemBase & from_problem = _multi_app->problemBase(); - for (unsigned int i = 0; i < _multi_app->numGlobalApps(); i++) - if (_multi_app->hasLocalApp(i)) + FEProblemBase & from_problem = _to_multi_app->problemBase(); + for (unsigned int i = 0; i < _to_multi_app->numGlobalApps(); i++) + if (_to_multi_app->hasLocalApp(i)) { - FEProblemBase & to_problem = _multi_app->appProblemBase(i); + FEProblemBase & to_problem = _to_multi_app->appProblemBase(i); MooseVariable & to_var = to_problem.getStandardVariable(0, _to_var_name); Adaptivity & adapt = to_problem.adaptivity(); adapt.setMarkerVariableName(to_var.name()); @@ -69,10 +69,10 @@ LevelSetMeshRefinementTransfer::execute() else if (_current_execute_flag == LevelSet::EXEC_ADAPT_MESH) { - for (unsigned int i = 0; i < _multi_app->numGlobalApps(); i++) - if (_multi_app->hasLocalApp(i)) + for (unsigned int i = 0; i < _to_multi_app->numGlobalApps(); i++) + if (_to_multi_app->hasLocalApp(i)) { - FEProblemBase & to_problem = _multi_app->appProblemBase(i); + FEProblemBase & to_problem = _to_multi_app->appProblemBase(i); Adaptivity & adapt = to_problem.adaptivity(); adapt.setAdaptivityOn(true); to_problem.adaptMesh(); diff --git a/modules/stochastic_tools/src/transfers/PODResidualTransfer.C b/modules/stochastic_tools/src/transfers/PODResidualTransfer.C index dba2c03ade17..32c6e6427530 100644 --- a/modules/stochastic_tools/src/transfers/PODResidualTransfer.C +++ b/modules/stochastic_tools/src/transfers/PODResidualTransfer.C @@ -36,7 +36,7 @@ PODResidualTransfer::execute() // Looping over sub-apps for (unsigned int base_i = 0; base_i < total_base_num; ++base_i) - if (_multi_app->hasLocalApp(base_i)) + if (_from_multi_app->hasLocalApp(base_i)) transferResidual(base_i, base_i); } @@ -54,7 +54,7 @@ PODResidualTransfer::transferResidual(dof_id_type base_i, dof_id_type multi_app_ const std::vector & tag_types = _trainer.getTagTypes(); // Getting reference to the non-linear system - FEProblemBase & app_problem = _multi_app->appProblemBase(multi_app_i); + FEProblemBase & app_problem = _from_multi_app->appProblemBase(multi_app_i); NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(); // Looping over the residual tags and extracting the corresponding vector. diff --git a/modules/stochastic_tools/src/transfers/PODSamplerSolutionTransfer.C b/modules/stochastic_tools/src/transfers/PODSamplerSolutionTransfer.C index e1a864e793fd..dc4e1c6ee834 100644 --- a/modules/stochastic_tools/src/transfers/PODSamplerSolutionTransfer.C +++ b/modules/stochastic_tools/src/transfers/PODSamplerSolutionTransfer.C @@ -29,7 +29,9 @@ PODSamplerSolutionTransfer::validParams() PODSamplerSolutionTransfer::PODSamplerSolutionTransfer(const InputParameters & parameters) : StochasticToolsTransfer(parameters), SurrogateModelInterface(this), - _pod_multi_app(std::dynamic_pointer_cast(_multi_app)), + _pod_multi_app(_from_multi_app + ? std::dynamic_pointer_cast(_from_multi_app) + : std::dynamic_pointer_cast(_to_multi_app)), _trainer(getSurrogateTrainer("trainer_name")) { if (!_pod_multi_app) @@ -39,14 +41,16 @@ PODSamplerSolutionTransfer::PODSamplerSolutionTransfer(const InputParameters & p void PODSamplerSolutionTransfer::initialSetup() { + const auto multi_app = _from_multi_app ? _from_multi_app : _to_multi_app; + // Checking if the subapplication has the requested variables const std::vector & var_names = _trainer.getVarNames(); - const dof_id_type n = _multi_app->numGlobalApps(); + const dof_id_type n = multi_app->numGlobalApps(); for (MooseIndex(n) i = 0; i < n; i++) { - if (_multi_app->hasLocalApp(i)) + if (multi_app->hasLocalApp(i)) for (auto var_name : var_names) - if (!_multi_app->appProblemBase(i).hasVariable(var_name)) + if (!multi_app->appProblemBase(i).hasVariable(var_name)) mooseError("Variable '" + var_name + "' not found on sub-application ", i, "!"); } } @@ -67,7 +71,7 @@ PODSamplerSolutionTransfer::execute() ++i) { // Getting reference to the solution vector of the sub-app. - FEProblemBase & app_problem = _multi_app->appProblemBase(i); + FEProblemBase & app_problem = _from_multi_app->appProblemBase(i); NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(); NumericVector & solution = nl.solution(); @@ -101,10 +105,10 @@ PODSamplerSolutionTransfer::execute() unsigned int var_base_num = _trainer.getBaseSize(var_i); for (unsigned int base_i = 0; base_i < var_base_num; ++base_i) { - if (_multi_app->hasLocalApp(counter)) + if (_to_multi_app->hasLocalApp(counter)) { // Getting the reference to the solution vector in the subapp. - FEProblemBase & app_problem = _multi_app->appProblemBase(counter); + FEProblemBase & app_problem = _to_multi_app->appProblemBase(counter); NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(); NumericVector & solution = nl.solution(); @@ -144,14 +148,14 @@ PODSamplerSolutionTransfer::executeFromMultiapp() { const std::vector & var_names = _trainer.getVarNames(); - const dof_id_type n = _multi_app->numGlobalApps(); + const dof_id_type n = _from_multi_app->numGlobalApps(); for (MooseIndex(n) i = 0; i < n; i++) { - if (_multi_app->hasLocalApp(i)) + if (_from_multi_app->hasLocalApp(i)) { // Getting reference to the solution vector of the sub-app. - FEProblemBase & app_problem = _multi_app->appProblemBase(i); + FEProblemBase & app_problem = _from_multi_app->appProblemBase(i); NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(); NumericVector & solution = nl.solution(); @@ -194,7 +198,7 @@ PODSamplerSolutionTransfer::executeToMultiapp() dof_id_type var_i = _trainer.getVariableIndex(_global_index); // Getting the reference to the solution vector in the subapp. - FEProblemBase & app_problem = _multi_app->appProblemBase(processor_id()); + FEProblemBase & app_problem = _to_multi_app->appProblemBase(processor_id()); NonlinearSystemBase & nl = app_problem.getNonlinearSystemBase(); NumericVector & solution = nl.solution(); diff --git a/modules/stochastic_tools/src/transfers/SamplerParameterTransfer.C b/modules/stochastic_tools/src/transfers/SamplerParameterTransfer.C index d7c8458425ea..a7ebe325bb2d 100644 --- a/modules/stochastic_tools/src/transfers/SamplerParameterTransfer.C +++ b/modules/stochastic_tools/src/transfers/SamplerParameterTransfer.C @@ -49,7 +49,7 @@ void SamplerParameterTransfer::execute() { mooseAssert((_sampler_ptr->getNumberOfLocalRows() == 0) || - (_sampler_ptr->getNumberOfLocalRows() == _multi_app->numLocalApps()), + (_sampler_ptr->getNumberOfLocalRows() == _to_multi_app->numLocalApps()), "The number of MultiApps and the number of sample rows must be the same."); // Loop over all sub-apps @@ -57,7 +57,7 @@ SamplerParameterTransfer::execute() row_index < _sampler_ptr->getLocalRowEnd(); row_index++) { - mooseAssert(_multi_app->hasLocalApp(row_index), + mooseAssert(_to_multi_app->hasLocalApp(row_index), "The current sample row index is not a valid global MultiApp index."); // Get the sub-app SamplerReceiver object and perform error checking @@ -74,7 +74,7 @@ SamplerParameterTransfer::execute() void SamplerParameterTransfer::executeToMultiapp() { - if (_multi_app->isRootProcessor()) + if (_to_multi_app->isRootProcessor()) { SamplerReceiver * ptr = getReceiver(_app_index); ptr->transfer(_parameter_names, _row_data); @@ -85,11 +85,11 @@ SamplerReceiver * SamplerParameterTransfer::getReceiver(unsigned int app_index) { // Test that the sub-application has the given Control object - FEProblemBase & to_problem = _multi_app->appProblemBase(app_index); + FEProblemBase & to_problem = _to_multi_app->appProblemBase(app_index); ExecuteMooseObjectWarehouse & control_wh = to_problem.getControlWarehouse(); if (!control_wh.hasActiveObject(_receiver_name)) mooseError("The sub-application (", - _multi_app->name(), + _to_multi_app->name(), ") does not contain a Control object with the name '", _receiver_name, "'."); @@ -100,7 +100,7 @@ SamplerParameterTransfer::getReceiver(unsigned int app_index) if (!ptr) mooseError( "The sub-application (", - _multi_app->name(), + _to_multi_app->name(), ") Control object for the 'to_control' parameter must be of type 'SamplerReceiver'."); return ptr; diff --git a/modules/stochastic_tools/src/transfers/SamplerPostprocessorTransfer.C b/modules/stochastic_tools/src/transfers/SamplerPostprocessorTransfer.C index 65c90cd22d42..211d3a730682 100644 --- a/modules/stochastic_tools/src/transfers/SamplerPostprocessorTransfer.C +++ b/modules/stochastic_tools/src/transfers/SamplerPostprocessorTransfer.C @@ -87,18 +87,18 @@ SamplerPostprocessorTransfer::initialSetup() mooseError("The 'results' object must be a 'StochasticResults' object."); // Check that postprocessor on sub-application exists and create vectors on results VPP - const dof_id_type n = _multi_app->numGlobalApps(); + const dof_id_type n = _from_multi_app->numGlobalApps(); for (MooseIndex(n) i = 0; i < n; i++) { - if (_multi_app->hasLocalApp(i)) + if (_from_multi_app->hasLocalApp(i)) { - FEProblemBase & app_problem = _multi_app->appProblemBase(i); + FEProblemBase & app_problem = _from_multi_app->appProblemBase(i); for (const auto & sub_pp_name : _sub_pp_names) if (!app_problem.hasPostprocessorValueByName(sub_pp_name)) mooseError("Unknown postprocesssor name '", sub_pp_name, "' on sub-application '", - _multi_app->name(), + _from_multi_app->name(), "'"); } } @@ -120,14 +120,14 @@ SamplerPostprocessorTransfer::initializeFromMultiapp() void SamplerPostprocessorTransfer::executeFromMultiapp() { - if (_multi_app->isRootProcessor()) + if (_from_multi_app->isRootProcessor()) { - const dof_id_type n = _multi_app->numGlobalApps(); + const dof_id_type n = _from_multi_app->numGlobalApps(); for (MooseIndex(n) i = 0; i < n; i++) { - if (_multi_app->hasLocalApp(i)) + if (_from_multi_app->hasLocalApp(i)) { - FEProblemBase & app_problem = _multi_app->appProblemBase(i); + FEProblemBase & app_problem = _from_multi_app->appProblemBase(i); if (app_problem.converged() || _keep_diverge) for (std::size_t j = 0; j < _sub_pp_names.size(); ++j) _current_data[j].emplace_back( @@ -159,7 +159,7 @@ SamplerPostprocessorTransfer::execute() current.reserve(_sampler_ptr->getNumberOfLocalRows()); for (dof_id_type i = _sampler_ptr->getLocalRowBegin(); i < _sampler_ptr->getLocalRowEnd(); ++i) { - FEProblemBase & app_problem = _multi_app->appProblemBase(i); + FEProblemBase & app_problem = _from_multi_app->appProblemBase(i); if (app_problem.converged() || _keep_diverge) current.emplace_back(app_problem.getPostprocessorValueByName(_sub_pp_names[j])); else diff --git a/modules/stochastic_tools/src/transfers/SamplerReporterTransfer.C b/modules/stochastic_tools/src/transfers/SamplerReporterTransfer.C index 3c94568ecb8b..ed91a784b49f 100644 --- a/modules/stochastic_tools/src/transfers/SamplerReporterTransfer.C +++ b/modules/stochastic_tools/src/transfers/SamplerReporterTransfer.C @@ -66,9 +66,9 @@ SamplerReporterTransfer::initializeFromMultiapp() void SamplerReporterTransfer::executeFromMultiapp() { - if (_multi_app->isRootProcessor()) + if (_from_multi_app->isRootProcessor()) { - const dof_id_type n = _multi_app->numGlobalApps(); + const dof_id_type n = _from_multi_app->numGlobalApps(); for (MooseIndex(n) i = 0; i < n; i++) transferStochasticReporters(_global_index, i); } @@ -89,19 +89,19 @@ SamplerReporterTransfer::execute() void SamplerReporterTransfer::intitializeStochasticReporters() { - const dof_id_type n = _multi_app->numGlobalApps(); + const dof_id_type n = _from_multi_app->numGlobalApps(); for (const auto & sub_rname : _sub_reporter_names) for (MooseIndex(n) i = 0; i < n; i++) - if (_multi_app->hasLocalApp(i)) - addReporterTransferMode(sub_rname, REPORTER_MODE_ROOT, _multi_app->appProblemBase(i)); + if (_from_multi_app->hasLocalApp(i)) + addReporterTransferMode(sub_rname, REPORTER_MODE_ROOT, _from_multi_app->appProblemBase(i)); const std::string prefix = isParamValid("prefix") ? getParam("prefix") : name(); for (const auto & sub_rname : _sub_reporter_names) for (MooseIndex(n) i = 0; i < n; i++) - if (_multi_app->hasLocalApp(i)) + if (_from_multi_app->hasLocalApp(i)) { - const ReporterData & rdata = _multi_app->appProblemBase(i).getReporterData(); + const ReporterData & rdata = _from_multi_app->appProblemBase(i).getReporterData(); ReporterName rname = _results->declareStochasticReporterClone(*_sampler_ptr, rdata, sub_rname, prefix); if (rname.empty()) @@ -122,16 +122,16 @@ void SamplerReporterTransfer::transferStochasticReporters(dof_id_type global_index, dof_id_type app_index) { - if (_multi_app->hasLocalApp(app_index)) + if (_from_multi_app->hasLocalApp(app_index)) { const dof_id_type local_index = global_index - _sampler_ptr->getLocalRowBegin(); for (unsigned int r = 0; r < _sub_reporter_names.size(); ++r) transferToVectorReporter(_sub_reporter_names[r], _reporter_names[r], - _multi_app->appProblemBase(app_index), - _multi_app->problemBase(), + _from_multi_app->appProblemBase(app_index), + _from_multi_app->problemBase(), local_index); - (*_converged)[local_index] = _multi_app->appProblemBase(app_index).converged(); + (*_converged)[local_index] = _from_multi_app->appProblemBase(app_index).converged(); } } diff --git a/modules/stochastic_tools/src/transfers/StochasticToolsTransfer.C b/modules/stochastic_tools/src/transfers/StochasticToolsTransfer.C index 257d767810ac..6e6c2be5bfb5 100644 --- a/modules/stochastic_tools/src/transfers/StochasticToolsTransfer.C +++ b/modules/stochastic_tools/src/transfers/StochasticToolsTransfer.C @@ -24,21 +24,23 @@ StochasticToolsTransfer::validParams() StochasticToolsTransfer::StochasticToolsTransfer(const InputParameters & parameters) : MultiAppTransfer(parameters), SamplerInterface(this) { + const auto multi_app = _from_multi_app ? _from_multi_app : _to_multi_app; + // When the MultiApp is running in batch mode the execute flags for the transfer object must // be removed. If not the 'regular' transfer that occurs will potentially destroy data // populated during the calls from the MultiApp in batch mode. To prevent the Transfer from // running the execute flags must be removed. This is done automatically here, unless // 'execute_on' was modified by the user, which an error is produced. - if (_multi_app->isParamValid("mode") && - (_multi_app->getParam("mode") == "batch-reset" || - _multi_app->getParam("mode") == "batch-restore")) + if (multi_app->isParamValid("mode") && + (multi_app->getParam("mode") == "batch-reset" || + multi_app->getParam("mode") == "batch-restore")) { if (parameters.isParamSetByUser("execute_on")) paramError("execute_on", "The 'execute_on' parameter for the '", name(), "' transfer was set, but the parent MultiApp object (", - _multi_app->name(), + multi_app->name(), ") is running in 'batch' mode. For this case the 'execute_on' parameter must not " "be set by the user or set to NONE."); else @@ -66,9 +68,9 @@ StochasticToolsTransfer::StochasticToolsTransfer(const InputParameters & paramet _sampler_ptr = &(getSampler("sampler")); SamplerTransientMultiApp * ptr_transient = - dynamic_cast(_multi_app.get()); + dynamic_cast(multi_app.get()); SamplerFullSolveMultiApp * ptr_fullsolve = - dynamic_cast(_multi_app.get()); + dynamic_cast(multi_app.get()); if (!ptr_transient && !ptr_fullsolve) mooseError("The 'multi_app' parameter must provide either a 'SamplerTransientMultiApp' or " @@ -86,9 +88,9 @@ StochasticToolsTransfer::StochasticToolsTransfer(const InputParameters & paramet "parameter is being removed, please update your input file(s)."); std::shared_ptr ptr_transient = - std::dynamic_pointer_cast(_multi_app); + std::dynamic_pointer_cast(multi_app); std::shared_ptr ptr_fullsolve = - std::dynamic_pointer_cast(_multi_app); + std::dynamic_pointer_cast(multi_app); if (!ptr_transient && !ptr_fullsolve) mooseError("The 'multi_app' parameter must provide either a 'SamplerTransientMultiApp' or "