morphogen 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (381) hide show
  1. morphogen/__init__.py +60 -0
  2. morphogen/__main__.py +7 -0
  3. morphogen/_platform.py +55 -0
  4. morphogen/_public.py +153 -0
  5. morphogen/_utils.py +80 -0
  6. morphogen/_version.py +1 -0
  7. morphogen/analysis/__init__.py +174 -0
  8. morphogen/analysis/anisotropy.py +135 -0
  9. morphogen/analysis/basic.py +38 -0
  10. morphogen/analysis/cells.py +54 -0
  11. morphogen/analysis/connectivity.py +156 -0
  12. morphogen/analysis/correlation.py +195 -0
  13. morphogen/analysis/covariance.py +80 -0
  14. morphogen/analysis/fractal.py +54 -0
  15. morphogen/analysis/measures.py +188 -0
  16. morphogen/analysis/morphology.py +265 -0
  17. morphogen/analysis/particles.py +160 -0
  18. morphogen/analysis/percolation.py +2034 -0
  19. morphogen/analysis/power_laws.py +143 -0
  20. morphogen/analysis/random_sets.py +351 -0
  21. morphogen/analysis/stereology.py +289 -0
  22. morphogen/analysis/surface.py +107 -0
  23. morphogen/analysis/topology.py +92 -0
  24. morphogen/automata/__init__.py +74 -0
  25. morphogen/automata/engine.py +211 -0
  26. morphogen/automata/growth.py +686 -0
  27. morphogen/automata/lattice.py +143 -0
  28. morphogen/automata/reaction.py +255 -0
  29. morphogen/automata/rules.py +353 -0
  30. morphogen/automata/tilings.py +1715 -0
  31. morphogen/cli.py +3526 -0
  32. morphogen/config.py +2167 -0
  33. morphogen/contact/__init__.py +113 -0
  34. morphogen/contact/experiments.py +368 -0
  35. morphogen/contact/hertz.py +104 -0
  36. morphogen/contact/kernels.py +827 -0
  37. morphogen/contact/periodic.py +278 -0
  38. morphogen/contact/problem.py +578 -0
  39. morphogen/contact/surfaces.py +310 -0
  40. morphogen/coupling/__init__.py +43 -0
  41. morphogen/coupling/ageing.py +143 -0
  42. morphogen/coupling/diffusion.py +226 -0
  43. morphogen/coupling/fenicsx.py +484 -0
  44. morphogen/coupling/oxidation.py +447 -0
  45. morphogen/coupling/plates.py +247 -0
  46. morphogen/design/__init__.py +51 -0
  47. morphogen/design/csp.py +616 -0
  48. morphogen/design/frames.py +277 -0
  49. morphogen/design/search.py +287 -0
  50. morphogen/design/tiles.py +336 -0
  51. morphogen/distributions.py +312 -0
  52. morphogen/domain.py +203 -0
  53. morphogen/dynamics/__init__.py +179 -0
  54. morphogen/dynamics/model.py +638 -0
  55. morphogen/dynamics/openradioss.py +643 -0
  56. morphogen/dynamics/results.py +515 -0
  57. morphogen/fracture/__init__.py +170 -0
  58. morphogen/fracture/bulk.py +349 -0
  59. morphogen/fracture/cohesive.py +1614 -0
  60. morphogen/fracture/ductile.py +557 -0
  61. morphogen/fracture/lefm.py +1168 -0
  62. morphogen/fracture/mixed.py +1178 -0
  63. morphogen/fracture/periodic.py +733 -0
  64. morphogen/fracture/phasefield.py +714 -0
  65. morphogen/fracture/phasefield_finite.py +368 -0
  66. morphogen/fracture/surface.py +572 -0
  67. morphogen/fracture/toughness.py +566 -0
  68. morphogen/gpu.py +108 -0
  69. morphogen/gui/__init__.py +8 -0
  70. morphogen/gui/api.py +1927 -0
  71. morphogen/gui/desktop.py +387 -0
  72. morphogen/gui/icon.png +0 -0
  73. morphogen/gui/index.html +1909 -0
  74. morphogen/gui/server.py +229 -0
  75. morphogen/gui/tools/__init__.py +629 -0
  76. morphogen/gui/tools/compare.py +149 -0
  77. morphogen/gui/tools/contact.py +391 -0
  78. morphogen/gui/tools/couplings.py +1085 -0
  79. morphogen/gui/tools/crackfront.py +245 -0
  80. morphogen/gui/tools/design.py +224 -0
  81. morphogen/gui/tools/dynamics.py +824 -0
  82. morphogen/gui/tools/fracture.py +2586 -0
  83. morphogen/gui/tools/interop.py +1654 -0
  84. morphogen/gui/tools/learning.py +595 -0
  85. morphogen/gui/tools/mechanics.py +3881 -0
  86. morphogen/gui/tools/nonlocal_damage.py +1321 -0
  87. morphogen/gui/tools/phasefield.py +215 -0
  88. morphogen/gui/tools/physics.py +597 -0
  89. morphogen/gui/tools/stability.py +1756 -0
  90. morphogen/gui/tools/statistics.py +1408 -0
  91. morphogen/gui/viewer.js +1100 -0
  92. morphogen/homogenization/__init__.py +210 -0
  93. morphogen/homogenization/acoustics.py +566 -0
  94. morphogen/homogenization/amg.py +1005 -0
  95. morphogen/homogenization/apparent.py +1046 -0
  96. morphogen/homogenization/behaviours.cu +2285 -0
  97. morphogen/homogenization/behaviours.py +944 -0
  98. morphogen/homogenization/beremin.py +263 -0
  99. morphogen/homogenization/bifurcation.py +1085 -0
  100. morphogen/homogenization/bloch.py +507 -0
  101. morphogen/homogenization/bounds.py +226 -0
  102. morphogen/homogenization/core.py +458 -0
  103. morphogen/homogenization/coupled.py +1135 -0
  104. morphogen/homogenization/crystal.py +2967 -0
  105. morphogen/homogenization/dilatation.py +454 -0
  106. morphogen/homogenization/elements.py +157 -0
  107. morphogen/homogenization/fatigue.py +768 -0
  108. morphogen/homogenization/fe.py +1739 -0
  109. morphogen/homogenization/fenicsx.py +307 -0
  110. morphogen/homogenization/fft.py +499 -0
  111. morphogen/homogenization/fft_nonlinear.py +391 -0
  112. morphogen/homogenization/finite.py +613 -0
  113. morphogen/homogenization/flow.py +361 -0
  114. morphogen/homogenization/frames.py +1125 -0
  115. morphogen/homogenization/gpu.py +1241 -0
  116. morphogen/homogenization/harmonic.py +427 -0
  117. morphogen/homogenization/identification.py +579 -0
  118. morphogen/homogenization/kernels.py +760 -0
  119. morphogen/homogenization/laws.py +2061 -0
  120. morphogen/homogenization/materials.py +1080 -0
  121. morphogen/homogenization/mfront.py +589 -0
  122. morphogen/homogenization/networks.py +880 -0
  123. morphogen/homogenization/nonlinear.py +2612 -0
  124. morphogen/homogenization/ntfa.py +577 -0
  125. morphogen/homogenization/paths.py +476 -0
  126. morphogen/homogenization/photonic.py +467 -0
  127. morphogen/homogenization/poroelastic.py +212 -0
  128. morphogen/homogenization/porous.py +1120 -0
  129. morphogen/homogenization/postprocess.py +290 -0
  130. morphogen/homogenization/solvers.py +1140 -0
  131. morphogen/homogenization/tensors.py +240 -0
  132. morphogen/homogenization/voxel_fe.py +457 -0
  133. morphogen/homogenization/xfem.py +1016 -0
  134. morphogen/homogenization/zmat.py +1258 -0
  135. morphogen/iga/__init__.py +48 -0
  136. morphogen/iga/analysis.py +508 -0
  137. morphogen/iga/basis.py +214 -0
  138. morphogen/iga/geometry.py +501 -0
  139. morphogen/iga/homogenization.py +219 -0
  140. morphogen/iga/optimization.py +700 -0
  141. morphogen/iga/shapes.py +331 -0
  142. morphogen/image.py +128 -0
  143. morphogen/io/__init__.py +246 -0
  144. morphogen/io/amitex.py +741 -0
  145. morphogen/io/ebsd.py +881 -0
  146. morphogen/io/export.py +196 -0
  147. morphogen/io/geometry.py +158 -0
  148. morphogen/io/medit.py +147 -0
  149. morphogen/io/meshes.py +844 -0
  150. morphogen/io/metaimage.py +97 -0
  151. morphogen/io/mfront.py +1104 -0
  152. morphogen/io/native.py +57 -0
  153. morphogen/io/radioss.py +764 -0
  154. morphogen/io/solvers.py +371 -0
  155. morphogen/io/tables.py +278 -0
  156. morphogen/io/tiff.py +66 -0
  157. morphogen/io/vtk.py +701 -0
  158. morphogen/io/xdmf.py +200 -0
  159. morphogen/kernels/__init__.py +5 -0
  160. morphogen/kernels/annealing.py +192 -0
  161. morphogen/kernels/cells.py +69 -0
  162. morphogen/kernels/collective.py +87 -0
  163. morphogen/kernels/force_biased.py +595 -0
  164. morphogen/kernels/implicit.py +145 -0
  165. morphogen/kernels/leaves.py +132 -0
  166. morphogen/kernels/packing.py +304 -0
  167. morphogen/kernels/raster.py +279 -0
  168. morphogen/kernels/rsa.py +371 -0
  169. morphogen/kernels/union.py +135 -0
  170. morphogen/kernels/watershed.py +190 -0
  171. morphogen/laser/__init__.py +234 -0
  172. morphogen/laser/architectures.py +375 -0
  173. morphogen/laser/bending.py +216 -0
  174. morphogen/laser/cells.py +1694 -0
  175. morphogen/laser/design.py +940 -0
  176. morphogen/laser/figures.py +227 -0
  177. morphogen/laser/heat.py +804 -0
  178. morphogen/laser/materials.py +691 -0
  179. morphogen/laser/mechanics.py +1670 -0
  180. morphogen/laser/metallurgy.py +615 -0
  181. morphogen/laser/patterns.py +828 -0
  182. morphogen/laser/periodic.py +392 -0
  183. morphogen/laser/process.py +362 -0
  184. morphogen/laser/run.py +534 -0
  185. morphogen/laser/solid.py +216 -0
  186. morphogen/laser/specimen.py +200 -0
  187. morphogen/laser/structures.py +708 -0
  188. morphogen/learning/__init__.py +81 -0
  189. morphogen/learning/behaviour.py +398 -0
  190. morphogen/learning/data.py +328 -0
  191. morphogen/learning/learned.cpp +668 -0
  192. morphogen/learning/logarithmic.py +202 -0
  193. morphogen/learning/mfront.py +409 -0
  194. morphogen/learning/networks.py +767 -0
  195. morphogen/learning/run.py +212 -0
  196. morphogen/learning/standard.py +814 -0
  197. morphogen/learning/training.py +235 -0
  198. morphogen/mesh/__init__.py +29 -0
  199. morphogen/mesh/conforming.py +1423 -0
  200. morphogen/mesh/curved.py +600 -0
  201. morphogen/mesh/exact.py +1717 -0
  202. morphogen/mesh/octree.py +350 -0
  203. morphogen/mesh/polycrystal.py +1041 -0
  204. morphogen/mesh/polyhedra.py +677 -0
  205. morphogen/mesh/regularize.py +570 -0
  206. morphogen/mesh/surface.py +97 -0
  207. morphogen/mesh/tetra.py +512 -0
  208. morphogen/mesh/voxel.py +124 -0
  209. morphogen/micromorphic/__init__.py +31 -0
  210. morphogen/micromorphic/damage.py +359 -0
  211. morphogen/micromorphic/ductile.py +534 -0
  212. morphogen/micromorphic/finite.py +220 -0
  213. morphogen/micromorphic/microfoam.py +787 -0
  214. morphogen/micromorphic/space.py +341 -0
  215. morphogen/models/__init__.py +195 -0
  216. morphogen/models/affine.py +384 -0
  217. morphogen/models/automata.py +592 -0
  218. morphogen/models/base.py +500 -0
  219. morphogen/models/boolean.py +310 -0
  220. morphogen/models/bouligand.py +331 -0
  221. morphogen/models/branching.py +241 -0
  222. morphogen/models/combination.py +430 -0
  223. morphogen/models/deadleaves.py +766 -0
  224. morphogen/models/design.py +289 -0
  225. morphogen/models/ebsd.py +206 -0
  226. morphogen/models/fibers.py +1476 -0
  227. morphogen/models/foam.py +594 -0
  228. morphogen/models/gaussian.py +314 -0
  229. morphogen/models/growth.py +459 -0
  230. morphogen/models/hardcore.py +317 -0
  231. morphogen/models/hierarchical.py +269 -0
  232. morphogen/models/honeycomb.py +749 -0
  233. morphogen/models/implicit.py +177 -0
  234. morphogen/models/laminate.py +470 -0
  235. morphogen/models/lattice.py +351 -0
  236. morphogen/models/lattice_field.py +345 -0
  237. morphogen/models/levelset_foam.py +563 -0
  238. morphogen/models/nacre.py +411 -0
  239. morphogen/models/nested.py +924 -0
  240. morphogen/models/osteons.py +374 -0
  241. morphogen/models/pieces.py +167 -0
  242. morphogen/models/planar.py +126 -0
  243. morphogen/models/reconstruction.py +203 -0
  244. morphogen/models/scale_rich.py +1409 -0
  245. morphogen/models/scale_rich_3d.py +1616 -0
  246. morphogen/models/tessellation.py +735 -0
  247. morphogen/models/tiling_field.py +302 -0
  248. morphogen/models/tpms.py +197 -0
  249. morphogen/models/transparent.py +406 -0
  250. morphogen/models/union.py +141 -0
  251. morphogen/optimization/__init__.py +43 -0
  252. morphogen/optimization/cell.py +295 -0
  253. morphogen/optimization/filters.py +115 -0
  254. morphogen/optimization/mma.py +226 -0
  255. morphogen/optimization/run.py +219 -0
  256. morphogen/optimization/topology.py +632 -0
  257. morphogen/orientations.py +568 -0
  258. morphogen/parallel.py +599 -0
  259. morphogen/pipeline.py +978 -0
  260. morphogen/primitives.py +188 -0
  261. morphogen/results.py +564 -0
  262. morphogen/rve/__init__.py +73 -0
  263. morphogen/rve/report.py +627 -0
  264. morphogen/rve/scaling.py +363 -0
  265. morphogen/rve/study.py +1054 -0
  266. morphogen/rve/windows.py +345 -0
  267. morphogen/seeding.py +51 -0
  268. morphogen/settings.py +118 -0
  269. morphogen/stopping.py +48 -0
  270. morphogen/symmetry.py +384 -0
  271. morphogen/templates/activator_inhibitor.yaml +22 -0
  272. morphogen/templates/aggregation.yaml +23 -0
  273. morphogen/templates/boolean_cylinders.yaml +20 -0
  274. morphogen/templates/boolean_spheres.yaml +22 -0
  275. morphogen/templates/bouligand.yaml +28 -0
  276. morphogen/templates/cellular_automaton.yaml +25 -0
  277. morphogen/templates/coated_spheres.yaml +26 -0
  278. morphogen/templates/contact_fibers.yaml +27 -0
  279. morphogen/templates/cox_nanocomposite.yaml +22 -0
  280. morphogen/templates/cross_linked_fibers.yaml +26 -0
  281. morphogen/templates/curved_arcs.yaml +28 -0
  282. morphogen/templates/curved_fibers.yaml +24 -0
  283. morphogen/templates/dead_leaves.yaml +22 -0
  284. morphogen/templates/delaunay_network.yaml +22 -0
  285. morphogen/templates/dense_packing.yaml +22 -0
  286. morphogen/templates/diffusion.yaml +33 -0
  287. morphogen/templates/ebsd.yaml +34 -0
  288. morphogen/templates/ebsd_map.ctf +3087 -0
  289. morphogen/templates/fracture_composite.yaml +42 -0
  290. morphogen/templates/fracture_rubber.yaml +47 -0
  291. morphogen/templates/gaussian_excursion.yaml +17 -0
  292. morphogen/templates/graded_lattice.yaml +24 -0
  293. morphogen/templates/hardcore_fibers.yaml +22 -0
  294. morphogen/templates/hardcore_spheres.yaml +22 -0
  295. morphogen/templates/hierarchical_lattice.yaml +22 -0
  296. morphogen/templates/homogenization_acoustic_foam.yaml +36 -0
  297. morphogen/templates/homogenization_beremin.yaml +50 -0
  298. morphogen/templates/homogenization_cleavage.yaml +51 -0
  299. morphogen/templates/homogenization_cleavage_band.yaml +55 -0
  300. morphogen/templates/homogenization_contact.yaml +43 -0
  301. morphogen/templates/homogenization_crack_closure.yaml +49 -0
  302. morphogen/templates/homogenization_crystal_plasticity.yaml +50 -0
  303. morphogen/templates/homogenization_crystal_plasticity_fft.yaml +52 -0
  304. morphogen/templates/homogenization_crystallization.yaml +30 -0
  305. morphogen/templates/homogenization_debonding.yaml +43 -0
  306. morphogen/templates/homogenization_detwinning.yaml +58 -0
  307. morphogen/templates/homogenization_ductile_damage.yaml +47 -0
  308. morphogen/templates/homogenization_fatigue.yaml +40 -0
  309. morphogen/templates/homogenization_grain_fracture.yaml +56 -0
  310. morphogen/templates/homogenization_gyroid.yaml +30 -0
  311. morphogen/templates/homogenization_intergranular.yaml +43 -0
  312. morphogen/templates/homogenization_interphase.yaml +42 -0
  313. morphogen/templates/homogenization_mixed_loading.yaml +33 -0
  314. morphogen/templates/homogenization_ntfa.yaml +56 -0
  315. morphogen/templates/homogenization_piezoelectric.yaml +27 -0
  316. morphogen/templates/homogenization_polycrystal.yaml +30 -0
  317. morphogen/templates/homogenization_polycrystal_inclusions.yaml +30 -0
  318. morphogen/templates/homogenization_poroelastic.yaml +26 -0
  319. morphogen/templates/homogenization_rubber.yaml +40 -0
  320. morphogen/templates/homogenization_slip_cleavage.yaml +50 -0
  321. morphogen/templates/homogenization_spheres.yaml +31 -0
  322. morphogen/templates/homogenization_transport.yaml +29 -0
  323. morphogen/templates/homogenization_twinning.yaml +54 -0
  324. morphogen/templates/homogenization_viscoelastic.yaml +34 -0
  325. morphogen/templates/homogenization_viscoplastic.yaml +42 -0
  326. morphogen/templates/homogenization_voronoi.yaml +26 -0
  327. morphogen/templates/homogenization_xfem.yaml +23 -0
  328. morphogen/templates/hyperuniform_cells.yaml +24 -0
  329. morphogen/templates/laguerre.yaml +18 -0
  330. morphogen/templates/laser_bending.yaml +35 -0
  331. morphogen/templates/laser_cells.yaml +38 -0
  332. morphogen/templates/laser_corrugated.yaml +36 -0
  333. morphogen/templates/level_set_foam.yaml +29 -0
  334. morphogen/templates/menger_sponge.yaml +17 -0
  335. morphogen/templates/nacre.yaml +25 -0
  336. morphogen/templates/nucleation_growth.yaml +25 -0
  337. morphogen/templates/optimize_auxetic.yaml +29 -0
  338. morphogen/templates/optimize_auxetic_3d.yaml +27 -0
  339. morphogen/templates/optimize_bulk.yaml +23 -0
  340. morphogen/templates/optimize_conductivity.yaml +20 -0
  341. morphogen/templates/osteons.yaml +28 -0
  342. morphogen/templates/penetrable_spheres.yaml +21 -0
  343. morphogen/templates/poisson_fibers.yaml +21 -0
  344. morphogen/templates/random_laminate.yaml +26 -0
  345. morphogen/templates/random_lattice.yaml +27 -0
  346. morphogen/templates/random_walk_fibers.yaml +26 -0
  347. morphogen/templates/reaction_diffusion.yaml +22 -0
  348. morphogen/templates/reconstruction.yaml +22 -0
  349. morphogen/templates/rve_cellular_automaton.yaml +23 -0
  350. morphogen/templates/rve_crystal_plasticity_fft.yaml +51 -0
  351. morphogen/templates/rve_multiphysics.yaml +43 -0
  352. morphogen/templates/rve_poisson_fibers.yaml +22 -0
  353. morphogen/templates/rve_poisson_fibers_fe.yaml +33 -0
  354. morphogen/templates/rve_voronoi_windows.yaml +20 -0
  355. morphogen/templates/scale_rich_network.yaml +29 -0
  356. morphogen/templates/scale_rich_plates.yaml +30 -0
  357. morphogen/templates/sequential_dead_leaves.yaml +25 -0
  358. morphogen/templates/strut_lattice.yaml +21 -0
  359. morphogen/templates/tile_assembly.yaml +28 -0
  360. morphogen/templates/tpms.yaml +19 -0
  361. morphogen/templates/trabecular_bone.yaml +29 -0
  362. morphogen/templates/transparent_dead_leaves.yaml +23 -0
  363. morphogen/templates/vascular_tree.yaml +23 -0
  364. morphogen/templates/voronoi.yaml +20 -0
  365. morphogen/templates/wood.yaml +34 -0
  366. morphogen/templates/workflow.yaml +56 -0
  367. morphogen/texture.py +665 -0
  368. morphogen/theory.py +162 -0
  369. morphogen/viz/__init__.py +12 -0
  370. morphogen/viz/export.py +244 -0
  371. morphogen/viz/figures.py +707 -0
  372. morphogen/viz/png.py +148 -0
  373. morphogen/viz/sections.py +696 -0
  374. morphogen/viz/surfaces.py +1352 -0
  375. morphogen/viz/video.py +904 -0
  376. morphogen/viz/workflow.py +488 -0
  377. morphogen-1.0.0.dist-info/METADATA +2316 -0
  378. morphogen-1.0.0.dist-info/RECORD +381 -0
  379. morphogen-1.0.0.dist-info/WHEEL +4 -0
  380. morphogen-1.0.0.dist-info/entry_points.txt +5 -0
  381. morphogen-1.0.0.dist-info/licenses/LICENSE +674 -0
@@ -0,0 +1,1140 @@
1
+ """Linear solvers for the symmetric positive definite systems of the FE problems.
2
+
3
+ * ``direct``: sparse LU factorization (SuperLU, from SciPy), reused for every right-hand
4
+ side. Exact and fast for small systems; its memory grows quickly in 3D.
5
+ * ``amg``: conjugate gradient preconditioned by smoothed-aggregation algebraic multigrid
6
+ (:mod:`morphogen.homogenization.amg`), with the rigid-body modes (or constants) as
7
+ near-null space. For 10-node tetrahedra, the preconditioner has two levels: a
8
+ Gauss-Seidel sweep on the quadratic system, and an AMG cycle on the linear system of
9
+ the corner nodes (Galerkin projection). The hierarchy is reused for every right-hand
10
+ side.
11
+ * ``cg``: conjugate gradient with a Jacobi preconditioner (slow on large or
12
+ high-contrast problems; for checks).
13
+ * ``auto``: ``direct`` up to :data:`DIRECT_LIMIT` unknowns, ``amg`` above.
14
+
15
+ The tangents of the nonlinear problems whose behaviours have nonsymmetric consistent
16
+ tangents (``symmetric=False``: crystal and porous plasticity, the Armstrong-Frederick
17
+ hardening, learned laws that are not potentials, MFront behaviours without a symmetric
18
+ tangent) are solved by sparse LU (``direct``) or by GMRES with deflated restarts
19
+ (:func:`gmres`) with the preconditioner of their symmetric part (``amg``, ``cg``); along
20
+ the loading paths of the voxels GMRES recycles its space from a solve to the next once
21
+ one restarts (GCRO-DR, :class:`Recycling`) and stops where it stagnates
22
+ (:data:`GMRES_STAGNATION`).
23
+ """
24
+
25
+ from __future__ import annotations
26
+
27
+ import logging
28
+ import math
29
+ import time
30
+ from dataclasses import dataclass
31
+
32
+ import numpy as np
33
+ import scipy.linalg as sla
34
+ import scipy.sparse as sp
35
+ import scipy.sparse.linalg as spla
36
+ from numba import njit, prange
37
+
38
+ from morphogen.homogenization.amg import NodeBlocks, SmoothedAggregation, Smoother, product
39
+ from morphogen.settings import without_blas_threads
40
+ from morphogen.stopping import checkpoint
41
+
42
+ log = logging.getLogger("morphogen")
43
+
44
+ DIRECT_LIMIT = 6_000
45
+ #: A multigrid preconditioner kept from a step of a loading path to the next is built again
46
+ #: when the iterations that a solve takes for a decade of its residual have grown by this
47
+ #: factor since it was built (:meth:`LinearSolver.worn`): building it costs as much as
48
+ #: the iterations that a third more would add to a step
49
+ WORN = 1.3
50
+ #: The dimension of the Krylov spaces of :func:`gmres` before it restarts (the vectors of
51
+ #: its basis kept in memory)
52
+ GMRES_RESTART = 50
53
+ #: The approximate eigenvectors that :func:`gmres` keeps at its restarts: the harmonic Ritz
54
+ #: vectors of the harmonic Ritz values of least modulus (deflated restarting; 0: the plain
55
+ #: restarts)
56
+ GMRES_DEFLATE = 20
57
+ #: The cycles over which the residual of a solve of :func:`gmres` along a loading path must
58
+ #: at least halve (``stagnation``): 240 iterations of restarted cycles. Measured on the
59
+ #: softening composites of 16^3 voxels (Rousselier and GTN at high triaxialities), it stops
60
+ #: all the solves that would end at their ``maxiter`` above their tolerance (35 of them,
61
+ #: after about 300 iterations instead of 1,320) and none of the 95 that converge after
62
+ #: restarts (up to 528 iterations, after slow cycles); six cycles would stop three of
63
+ #: these, three cycles eight
64
+ GMRES_STAGNATION = 8
65
+ METHODS = ("auto", "direct", "amg", "cg")
66
+ #: Solvers of finite element problems: morphoGen's linear solvers, or FEniCSx
67
+ #: (:mod:`morphogen.homogenization.fenicsx`).
68
+ SOLVERS = (*METHODS, "fenicsx")
69
+ #: Right-hand sides smaller than this fraction of their scale (the norm they would have
70
+ #: without cancellation between elements) are round-off: the affine field solves the
71
+ #: problem (homogeneous medium, laminate loaded along its layers...).
72
+ NEGLIGIBLE = 1e-12
73
+
74
+
75
+ def dot(a: np.ndarray, b: np.ndarray) -> float:
76
+ """Dot product of two vectors in a single thread. BLAS (``a @ b``) splits long
77
+ vectors over its own threads, which compete with those of the compiled kernels and
78
+ slow the solvers down several times — during the product and for a tenth of a second
79
+ after it, while its idle threads wait for the next one (OpenBLAS); this sum is also
80
+ the same for any number of threads."""
81
+ return float(np.einsum("i,i->", a, b))
82
+
83
+
84
+ def norm(a: np.ndarray) -> float:
85
+ """Euclidean norm of a vector (see :func:`dot`)."""
86
+ return math.sqrt(dot(a, a))
87
+
88
+
89
+ #: The sums of the products of two arrays of one or two dimensions
90
+ _TIMES = {(1, 1): "i,i->", (2, 1): "ij,j->i", (1, 2): "i,ij->j", (2, 2): "ij,jk->ik"}
91
+
92
+
93
+ def times(a: np.ndarray, b: np.ndarray) -> np.ndarray:
94
+ """``a @ b`` for arrays of one or two dimensions, in a single thread: the products
95
+ of long arrays between two compiled kernels (see :func:`dot`)."""
96
+ return np.einsum(_TIMES[a.ndim, b.ndim], a, b)
97
+
98
+
99
+ def choose_method(method: str, n: int) -> str:
100
+ """Concrete solver for ``n`` unknowns (resolves ``auto``)."""
101
+ if method not in METHODS:
102
+ raise ValueError(f"solver: one of {METHODS}, got {method!r}")
103
+ if method != "auto":
104
+ return method
105
+ return "direct" if n <= DIRECT_LIMIT else "amg"
106
+
107
+
108
+ @dataclass
109
+ class SolveInfo:
110
+ method: str
111
+ n_dofs: int
112
+ iterations: int
113
+ residual: float
114
+ setup_s: float
115
+ solve_s: float
116
+
117
+ def to_dict(self) -> dict:
118
+ return {
119
+ "solver": self.method,
120
+ "n_dofs": self.n_dofs,
121
+ "iterations": self.iterations,
122
+ "residual": self.residual,
123
+ "setup_s": round(self.setup_s, 4),
124
+ "solve_s": round(self.solve_s, 4),
125
+ }
126
+
127
+
128
+ @without_blas_threads
129
+ def conjugate_gradient(apply, precondition, rhs, tol, maxiter):
130
+ """Preconditioned conjugate gradient from ``x = 0``.
131
+
132
+ ``apply(v)`` and ``precondition(r)`` return new arrays. Stops when the residual is
133
+ below ``tol`` times the norm of ``rhs``; returns (x, iterations, relative residual).
134
+ A direction of negative curvature (an indefinite operator: the tangent of softening
135
+ points) hands the system to :func:`minres`, from the current iterate or from zero,
136
+ whichever leaves the smaller residual.
137
+ Each iteration is a checkpoint of :mod:`morphogen.stopping`.
138
+ """
139
+ x = np.zeros_like(rhs)
140
+ norm_b = norm(rhs)
141
+ if norm_b == 0.0:
142
+ return x, 0, 0.0
143
+ r = rhs.copy()
144
+ z = precondition(r)
145
+ p = z.copy()
146
+ rz = dot(r, z)
147
+ res = 1.0
148
+ it = 0
149
+ for it in range(1, maxiter + 1):
150
+ checkpoint()
151
+ ap = apply(p)
152
+ pap = dot(p, ap)
153
+ if pap <= 0.0:
154
+ norm_r = norm(r)
155
+ if pap < 0.0 and norm_r > 0.0:
156
+ if norm_r > norm_b: # an iterate worse than zero
157
+ x[:] = 0.0
158
+ r = rhs.copy()
159
+ norm_r = norm_b
160
+ dx, more, rel = minres(apply, precondition, r, tol * norm_b / norm_r,
161
+ maxiter - it) # fmt: skip
162
+ return x + dx, it + more, rel * norm_r / norm_b
163
+ break
164
+ alpha = rz / pap
165
+ x += alpha * p
166
+ r -= alpha * ap
167
+ res = norm(r) / norm_b
168
+ if res <= tol:
169
+ break
170
+ z = precondition(r)
171
+ rz_new = dot(r, z)
172
+ if rz_new <= 0.0: # the residual is round-off the preconditioner cancels
173
+ break
174
+ p *= rz_new / rz
175
+ p += z
176
+ rz = rz_new
177
+ return x, it, res
178
+
179
+
180
+ @without_blas_threads
181
+ def minres(apply, precondition, rhs, tol, maxiter):
182
+ """Preconditioned MINRES (Paige & Saunders 1975) from ``x = 0``, for a symmetric
183
+ operator that may be indefinite (the tangent of softening points) and a symmetric
184
+ positive definite preconditioner.
185
+
186
+ Each cycle stops when the residual in the norm of the preconditioner falls below the
187
+ target; the residual is then computed in the Euclidean norm, and another cycle starts
188
+ from it while it is above ``tol`` times the norm of ``rhs``. Returns (x, iterations,
189
+ relative residual), as :func:`conjugate_gradient`.
190
+ """
191
+ x = np.zeros_like(rhs)
192
+ norm_b = norm(rhs)
193
+ if norm_b == 0.0:
194
+ return x, 0, 0.0
195
+ r = rhs.copy()
196
+ res = 1.0
197
+ total = 0
198
+ while total < maxiter:
199
+ dx, its = _minres_cycle(apply, precondition, r, tol / res, maxiter - total)
200
+ total += its
201
+ x += dx
202
+ r = rhs - apply(x)
203
+ res = norm(r) / norm_b
204
+ if res <= tol or its == 0:
205
+ break
206
+ return x, total, res
207
+
208
+
209
+ def _minres_cycle(apply, precondition, rhs, tol, maxiter):
210
+ """One cycle of :func:`minres` from ``x = 0``: (x, iterations)."""
211
+ x = np.zeros_like(rhs)
212
+ r1 = rhs.copy()
213
+ y = precondition(r1)
214
+ beta1 = dot(r1, y)
215
+ if not beta1 > 0.0:
216
+ return x, 0
217
+ beta1 = math.sqrt(beta1)
218
+ oldb, beta, dbar, epsln, phibar, cs, sn = 0.0, beta1, 0.0, 0.0, beta1, -1.0, 0.0
219
+ w = np.zeros_like(rhs)
220
+ w2 = np.zeros_like(rhs)
221
+ r2 = r1
222
+ it = 0
223
+ for it in range(1, maxiter + 1):
224
+ checkpoint()
225
+ v = y / beta
226
+ y = apply(v)
227
+ if it >= 2:
228
+ y -= (beta / oldb) * r1
229
+ alfa = dot(v, y)
230
+ y -= (alfa / beta) * r2
231
+ r1 = r2
232
+ r2 = y
233
+ y = precondition(r2)
234
+ oldb = beta
235
+ beta = dot(r2, y)
236
+ if beta < 0.0: # a preconditioner that is not positive definite
237
+ break
238
+ beta = math.sqrt(beta)
239
+ # the previous rotation, then the next one
240
+ oldeps = epsln
241
+ delta = cs * dbar + sn * alfa
242
+ gbar = sn * dbar - cs * alfa
243
+ epsln = sn * beta
244
+ dbar = -cs * beta
245
+ gamma = max(math.hypot(gbar, beta), 1e-300)
246
+ cs = gbar / gamma
247
+ sn = beta / gamma
248
+ phi = cs * phibar
249
+ phibar = sn * phibar
250
+ w1 = w2
251
+ w2 = w
252
+ w = (v - oldeps * w1 - delta * w2) / gamma
253
+ x += phi * w
254
+ if abs(phibar) <= tol * beta1 or beta == 0.0:
255
+ break
256
+ return x, it
257
+
258
+
259
+ #: The sums of the Gram-Schmidt passes of :func:`gmres` run by blocks of at least _BLOCK
260
+ #: values, at most _MAX_BLOCKS of them: their number is set by the length of the vectors,
261
+ #: so that the sums are the same for any number of threads, and whether one thread or
262
+ #: several sweep them. Vectors of a single block (shorter than _PARALLEL) are swept by one
263
+ #: thread (waking the threads would take longer; from two blocks on, the threads take a
264
+ #: GMRES iteration on 24^3 voxels from 15.7 to 9.3 ms, 6.5 ms for the conjugate gradients)
265
+ _BLOCK = 4096
266
+ _MAX_BLOCKS = 256
267
+ _PARALLEL = 2 * _BLOCK
268
+ #: A Gram-Schmidt pass of :func:`gmres` that leaves less than this fraction of the norm of
269
+ #: the vector is done again (Daniel, Gragg, Kaufman & Stewart 1976: twice is enough)
270
+ _REORTHOGONALIZE = 1.0 / math.sqrt(2.0)
271
+
272
+
273
+ @njit(cache=True)
274
+ def _block_dots(basis, k, w, lo, hi, out):
275
+ """``out[:k] = basis[:k, lo:hi] w[lo:hi]``, by four interleaved partial sums."""
276
+ for i in range(k):
277
+ a0 = a1 = a2 = a3 = 0.0
278
+ t = lo
279
+ while t + 4 <= hi:
280
+ a0 += basis[i, t] * w[t]
281
+ a1 += basis[i, t + 1] * w[t + 1]
282
+ a2 += basis[i, t + 2] * w[t + 2]
283
+ a3 += basis[i, t + 3] * w[t + 3]
284
+ t += 4
285
+ while t < hi:
286
+ a0 += basis[i, t] * w[t]
287
+ t += 1
288
+ out[i] = (a0 + a1) + (a2 + a3)
289
+
290
+
291
+ @njit(parallel=True, cache=True)
292
+ def _parallel_dots(basis, k, w, partial):
293
+ n, n_blocks = w.size, partial.shape[0]
294
+ for b in prange(n_blocks):
295
+ _block_dots(basis, k, w, b * n // n_blocks, (b + 1) * n // n_blocks, partial[b])
296
+
297
+
298
+ @njit(cache=True)
299
+ def _serial_dots(basis, k, w, partial):
300
+ n, n_blocks = w.size, partial.shape[0]
301
+ for b in range(n_blocks):
302
+ _block_dots(basis, k, w, b * n // n_blocks, (b + 1) * n // n_blocks, partial[b])
303
+
304
+
305
+ def _dots(basis, k: int, w, out) -> None:
306
+ """``out[:k] = basis[:k] w``: the dot products of ``w`` with the first ``k`` vectors of
307
+ ``basis`` (its rows), in one sweep of them."""
308
+ n = w.size
309
+ partial = np.empty((min(max(n // _BLOCK, 1), _MAX_BLOCKS), k))
310
+ (_parallel_dots if n >= _PARALLEL else _serial_dots)(basis, k, w, partial)
311
+ out[:k] = partial.sum(axis=0)
312
+
313
+
314
+ @njit(cache=True)
315
+ def _block_subtract(basis, k, coef, w, lo, hi):
316
+ for i in range(k):
317
+ c = coef[i]
318
+ for t in range(lo, hi):
319
+ w[t] -= c * basis[i, t]
320
+
321
+
322
+ @njit(parallel=True, cache=True)
323
+ def _parallel_subtract(basis, k, coef, w, n_blocks):
324
+ n = w.size
325
+ for b in prange(n_blocks):
326
+ _block_subtract(basis, k, coef, w, b * n // n_blocks, (b + 1) * n // n_blocks)
327
+
328
+
329
+ def _subtract(basis, k: int, coef, w) -> None:
330
+ """``w -= basis[:k]^T coef[:k]``, in place (the vectors in the order of the basis)."""
331
+ n = w.size
332
+ if n >= _PARALLEL:
333
+ _parallel_subtract(basis, k, coef, w, min(max(n // _BLOCK, 1), _MAX_BLOCKS))
334
+ else:
335
+ _block_subtract(basis, k, coef, w, 0, n)
336
+
337
+
338
+ #: The columns of the basis that :func:`_combine` turns at once
339
+ _CHUNK = 256
340
+
341
+
342
+ @njit(cache=True)
343
+ def _block_combine(basis, rows, p, lo, hi):
344
+ """``basis[i, lo:hi] = sum_j p[j, i] basis[j, lo:hi]`` for the columns ``i`` of ``p`` and
345
+ ``j < rows``, in place, by chunks of the columns of the basis."""
346
+ cols = p.shape[1]
347
+ acc = np.empty((cols, _CHUNK))
348
+ for start in range(lo, hi, _CHUNK):
349
+ width = min(_CHUNK, hi - start)
350
+ acc[:, :width] = 0.0
351
+ for j in range(rows):
352
+ for i in range(cols):
353
+ c = p[j, i]
354
+ for t in range(width):
355
+ acc[i, t] += c * basis[j, start + t]
356
+ for i in range(cols):
357
+ for t in range(width):
358
+ basis[i, start + t] = acc[i, t]
359
+
360
+
361
+ @njit(parallel=True, cache=True)
362
+ def _parallel_combine(basis, rows, p, n_blocks):
363
+ n = basis.shape[1]
364
+ for b in prange(n_blocks):
365
+ _block_combine(basis, rows, p, b * n // n_blocks, (b + 1) * n // n_blocks)
366
+
367
+
368
+ def _combine(basis, rows: int, p) -> None:
369
+ """``basis[:c] = p^T basis[:rows]`` in place (``p``: rows x c): the vectors that a
370
+ deflated restart of :func:`gmres` keeps, each sum in the order of the basis whatever
371
+ the threads."""
372
+ n = basis.shape[1]
373
+ p = np.ascontiguousarray(p, dtype=np.float64)
374
+ if n >= _PARALLEL:
375
+ _parallel_combine(basis, rows, p, min(max(n // _BLOCK, 1), _MAX_BLOCKS))
376
+ else:
377
+ _block_combine(basis, rows, p, 0, n)
378
+
379
+
380
+ def _deflation(h, c, y, m: int, k: int):
381
+ """The start of the cycle that follows a full cycle of :func:`gmres` (``m`` iterations)
382
+ in deflated restarting (Morgan 2002): ``h`` is the Arnoldi matrix of the cycle ((m + 1)
383
+ x m, in its basis), ``c`` the coordinates of its first residual, ``y`` the solution of
384
+ its least squares problem. The harmonic Ritz vectors of the ``k`` harmonic Ritz values
385
+ of least modulus (the eigenvectors of ``H + h_{m+1,m}^2 H^-T e_m e_m^T``, ``H`` the square
386
+ part of ``h``; a complex pair kept whole by the real and imaginary parts of its vectors)
387
+ and the residual of the least squares problem, ``z = c - h y``, span the next start:
388
+ their harmonic residuals are all parallel to ``z``, so that the Arnoldi relation holds
389
+ on them. Returns their orthonormal basis ``P`` ((m + 1) x (k' + 1): the harmonic Ritz
390
+ vectors, then ``z``), the Arnoldi matrix on it, ``P^T h P[:m, :k']``, a full block, and
391
+ the coordinates ``P^T z`` of the residual; None when ``H`` is singular or the vectors
392
+ are not finite or not independent of ``z`` (the cycle then restarts from the residual
393
+ alone)."""
394
+ z = c - h @ y[:m]
395
+ hm = h[:m, :m]
396
+ em = np.zeros(m)
397
+ em[-1] = 1.0
398
+ try:
399
+ a = hm.copy()
400
+ a[:, -1] += h[m, m - 1] ** 2 * np.linalg.solve(hm.T, em)
401
+ theta, g = np.linalg.eig(a)
402
+ except np.linalg.LinAlgError:
403
+ return None
404
+ if not (np.all(np.isfinite(theta)) and np.all(np.isfinite(g))):
405
+ return None
406
+ vectors = []
407
+ for i in np.argsort(np.abs(theta), kind="stable"):
408
+ if len(vectors) >= k:
409
+ break
410
+ if theta[i].imag > 0.0: # a pair: its conjugate gives the same two real vectors
411
+ vectors += [g[:, i].real, g[:, i].imag]
412
+ elif theta[i].imag == 0.0:
413
+ vectors.append(g[:, i].real)
414
+ q, tri = np.linalg.qr(np.column_stack(vectors))
415
+ diag = np.abs(np.diag(tri))
416
+ q = q[:, diag > 1e-12 * diag.max()]
417
+ kept = q.shape[1]
418
+ p = np.zeros((m + 1, kept + 1))
419
+ p[:m, :kept] = q
420
+ perp = z.copy()
421
+ for _ in range(2):
422
+ perp -= p[:, :kept] @ (p[:, :kept].T @ perp)
423
+ size = float(np.linalg.norm(perp))
424
+ if not size > 1e-12 * float(np.linalg.norm(z)):
425
+ return None
426
+ p[:, kept] = perp / size
427
+ return p, p.T @ h @ p[:m, :kept], p.T @ z
428
+
429
+
430
+ @without_blas_threads
431
+ def gmres(apply, precondition, rhs, tol, maxiter, restart: int = GMRES_RESTART,
432
+ deflate: int = GMRES_DEFLATE, recycle: Recycling | None = None,
433
+ stagnation: int = 0): # fmt: skip
434
+ """GMRES (Saad & Schultz 1986) from ``x = 0``, preconditioned on the right, with
435
+ deflated restarts (Morgan 2002): the solver of the tangent operators that are not
436
+ symmetric (the full consistent tangents of the points), with the preconditioner of
437
+ :func:`conjugate_gradient` (``apply(v)`` and ``precondition(r)`` return new arrays).
438
+
439
+ Each cycle builds an orthonormal basis of the Krylov space of ``apply(precondition(.))``
440
+ by classical Gram-Schmidt (each pass sweeps the basis once for all its dot products,
441
+ summed by blocks that do not depend on the threads), passed again where it cancels
442
+ most of the vector (twice is enough: orthogonal to round-off), and minimizes the
443
+ residual over it by the Givens rotations of its Hessenberg matrix. Preconditioned on
444
+ the right, the residual minimized is that of the system, ``rhs - apply(x)``: the cycle
445
+ stops when it falls below ``tol`` times the norm of ``rhs``, the test of the conjugate
446
+ gradients. After ``restart`` iterations (the vectors kept) ``x`` is updated, and the
447
+ next cycle starts from the harmonic Ritz vectors of the ``deflate`` harmonic Ritz values
448
+ of least modulus and from the residual (:func:`_deflation`; their combinations replace
449
+ the first vectors of the basis): the approximate eigenvectors of the eigenvalues
450
+ nearest zero, which a plain restart loses and the next cycle must find again (restarted
451
+ GMRES stalls on them: the indefinite tangents of softening points), stay in the space,
452
+ and the cycle minimizes the residual over them and the Krylov vectors of the residual.
453
+ The Hessenberg matrix of such a cycle has a full first block, factorized by Householder
454
+ reflections, its next columns by rotations. A cycle that does not decrease the residual
455
+ ends the solve; after deflated restarts the residual that reaches the tolerance is
456
+ computed again (a product) and, if it is above it, the next cycle starts from it alone,
457
+ as with ``deflate=0`` (the plain restarted GMRES: each cycle from the residual computed
458
+ again) or a singular Hessenberg matrix. A solve that does not restart is that of the
459
+ plain GMRES. Returns (x, iterations, relative residual); each iteration takes a product
460
+ by the operator and one by the preconditioner, each cycle one more by the
461
+ preconditioner, each residual computed again one more by the operator. Each iteration
462
+ is a checkpoint of :mod:`morphogen.stopping`.
463
+
464
+ ``recycle`` (a :class:`Recycling` shared by a sequence of solves): a solve that
465
+ restarts leaves it the vectors of its last deflated restart, and the next solves of the
466
+ sequence start from them by GCRO-DR (:func:`_gcro_dr`); the solves before the first
467
+ restart are those without it, to the bit. ``stagnation``: a solve whose residual has
468
+ not been halved over its last ``stagnation`` cycles stops there (0: never), an inexact
469
+ correction of Newton's method that more cycles would improve only slowly.
470
+ """
471
+ x = np.zeros_like(rhs)
472
+ norm_b = norm(rhs)
473
+ if norm_b == 0.0:
474
+ return x, 0, 0.0
475
+ m = max(1, min(int(restart), int(maxiter)))
476
+ k_max = min(int(deflate), m - 2)
477
+ if recycle is not None and recycle.u is not None and k_max > 0:
478
+ out = _gcro_dr(_HOST, apply, precondition, rhs, tol, maxiter, m, k_max, recycle,
479
+ stagnation) # fmt: skip
480
+ if out is not None:
481
+ return out
482
+ ends = [1.0] # the relative residuals at the ends of the cycles
483
+ basis = np.empty((m + 1, rhs.size))
484
+ h = np.zeros((m + 1, m)) # the Arnoldi matrix of the cycle
485
+ tri = np.zeros((m + 1, m)) # its triangular factor
486
+ cs, sn, g, coef = np.zeros(m), np.zeros(m), np.zeros(m + 1), np.zeros(m + 1)
487
+ c = np.zeros(m + 1) # the coordinates of the residual at the start of the cycle
488
+ q0 = np.ones((1, 1)) # the orthogonal factor of the block of the kept vectors
489
+ beta, total, res = norm_b, 0, 1.0
490
+ s, deflated = 0, False # the kept vectors (and the residual after them)
491
+ np.divide(rhs, beta, out=basis[0])
492
+ c[0] = beta
493
+ while total < maxiter:
494
+ g[:] = 0.0
495
+ g[: s + 1] = q0.T @ c[: s + 1]
496
+ k = s
497
+ for j in range(s, min(m, s + maxiter - total)):
498
+ checkpoint()
499
+ w = apply(precondition(basis[j]))
500
+ h[:, j] = 0.0
501
+ before = norm(w)
502
+ for _ in range(2):
503
+ _dots(basis, j + 1, w, coef)
504
+ _subtract(basis, j + 1, coef, w)
505
+ h[: j + 1, j] += coef[: j + 1]
506
+ after = norm(w)
507
+ if after > _REORTHOGONALIZE * before: # orthogonal enough
508
+ break
509
+ before = after
510
+ h[j + 1, j] = after
511
+ col = tri[:, j]
512
+ col[:] = h[:, j]
513
+ if s:
514
+ col[: s + 1] = q0.T @ col[: s + 1]
515
+ for i in range(s, j): # the rotations of the previous columns
516
+ a, b = col[i], col[i + 1]
517
+ col[i] = cs[i] * a + sn[i] * b
518
+ col[i + 1] = cs[i] * b - sn[i] * a
519
+ gamma = math.hypot(col[j], after)
520
+ if not gamma > 0.0: # the operator vanishes on the space (or is not finite)
521
+ break
522
+ cs[j], sn[j] = col[j] / gamma, after / gamma
523
+ col[j], col[j + 1] = gamma, 0.0
524
+ g[j + 1] = -sn[j] * g[j]
525
+ g[j] *= cs[j]
526
+ k = j + 1
527
+ total += 1
528
+ res = abs(g[k]) / norm_b
529
+ if res <= tol or after == 0.0:
530
+ break
531
+ np.divide(w, after, out=basis[j + 1])
532
+ if k == 0:
533
+ break
534
+ y = np.zeros(m + 1)
535
+ for i in range(k - 1, -1, -1): # the triangular system of the rotated Hessenberg
536
+ y[i] = (g[i] - tri[i, i + 1 : k] @ y[i + 1 : k]) / tri[i, i]
537
+ combined = np.zeros_like(rhs)
538
+ _subtract(basis, k, -y, combined)
539
+ x += precondition(combined)
540
+ if total >= maxiter or (res <= tol and not deflated):
541
+ break
542
+ previous = beta
543
+ full = res > tol and k == m and h[m, m - 1] > 0.0
544
+ if full and stagnation:
545
+ ends.append(res)
546
+ if _stagnates(ends, stagnation):
547
+ break
548
+ start = _deflation(h, c, y, m, k_max) if full and k_max > 0 else None
549
+ if start is not None:
550
+ p, hk, ck = start
551
+ beta = res * norm_b
552
+ if not beta < previous:
553
+ break
554
+ s = p.shape[1] - 1
555
+ _combine(basis, m + 1, p)
556
+ h[:] = 0.0
557
+ h[: s + 1, :s] = hk
558
+ c[:] = 0.0
559
+ c[: s + 1] = ck
560
+ q0, r0 = np.linalg.qr(hk, mode="complete")
561
+ tri[:] = 0.0
562
+ tri[: s + 1, :s] = r0
563
+ deflated = True
564
+ continue
565
+ r = rhs - apply(x)
566
+ beta = norm(r)
567
+ res = beta / norm_b
568
+ if res <= tol or not beta < previous:
569
+ break
570
+ if deflated and recycle is not None: # before the plain cycle overwrites them
571
+ _keep(_HOST, recycle, basis, h[: s + 1, :s])
572
+ np.divide(r, beta, out=basis[0])
573
+ c[:] = 0.0
574
+ c[0] = beta
575
+ s, q0, deflated = 0, np.ones((1, 1)), False
576
+ if deflated and recycle is not None:
577
+ _keep(_HOST, recycle, basis, h[: s + 1, :s])
578
+ return x, total, res
579
+
580
+
581
+ def _stagnates(ends, cycles: int) -> bool:
582
+ """Whether the relative residuals ``ends`` at the ends of the cycles of a solve (1 at
583
+ its start) have not been halved over its last ``cycles`` cycles."""
584
+ return len(ends) > cycles and ends[-1] > 0.5 * ends[-1 - cycles]
585
+
586
+
587
+ class Recycling:
588
+ """The space that :func:`gmres` recycles from a solve to the next of a sequence of
589
+ systems whose operators change little, those of the Newton iterations of a loading
590
+ path: GCRO-DR (Parks, de Sturler, Mackey, Johnson & Maiti 2006), the recycled vectors
591
+ approximate eigenvectors of the eigenvalues nearest zero of the preconditioned
592
+ operator, which restarted GMRES must otherwise find again in each solve. Empty until a
593
+ solve of the sequence restarts, so that the solves before are those of GMRES with
594
+ deflated restarts to the bit and cost nothing more: that solve leaves it the harmonic
595
+ Ritz vectors of its last deflated restart, and each later solve minimizes its residual
596
+ over them and the Krylov vectors of the operator projected out of their images, and
597
+ keeps the harmonic Ritz vectors of that space at each of its own restarts. The solves
598
+ of the same operator reuse the images of the recycled vectors; a new operator
599
+ (:meth:`renew`: the tangent of the next Newton iteration) has them computed again by its
600
+ first solve, ``k`` products (:attr:`products`), if the last solve filled a cycle
601
+ (:attr:`needed`), and drops the space otherwise. The vectors live where those of the
602
+ solves do (on the GPU for :func:`morphogen.homogenization.gpu.gmres`)."""
603
+
604
+ def __init__(self):
605
+ #: The recycled vectors (rows), in the variable of the preconditioned operator (the
606
+ #: corrections are their images by the preconditioner), or None
607
+ self.u = None
608
+ #: Their images by the preconditioned operator, orthonormal
609
+ self.c = None
610
+ #: Whether :attr:`c` are the images by the operator of the next solve
611
+ self.current = False
612
+ #: Whether the last solve filled a cycle (it restarted, or would have without the
613
+ #: recycled vectors): otherwise a new operator drops them (:meth:`renew`)
614
+ self.needed = False
615
+ #: The products by the operators spent on the images of the recycled vectors, and
616
+ #: the solves started from them
617
+ self.products = 0
618
+ self.solves = 0
619
+
620
+ def renew(self) -> None:
621
+ """The next solves are those of another operator: the recycled vectors are kept if
622
+ the last solve filled a cycle (:attr:`needed`), and their images will be computed
623
+ again."""
624
+ if not self.needed:
625
+ self.clear()
626
+ self.current = False
627
+
628
+ def clear(self) -> None:
629
+ """Drop the recycled space: the next solves are those of :func:`gmres` without it."""
630
+ self.u = self.c = None
631
+ self.current = False
632
+
633
+
634
+ class _Host:
635
+ """The operations on the vectors of :func:`_gcro_dr` and :func:`_keep` on the host: the
636
+ kernels of :func:`gmres`, whose sums do not depend on the threads."""
637
+
638
+ norm = staticmethod(norm)
639
+ zeros_like = staticmethod(np.zeros_like)
640
+
641
+ @staticmethod
642
+ def empty(rows: int, n: int) -> np.ndarray:
643
+ return np.empty((rows, n))
644
+
645
+ @staticmethod
646
+ def dots(basis, k: int, w) -> np.ndarray:
647
+ out = np.empty(k)
648
+ _dots(basis, k, w, out)
649
+ return out
650
+
651
+ @staticmethod
652
+ def subtract(basis, k: int, coef, w) -> None:
653
+ _subtract(basis, k, np.ascontiguousarray(coef, dtype=np.float64), w)
654
+
655
+ @staticmethod
656
+ def combine(basis, rows: int, p) -> None:
657
+ _combine(basis, rows, p)
658
+
659
+
660
+ _HOST = _Host()
661
+
662
+
663
+ def _keep(ops, recycle: Recycling, basis, hk) -> None:
664
+ """The space that a solve of :func:`gmres` which restarted leaves to ``recycle``: the
665
+ ``s`` vectors ``W`` of its last deflated start (``basis[:s]``), whose images by the
666
+ preconditioned operator are ``basis[:s + 1] hk`` (the Arnoldi relation of the deflated
667
+ restart): with ``hk = Q R``, the vectors ``W R^-1`` and their images ``basis[:s + 1]
668
+ Q``, orthonormal."""
669
+ s = hk.shape[1]
670
+ if s == 0:
671
+ return
672
+ q, r = np.linalg.qr(hk)
673
+ diag = np.abs(np.diag(r))
674
+ if not diag.min() > 1e-12 * diag.max():
675
+ return
676
+ p = np.zeros((s + 1, 2 * s))
677
+ p[:s, :s] = sla.solve_triangular(r, np.eye(s))
678
+ p[:, s:] = q
679
+ work = ops.empty(2 * s, basis.shape[1])
680
+ work[: s + 1] = basis[: s + 1]
681
+ ops.combine(work, s + 1, p)
682
+ recycle.u, recycle.c, recycle.current, recycle.needed = work[:s], work[s:], True, True
683
+
684
+
685
+ def _orthonormalize(ops, rows, k: int):
686
+ """Gram-Schmidt of the ``k`` vectors ``rows`` in place, each pass done again where it
687
+ cancels most of the vector: their triangular factor (``rows = Q R``), or None when they
688
+ are not independent."""
689
+ r = np.zeros((k, k))
690
+ largest = 0.0
691
+ for i in range(k):
692
+ w = rows[i]
693
+ before = ops.norm(w)
694
+ largest = max(largest, before)
695
+ after = before
696
+ for _ in range(2 if i else 0):
697
+ coef = ops.dots(rows, i, w)
698
+ ops.subtract(rows, i, coef, w)
699
+ r[:i, i] += coef
700
+ after = ops.norm(w)
701
+ if after > _REORTHOGONALIZE * before:
702
+ break
703
+ before = after
704
+ if not after > 1e-10 * largest:
705
+ return None
706
+ r[i, i] = after
707
+ w /= after
708
+ return r
709
+
710
+
711
+ def _recycled_space(h, e, m: int, k: int, keep: int):
712
+ """The space that a full cycle of :func:`_gcro_dr` recycles: the harmonic Ritz vectors
713
+ of its ``keep`` harmonic Ritz values of least modulus with respect to its images (a
714
+ complex pair kept whole), the solutions ``z`` of ``G^T G z = theta G^T W^T V z``
715
+ (Parks et al. 2006), ``G = h`` the matrix of the cycle from its solution space ``V``
716
+ (the ``k`` recycled vectors, scaled to unit norms, and the ``m - k`` Krylov vectors) to
717
+ its image space ``W`` (the images of the recycled vectors and the ``m - k + 1`` Krylov
718
+ vectors, orthonormal), ``W^T V = [[e[:k], 0], [e[k:], I]]`` (``e = W^T U``). Returns
719
+ ``(Z, Q)``: the new vectors ``V Z`` and their images ``W Q``, orthonormal (``G Z = Q``);
720
+ None when the problem is singular or its vectors are not independent."""
721
+ t = np.zeros((m + 1, m))
722
+ t[:, :k] = e
723
+ rows = np.arange(k, m)
724
+ t[rows, rows] = 1.0
725
+ try:
726
+ theta, z = sla.eig(h.T @ h, h.T @ t)
727
+ except (np.linalg.LinAlgError, ValueError):
728
+ return None
729
+ vectors = []
730
+ for i in np.argsort(np.abs(theta), kind="stable"):
731
+ if len(vectors) >= keep:
732
+ break
733
+ if not (np.isfinite(theta[i]) and np.all(np.isfinite(z[:, i]))):
734
+ continue
735
+ if theta[i].imag > 0.0: # a pair: its conjugate gives the same two real vectors
736
+ vectors += [z[:, i].real, z[:, i].imag]
737
+ elif theta[i].imag == 0.0:
738
+ vectors.append(z[:, i].real)
739
+ if not vectors:
740
+ return None
741
+ p, tri = np.linalg.qr(np.column_stack(vectors))
742
+ diag = np.abs(np.diag(tri))
743
+ p = p[:, diag > 1e-12 * diag.max()]
744
+ q, tri = np.linalg.qr(h @ p)
745
+ diag = np.abs(np.diag(tri))
746
+ if not diag.min() > 1e-12 * diag.max():
747
+ return None
748
+ return p @ sla.solve_triangular(tri, np.eye(tri.shape[0])), q
749
+
750
+
751
+ def _gcro_dr(ops, apply, precondition, rhs, tol, maxiter, m: int, k_max: int,
752
+ recycle: Recycling, stagnation: int): # fmt: skip
753
+ """GCRO-DR (Parks, de Sturler, Mackey, Johnson & Maiti 2006): :func:`gmres` from the
754
+ space of ``recycle`` (``ops``: the operations on the vectors, :class:`_Host` or those of
755
+ the GPU). With ``U`` its ``k`` vectors and ``C`` their orthonormal images by the
756
+ preconditioned operator ``A M^-1`` (computed again for a new operator by ``k`` products
757
+ and Gram-Schmidt, ``U`` turned with them), each cycle projects its residual out of
758
+ ``C``, builds from it the Krylov vectors ``V`` of ``(I - C C^T) A M^-1`` (``m - k`` of
759
+ them, each orthogonalized against ``C`` and the previous ones by the passes of
760
+ :func:`gmres`, which give the coefficients ``B = C^T A M^-1 V``) and minimizes the
761
+ residual over ``[U, V]``, whose images are ``[C, V] G`` with ``G = [[D, B], [0, H]]``
762
+ (``D`` the inverse norms of the ``U``, ``H`` the Hessenberg matrix): the least squares
763
+ problem of :func:`gmres` with a diagonal first block and the same rotations. A full
764
+ cycle keeps the harmonic Ritz vectors of ``[U, V]`` (:func:`_recycled_space`) as the
765
+ next ``U``, combined in place with their images and the residual that starts the next
766
+ cycle (``m + k + 2`` vectors at most). After more than one cycle the residual that reaches the
767
+ tolerance is computed again (a product), and the cycles go on from it while it is
768
+ above. Returns (x, iterations, relative residual), the products of the images counted
769
+ as iterations; None when the images are not independent (the space is dropped)."""
770
+ norm_b = ops.norm(rhs)
771
+ n = rhs.size
772
+ k = recycle.u.shape[0]
773
+ rows = m + 1 + max(k, k_max + 1) # U, C and the Krylov vectors (a pair kept whole)
774
+ work = ops.empty(rows, n)
775
+ work[:k] = recycle.u
776
+ current = recycle.current
777
+ if current:
778
+ work[k : 2 * k] = recycle.c
779
+ recycle.u = recycle.c = None # the space lives in work while the solve runs
780
+ total = 0
781
+ if not current: # the images by the new operator
782
+ for i in range(k):
783
+ checkpoint()
784
+ work[k + i] = apply(precondition(work[i]))
785
+ total = k
786
+ recycle.products += k
787
+ r = _orthonormalize(ops, work[k : 2 * k], k)
788
+ if r is None:
789
+ recycle.clear()
790
+ return None
791
+ ops.combine(work, k, sla.solve_triangular(r, np.eye(k)))
792
+ recycle.solves += 1
793
+ x = ops.zeros_like(rhs)
794
+ coords = np.zeros(m + 1) # the residual at the start of the cycle, on [C, V]
795
+ start, ends, res, cycles = rhs, [1.0], 1.0, 0
796
+ while True:
797
+ basis = work[k:] # C, then the Krylov vectors
798
+ if start is not None: # the residual projected out of C
799
+ basis[k] = start
800
+ coords[:] = 0.0
801
+ before = ops.norm(basis[k])
802
+ for _ in range(2):
803
+ coef = ops.dots(basis, k, basis[k])
804
+ ops.subtract(basis, k, coef, basis[k])
805
+ coords[:k] += coef
806
+ after = ops.norm(basis[k])
807
+ if after > _REORTHOGONALIZE * before:
808
+ break
809
+ before = after
810
+ coords[k] = after
811
+ if after > 0.0:
812
+ basis[k] /= after
813
+ start = None
814
+ d = np.array([ops.norm(work[i]) for i in range(k)])
815
+ h = np.zeros((m + 1, m))
816
+ h[np.arange(k), np.arange(k)] = 1.0 / d
817
+ tri = h.copy()
818
+ g = coords.copy()
819
+ cs, sn = np.zeros(m), np.zeros(m)
820
+ kend = k
821
+ res = abs(g[k]) / norm_b
822
+ for j in range(k, min(m, k + maxiter - total) if res > tol else k):
823
+ checkpoint()
824
+ w = apply(precondition(basis[j]))
825
+ before = ops.norm(w)
826
+ for _ in range(2):
827
+ coef = ops.dots(basis, j + 1, w)
828
+ ops.subtract(basis, j + 1, coef, w)
829
+ h[: j + 1, j] += coef
830
+ after = ops.norm(w)
831
+ if after > _REORTHOGONALIZE * before:
832
+ break
833
+ before = after
834
+ h[j + 1, j] = after
835
+ col = tri[:, j]
836
+ col[:] = h[:, j]
837
+ for i in range(k, j):
838
+ a, b = col[i], col[i + 1]
839
+ col[i] = cs[i] * a + sn[i] * b
840
+ col[i + 1] = cs[i] * b - sn[i] * a
841
+ gamma = math.hypot(col[j], after)
842
+ if not gamma > 0.0:
843
+ break
844
+ cs[j], sn[j] = col[j] / gamma, after / gamma
845
+ col[j], col[j + 1] = gamma, 0.0
846
+ g[j + 1] = -sn[j] * g[j]
847
+ g[j] *= cs[j]
848
+ kend = j + 1
849
+ total += 1
850
+ res = abs(g[kend]) / norm_b
851
+ if res <= tol or after == 0.0:
852
+ break
853
+ basis[j + 1] = w / after
854
+ cycles += 1
855
+ y = np.zeros(m)
856
+ for i in range(kend - 1, -1, -1):
857
+ y[i] = (g[i] - tri[i, i + 1 : kend] @ y[i + 1 : kend]) / tri[i, i]
858
+ combined = ops.zeros_like(rhs)
859
+ ops.subtract(work, k, -y[:k] / d, combined)
860
+ if kend > k:
861
+ ops.subtract(basis[k:], kend - k, -y[k:kend], combined)
862
+ x += precondition(combined)
863
+ if res <= tol or total >= maxiter or kend < m:
864
+ if res <= tol and cycles > 1:
865
+ r = rhs - apply(x)
866
+ true = ops.norm(r) / norm_b
867
+ if true > tol and true < ends[-1] and total < maxiter:
868
+ ends.append(true)
869
+ start = r
870
+ continue
871
+ res = true
872
+ break
873
+ ends.append(res)
874
+ if not ends[-1] < ends[-2] or (stagnation and _stagnates(ends, stagnation)):
875
+ break
876
+ e = np.empty((m + 1, k)) # W^T U, scaled as the first columns of h
877
+ for i in range(k):
878
+ e[:, i] = ops.dots(basis, m + 1, work[i]) / d[i]
879
+ z = coords - h @ y # the residual of the cycle on [C, V]
880
+ new = _recycled_space(h, e, m, k, k_max)
881
+ kk = k if new is None else new[1].shape[1]
882
+ p = np.zeros((k + m + 1, 2 * kk + 1))
883
+ if new is None: # U and C kept
884
+ p[np.arange(k), np.arange(k)] = 1.0
885
+ q = np.eye(m + 1, k)
886
+ else:
887
+ zs, q = new
888
+ p[:k, :kk] = zs[:k] / d[:, None]
889
+ p[2 * k : k + m, :kk] = zs[k:]
890
+ cq = q.T @ z
891
+ perp = z - q @ cq
892
+ perp -= q @ (q.T @ perp)
893
+ size = float(np.linalg.norm(perp))
894
+ p[k:, kk : 2 * kk] = q
895
+ if size > 0.0:
896
+ p[k:, 2 * kk] = perp / size
897
+ ops.combine(work, k + m + 1, p)
898
+ k = kk
899
+ coords[:] = 0.0
900
+ coords[:k] = cq
901
+ coords[k] = size
902
+ recycle.u, recycle.c, recycle.current = work[:k].copy(), work[k : 2 * k].copy(), True
903
+ recycle.needed = cycles > 1 or kend == m
904
+ return x, total, res
905
+
906
+
907
+ class TwoLevelPreconditioner:
908
+ """Symmetric two-level preconditioner of ``A`` with a coarse space ``P``.
909
+
910
+ A forward Gauss-Seidel sweep on ``A``, a correction by one AMG V-cycle on the
911
+ Galerkin matrix ``P^T A P``, then a backward sweep (so the preconditioner is
912
+ symmetric). With ``P`` the interpolation of the linear (4-node) elements into the
913
+ quadratic (10-node) ones, the coarse matrix is the stiffness of the linear
914
+ elements, on which smoothed aggregation is efficient. ``dofs``, ``coarse_dofs``:
915
+ the degree of freedom ``3 node + component`` of each unknown of the matrix and of
916
+ the coarse space, where they are components of the nodes of a mesh (the sweeps then
917
+ work by nodes, see :class:`~morphogen.homogenization.amg.Smoother`); ``blocks``:
918
+ the blocks of the matrix, where they have been made already
919
+ (:class:`~morphogen.homogenization.amg.NodeBlocks`).
920
+ """
921
+
922
+ def __init__(self, matrix, prolongation, near_null=None, dofs=None, coarse_dofs=None,
923
+ blocks=None): # fmt: skip
924
+ self.matrix = sp.csr_matrix(matrix)
925
+ self.matrix.sort_indices()
926
+ self.p = sp.csr_matrix(prolongation)
927
+ self.pt = self.p.T.tocsr()
928
+ coarse = (self.pt @ self.matrix @ self.p).tocsr()
929
+ self.amg = SmoothedAggregation(coarse, _candidates(near_null), dofs=coarse_dofs)
930
+ self.smoother = Smoother(self.matrix, dofs, blocks)
931
+ if self.smoother.coloured:
932
+ # the cycle in the order of the colours of the smoother: the interpolation
933
+ # with its rows at their places in that order
934
+ rows = self.p.tocoo()
935
+ shape = (self.smoother.size, self.p.shape[1])
936
+ places = (self.smoother.place[rows.row], rows.col)
937
+ self.p = sp.csr_matrix((rows.data, places), shape=shape)
938
+ self.pt = self.p.T.tocsr()
939
+
940
+ def refresh(self, blocks: NodeBlocks) -> bool:
941
+ """The sweeps for the values that the blocks of the matrix have now (see
942
+ :meth:`~morphogen.homogenization.amg.Smoother.refresh`); the coarse space and its
943
+ multigrid cycle are kept. The preconditioner stays symmetric positive definite:
944
+ the coarse correction adds a positive term to the symmetric sweeps of the matrix
945
+ as it is."""
946
+ return self.smoother.refresh(blocks)
947
+
948
+ def __call__(self, r: np.ndarray) -> np.ndarray:
949
+ smoother = self.smoother
950
+ if smoother.coloured:
951
+ b = smoother.sorted(np.ascontiguousarray(r, dtype=float))
952
+ x = smoother.start(b)
953
+ residual = smoother.residual(x)
954
+ x += product(self.p, self.amg(product(self.pt, residual)))
955
+ smoother.relax(x, b, False)
956
+ return smoother.unsorted(x)
957
+ x = np.zeros_like(r)
958
+ smoother.sweep(x, r, True)
959
+ residual = r - product(self.matrix, x)
960
+ x += product(self.p, self.amg(product(self.pt, residual)))
961
+ smoother.sweep(x, r, False)
962
+ return x
963
+
964
+
965
+ def _candidates(near_null):
966
+ if near_null is None:
967
+ return None
968
+ b = np.asarray(near_null, dtype=float)
969
+ return b[:, np.linalg.norm(b, axis=0) > 0]
970
+
971
+
972
+ class LinearSolver:
973
+ """Solver of A x = b for a fixed matrix A and several right-hand sides.
974
+
975
+ ``near_null``: low-energy modes (columns) of A, used by algebraic multigrid.
976
+ ``coarse``: ``(P, near_null_coarse)``, a coarse space for the two-level ``amg``
977
+ preconditioner (see :class:`TwoLevelPreconditioner`). ``symmetric``: false for a
978
+ matrix that is not symmetric (the tangent of nonsymmetric consistent tangents), which
979
+ the iterative methods solve by :func:`gmres` with the preconditioner of its symmetric
980
+ part (LU factorizes it as it is). ``dofs``: the degree of freedom ``3 node +
981
+ component`` of each unknown, where the unknowns are components of the displacements
982
+ of the nodes of a mesh (the sweeps of the multigrid preconditioners then work by
983
+ nodes, see :class:`~morphogen.homogenization.amg.Smoother`; ``coarse`` may carry
984
+ those of its coarse space as a third item). The products of the iterative methods
985
+ then read the matrix by the 3 x 3 blocks of its nodes (:attr:`blocks`:
986
+ :class:`~morphogen.homogenization.amg.NodeBlocks`, the same products to the bit).
987
+ """
988
+
989
+ def __init__(
990
+ self,
991
+ matrix,
992
+ *,
993
+ method="auto",
994
+ tol=1e-8,
995
+ maxiter=None,
996
+ near_null=None,
997
+ coarse=None,
998
+ symmetric: bool = True,
999
+ dofs=None,
1000
+ ):
1001
+ self.matrix = sp.csr_matrix(matrix)
1002
+ n = self.matrix.shape[0]
1003
+ method = choose_method(method, n)
1004
+ self.method = method
1005
+ self.tol = float(tol)
1006
+ self.maxiter = maxiter
1007
+ self.symmetric = bool(symmetric)
1008
+ t0 = time.perf_counter()
1009
+ self._precondition = None
1010
+ self._dofs = dofs
1011
+ #: The matrix by blocks of nodes, for the products of the iterative methods
1012
+ self.blocks = None
1013
+ #: The iterations per decade of the residual of the solves since the last step
1014
+ #: of a path, and those of the step for which the preconditioner was built
1015
+ self._wear = {"rates": [], "reference": None}
1016
+ if n and method != "direct":
1017
+ # (rows in the order of their columns: the order in which the blocks sum them)
1018
+ self.matrix.sort_indices()
1019
+ self.blocks = NodeBlocks.of(self.matrix, dofs)
1020
+ # the preconditioners are those of the symmetric part (which has its own blocks)
1021
+ part = self.matrix if self.symmetric else (0.5 * (self.matrix + self.matrix.T)).tocsr()
1022
+ shared = self.blocks if self.symmetric else None
1023
+ if n == 0:
1024
+ pass
1025
+ elif method == "direct":
1026
+ self._lu = spla.splu(
1027
+ self.matrix.tocsc(), permc_spec="MMD_AT_PLUS_A", options={"SymmetricMode": True}
1028
+ )
1029
+ elif method == "amg":
1030
+ if coarse is not None:
1031
+ p, coarse_null = coarse[:2]
1032
+ coarse_dofs = coarse[2] if len(coarse) > 2 else None
1033
+ self._precondition = TwoLevelPreconditioner(part, p, coarse_null, dofs,
1034
+ coarse_dofs, shared) # fmt: skip
1035
+ self.method = "amg-2level"
1036
+ else:
1037
+ self._precondition = SmoothedAggregation(part, _candidates(near_null), dofs=dofs,
1038
+ blocks=shared) # fmt: skip
1039
+ else:
1040
+ d = self.matrix.diagonal()
1041
+ inv = 1.0 / np.where(d > 0, d, 1.0)
1042
+ self._precondition = lambda r: inv * r
1043
+ self.setup_s = time.perf_counter() - t0
1044
+
1045
+ def updated(self, matrix, symmetric: bool | None = None) -> LinearSolver:
1046
+ """Solver of another matrix of the same size (a new tangent operator): direct
1047
+ methods factorize it, iterative ones keep this preconditioner (``symmetric``: whether
1048
+ the matrix is, by default as this one). This solver is not to be used afterwards:
1049
+ the blocks of its matrix take the values of the new one, and with them the sweeps
1050
+ of a multigrid preconditioner that work by nodes (for a symmetric matrix of the
1051
+ same pattern) — the interpolations and the coarse levels are kept, and the
1052
+ preconditioner stays symmetric positive definite."""
1053
+ if self.method == "direct" or self._precondition is None:
1054
+ return LinearSolver(matrix, method="direct", tol=self.tol, maxiter=self.maxiter)
1055
+ out = object.__new__(LinearSolver)
1056
+ out.__dict__.update(self.__dict__)
1057
+ out.matrix = sp.csr_matrix(matrix)
1058
+ out.setup_s = 0.0
1059
+ if symmetric is not None:
1060
+ out.symmetric = bool(symmetric)
1061
+ if self.blocks is not None:
1062
+ if not self.blocks.refresh(out.matrix): # (other stored values: other blocks)
1063
+ out.blocks = NodeBlocks.of(out.matrix, self._dofs)
1064
+ elif out.symmetric and hasattr(self._precondition, "refresh"):
1065
+ self._precondition.refresh(self.blocks)
1066
+ return out
1067
+
1068
+ def worn(self) -> bool:
1069
+ """Whether the preconditioner is to be built again, asked at the start of a step
1070
+ of a loading path, before the solver is :meth:`updated`: true for a factorization
1071
+ or a diagonal (built with the solver); for a multigrid preconditioner, when the
1072
+ solves of the last step took :data:`WORN` times the iterations per decade of
1073
+ their residual that those of the step for which it was built took (the tangent
1074
+ has moved away from the one whose coarse levels it holds)."""
1075
+ if not isinstance(self._precondition, TwoLevelPreconditioner | SmoothedAggregation):
1076
+ return True
1077
+ wear = self._wear
1078
+ rates, wear["rates"] = wear["rates"], []
1079
+ if not rates:
1080
+ return False
1081
+ last = sum(rates) / len(rates)
1082
+ if wear["reference"] is None:
1083
+ wear["reference"] = last
1084
+ return False
1085
+ return last > WORN * wear["reference"]
1086
+
1087
+ def _product(self, x: np.ndarray) -> np.ndarray:
1088
+ """``A x``: by the blocks of the nodes where the matrix has them."""
1089
+ if self.blocks is not None and x.ndim == 1:
1090
+ return self.blocks.product(x)
1091
+ return product(self.matrix, x)
1092
+
1093
+ def solve(
1094
+ self, rhs: np.ndarray, x0: np.ndarray | None = None, tol: float | None = None
1095
+ ) -> tuple[np.ndarray, SolveInfo]:
1096
+ """Solution of ``A x = rhs``; iterative methods start from ``x0`` if given and
1097
+ stop at the relative residual ``tol`` (default: that of the solver)."""
1098
+ n = self.matrix.shape[0]
1099
+ b = np.asarray(rhs, dtype=float)
1100
+ t0 = time.perf_counter()
1101
+ norm_b = norm(b.ravel())
1102
+ if n == 0 or norm_b == 0.0:
1103
+ return np.zeros(n), SolveInfo(self.method, n, 0, 0.0, self.setup_s, 0.0)
1104
+ tol = self.tol if tol is None else float(tol)
1105
+ iterations = 0
1106
+ if self.method == "direct":
1107
+ x = self._lu.solve(b)
1108
+ else:
1109
+ default = 50_000 if self.method == "cg" else 2_000
1110
+ start = None if x0 is None or not np.any(x0) else np.asarray(x0, dtype=float)
1111
+ r = b if start is None else b - self._product(start)
1112
+ norm_r = norm(r)
1113
+ if norm_r <= tol * norm_b:
1114
+ x = np.zeros(n) if start is None else start.copy()
1115
+ else:
1116
+ krylov = conjugate_gradient if self.symmetric else gmres
1117
+ x, iterations, _ = krylov(
1118
+ self._product,
1119
+ self._precondition,
1120
+ r,
1121
+ tol * norm_b / norm_r,
1122
+ self.maxiter or default,
1123
+ )
1124
+ if start is not None:
1125
+ x += start
1126
+ residual = norm((b - self._product(x)).ravel()) / norm_b
1127
+ if iterations and start is None: # (the wear of the preconditioner: see worn)
1128
+ decades = max(-math.log10(max(residual, 1e-300)), 0.5)
1129
+ self._wear["rates"].append(iterations / decades)
1130
+ if self.method != "direct" and residual > 10 * tol:
1131
+ log.warning(
1132
+ "%s solver stopped at relative residual %.2e (tolerance %.1e)",
1133
+ self.method,
1134
+ residual,
1135
+ tol,
1136
+ )
1137
+ info = SolveInfo(
1138
+ self.method, n, iterations, residual, self.setup_s, time.perf_counter() - t0
1139
+ )
1140
+ return x, info