From f2840938b09cfbbc32f6fcfc46daba3399c03460 Mon Sep 17 00:00:00 2001 From: jhdark Date: Mon, 13 Jan 2025 15:41:54 -0500 Subject: [PATCH 1/3] only print messages on master process --- festim/concentration/mobile.py | 3 ++- festim/concentration/traps/extrinsic_trap.py | 3 ++- festim/generic_simulation.py | 15 ++++++++++----- festim/h_transport_problem.py | 14 ++++++++++---- festim/meshing/mesh_from_xdmf.py | 4 +++- festim/temperature/temperature_solver.py | 10 +++++++--- 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/festim/concentration/mobile.py b/festim/concentration/mobile.py index ec2f666f1..383ea3d5d 100644 --- a/festim/concentration/mobile.py +++ b/festim/concentration/mobile.py @@ -178,7 +178,8 @@ def create_source_form(self, dx): F_source = 0 expressions_source = [] - print("Defining source terms") + if MPI.comm_world.rank == 0: + print("Defining source terms") for source in self.sources: if type(source.volume) is list: volumes = source.volume diff --git a/festim/concentration/traps/extrinsic_trap.py b/festim/concentration/traps/extrinsic_trap.py index 78a450309..7952e770e 100644 --- a/festim/concentration/traps/extrinsic_trap.py +++ b/festim/concentration/traps/extrinsic_trap.py @@ -65,7 +65,8 @@ def newton_solver(self, value): self._newton_solver = value elif isinstance(value, NewtonSolver): if self._newton_solver: - print("Settings for the Newton solver will be overwritten") + if MPI.comm_world.rank == 0: + print("Settings for the Newton solver will be overwritten") self._newton_solver = value else: raise TypeError("accepted type for newton_solver is fenics.NewtonSolver") diff --git a/festim/generic_simulation.py b/festim/generic_simulation.py index 9c8bbd826..abe872ea4 100644 --- a/festim/generic_simulation.py +++ b/festim/generic_simulation.py @@ -461,7 +461,8 @@ def run_transient(self): self.h_transport_problem.compute_jacobian() # Time-stepping - print("Time stepping...") + if MPI.comm_world.rank == 0: + print("Time stepping...") while self.t < self.settings.final_time and not np.isclose( self.t, self.settings.final_time, atol=0 ): @@ -469,7 +470,8 @@ def run_transient(self): def run_steady(self): # Solve steady state - print("Solving steady state problem...") + if MPI.comm_world.rank == 0: + print("Solving steady state problem...") nb_iterations, converged = self.h_transport_problem.solve_once() @@ -480,7 +482,8 @@ def run_steady(self): # print final message if converged: msg = "Solved problem in {:.2f} s".format(elapsed_time) - print(msg) + if MPI.comm_world.rank == 0: + print(msg) else: msg = "The solver diverged in " msg += "{:.0f} iteration(s) ({:.2f} s)".format(nb_iterations, elapsed_time) @@ -517,9 +520,11 @@ def display_time(self): not np.isclose(self.t, self.settings.final_time, atol=0) and self.log_level == 40 ): - print(msg, end="\r") + if MPI.comm_world.rank == 0: + print(msg, end="\r") else: - print(msg) + if MPI.comm_world.rank == 0: + print(msg) def run_post_processing(self): """Create post processing functions and compute/write the exports""" diff --git a/festim/h_transport_problem.py b/festim/h_transport_problem.py index 7d4c4859f..68d33c320 100644 --- a/festim/h_transport_problem.py +++ b/festim/h_transport_problem.py @@ -55,7 +55,8 @@ def newton_solver(self, value): self._newton_solver = value elif isinstance(value, NewtonSolver): if self._newton_solver: - print("Settings for the Newton solver will be overwritten") + if MPI.comm_world.rank == 0: + print("Settings for the Newton solver will be overwritten") self._newton_solver = value else: raise TypeError("accepted type for newton_solver is fenics.NewtonSolver") @@ -102,7 +103,8 @@ def initialise(self, mesh, materials, dt=None): self.define_newton_solver() # Boundary conditions - print("Defining boundary conditions") + if MPI.comm_world.rank == 0: + print("Defining boundary conditions") self.create_dirichlet_bcs(materials, mesh) if self.settings.transient: self.traps.define_variational_problem_extrinsic_traps(mesh.dx, dt, self.T) @@ -177,7 +179,9 @@ def initialise_concentrations(self): concentration.test_function = list(split(self.v))[index] index += 1 - print("Defining initial values") + if MPI.comm_world.rank == 0: + print("Defining initial values") + field_to_component = { "solute": 0, "0": 0, @@ -253,7 +257,9 @@ def define_variational_problem(self, materials, mesh, dt=None): dt (festim.Stepsize, optional): the stepsize, only needed if self.settings.transient is True. Defaults to None. """ - print("Defining variational problem") + if MPI.comm_world.rank == 0: + print("Defining variational problem") + expressions = [] F = 0 diff --git a/festim/meshing/mesh_from_xdmf.py b/festim/meshing/mesh_from_xdmf.py index ea4411f0d..386e1cb5f 100644 --- a/festim/meshing/mesh_from_xdmf.py +++ b/festim/meshing/mesh_from_xdmf.py @@ -43,6 +43,8 @@ def define_markers(self): f.XDMFFile(self.boundary_file).read(surface_markers, "f") surface_markers = f.MeshFunction("size_t", mesh, surface_markers) - print("Succesfully load mesh with " + str(len(volume_markers)) + " cells") + if f.MPI.comm_world.rank == 0: + print("Succesfully load mesh with " + str(len(volume_markers)) + " cells") + self.volume_markers = volume_markers self.surface_markers = surface_markers diff --git a/festim/temperature/temperature_solver.py b/festim/temperature/temperature_solver.py index c35a635a0..67c8210d5 100644 --- a/festim/temperature/temperature_solver.py +++ b/festim/temperature/temperature_solver.py @@ -73,7 +73,8 @@ def newton_solver(self, value): self._newton_solver = value elif isinstance(value, f.NewtonSolver): if self._newton_solver: - print("Settings for the Newton solver will be overwritten") + if f.MPI.comm_world.rank == 0: + print("Settings for the Newton solver will be overwritten") self._newton_solver = value else: raise TypeError("accepted type for newton_solver is fenics.NewtonSolver") @@ -131,7 +132,9 @@ def create_functions(self, materials, mesh, dt=None): self.define_newton_solver() if not self.transient: - print("Solving stationary heat equation") + if f.MPI.comm_world.rank == 0: + print("Solving stationary heat equation") + dT = f.TrialFunction(self.T.function_space()) JT = f.derivative(self.F, self.T, dT) problem = festim.Problem(JT, self.F, self.dirichlet_bcs) @@ -153,8 +156,9 @@ def define_variational_problem(self, materials, mesh, dt=None): dt (festim.Stepsize, optional): the stepsize. Only needed if self.transient is True. Defaults to None. """ + if f.MPI.comm_world.rank == 0: + print("Defining variational problem heat transfers") - print("Defining variational problem heat transfers") T, T_n = self.T, self.T_n v_T = self.v_T From fc505341e7143053de0430efef8b75ba0e7fe8cd Mon Sep 17 00:00:00 2001 From: jhdark Date: Tue, 14 Jan 2025 15:50:07 -0500 Subject: [PATCH 2/3] new festim print function --- festim/__init__.py | 1 + festim/helpers.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/festim/__init__.py b/festim/__init__.py index 3b199d1ad..e42d4d945 100644 --- a/festim/__init__.py +++ b/festim/__init__.py @@ -27,6 +27,7 @@ as_constant, as_expression, as_constant_or_expression, + festim_print, ) from .meshing.mesh import Mesh diff --git a/festim/helpers.py b/festim/helpers.py index a36a673cf..f790050b0 100644 --- a/festim/helpers.py +++ b/festim/helpers.py @@ -1,6 +1,6 @@ import festim import xml.etree.ElementTree as ET -from fenics import Expression, UserExpression, Constant +from fenics import Expression, UserExpression, Constant, MPI import sympy as sp @@ -108,3 +108,8 @@ def extract_xdmf_labels(filename): unique_labels = list(set(labels)) return unique_labels + + +def festim_print(msg, end="\n"): + if MPI.comm_world.rank == 0: + print(msg, end=end) From 3eef19e2bf86ba3889e7563944c8798311fe5086 Mon Sep 17 00:00:00 2001 From: jhdark Date: Tue, 14 Jan 2025 15:50:16 -0500 Subject: [PATCH 3/3] use festim print --- festim/concentration/mobile.py | 12 +++++++++--- festim/concentration/traps/extrinsic_trap.py | 5 ++--- festim/generic_simulation.py | 15 +++++---------- festim/h_transport_problem.py | 11 +++++------ festim/meshing/mesh_from_xdmf.py | 7 ++++--- festim/temperature/temperature_solver.py | 11 +++++------ 6 files changed, 30 insertions(+), 31 deletions(-) diff --git a/festim/concentration/mobile.py b/festim/concentration/mobile.py index 383ea3d5d..af57b0d33 100644 --- a/festim/concentration/mobile.py +++ b/festim/concentration/mobile.py @@ -1,4 +1,11 @@ -from festim import Concentration, FluxBC, k_B, RadioactiveDecay, SurfaceKinetics +from festim import ( + Concentration, + FluxBC, + k_B, + RadioactiveDecay, + SurfaceKinetics, + festim_print, +) from fenics import * @@ -178,8 +185,7 @@ def create_source_form(self, dx): F_source = 0 expressions_source = [] - if MPI.comm_world.rank == 0: - print("Defining source terms") + festim_print("Defining source terms") for source in self.sources: if type(source.volume) is list: volumes = source.volume diff --git a/festim/concentration/traps/extrinsic_trap.py b/festim/concentration/traps/extrinsic_trap.py index 7952e770e..8eeef4d77 100644 --- a/festim/concentration/traps/extrinsic_trap.py +++ b/festim/concentration/traps/extrinsic_trap.py @@ -1,4 +1,4 @@ -from festim import Trap, as_constant_or_expression +from festim import Trap, as_constant_or_expression, festim_print from fenics import NewtonSolver, MPI @@ -65,8 +65,7 @@ def newton_solver(self, value): self._newton_solver = value elif isinstance(value, NewtonSolver): if self._newton_solver: - if MPI.comm_world.rank == 0: - print("Settings for the Newton solver will be overwritten") + festim_print("Settings for the Newton solver will be overwritten") self._newton_solver = value else: raise TypeError("accepted type for newton_solver is fenics.NewtonSolver") diff --git a/festim/generic_simulation.py b/festim/generic_simulation.py index abe872ea4..9a28a2b1e 100644 --- a/festim/generic_simulation.py +++ b/festim/generic_simulation.py @@ -461,8 +461,7 @@ def run_transient(self): self.h_transport_problem.compute_jacobian() # Time-stepping - if MPI.comm_world.rank == 0: - print("Time stepping...") + festim.festim_print("Time stepping...") while self.t < self.settings.final_time and not np.isclose( self.t, self.settings.final_time, atol=0 ): @@ -470,8 +469,7 @@ def run_transient(self): def run_steady(self): # Solve steady state - if MPI.comm_world.rank == 0: - print("Solving steady state problem...") + festim.festim_print("Solving steady state problem...") nb_iterations, converged = self.h_transport_problem.solve_once() @@ -482,8 +480,7 @@ def run_steady(self): # print final message if converged: msg = "Solved problem in {:.2f} s".format(elapsed_time) - if MPI.comm_world.rank == 0: - print(msg) + festim.festim_print(msg) else: msg = "The solver diverged in " msg += "{:.0f} iteration(s) ({:.2f} s)".format(nb_iterations, elapsed_time) @@ -520,11 +517,9 @@ def display_time(self): not np.isclose(self.t, self.settings.final_time, atol=0) and self.log_level == 40 ): - if MPI.comm_world.rank == 0: - print(msg, end="\r") + festim.festim_print(msg, end="\r") else: - if MPI.comm_world.rank == 0: - print(msg) + festim.festim_print(msg) def run_post_processing(self): """Create post processing functions and compute/write the exports""" diff --git a/festim/h_transport_problem.py b/festim/h_transport_problem.py index 68d33c320..24cc3f51a 100644 --- a/festim/h_transport_problem.py +++ b/festim/h_transport_problem.py @@ -55,8 +55,9 @@ def newton_solver(self, value): self._newton_solver = value elif isinstance(value, NewtonSolver): if self._newton_solver: - if MPI.comm_world.rank == 0: - print("Settings for the Newton solver will be overwritten") + festim.festim_print( + "Settings for the Newton solver will be overwritten" + ) self._newton_solver = value else: raise TypeError("accepted type for newton_solver is fenics.NewtonSolver") @@ -103,8 +104,7 @@ def initialise(self, mesh, materials, dt=None): self.define_newton_solver() # Boundary conditions - if MPI.comm_world.rank == 0: - print("Defining boundary conditions") + festim.festim_print("Defining boundary conditions") self.create_dirichlet_bcs(materials, mesh) if self.settings.transient: self.traps.define_variational_problem_extrinsic_traps(mesh.dx, dt, self.T) @@ -179,8 +179,7 @@ def initialise_concentrations(self): concentration.test_function = list(split(self.v))[index] index += 1 - if MPI.comm_world.rank == 0: - print("Defining initial values") + festim.festim_print("Defining initial values") field_to_component = { "solute": 0, diff --git a/festim/meshing/mesh_from_xdmf.py b/festim/meshing/mesh_from_xdmf.py index 386e1cb5f..f5631af75 100644 --- a/festim/meshing/mesh_from_xdmf.py +++ b/festim/meshing/mesh_from_xdmf.py @@ -1,5 +1,5 @@ import fenics as f -from festim import Mesh +from festim import Mesh, festim_print class MeshFromXDMF(Mesh): @@ -43,8 +43,9 @@ def define_markers(self): f.XDMFFile(self.boundary_file).read(surface_markers, "f") surface_markers = f.MeshFunction("size_t", mesh, surface_markers) - if f.MPI.comm_world.rank == 0: - print("Succesfully load mesh with " + str(len(volume_markers)) + " cells") + festim_print( + "Succesfully load mesh with " + str(len(volume_markers)) + " cells" + ) self.volume_markers = volume_markers self.surface_markers = surface_markers diff --git a/festim/temperature/temperature_solver.py b/festim/temperature/temperature_solver.py index 67c8210d5..c5204635e 100644 --- a/festim/temperature/temperature_solver.py +++ b/festim/temperature/temperature_solver.py @@ -73,8 +73,9 @@ def newton_solver(self, value): self._newton_solver = value elif isinstance(value, f.NewtonSolver): if self._newton_solver: - if f.MPI.comm_world.rank == 0: - print("Settings for the Newton solver will be overwritten") + festim.festim_print( + "Settings for the Newton solver will be overwritten" + ) self._newton_solver = value else: raise TypeError("accepted type for newton_solver is fenics.NewtonSolver") @@ -132,8 +133,7 @@ def create_functions(self, materials, mesh, dt=None): self.define_newton_solver() if not self.transient: - if f.MPI.comm_world.rank == 0: - print("Solving stationary heat equation") + festim.festim_print("Solving stationary heat equation") dT = f.TrialFunction(self.T.function_space()) JT = f.derivative(self.F, self.T, dT) @@ -156,8 +156,7 @@ def define_variational_problem(self, materials, mesh, dt=None): dt (festim.Stepsize, optional): the stepsize. Only needed if self.transient is True. Defaults to None. """ - if f.MPI.comm_world.rank == 0: - print("Defining variational problem heat transfers") + festim.festim_print("Defining variational problem heat transfers") T, T_n = self.T, self.T_n v_T = self.v_T