diff --git a/festim/__init__.py b/festim/__init__.py index 1fe1d1397..e2c4ee4c8 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/concentration/mobile.py b/festim/concentration/mobile.py index ec2f666f1..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,7 +185,7 @@ def create_source_form(self, dx): F_source = 0 expressions_source = [] - 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 78a450309..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,7 +65,7 @@ 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") + 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 2bae174f3..beb556f47 100644 --- a/festim/generic_simulation.py +++ b/festim/generic_simulation.py @@ -476,7 +476,7 @@ def run_transient(self): self.h_transport_problem.compute_jacobian() # Time-stepping - 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 ): @@ -484,7 +484,7 @@ def run_transient(self): def run_steady(self): # Solve steady state - print("Solving steady state problem...") + festim.festim_print("Solving steady state problem...") nb_iterations, converged = self.h_transport_problem.solve_once() @@ -495,7 +495,7 @@ def run_steady(self): # print final message if converged: msg = "Solved problem in {:.2f} s".format(elapsed_time) - print(msg) + festim.festim_print(msg) else: msg = "The solver diverged in " msg += "{:.0f} iteration(s) ({:.2f} s)".format(nb_iterations, elapsed_time) @@ -532,9 +532,9 @@ def display_time(self): not np.isclose(self.t, self.settings.final_time, atol=0) and self.log_level == 40 ): - print(msg, end="\r") + festim.festim_print(msg, end="\r") else: - 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 7d4c4859f..24cc3f51a 100644 --- a/festim/h_transport_problem.py +++ b/festim/h_transport_problem.py @@ -55,7 +55,9 @@ 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") + 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") @@ -102,7 +104,7 @@ def initialise(self, mesh, materials, dt=None): self.define_newton_solver() # Boundary conditions - 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) @@ -177,7 +179,8 @@ def initialise_concentrations(self): concentration.test_function = list(split(self.v))[index] index += 1 - print("Defining initial values") + festim.festim_print("Defining initial values") + field_to_component = { "solute": 0, "0": 0, @@ -253,7 +256,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/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) diff --git a/festim/meshing/mesh_from_xdmf.py b/festim/meshing/mesh_from_xdmf.py index ea4411f0d..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,6 +43,9 @@ 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") + 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 c35a635a0..c5204635e 100644 --- a/festim/temperature/temperature_solver.py +++ b/festim/temperature/temperature_solver.py @@ -73,7 +73,9 @@ 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") + 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") @@ -131,7 +133,8 @@ def create_functions(self, materials, mesh, dt=None): self.define_newton_solver() if not self.transient: - 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) problem = festim.Problem(JT, self.F, self.dirichlet_bcs) @@ -153,8 +156,8 @@ 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. """ + festim.festim_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