From 084a6d0aafb46eec25ab201edd276995707b0f0c Mon Sep 17 00:00:00 2001 From: Kei Date: Tue, 9 May 2023 12:47:00 +0200 Subject: [PATCH 1/7] trying to add support to use dictionary inside config file --- turtleFSI/config_files/test_dict.config | 6 ++++++ turtleFSI/utils/argpar.py | 22 +++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 turtleFSI/config_files/test_dict.config diff --git a/turtleFSI/config_files/test_dict.config b/turtleFSI/config_files/test_dict.config new file mode 100644 index 0000000..65f0e25 --- /dev/null +++ b/turtleFSI/config_files/test_dict.config @@ -0,0 +1,6 @@ +[solid] +solid_properties : { + material_model': 'MooneyRivlin', + 'rho_s': '1.0E3', + 'mus_sl': '0.1724E6', + 'lambda_s': '1.55E6'} diff --git a/turtleFSI/utils/argpar.py b/turtleFSI/utils/argpar.py index 4b51eac..590d91e 100644 --- a/turtleFSI/utils/argpar.py +++ b/turtleFSI/utils/argpar.py @@ -18,7 +18,7 @@ import configargparse import string import ast - +import yaml class StoreDictKeyPair(configargparse.Action): def __init__(self, option_strings, dest, nargs=None, **kwargs): @@ -120,13 +120,13 @@ def restricted_float(x): def parse(): - parser = configargparse.ArgumentParser(description=( - "turtleFSI is an open source Fluid-Structure Interaction (FSI) solver written in Python " - + "and built upon the FEniCS finite element library. The purpose of turtleFSI is to " - + "provide a user friendly and numerically robust monolithic FSI solver able to handle " - + "problems characterized by large deformation. turtleFSI benefites from the state-of-the-art " - + "parrallel computing features available from the FEniCS library and can be executed with " - + "MPI on large computing resources.")) + parser = configargparse.ArgParser(config_file_parser_class=configargparse.ConfigparserConfigFileParser, + description=("turtleFSI is an open source Fluid-Structure Interaction (FSI) solver written in Python " + + "and built upon the FEniCS finite element library. The purpose of turtleFSI is to " + + "provide a user friendly and numerically robust monolithic FSI solver able to handle " + + "problems characterized by large deformation. turtleFSI benefites from the state-of-the-art " + + "parrallel computing features available from the FEniCS library and can be executed with " + + "MPI on large computing resources.")) # Configuration file parser.add_argument('-c', '--config', is_config_file=True, @@ -263,9 +263,13 @@ def parse(): " by providing a key=value, e.g. folder=TF_fsi_results. You can provide" + " multiple key=value pairs seperated by a whitespace", default=None) - + + # TODO: move this above and also add fluid_properties + parser.add_argument("--solid_properties", type=yaml.safe_load) # Parse arguments args, unknownargs = parser.parse_known_args() + # TODO: Now solid_properties can be read as a dictionary from the config file, but the values are read as strings. This should be fixed. + # from IPython import embed; embed(); exit(1) # Add dt and T as parameters for time-step and end-time respectively d = {'dt': args.__dict__['time_step'], From 581d4f681008fd4587f345907a7057e560d9ec83 Mon Sep 17 00:00:00 2001 From: Kei Date: Tue, 9 May 2023 21:16:40 +0200 Subject: [PATCH 2/7] fix error in domain.py: if fluid_properties is given, do not go into the loop --- turtleFSI/modules/domain.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/turtleFSI/modules/domain.py b/turtleFSI/modules/domain.py index b2f173a..1229558 100644 --- a/turtleFSI/modules/domain.py +++ b/turtleFSI/modules/domain.py @@ -22,15 +22,18 @@ def assign_domain_properties(dx, dx_f_id, rho_f, mu_f, fluid_properties, dx_s_id # 1. Create differential for each fluid region, and organize into fluid_properties list of dicts dx_f = {} - if isinstance(dx_f_id, list): # If dx_f_id is a list (i.e, if there are multiple fluid regions): - for fluid_region in range(len(dx_f_id)): - dx_f[fluid_region] = dx(dx_f_id[fluid_region], subdomain_data=domains) # Create dx_f for each fluid region - fluid_properties.append({"dx_f_id":dx_f_id[fluid_region],"rho_f":rho_f[fluid_region],"mu_f":mu_f[fluid_region]}) - dx_f_id_list=dx_f_id - else: - dx_f[0] = dx(dx_f_id, subdomain_data=domains) - dx_f_id_list=[dx_f_id] - fluid_properties.append({"dx_f_id":dx_f_id,"rho_f":rho_f,"mu_f":mu_f}) + if len(fluid_properties) == 0: + if isinstance(dx_f_id, list): # If dx_f_id is a list (i.e, if there are multiple fluid regions): + for fluid_region in range(len(dx_f_id)): + dx_f[fluid_region] = dx(dx_f_id[fluid_region], subdomain_data=domains) # Create dx_f for each fluid region + fluid_properties.append({"dx_f_id":dx_f_id[fluid_region],"rho_f":rho_f[fluid_region],"mu_f":mu_f[fluid_region]}) + dx_f_id_list=dx_f_id + else: + dx_f[0] = dx(dx_f_id, subdomain_data=domains) + dx_f_id_list=[dx_f_id] + fluid_properties.append({"dx_f_id":dx_f_id,"rho_f":rho_f,"mu_f":mu_f}) + elif isinstance(fluid_properties, dict): + fluid_properties = [fluid_properties] # Create solid region differentials dx_s = {} From a19f03e13e53c4fb5971146f34bf126d2804b814 Mon Sep 17 00:00:00 2001 From: Kei Date: Tue, 9 May 2023 21:17:10 +0200 Subject: [PATCH 3/7] update example config so that it is compatible with new version --- docs/examples/example.config | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/examples/example.config b/docs/examples/example.config index da8c21a..22cc20c 100644 --- a/docs/examples/example.config +++ b/docs/examples/example.config @@ -1,5 +1,5 @@ # Configuration file for turtleFSI - +[Parameter for turtleFSI] # Section is required by configparser but is not used anywhere in turtleFSI ################################################################################ # Define solver, numerics, and problem file ################################################################################ @@ -22,21 +22,21 @@ theta=0.501 ################################################################################ # Turn on/off solving of the fluid problem ('fluid', 'no_fluid') -fluid="fluid" +fluid=fluid # Turn on/off solving of the solid problem ('solid', 'no_solid') -solid="solid" +solid=solid # Use Robin boundary conditions for solid robin_bc=False # Set approach for extrapolating the deformation into the fluid domain # ('laplace', 'elastic', 'biharmonic', 'no_extrapolation') -extrapolation="laplace" +extrapolation=laplace # Set the sub type of the extrapolation method ('constant'," 'small_constant', # 'volume', 'volume_change', 'constrained_disp', 'constrained_disp_vel') -extrapolation-sub-type="constant" +extrapolation-sub-type=constant # List of boundary ids for the weak formulation of the biharmonic mesh lifting # operator with 'constrained_disp_vel' @@ -95,7 +95,7 @@ dx-s-id=2 # Selected linear solver for each Newton iteration, to see a complete list # run list_linear_solvers() -linear-solver="mumps" +linear-solver=mumps # Absolute error tolerance for the Newton iterations atol=1e-7 @@ -140,7 +140,7 @@ save-deg=1 checkpoint-step=500 # Path to store the results. You can store multiple simulations in one folder -folder="results" +folder=results # Over write the standard 1, 2, 3 name of the sub folders #sub-folder=None From 02ce9ba5a55826bf8298bf6e758877005fead8e4 Mon Sep 17 00:00:00 2001 From: Kei Date: Tue, 9 May 2023 21:18:20 +0200 Subject: [PATCH 4/7] added fluid/solid_properties inside argpar.py: Now those two variables can be given as dict from config file --- turtleFSI/utils/argpar.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/turtleFSI/utils/argpar.py b/turtleFSI/utils/argpar.py index 590d91e..bbf88d0 100644 --- a/turtleFSI/utils/argpar.py +++ b/turtleFSI/utils/argpar.py @@ -186,6 +186,12 @@ def parse(): help="1st Lame Coef. for the solid") parser.add_argument("--gravity", type=float, default=None, help="Gravitational force on the solid") + + parser.add_argument("--solid_properties", type=yaml.safe_load, default=None, + help="Solid properties as a dictionary") + + parser.add_argument("--fluid_properties", type=yaml.safe_load, default=None, + help="Fluid properties as a dictionary") # Domain settings parser.add_argument("--dx-f-id", type=int, default=None, @@ -264,12 +270,17 @@ def parse(): " multiple key=value pairs seperated by a whitespace", default=None) - # TODO: move this above and also add fluid_properties - parser.add_argument("--solid_properties", type=yaml.safe_load) # Parse arguments args, unknownargs = parser.parse_known_args() - # TODO: Now solid_properties can be read as a dictionary from the config file, but the values are read as strings. This should be fixed. - # from IPython import embed; embed(); exit(1) + + if args.__dict__["solid_properties"]: + for k, v in args.__dict__["solid_properties"].items(): + if k != "material_model": + args.__dict__["solid_properties"][k] = float(v) + + if args.__dict__["fluid_properties"]: + for k, v in args.__dict__["fluid_properties"].items(): + args.__dict__["fluid_properties"][k] = float(v) # Add dt and T as parameters for time-step and end-time respectively d = {'dt': args.__dict__['time_step'], From 12b02848905ef555900d464fcd25d86f10578f7e Mon Sep 17 00:00:00 2001 From: Kei Date: Tue, 9 May 2023 21:22:02 +0200 Subject: [PATCH 5/7] remove test config file --- turtleFSI/config_files/test_dict.config | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 turtleFSI/config_files/test_dict.config diff --git a/turtleFSI/config_files/test_dict.config b/turtleFSI/config_files/test_dict.config deleted file mode 100644 index 65f0e25..0000000 --- a/turtleFSI/config_files/test_dict.config +++ /dev/null @@ -1,6 +0,0 @@ -[solid] -solid_properties : { - material_model': 'MooneyRivlin', - 'rho_s': '1.0E3', - 'mus_sl': '0.1724E6', - 'lambda_s': '1.55E6'} From 02bde86c99c20604f768eac283b16df1282d1173 Mon Sep 17 00:00:00 2001 From: Kei Date: Wed, 10 May 2023 08:24:51 +0200 Subject: [PATCH 6/7] added pyyaml --- environment.yml | 1 + setup.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index c32df29..881416b 100644 --- a/environment.yml +++ b/environment.yml @@ -10,5 +10,6 @@ dependencies: - scipy - configargparse - numpy + - pyyaml - pip: - cppimport \ No newline at end of file diff --git a/setup.py b/setup.py index e1fc346..1055ce2 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = fh.read() DEPENDENCIES = ['configargparse', "fenics-dolfin", - "scipy", "cppimport", "numpy"] + "scipy", "cppimport", "numpy", "pyyaml"] TEST_DEPENDENCIES = ['pytest'] VERSION = "1.5" From b1c99783275c611f88526cbba710a768643520f2 Mon Sep 17 00:00:00 2001 From: Kei Date: Wed, 10 May 2023 08:53:26 +0200 Subject: [PATCH 7/7] clean up config files and fix typo --- docs/examples/example.config | 2 +- docs/examples/example.yaml | 16 ++++++++-------- turtleFSI/utils/argpar.py | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/examples/example.config b/docs/examples/example.config index 22cc20c..4621824 100644 --- a/docs/examples/example.config +++ b/docs/examples/example.config @@ -1,5 +1,5 @@ # Configuration file for turtleFSI -[Parameter for turtleFSI] # Section is required by configparser but is not used anywhere in turtleFSI +[Parameter for turtleFSI] # Section is required by configargparse but is not used anywhere in turtleFSI ################################################################################ # Define solver, numerics, and problem file ################################################################################ diff --git a/docs/examples/example.yaml b/docs/examples/example.yaml index 8d50a65..fb72a58 100644 --- a/docs/examples/example.yaml +++ b/docs/examples/example.yaml @@ -1,5 +1,5 @@ # Configuration file for turtleFSI - +[Parameter for turtleFSI] # Section is required by configargparse but is not used anywhere in turtleFSI ################################################################################ # Define solver, numerics, and problem file ################################################################################ @@ -7,7 +7,7 @@ # Name of problem file to solve. Could either be located in the turtleFSI # repository (TF_cfd, TF_csm, TF_fsi, turtle_demo) or it could be a problem # file you have created locally. -problem: "turtle_demo" +problem: turtle_demo # Setting temporal integration. # (theta=0 : first order explicit forward Euler scheme) @@ -22,21 +22,21 @@ theta: 0.501 ################################################################################ # Turn on/off solving of the fluid problem ('fluid', 'no_fluid') -fluid: "fluid" +fluid: fluid # Turn on/off solving of the solid problem ('solid', 'no_solid') -solid: "solid" +solid: solid # Use Robin boundary conditions for solid robin_bc: False # Set approach for extrapolating the deformation into the fluid domain # ('laplace', 'elastic', 'biharmonic', 'no_extrapolation') -extrapolation: "laplace" +extrapolation: laplace # Set the sub type of the extrapolation method ('constant'," 'small_constant', # 'volume', 'volume_change', 'constrained_disp', 'constrained_disp_vel') -extrapolation-sub-type: "constant" +extrapolation-sub-type: constant # List of boundary ids for the weak formulation of the biharmonic mesh lifting # operator with 'constrained_disp_vel' @@ -95,7 +95,7 @@ dx-s-id: 2 # Selected linear solver for each Newton iteration, to see a complete list # run list_linear_solvers() -linear-solver: "mumps" +linear-solver: mumps # Absolute error tolerance for the Newton iterations atol: 1e-7 @@ -140,7 +140,7 @@ save-deg: 1 checkpoint-step: 500 # Path to store the results. You can store multiple simulations in one folder -folder: "results" +folder: results # Over write the standard 1, 2, 3 name of the sub folders #sub-folder: None diff --git a/turtleFSI/utils/argpar.py b/turtleFSI/utils/argpar.py index bbf88d0..3f2d4a0 100644 --- a/turtleFSI/utils/argpar.py +++ b/turtleFSI/utils/argpar.py @@ -269,7 +269,7 @@ def parse(): " by providing a key=value, e.g. folder=TF_fsi_results. You can provide" + " multiple key=value pairs seperated by a whitespace", default=None) - + # Parse arguments args, unknownargs = parser.parse_known_args()