Skip to content

Commit 252a5a9

Browse files
committed
Clean solvers
1 parent b2018a4 commit 252a5a9

4 files changed

Lines changed: 16 additions & 75 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ build/
1919

2020
*.out
2121
*.pkl
22-
*.txt
22+
*.txt
23+
24+
*.core

doc/demo/demo_plasticity_mohr_coulomb.py

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
# format_name: percent
1010
# format_version: '1.3'
1111
# jupytext_version: 1.11.2
12+
# kernelspec:
13+
# display_name: dolfinx-env
14+
# language: python
15+
# name: python3
1216
# ---
1317

1418
# %% [markdown]
@@ -113,7 +117,7 @@
113117

114118
# %%
115119
L, H = (1.2, 1.0)
116-
Nx, Ny = (200, 200)
120+
Nx, Ny = (25, 25)
117121
gamma = 1.0 / 6778
118122
domain = mesh.create_rectangle(MPI.COMM_WORLD, [np.array([0, 0]), np.array([L, H])], [Nx, Ny])
119123

@@ -669,10 +673,11 @@ def F_ext(v):
669673
# parameters of the manual Newton method
670674
max_iterations, relative_tolerance = 200, 1e-8
671675

672-
load_steps_1 = np.linspace(1.5, 21, 40)
673-
load_steps_2 = np.linspace(21, 22.75, 20)[1:]
676+
load_steps_1 = np.linspace(2, 22.9, 50)
677+
load_steps_2 = np.array([22.96, 22.99])
674678
load_steps = np.concatenate([load_steps_1, load_steps_2])
675-
load_steps = np.concatenate([np.linspace(1.1, 22.3, 100)[:-1]])
679+
680+
# load_steps = np.concatenate([np.linspace(1.1, 22.3, 100)[:-1]])
676681
num_increments = len(load_steps)
677682
results = np.zeros((num_increments + 1, 2))
678683

@@ -702,8 +707,6 @@ def constitutive_update():
702707
external_operator_problem = SNESProblem(Du, F_replaced, J_replaced, bcs=bcs, petsc_options=petsc_options, system_update=constitutive_update)
703708

704709
# %%
705-
timer_total = common.Timer("Total_timer")
706-
timer_total.start()
707710
for i, load in enumerate(load_steps):
708711
q.value = load * np.array([0, -gamma])
709712

@@ -721,14 +724,8 @@ def constitutive_update():
721724

722725
if len(points_on_process) > 0:
723726
results[i + 1, :] = (-u.eval(points_on_process, cells)[0], load)
724-
timer_total.stop()
725-
total_time = timer_total.elapsed()[0]
726727

727728
print(f"Slope stability factor: {-q.value[-1]*H/c}")
728-
print(f"Total time: {total_time}")
729-
730-
# %%
731-
external_operator_problem.performance_monitor
732729

733730
# %% [markdown]
734731
# ## Verification
@@ -768,34 +765,6 @@ def constitutive_update():
768765
plt.grid()
769766
plt.legend()
770767

771-
# %%
772-
external_operator_problem.performance_monitor
773-
774-
# %%
775-
import pickle
776-
with open("performance_monitor_100_100", "wb") as f:
777-
pickle.dump(external_operator_problem.performance_monitor, f)
778-
779-
# %%
780-
# pickle_file = Path("performance_monitor_200_200")
781-
with open("performance_monitor_200_200", 'rb') as f:
782-
performance_monitor_200_200 = pickle.load(f)
783-
784-
# %%
785-
summary_monitor = performance_monitor_200_200.copy()
786-
787-
cols = ["matrix_assembling", "vector_assembling", "linear_solver", "constitutive_model_update"]
788-
summary_monitor["linear_solver"] = summary_monitor["nonlinear_solver"] - summary_monitor["matrix_assembling"] - summary_monitor["vector_assembling"] - summary_monitor["constitutive_model_update"]
789-
790-
fig, ax = plt.subplots(figsize=(10, 5))
791-
summary_monitor.plot(use_index=True, y=cols, kind="bar", stacked=True, ax=ax)
792-
793-
# %%
794-
fig, ax = plt.subplots(figsize=(10, 5))
795-
for col in cols:
796-
summary_monitor[col] = summary_monitor[col] / (summary_monitor["Newton_iterations"]+1)
797-
summary_monitor.plot(use_index=True, y=cols, kind="bar", stacked=True, ax=ax)
798-
799768
# %% [markdown]
800769
# The slope profile reaching its stability limit:
801770

doc/demo/solvers.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,6 @@ def __init__(
110110

111111
self.solver = self.solver_setup()
112112

113-
self.local_monitor = {"matrix_assembling": 0.0, "vector_assembling": 0.0, "constitutive_model_update": 0.0}
114-
self.performance_monitor = pd.DataFrame({
115-
# "loading_step": np.array([], dtype=np.int64),
116-
"Newton_iterations": np.array([], dtype=np.int64),
117-
"matrix_assembling": np.array([], dtype=np.float64),
118-
"vector_assembling": np.array([], dtype=np.float64),
119-
"nonlinear_solver": np.array([], dtype=np.float64),
120-
"constitutive_model_update": np.array([], dtype=np.float64),
121-
})
122-
self.timer = common.Timer("SNES")
123-
124113
def set_petsc_options(self):
125114
# Set PETSc options
126115
opts = PETSc.Options()
@@ -159,22 +148,16 @@ def F(self, snes: PETSc.SNES, x: PETSc.Vec, b: PETSc.Vec) -> None:
159148

160149
#TODO: SNES makes the iteration #0, where it calculates the b norm.
161150
#`system_update()` can be omitted in that case
162-
self.timer.start()
163151
self.system_update()
164-
self.timer.stop()
165-
self.local_monitor["constitutive_model_update"] += self.comm.allreduce(self.timer.elapsed()[0], op=MPI.SUM)
166-
167-
self.timer.start()
152+
168153
with b.localForm() as b_local:
169154
b_local.set(0.0)
170155
fem.petsc.assemble_vector(b, self.F_form)
171156

172157
fem.petsc.apply_lifting(b, [self.J_form], [self.bcs], [x], -1.0)
173158
b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE)
174159
fem.petsc.set_bc(b, self.bcs, x, -1.0)
175-
self.timer.stop()
176-
self.local_monitor["vector_assembling"] += self.comm.allreduce(self.timer.elapsed()[0], op=MPI.SUM)
177-
160+
178161
def J(self, snes, x: PETSc.Vec, A: PETSc.Mat, P: PETSc.Mat) -> None:
179162
"""Assemble the Jacobian matrix.
180163
@@ -183,26 +166,13 @@ def J(self, snes, x: PETSc.Vec, A: PETSc.Mat, P: PETSc.Mat) -> None:
183166
x: Vector containing the latest solution.
184167
A: Matrix to assemble the Jacobian into.
185168
"""
186-
self.timer.start()
187169
A.zeroEntries()
188170
fem.petsc.assemble_matrix(A, self.J_form, self.bcs)
189171
A.assemble()
190-
self.timer.stop()
191-
self.local_monitor["matrix_assembling"] += self.comm.allreduce(self.timer.elapsed()[0], op=MPI.SUM)
192-
172+
193173
def solve(self,) -> Tuple[int, int]:
194-
# self.local_monitor["loading_step"] = loading_step
195-
self.local_monitor["vector_assembling"] = 0.0
196-
self.local_monitor["matrix_assembling"] = 0.0
197-
self.local_monitor["constitutive_model_update"] = 0.0
198-
timer = common.Timer("nonlinear_solver")
199-
self.timer.start()
200174
self.solver.solve(None, self.u.x.petsc_vec)
201-
timer.stop()
202-
self.local_monitor["nonlinear_solver"] = self.comm.allreduce(self.timer.elapsed()[0], op=MPI.SUM)
203-
self.local_monitor["Newton_iterations"] = self.solver.getIterationNumber()
204175
self.u.x.scatter_forward()
205-
self.performance_monitor.loc[len(self.performance_monitor.index)] = self.local_monitor
206176
return (self.solver.getIterationNumber(), self.solver.getConvergedReason())
207177

208178
def __del__(self):

doc/demo/utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def build_cylinder_quarter(lc=0.3, R_e=1.3, R_i=1.0):
6060

6161
# NOTE: Do not forget to check the leaks produced by the following line of code
6262
# [WARNING] yaksa: 2 leaked handle pool objects
63-
mesh, cell_tags, facet_tags = gmshio.model_to_mesh(gmsh.model, MPI.COMM_WORLD, rank=model_rank, gdim=2)
63+
mesh, cell_tags, facet_tags, _, _, _ = gmshio.model_to_mesh(gmsh.model, MPI.COMM_WORLD, rank=model_rank, gdim=2)
6464

6565
mesh.topology.create_connectivity(mesh.topology.dim - 1, mesh.topology.dim)
6666
mesh.name = "quarter_cylinder"

0 commit comments

Comments
 (0)