-
Notifications
You must be signed in to change notification settings - Fork 249
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
[ROM] Unify ROM solver #9490
Merged
Merged
[ROM] Unify ROM solver #9490
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f8d983b
Deprecate old solvers and wrapper
rubenzorrilla d30f891
New generic ROM solver
rubenzorrilla 8d8e3e8
Minor simplification
rubenzorrilla ba4884d
Minor simplification
rubenzorrilla 6ac258b
Merge branch 'rom/unify-rom-solver' of https://github.com/KratosMulti…
rubenzorrilla d5c15ba
Using isinstance
rubenzorrilla 346e182
Fix syntax
rubenzorrilla 481d1b7
Fix fix syntax
rubenzorrilla 2e168f4
Merge branch 'master' into rom/unify-rom-solver
rubenzorrilla 73edefa
Update rom_analysis.py
Rbravo555 9071a16
Update applications/RomApplication/python_scripts/rom_solver.py
Rbravo555 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
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
61 changes: 61 additions & 0 deletions
61
applications/RomApplication/python_scripts/new_python_solvers_wrapper_rom.py
This file contains 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,61 @@ | ||
import sys | ||
import importlib | ||
|
||
import KratosMultiphysics | ||
from KratosMultiphysics import kratos_utilities | ||
from KratosMultiphysics.RomApplication import rom_solver | ||
|
||
def CreateSolverByParameters(model, solver_settings, parallelism, analysis_stage_module_name): | ||
|
||
if not isinstance(model, KratosMultiphysics.Model): | ||
raise Exception("input is expected to be provided as a Kratos Model object") | ||
|
||
if not isinstance(solver_settings, KratosMultiphysics.Parameters): | ||
raise Exception("input is expected to be provided as a Kratos Parameters object") | ||
|
||
# Get the corresponding application from the analysis_stage path | ||
split_analysis_stage_module_name = analysis_stage_module_name.split('.') | ||
application_module_name = split_analysis_stage_module_name[0] + "." + split_analysis_stage_module_name[1] | ||
if not kratos_utilities.CheckIfApplicationsAvailable(split_analysis_stage_module_name[1]): | ||
raise Exception("Module {} is not available.".format(application_module_name)) | ||
|
||
# Filter and retrieve the Python solvers wrapper from the corresponding application | ||
#TODO: This filtering wouldn't be required if we were using a unified solvers wrapper module name | ||
if application_module_name == "KratosMultiphysics.FluidDynamicsApplication": | ||
solvers_wrapper_module_module_name = "python_solvers_wrapper_fluid" | ||
elif application_module_name == "KratosMultiphysics.StructuralMechanicsApplication": | ||
solvers_wrapper_module_module_name = "python_solvers_wrapper_structural" | ||
elif application_module_name == "KratosMultiphysics.ConvectionDiffusionApplication": | ||
solvers_wrapper_module_module_name = "python_solvers_wrapper_convection_diffusion" | ||
else: | ||
err_msg = "Python module \'{0}\' is not available. Make sure \'{1}\' is compiled.".format(application_module_name, split_analysis_stage_module_name[1]) | ||
raise Exception(err_msg) | ||
solvers_wrapper_module = importlib.import_module(application_module_name + "." + solvers_wrapper_module_module_name) | ||
|
||
# Create a prototype class instance and get the module and name of the solver to be used as base | ||
# Note that an auxiliary Kratos parameter settings without the rom_settings field is created to avoid the defaults error thrown | ||
# Note that an auxiliary Kratos model is also created to avoid creating the main_model_part in the prototype class instance | ||
#TODO: We could do the same exercise as we do in the stage (module_name to ClassName equal to ModuleName if we standarize the solver names) | ||
aux_solver_settings = solver_settings.Clone() | ||
aux_solver_settings.RemoveValue("rom_settings") | ||
aux_base_solver_instance = solvers_wrapper_module.CreateSolverByParameters(KratosMultiphysics.Model(), aux_solver_settings, parallelism) | ||
|
||
# Create the ROM solver from the base solver | ||
rom_solver_instance = rom_solver.CreateSolver(type(aux_base_solver_instance), model, solver_settings) | ||
|
||
return rom_solver_instance | ||
|
||
def CreateSolver(model, custom_settings): | ||
|
||
if (type(model) != KratosMultiphysics.Model): | ||
raise Exception("input is expected to be provided as a Kratos Model object") | ||
|
||
if (type(custom_settings) != KratosMultiphysics.Parameters): | ||
raise Exception("input is expected to be provided as a Kratos Parameters object") | ||
|
||
parallelism = custom_settings["problem_data"]["parallel_type"].GetString() | ||
analysis_stage = custom_settings["analysis_stage"].GetString() | ||
solver_settings = custom_settings["solver_settings"] | ||
|
||
return CreateSolverByParameters(model, solver_settings, parallelism, analysis_stage) | ||
|
This file contains 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
This file contains 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
This file contains 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,58 @@ | ||
# Importing the Kratos Library | ||
import KratosMultiphysics | ||
|
||
# Import applications | ||
import KratosMultiphysics.RomApplication as KratosROM | ||
|
||
def CreateSolver(cls, model, custom_settings): | ||
class ROMSolver(cls): | ||
"""ROM solver generic main class. | ||
This class serves as a generic class to make a standard Kratos solver ROM-compatible. | ||
It extends the default parameters to include the \'rom_settings\' and overrides the | ||
creation of the builder and solver to use the ROM one. | ||
""" | ||
|
||
def __init__(self, model, custom_settings): | ||
super().__init__(model, custom_settings) | ||
KratosMultiphysics.Logger.PrintInfo("::[ROMSolver]:: ", "Construction finished") | ||
|
||
@classmethod | ||
def GetDefaultParameters(cls): | ||
default_settings = KratosMultiphysics.Parameters("""{ | ||
"rom_settings": { | ||
"nodal_unknowns": [], | ||
"number_of_rom_dofs": 0 | ||
} | ||
}""") | ||
default_settings.AddMissingParameters(super().GetDefaultParameters()) | ||
return default_settings | ||
|
||
def _CreateBuilderAndSolver(self): | ||
linear_solver = self._GetLinearSolver() | ||
rom_parameters = self._ValidateAndReturnRomParameters() | ||
builder_and_solver = KratosROM.ROMBuilderAndSolver(linear_solver, rom_parameters) | ||
return builder_and_solver | ||
|
||
def _ValidateAndReturnRomParameters(self): | ||
# Check that the number of ROM DOFs has been provided | ||
n_rom_dofs = self.settings["rom_settings"]["number_of_rom_dofs"].GetInt() | ||
if not n_rom_dofs > 0: | ||
err_msg = "\'number_of_rom_dofs\' in \'rom_settings\' is {}. Please set a larger than zero value.".format(n_rom_dofs) | ||
raise Exception(err_msg) | ||
|
||
# Check if the nodal unknowns have been provided by the user | ||
# If not, take the DOFs list from the base solver | ||
nodal_unknowns = self.settings["rom_settings"]["nodal_unknowns"].GetStringArray() | ||
if len(nodal_unknowns) == 0: | ||
solver_dofs_list = self.GetDofsList() | ||
if not len(solver_dofs_list) == 0: | ||
self.settings["rom_settings"]["nodal_unknowns"].SetStringArray(solver_dofs_list) | ||
else: | ||
err_msg = "\'nodal_unknowns\' in \'rom_settings\' is not provided and there is a not-valid implementation in base solver." | ||
err_msg += " Please manually set \'nodal_unknowns\' in \'rom_settings\'." | ||
raise Exception(err_msg) | ||
|
||
# Return the validated ROM parameters | ||
return self.settings["rom_settings"] | ||
|
||
return ROMSolver(model, custom_settings) |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend coming up with a better name rather than prepending with
new_
remember how long we were stuck with the
new_linear_solver_factory
😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄 Considering that this is a small research application I don't think we'll need to change keep backwards compatibility for a very long period of time. Indeed, the plan is to remove the old stuff ASAP. I'd keep the
new_
to constantly remind us to do it 😄.