Skip to content

Commit b494df6

Browse files
authored
Merge pull request #937 from jhdark/printing_in_parallel
Printing messages in parallel runs
2 parents af3b3c3 + 3eef19e commit b494df6

File tree

8 files changed

+43
-19
lines changed

8 files changed

+43
-19
lines changed

festim/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
as_constant,
2828
as_expression,
2929
as_constant_or_expression,
30+
festim_print,
3031
)
3132

3233
from .meshing.mesh import Mesh

festim/concentration/mobile.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
from festim import Concentration, FluxBC, k_B, RadioactiveDecay, SurfaceKinetics
1+
from festim import (
2+
Concentration,
3+
FluxBC,
4+
k_B,
5+
RadioactiveDecay,
6+
SurfaceKinetics,
7+
festim_print,
8+
)
29
from fenics import *
310

411

@@ -178,7 +185,7 @@ def create_source_form(self, dx):
178185
F_source = 0
179186
expressions_source = []
180187

181-
print("Defining source terms")
188+
festim_print("Defining source terms")
182189
for source in self.sources:
183190
if type(source.volume) is list:
184191
volumes = source.volume

festim/concentration/traps/extrinsic_trap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from festim import Trap, as_constant_or_expression
1+
from festim import Trap, as_constant_or_expression, festim_print
22
from fenics import NewtonSolver, MPI
33

44

@@ -65,7 +65,7 @@ def newton_solver(self, value):
6565
self._newton_solver = value
6666
elif isinstance(value, NewtonSolver):
6767
if self._newton_solver:
68-
print("Settings for the Newton solver will be overwritten")
68+
festim_print("Settings for the Newton solver will be overwritten")
6969
self._newton_solver = value
7070
else:
7171
raise TypeError("accepted type for newton_solver is fenics.NewtonSolver")

festim/generic_simulation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,15 @@ def run_transient(self):
476476
self.h_transport_problem.compute_jacobian()
477477

478478
# Time-stepping
479-
print("Time stepping...")
479+
festim.festim_print("Time stepping...")
480480
while self.t < self.settings.final_time and not np.isclose(
481481
self.t, self.settings.final_time, atol=0
482482
):
483483
self.iterate()
484484

485485
def run_steady(self):
486486
# Solve steady state
487-
print("Solving steady state problem...")
487+
festim.festim_print("Solving steady state problem...")
488488

489489
nb_iterations, converged = self.h_transport_problem.solve_once()
490490

@@ -495,7 +495,7 @@ def run_steady(self):
495495
# print final message
496496
if converged:
497497
msg = "Solved problem in {:.2f} s".format(elapsed_time)
498-
print(msg)
498+
festim.festim_print(msg)
499499
else:
500500
msg = "The solver diverged in "
501501
msg += "{:.0f} iteration(s) ({:.2f} s)".format(nb_iterations, elapsed_time)
@@ -532,9 +532,9 @@ def display_time(self):
532532
not np.isclose(self.t, self.settings.final_time, atol=0)
533533
and self.log_level == 40
534534
):
535-
print(msg, end="\r")
535+
festim.festim_print(msg, end="\r")
536536
else:
537-
print(msg)
537+
festim.festim_print(msg)
538538

539539
def run_post_processing(self):
540540
"""Create post processing functions and compute/write the exports"""

festim/h_transport_problem.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ def newton_solver(self, value):
5555
self._newton_solver = value
5656
elif isinstance(value, NewtonSolver):
5757
if self._newton_solver:
58-
print("Settings for the Newton solver will be overwritten")
58+
festim.festim_print(
59+
"Settings for the Newton solver will be overwritten"
60+
)
5961
self._newton_solver = value
6062
else:
6163
raise TypeError("accepted type for newton_solver is fenics.NewtonSolver")
@@ -102,7 +104,7 @@ def initialise(self, mesh, materials, dt=None):
102104
self.define_newton_solver()
103105

104106
# Boundary conditions
105-
print("Defining boundary conditions")
107+
festim.festim_print("Defining boundary conditions")
106108
self.create_dirichlet_bcs(materials, mesh)
107109
if self.settings.transient:
108110
self.traps.define_variational_problem_extrinsic_traps(mesh.dx, dt, self.T)
@@ -177,7 +179,8 @@ def initialise_concentrations(self):
177179
concentration.test_function = list(split(self.v))[index]
178180
index += 1
179181

180-
print("Defining initial values")
182+
festim.festim_print("Defining initial values")
183+
181184
field_to_component = {
182185
"solute": 0,
183186
"0": 0,
@@ -253,7 +256,9 @@ def define_variational_problem(self, materials, mesh, dt=None):
253256
dt (festim.Stepsize, optional): the stepsize, only needed if
254257
self.settings.transient is True. Defaults to None.
255258
"""
256-
print("Defining variational problem")
259+
if MPI.comm_world.rank == 0:
260+
print("Defining variational problem")
261+
257262
expressions = []
258263
F = 0
259264

festim/helpers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import festim
22
import xml.etree.ElementTree as ET
3-
from fenics import Expression, UserExpression, Constant
3+
from fenics import Expression, UserExpression, Constant, MPI
44
import sympy as sp
55

66

@@ -108,3 +108,8 @@ def extract_xdmf_labels(filename):
108108

109109
unique_labels = list(set(labels))
110110
return unique_labels
111+
112+
113+
def festim_print(msg, end="\n"):
114+
if MPI.comm_world.rank == 0:
115+
print(msg, end=end)

festim/meshing/mesh_from_xdmf.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fenics as f
2-
from festim import Mesh
2+
from festim import Mesh, festim_print
33

44

55
class MeshFromXDMF(Mesh):
@@ -43,6 +43,9 @@ def define_markers(self):
4343
f.XDMFFile(self.boundary_file).read(surface_markers, "f")
4444
surface_markers = f.MeshFunction("size_t", mesh, surface_markers)
4545

46-
print("Succesfully load mesh with " + str(len(volume_markers)) + " cells")
46+
festim_print(
47+
"Succesfully load mesh with " + str(len(volume_markers)) + " cells"
48+
)
49+
4750
self.volume_markers = volume_markers
4851
self.surface_markers = surface_markers

festim/temperature/temperature_solver.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ def newton_solver(self, value):
7373
self._newton_solver = value
7474
elif isinstance(value, f.NewtonSolver):
7575
if self._newton_solver:
76-
print("Settings for the Newton solver will be overwritten")
76+
festim.festim_print(
77+
"Settings for the Newton solver will be overwritten"
78+
)
7779
self._newton_solver = value
7880
else:
7981
raise TypeError("accepted type for newton_solver is fenics.NewtonSolver")
@@ -131,7 +133,8 @@ def create_functions(self, materials, mesh, dt=None):
131133
self.define_newton_solver()
132134

133135
if not self.transient:
134-
print("Solving stationary heat equation")
136+
festim.festim_print("Solving stationary heat equation")
137+
135138
dT = f.TrialFunction(self.T.function_space())
136139
JT = f.derivative(self.F, self.T, dT)
137140
problem = festim.Problem(JT, self.F, self.dirichlet_bcs)
@@ -153,8 +156,8 @@ def define_variational_problem(self, materials, mesh, dt=None):
153156
dt (festim.Stepsize, optional): the stepsize. Only needed if
154157
self.transient is True. Defaults to None.
155158
"""
159+
festim.festim_print("Defining variational problem heat transfers")
156160

157-
print("Defining variational problem heat transfers")
158161
T, T_n = self.T, self.T_n
159162
v_T = self.v_T
160163

0 commit comments

Comments
 (0)