From 095bafe20a2db3f9e611ae52ea04c678603d7bc6 Mon Sep 17 00:00:00 2001 From: Henrik Kjedsberg Date: Thu, 14 Mar 2024 12:21:00 +0100 Subject: [PATCH 1/3] Update import of oasismove and oasis in problem files --- src/vampy/simulation/Artery.py | 31 ++++++++++++++++++++----------- src/vampy/simulation/Atrium.py | 23 ++++++++++++++++------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/vampy/simulation/Artery.py b/src/vampy/simulation/Artery.py index 6861c8e6..c4fc4e84 100755 --- a/src/vampy/simulation/Artery.py +++ b/src/vampy/simulation/Artery.py @@ -1,14 +1,22 @@ +import importlib.util import json -import os import pickle from pprint import pprint -import numpy as np -from dolfin import set_log_level -if os.environ.get('OASIS_MODE') == 'TESTING': +import numpy as np +from dolfin import set_log_level, MPI + +# Check for oasis and oasismove +package_name_oasis = 'oasis' +package_name_oasismove = 'oasismove' +oasis_exists = importlib.util.find_spec(package_name_oasis) +oasismove_exists = importlib.util.find_spec(package_name_oasismove) +if oasismove_exists: from oasismove.problems.NSfracStep import * -else: +elif oasis_exists: from oasis.problems.NSfracStep import * +else: + print("Neither oasis nor oasismove is installed. Exiting simulation..") from vampy.simulation.Probe import Probes # type: ignore from vampy.simulation.Womersley import make_womersley_bcs, compute_boundary_geometry_acrn @@ -17,6 +25,7 @@ # FEniCS specific command to control the desired level of logging, here set to critical errors set_log_level(50) +comm = MPI.comm_world def problem_parameters(commandline_kwargs, NS_parameters, NS_expressions, **NS_namespace): @@ -75,7 +84,7 @@ def problem_parameters(commandline_kwargs, NS_parameters, NS_expressions, **NS_n case_name = mesh_file.split(".")[0] NS_parameters["folder"] = path.join(NS_parameters["folder"], case_name) - if MPI.rank(MPI.comm_world) == 0: + if MPI.rank(comm) == 0: print("=== Starting simulation for case: {} ===".format(case_name)) print("Running with the following parameters:") pprint(NS_parameters) @@ -131,7 +140,7 @@ def create_bcs(t, NS_expressions, V, Q, area_ratio, area_inlet, mesh, mesh_path, area_out.append(assemble(Constant(1.0) * dsi)) bc_p = [] - if MPI.rank(MPI.comm_world) == 0: + if MPI.rank(comm) == 0: print("=== Initial pressure and area fraction ===") for i, ID in enumerate(id_out): @@ -140,7 +149,7 @@ def create_bcs(t, NS_expressions, V, Q, area_ratio, area_inlet, mesh, mesh_path, bc = DirichletBC(Q, outflow, boundary, ID) bc_p.append(bc) NS_expressions[ID] = outflow - if MPI.rank(MPI.comm_world) == 0: + if MPI.rank(comm) == 0: print(f"Boundary ID={ID}, pressure: {p_initial:.5f}, area fraction: {area_ratio[i]:0.5f}") # No slip condition at wall @@ -171,7 +180,7 @@ def pre_solve_hook(mesh, V, Q, newfolder, mesh_path, restart_folder, velocity_de probe_points = np.array(json.load(infile)) # Store points file in checkpoint - if MPI.rank(MPI.comm_world) == 0: + if MPI.rank(comm) == 0: probe_points.dump(path.join(newfolder, "Checkpoint", "points")) eval_dict["centerline_u_x_probes"] = Probes(probe_points.flatten(), V) @@ -187,7 +196,7 @@ def pre_solve_hook(mesh, V, Q, newfolder, mesh_path, restart_folder, velocity_de files = NS_namespace["files"] # Save mesh as HDF5 file for post-processing - with HDF5File(MPI.comm_world, files["mesh"], "w") as mesh_file: + with HDF5File(comm, files["mesh"], "w") as mesh_file: mesh_file.write(mesh, "mesh") # Create vector function for storing velocity @@ -219,7 +228,7 @@ def temporal_hook(u_, p_, mesh, tstep, dump_probe_frequency, eval_dict, newfolde tstep, u_) # Compute flow rates and updated pressure at outlets, and mean velocity and Reynolds number at inlet - if MPI.rank(MPI.comm_world) == 0 and tstep % 10 == 0: + if MPI.rank(comm) == 0 and tstep % 10 == 0: U_mean = Q_in / area_inlet[0] diam_inlet = np.sqrt(4 * area_inlet[0] / np.pi) Re = U_mean * diam_inlet / nu diff --git a/src/vampy/simulation/Atrium.py b/src/vampy/simulation/Atrium.py index bd51dbc0..5ea34d0c 100644 --- a/src/vampy/simulation/Atrium.py +++ b/src/vampy/simulation/Atrium.py @@ -1,13 +1,22 @@ +import importlib.util import json -import os import pickle from pprint import pprint + +import numpy as np from dolfin import set_log_level, MPI -if os.environ.get('OASIS_MODE') == 'TESTING': +# Check for oasis and oasismove +package_name_oasis = 'oasis' +package_name_oasismove = 'oasismove' +oasis_exists = importlib.util.find_spec(package_name_oasis) +oasismove_exists = importlib.util.find_spec(package_name_oasismove) +if oasismove_exists: from oasismove.problems.NSfracStep import * -else: +elif oasis_exists: from oasis.problems.NSfracStep import * +else: + print("Neither oasis nor oasismove is installed. Exiting simulation..") from vampy.simulation.Probe import Probes # type: ignore from vampy.simulation.Womersley import make_womersley_bcs, compute_boundary_geometry_acrn @@ -74,7 +83,7 @@ def problem_parameters(commandline_kwargs, NS_parameters, scalar_components, Sch case_name = mesh_file.split(".")[0] NS_parameters["folder"] = path.join(NS_parameters["folder"], case_name) - if MPI.rank(MPI.comm_world) == 0: + if MPI.rank(comm) == 0: print("=== Starting simulation for Atrium.py ===") print("Running with the following parameters:") pprint(NS_parameters) @@ -165,7 +174,7 @@ def pre_solve_hook(V, Q, cardiac_cycle, dt, save_solution_after_cycle, mesh_path probe_points = np.array(json.load(infile)) # Store points file in checkpoint - if MPI.rank(MPI.comm_world) == 0: + if MPI.rank(comm) == 0: probe_points.dump(path.join(newfolder, "Checkpoint", "points")) eval_dict["centerline_u_x_probes"] = Probes(probe_points.flatten(), V) @@ -181,7 +190,7 @@ def pre_solve_hook(V, Q, cardiac_cycle, dt, save_solution_after_cycle, mesh_path files = NS_namespace["files"] # Save mesh as HDF5 file for post-processing - with HDF5File(MPI.comm_world, files["mesh"], "w") as mesh_file: + with HDF5File(comm, files["mesh"], "w") as mesh_file: mesh_file.write(mesh, "mesh") # Create vector function for storing velocity @@ -221,7 +230,7 @@ def temporal_hook(mesh, dt, t, save_solution_frequency, u_, NS_expressions, id_i U_mean = comm.gather(local_U_mean, 0) U_max = comm.gather(local_U_max, 0) - if MPI.rank(MPI.comm_world) == 0: + if MPI.rank(comm) == 0: u_mean = np.mean(U_mean) u_max = max(U_max) From 150648eb89ced781c4d9b79c8f40d6bd4334edaa Mon Sep 17 00:00:00 2001 From: Henrik Kjedsberg Date: Thu, 14 Mar 2024 12:21:30 +0100 Subject: [PATCH 2/3] Remove env variable in test --- .github/workflows/check_and_test_package.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/check_and_test_package.yml b/.github/workflows/check_and_test_package.yml index bf15459b..254405c7 100644 --- a/.github/workflows/check_and_test_package.yml +++ b/.github/workflows/check_and_test_package.yml @@ -97,8 +97,6 @@ jobs: - name: Run tests run: python3 -m pytest tests - env: - OASIS_MODE: TESTING - name: Upload coverage report to codecov if: matrix.os == 'ubuntu-latest' From 72114a1b92c26e922c857bdd593e3e29d7619840 Mon Sep 17 00:00:00 2001 From: Henrik Kjedsberg Date: Thu, 14 Mar 2024 12:27:50 +0100 Subject: [PATCH 3/3] Fix imports, flake8 --- src/vampy/simulation/Artery.py | 10 +++++----- src/vampy/simulation/Atrium.py | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/vampy/simulation/Artery.py b/src/vampy/simulation/Artery.py index c4fc4e84..7b2cca36 100755 --- a/src/vampy/simulation/Artery.py +++ b/src/vampy/simulation/Artery.py @@ -6,6 +6,11 @@ import numpy as np from dolfin import set_log_level, MPI +from vampy.simulation.Probe import Probes # type: ignore +from vampy.simulation.Womersley import make_womersley_bcs, compute_boundary_geometry_acrn +from vampy.simulation.simulation_common import get_file_paths, store_u_mean, print_mesh_information, \ + store_velocity_and_pressure_h5, dump_probes + # Check for oasis and oasismove package_name_oasis = 'oasis' package_name_oasismove = 'oasismove' @@ -18,11 +23,6 @@ else: print("Neither oasis nor oasismove is installed. Exiting simulation..") -from vampy.simulation.Probe import Probes # type: ignore -from vampy.simulation.Womersley import make_womersley_bcs, compute_boundary_geometry_acrn -from vampy.simulation.simulation_common import get_file_paths, store_u_mean, print_mesh_information, \ - store_velocity_and_pressure_h5, dump_probes - # FEniCS specific command to control the desired level of logging, here set to critical errors set_log_level(50) comm = MPI.comm_world diff --git a/src/vampy/simulation/Atrium.py b/src/vampy/simulation/Atrium.py index 5ea34d0c..6b4d5f91 100644 --- a/src/vampy/simulation/Atrium.py +++ b/src/vampy/simulation/Atrium.py @@ -6,6 +6,11 @@ import numpy as np from dolfin import set_log_level, MPI +from vampy.simulation.Probe import Probes # type: ignore +from vampy.simulation.Womersley import make_womersley_bcs, compute_boundary_geometry_acrn +from vampy.simulation.simulation_common import store_u_mean, get_file_paths, print_mesh_information, \ + store_velocity_and_pressure_h5, dump_probes + # Check for oasis and oasismove package_name_oasis = 'oasis' package_name_oasismove = 'oasismove' @@ -18,11 +23,6 @@ else: print("Neither oasis nor oasismove is installed. Exiting simulation..") -from vampy.simulation.Probe import Probes # type: ignore -from vampy.simulation.Womersley import make_womersley_bcs, compute_boundary_geometry_acrn -from vampy.simulation.simulation_common import store_u_mean, get_file_paths, print_mesh_information, \ - store_velocity_and_pressure_h5, dump_probes - # FEniCS specific command to control the desired level of logging, here set to critical errors set_log_level(50) comm = MPI.comm_world