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,1005 @@
1
+ """Smoothed-aggregation algebraic multigrid (Vanek, Mandel & Brezina 1996).
2
+
3
+ A preconditioner of symmetric positive definite systems such as the stiffness matrices
4
+ of the finite element problems. The hierarchy follows the classical recipe (also that
5
+ of pyamg's ``smoothed_aggregation_solver``):
6
+
7
+ * aggregates of unknowns from the graph of the matrix: an unknown with its neighbours
8
+ (pass 1), then the remaining unknowns joined to a neighbouring aggregate (pass 2) or
9
+ grouped together (pass 3);
10
+ * tentative prolongator: on each aggregate, an orthonormal basis of the near-null modes
11
+ (rigid-body motions in elasticity, constants in conduction), whose coefficients are
12
+ the near-null modes of the coarse level;
13
+ * prolongator smoothed by one damped Jacobi step, ``P = (I - 4/3 D^-1 A / rho) T``;
14
+ * Galerkin coarse matrices ``P^T A P``, down to a few hundred unknowns, solved by a
15
+ pseudo-inverse.
16
+
17
+ One V-cycle with a forward Gauss-Seidel sweep before the coarse correction and a
18
+ backward sweep after it is a symmetric preconditioner for conjugate gradients.
19
+
20
+ On large matrices the sweeps and the products run on several threads
21
+ (:class:`Smoother`, :func:`product`): the unknowns are coloured so that no two of a
22
+ colour are coupled, and those of a colour are relaxed together — a Gauss-Seidel sweep
23
+ in the order of the colours, the same for any number of threads, on a copy of the
24
+ matrix whose rows are sorted by colour (the rows of a colour scattered through the
25
+ matrix would be read no faster by four threads than all of them in their order by
26
+ one); the rows of a product are shared between the threads, each summed as SciPy sums
27
+ it. Where the unknowns are components of the displacements of the nodes of a mesh,
28
+ the colours are those of the nodes and the copy holds the 3 x 3 blocks of their
29
+ couplings: one column index for nine values, and the three components of a node read
30
+ together.
31
+ """
32
+
33
+ from __future__ import annotations
34
+
35
+ import numpy as np
36
+ import scipy.linalg as sla
37
+ import scipy.sparse as sp
38
+ import scipy.sparse.linalg as spla
39
+ from numba import njit, prange
40
+
41
+ #: Matrices with fewer stored values than this are multiplied on one thread: starting
42
+ #: the threads of the compiled loop would cost more than they bring
43
+ THREADED = 50_000
44
+ #: Matrices with fewer stored values than this are relaxed in the order of their
45
+ #: unknowns on one thread (a sweep in the order of the colours starts the threads once
46
+ #: per colour)
47
+ COLOURED = 200_000
48
+
49
+
50
+ @njit(cache=True)
51
+ def gauss_seidel(indptr, indices, data, x, b, forward):
52
+ """One Gauss-Seidel sweep on ``A x = b`` (CSR), in place, forward or backward."""
53
+ n = b.size
54
+ for step in range(n):
55
+ i = step if forward else n - 1 - step
56
+ diag = 0.0
57
+ s = b[i]
58
+ for k in range(indptr[i], indptr[i + 1]):
59
+ j = indices[k]
60
+ if j == i:
61
+ diag += data[k]
62
+ else:
63
+ s -= data[k] * x[j]
64
+ if diag != 0.0:
65
+ x[i] = s / diag
66
+
67
+
68
+ @njit(parallel=True, cache=True)
69
+ def _one_sided(indptr, indices):
70
+ """The number of stored values ``(i, j)`` of a CSR pattern with sorted rows whose
71
+ transposed place ``(j, i)`` is not stored: zero for a symmetric pattern."""
72
+ count = 0
73
+ for i in prange(indptr.size - 1):
74
+ for k in range(indptr[i], indptr[i + 1]):
75
+ j = indices[k]
76
+ lo, hi = indptr[j], indptr[j + 1]
77
+ while lo < hi:
78
+ mid = (lo + hi) // 2
79
+ if indices[mid] < i:
80
+ lo = mid + 1
81
+ else:
82
+ hi = mid
83
+ if lo == indptr[j + 1] or indices[lo] != i:
84
+ count += 1
85
+ return count
86
+
87
+
88
+ def _graph(matrix) -> tuple[np.ndarray, np.ndarray]:
89
+ """The CSR pattern of the couplings of a square matrix (CSR, or BSR: the couplings
90
+ of its blocks): its own when it is symmetric, that of ``A + A^T`` otherwise (a
91
+ product of sparse matrices drops the values that are exactly zero, on one side of
92
+ the diagonal only when round-off leaves something on the other; a matrix with rows
93
+ that are not sorted is taken as such a one)."""
94
+ indptr, indices = matrix.indptr, matrix.indices
95
+ if matrix.has_sorted_indices and not _one_sided(indptr, indices):
96
+ return indptr, indices
97
+ n = indptr.size - 1
98
+ ones = sp.csr_matrix((np.ones(indices.size), indices, indptr), shape=(n, n))
99
+ both = (ones + ones.T).tocsr()
100
+ return both.indptr, both.indices
101
+
102
+
103
+ @njit(cache=True)
104
+ def _colours(indptr, indices, n):
105
+ """Greedy colouring of the graph of a matrix whose pattern is symmetric: each unknown
106
+ takes the smallest colour that those coupled with it, among the ones before it, do
107
+ not have. Returns the unknowns sorted by colour (in their order within a colour)
108
+ and the offsets of the colours in that list."""
109
+ colour = np.full(n, -1, dtype=np.int64)
110
+ widest = 0
111
+ for i in range(n):
112
+ widest = max(widest, indptr[i + 1] - indptr[i])
113
+ taken = np.full(widest + 2, -1, dtype=np.int64) # the unknown that last met a colour
114
+ count = 0
115
+ for i in range(n):
116
+ for k in range(indptr[i], indptr[i + 1]):
117
+ c = colour[indices[k]]
118
+ if c >= 0:
119
+ taken[c] = i
120
+ c = 0
121
+ while taken[c] == i:
122
+ c += 1
123
+ colour[i] = c
124
+ count = max(count, c + 1)
125
+ starts = np.zeros(count + 1, dtype=np.int64)
126
+ for i in range(n):
127
+ starts[colour[i] + 1] += 1
128
+ for c in range(count):
129
+ starts[c + 1] += starts[c]
130
+ order = np.empty(n, dtype=np.int64)
131
+ place = starts[:count].copy()
132
+ for i in range(n):
133
+ order[place[colour[i]]] = i
134
+ place[colour[i]] += 1
135
+ return order, starts
136
+
137
+
138
+ @njit(cache=True)
139
+ def _sorted_by_colour(indptr, indices, data, order):
140
+ """A matrix whose unknowns are taken in the order ``order`` (rows and columns: the
141
+ new unknown ``k`` is the old one ``order[k]``), split into its diagonal and the CSR
142
+ arrays of its strictly lower and upper parts (the stored values of each row in the
143
+ order that they had), with 32-bit column indices and values in single precision:
144
+ the copy that the sweeps read. A preconditioner needs no more, and a sweep is bound
145
+ by the memory that it reads: a forward sweep from zero reads the lower part, the
146
+ residual that follows it the upper one."""
147
+ n = order.size
148
+ place = np.empty(n, dtype=np.int32)
149
+ for k in range(n):
150
+ place[order[k]] = k
151
+ lower = 0
152
+ for k in range(n):
153
+ i = order[k]
154
+ for q in range(indptr[i], indptr[i + 1]):
155
+ if place[indices[q]] < k:
156
+ lower += 1
157
+ upper = indices.size - lower # (with the diagonal: an upper bound)
158
+ l_indptr = np.empty(n + 1, dtype=np.int64)
159
+ u_indptr = np.empty(n + 1, dtype=np.int64)
160
+ l_indices = np.empty(lower, dtype=np.int32)
161
+ u_indices = np.empty(upper, dtype=np.int32)
162
+ l_data = np.empty(lower, dtype=np.float32)
163
+ u_data = np.empty(upper, dtype=np.float32)
164
+ diag = np.zeros(n, dtype=np.float32)
165
+ nl = 0
166
+ nu = 0
167
+ for k in range(n):
168
+ i = order[k]
169
+ l_indptr[k] = nl
170
+ u_indptr[k] = nu
171
+ for q in range(indptr[i], indptr[i + 1]):
172
+ j = place[indices[q]]
173
+ if j < k:
174
+ l_indices[nl] = j
175
+ l_data[nl] = data[q]
176
+ nl += 1
177
+ elif j > k:
178
+ u_indices[nu] = j
179
+ u_data[nu] = data[q]
180
+ nu += 1
181
+ else:
182
+ diag[k] += data[q]
183
+ l_indptr[n] = nl
184
+ u_indptr[n] = nu
185
+ return l_indptr, l_indices, l_data, u_indptr, u_indices[:nu], u_data[:nu], diag
186
+
187
+
188
+ @njit(cache=True)
189
+ def _sorted_blocks(indptr, indices, data, order):
190
+ """:func:`_sorted_by_colour` for the 3 x 3 blocks of a matrix (block CSR: ``data``
191
+ holds the nine values of each stored block, row by row; ``order``: the nodes sorted
192
+ by colour): the blocks below and above the diagonal, the diagonal blocks, and the
193
+ place of each stored block among them (those below, then those above, then the
194
+ diagonal ones: :func:`_refresh_blocks` writes other values there)."""
195
+ n = order.size
196
+ place = np.empty(n, dtype=np.int32)
197
+ for k in range(n):
198
+ place[order[k]] = k
199
+ lower = 0
200
+ for k in range(n):
201
+ i = order[k]
202
+ for q in range(indptr[i], indptr[i + 1]):
203
+ if place[indices[q]] < k:
204
+ lower += 1
205
+ upper = indices.size - lower # (with the diagonal: an upper bound)
206
+ l_indptr = np.empty(n + 1, dtype=np.int64)
207
+ u_indptr = np.empty(n + 1, dtype=np.int64)
208
+ l_indices = np.empty(lower, dtype=np.int32)
209
+ u_indices = np.empty(upper, dtype=np.int32)
210
+ l_data = np.empty((lower, 9), dtype=np.float32)
211
+ u_data = np.empty((upper, 9), dtype=np.float32)
212
+ diag = np.zeros((n, 9), dtype=np.float32)
213
+ dest = np.empty(indices.size, dtype=np.int64)
214
+ nl = 0
215
+ nu = 0
216
+ for k in range(n):
217
+ i = order[k]
218
+ l_indptr[k] = nl
219
+ u_indptr[k] = nu
220
+ for q in range(indptr[i], indptr[i + 1]):
221
+ j = place[indices[q]]
222
+ if j < k:
223
+ l_indices[nl] = j
224
+ for r in range(9):
225
+ l_data[nl, r] = data[q, r]
226
+ dest[q] = nl
227
+ nl += 1
228
+ elif j > k:
229
+ u_indices[nu] = j
230
+ for r in range(9):
231
+ u_data[nu, r] = data[q, r]
232
+ dest[q] = -1 - nu # (shifted below, once the blocks below are counted)
233
+ nu += 1
234
+ else:
235
+ for r in range(9):
236
+ diag[k, r] += data[q, r]
237
+ dest[q] = -1 - upper - k
238
+ l_indptr[n] = nl
239
+ u_indptr[n] = nu
240
+ for q in range(dest.size):
241
+ if dest[q] < -upper:
242
+ dest[q] = nl + nu + (-1 - upper - dest[q])
243
+ elif dest[q] < 0:
244
+ dest[q] = nl + (-1 - dest[q])
245
+ return l_indptr, l_indices, l_data, u_indptr, u_indices[:nu], u_data[:nu], diag, dest
246
+
247
+
248
+ @njit(parallel=True, cache=True)
249
+ def _refresh_blocks(data, dest, l_data, u_data, diag):
250
+ """Other values of the blocks of a matrix (``data``: nine per stored block) at their
251
+ places in the copy of the sweeps (``dest``: :func:`_sorted_blocks`), in single
252
+ precision."""
253
+ nl = l_data.shape[0]
254
+ nu = u_data.shape[0]
255
+ for q in prange(dest.size):
256
+ d = dest[q]
257
+ if d < nl:
258
+ for r in range(9):
259
+ l_data[d, r] = data[q, r]
260
+ elif d < nl + nu:
261
+ for r in range(9):
262
+ u_data[d - nl, r] = data[q, r]
263
+ else:
264
+ for r in range(9):
265
+ diag[d - nl - nu, r] = data[q, r]
266
+
267
+
268
+ def _node_blocks(matrix: sp.csr_matrix, dofs: np.ndarray):
269
+ """A matrix whose unknowns are components of the nodes of a mesh, by 3 x 3 blocks of
270
+ nodes: ``dofs`` gives the degree of freedom ``3 node + component`` of each unknown,
271
+ in increasing order. Returns the blocks (BSR, over the nodes that have an unknown:
272
+ a component that is not one — a held one — is an empty row and column of its block)
273
+ and the place ``3 rank + component`` of each unknown among them; none for unknowns
274
+ in another order."""
275
+ n = matrix.shape[0]
276
+ if dofs.shape != (n,) or n == 0 or np.any(np.diff(dofs) <= 0):
277
+ return None
278
+ rank = np.concatenate(([0], np.cumsum(np.diff(dofs // 3) > 0))) # the node of each unknown
279
+ slot = 3 * rank + dofs % 3
280
+ size = 3 * (int(rank[-1]) + 1)
281
+ if size != n:
282
+ counts = np.zeros(size, dtype=np.int64)
283
+ counts[slot] = np.diff(matrix.indptr)
284
+ indptr = np.zeros(size + 1, dtype=np.int64)
285
+ np.cumsum(counts, out=indptr[1:])
286
+ columns = slot.astype(np.int32)[matrix.indices]
287
+ matrix = sp.csr_matrix((matrix.data, columns, indptr), shape=(size, size))
288
+ blocks = matrix.tobsr(blocksize=(3, 3))
289
+ blocks.sort_indices()
290
+ return blocks, slot
291
+
292
+
293
+ def _cuts(indptr: np.ndarray, chunks: int = 64) -> np.ndarray:
294
+ """Rows that cut a CSR pattern into chunks of equal numbers of stored values: the
295
+ rows of the part above the diagonal of a matrix sorted by colour go from full (the
296
+ first colour) to empty (the last one), and equal shares of the rows would leave most
297
+ of the work to one thread."""
298
+ targets = (np.arange(chunks + 1) * int(indptr[-1])) // chunks
299
+ cuts = np.searchsorted(indptr, targets, side="left")
300
+ cuts[0], cuts[-1] = 0, indptr.size - 1
301
+ return np.unique(cuts).astype(np.int64)
302
+
303
+
304
+ @njit(parallel=True, cache=True)
305
+ def _forward(l_indptr, l_indices, l_data, diag, starts, b, x):
306
+ """``x = (D + L)^-1 b`` for a matrix sorted by colour: the forward Gauss-Seidel sweep
307
+ from zero, colour by colour (``starts``: the offsets of the colours) — the unknowns
308
+ of a colour, which no stored value couples, are relaxed in parallel."""
309
+ for c in range(starts.size - 1):
310
+ for i in prange(starts[c], starts[c + 1]):
311
+ s = b[i]
312
+ for k in range(l_indptr[i], l_indptr[i + 1]):
313
+ s -= l_data[k] * x[l_indices[k]]
314
+ x[i] = s / diag[i] if diag[i] != 0.0 else 0.0
315
+
316
+
317
+ @njit(parallel=True, cache=True)
318
+ def _upper(u_indptr, u_indices, u_data, cuts, x, out):
319
+ """``out = -U x``: the residual ``b - A x`` of the ``x`` of a forward sweep from
320
+ zero, for which ``b - (D + L) x`` is zero (``cuts``: the rows that share the stored
321
+ values equally between the threads, :func:`_cuts`)."""
322
+ for t in prange(cuts.size - 1):
323
+ for i in range(cuts[t], cuts[t + 1]):
324
+ s = 0.0
325
+ for k in range(u_indptr[i], u_indptr[i + 1]):
326
+ s -= u_data[k] * x[u_indices[k]]
327
+ out[i] = s
328
+
329
+
330
+ @njit(parallel=True, cache=True)
331
+ def _relax(l_indptr, l_indices, l_data, u_indptr, u_indices, u_data, diag, starts, x, b,
332
+ forward): # fmt: skip
333
+ """One Gauss-Seidel sweep on ``A x = b``, in place, for a matrix sorted by colour: in
334
+ the order of the colours or the reverse one, the unknowns of a colour in parallel."""
335
+ count = starts.size - 1
336
+ for step in range(count):
337
+ c = step if forward else count - 1 - step
338
+ for i in prange(starts[c], starts[c + 1]):
339
+ s = b[i]
340
+ for k in range(l_indptr[i], l_indptr[i + 1]):
341
+ s -= l_data[k] * x[l_indices[k]]
342
+ for k in range(u_indptr[i], u_indptr[i + 1]):
343
+ s -= u_data[k] * x[u_indices[k]]
344
+ if diag[i] != 0.0:
345
+ x[i] = s / diag[i]
346
+
347
+
348
+ @njit(parallel=True, cache=True)
349
+ def _forward3(l_indptr, l_indices, l_data, diag, starts, b, x):
350
+ """:func:`_forward` by 3 x 3 blocks: the nodes of a colour in parallel, the three
351
+ components of a node one after the other."""
352
+ for c in range(starts.size - 1):
353
+ for i in prange(starts[c], starts[c + 1]):
354
+ s0 = b[3 * i]
355
+ s1 = b[3 * i + 1]
356
+ s2 = b[3 * i + 2]
357
+ for k in range(l_indptr[i], l_indptr[i + 1]):
358
+ j = 3 * l_indices[k]
359
+ x0 = x[j]
360
+ x1 = x[j + 1]
361
+ x2 = x[j + 2]
362
+ s0 -= l_data[k, 0] * x0 + l_data[k, 1] * x1 + l_data[k, 2] * x2
363
+ s1 -= l_data[k, 3] * x0 + l_data[k, 4] * x1 + l_data[k, 5] * x2
364
+ s2 -= l_data[k, 6] * x0 + l_data[k, 7] * x1 + l_data[k, 8] * x2
365
+ d = diag[i]
366
+ y0 = s0 / d[0] if d[0] != 0.0 else 0.0
367
+ s1 -= d[3] * y0
368
+ y1 = s1 / d[4] if d[4] != 0.0 else 0.0
369
+ s2 -= d[6] * y0 + d[7] * y1
370
+ y2 = s2 / d[8] if d[8] != 0.0 else 0.0
371
+ x[3 * i] = y0
372
+ x[3 * i + 1] = y1
373
+ x[3 * i + 2] = y2
374
+
375
+
376
+ @njit(parallel=True, cache=True)
377
+ def _upper3(u_indptr, u_indices, u_data, diag, cuts, x, out):
378
+ """:func:`_upper` by 3 x 3 blocks: the blocks above the diagonal and the part of the
379
+ diagonal blocks above the diagonal."""
380
+ for t in prange(cuts.size - 1):
381
+ for i in range(cuts[t], cuts[t + 1]):
382
+ s0 = 0.0
383
+ s1 = 0.0
384
+ s2 = 0.0
385
+ for k in range(u_indptr[i], u_indptr[i + 1]):
386
+ j = 3 * u_indices[k]
387
+ x0 = x[j]
388
+ x1 = x[j + 1]
389
+ x2 = x[j + 2]
390
+ s0 -= u_data[k, 0] * x0 + u_data[k, 1] * x1 + u_data[k, 2] * x2
391
+ s1 -= u_data[k, 3] * x0 + u_data[k, 4] * x1 + u_data[k, 5] * x2
392
+ s2 -= u_data[k, 6] * x0 + u_data[k, 7] * x1 + u_data[k, 8] * x2
393
+ d = diag[i]
394
+ out[3 * i] = s0 - d[1] * x[3 * i + 1] - d[2] * x[3 * i + 2]
395
+ out[3 * i + 1] = s1 - d[5] * x[3 * i + 2]
396
+ out[3 * i + 2] = s2
397
+
398
+
399
+ @njit(parallel=True, cache=True)
400
+ def _relax3(l_indptr, l_indices, l_data, u_indptr, u_indices, u_data, diag, starts, x, b,
401
+ forward): # fmt: skip
402
+ """:func:`_relax` by 3 x 3 blocks: the colours of the nodes in their order or the
403
+ reverse one, the three components of a node in theirs or the reverse one."""
404
+ count = starts.size - 1
405
+ for step in range(count):
406
+ c = step if forward else count - 1 - step
407
+ for i in prange(starts[c], starts[c + 1]):
408
+ s0 = b[3 * i]
409
+ s1 = b[3 * i + 1]
410
+ s2 = b[3 * i + 2]
411
+ for k in range(l_indptr[i], l_indptr[i + 1]):
412
+ j = 3 * l_indices[k]
413
+ x0 = x[j]
414
+ x1 = x[j + 1]
415
+ x2 = x[j + 2]
416
+ s0 -= l_data[k, 0] * x0 + l_data[k, 1] * x1 + l_data[k, 2] * x2
417
+ s1 -= l_data[k, 3] * x0 + l_data[k, 4] * x1 + l_data[k, 5] * x2
418
+ s2 -= l_data[k, 6] * x0 + l_data[k, 7] * x1 + l_data[k, 8] * x2
419
+ for k in range(u_indptr[i], u_indptr[i + 1]):
420
+ j = 3 * u_indices[k]
421
+ x0 = x[j]
422
+ x1 = x[j + 1]
423
+ x2 = x[j + 2]
424
+ s0 -= u_data[k, 0] * x0 + u_data[k, 1] * x1 + u_data[k, 2] * x2
425
+ s1 -= u_data[k, 3] * x0 + u_data[k, 4] * x1 + u_data[k, 5] * x2
426
+ s2 -= u_data[k, 6] * x0 + u_data[k, 7] * x1 + u_data[k, 8] * x2
427
+ d = diag[i]
428
+ y0 = x[3 * i]
429
+ y1 = x[3 * i + 1]
430
+ y2 = x[3 * i + 2]
431
+ if forward:
432
+ if d[0] != 0.0:
433
+ y0 = (s0 - d[1] * y1 - d[2] * y2) / d[0]
434
+ if d[4] != 0.0:
435
+ y1 = (s1 - d[3] * y0 - d[5] * y2) / d[4]
436
+ if d[8] != 0.0:
437
+ y2 = (s2 - d[6] * y0 - d[7] * y1) / d[8]
438
+ else:
439
+ if d[8] != 0.0:
440
+ y2 = (s2 - d[6] * y0 - d[7] * y1) / d[8]
441
+ if d[4] != 0.0:
442
+ y1 = (s1 - d[3] * y0 - d[5] * y2) / d[4]
443
+ if d[0] != 0.0:
444
+ y0 = (s0 - d[1] * y1 - d[2] * y2) / d[0]
445
+ x[3 * i] = y0
446
+ x[3 * i + 1] = y1
447
+ x[3 * i + 2] = y2
448
+
449
+
450
+ @njit(parallel=True, cache=True)
451
+ def _matvec(indptr, indices, data, x, out):
452
+ """``out = A x`` (CSR), the rows shared between the threads; each row is summed in
453
+ the order of its stored values, as SciPy sums it: the same product to the bit."""
454
+ for i in prange(out.size):
455
+ s = 0.0
456
+ for k in range(indptr[i], indptr[i + 1]):
457
+ s += data[k] * x[indices[k]]
458
+ out[i] = s
459
+
460
+
461
+ @njit(parallel=True, cache=True)
462
+ def _dense(matrix, x, out):
463
+ """``out = M x`` for a dense matrix, its rows shared between the threads."""
464
+ for i in prange(out.size):
465
+ row = matrix[i]
466
+ s = 0.0
467
+ for j in range(x.size):
468
+ s += row[j] * x[j]
469
+ out[i] = s
470
+
471
+
472
+ def dense_product(matrix: np.ndarray, x: np.ndarray) -> np.ndarray:
473
+ """``M x`` of a dense matrix and a vector on the threads of the compiled kernels. The
474
+ product of BLAS (``M @ x``) runs on its own threads, which then stay busy for a tenth
475
+ of a second (OpenBLAS: they wait for the next product) and take the cores of the
476
+ kernels that follow: a sweep or a product several times slower, at each iteration
477
+ where the coarsest level of a multigrid cycle is such a product."""
478
+ out = np.empty(matrix.shape[0])
479
+ _dense(np.ascontiguousarray(matrix), np.ascontiguousarray(x, dtype=np.float64), out)
480
+ return out
481
+
482
+
483
+ def product(matrix: sp.csr_matrix, x: np.ndarray) -> np.ndarray:
484
+ """``A x`` of a CSR matrix and a vector: on several threads for a large matrix (the
485
+ product of SciPy runs on one), with the same result to the bit."""
486
+ if (
487
+ matrix.nnz < THREADED
488
+ or matrix.format != "csr"
489
+ or matrix.data.dtype != np.float64
490
+ or x.ndim != 1
491
+ or x.dtype != np.float64
492
+ ):
493
+ return matrix @ x
494
+ out = np.empty(matrix.shape[0])
495
+ _matvec(matrix.indptr, matrix.indices, matrix.data, np.ascontiguousarray(x), out)
496
+ return out
497
+
498
+
499
+ @njit(parallel=True, cache=True)
500
+ def _block_matvec(indptr, indices, data, cuts, x, out):
501
+ """``out = A x`` for a matrix stored by 3 x 3 blocks of nodes (block CSR, nine values
502
+ per block, row by row), the rows of the nodes shared between the threads by equal
503
+ numbers of blocks (``cuts``). The three components of a row are summed term by term
504
+ in the order of the columns, as the product of the stored values of the matrix sums
505
+ them (a value that is not stored adds a zero): the same product to the bit."""
506
+ for t in prange(cuts.size - 1):
507
+ for i in range(cuts[t], cuts[t + 1]):
508
+ s0 = 0.0
509
+ s1 = 0.0
510
+ s2 = 0.0
511
+ for k in range(indptr[i], indptr[i + 1]):
512
+ j = 3 * indices[k]
513
+ x0 = x[j]
514
+ x1 = x[j + 1]
515
+ x2 = x[j + 2]
516
+ s0 += data[k, 0] * x0
517
+ s0 += data[k, 1] * x1
518
+ s0 += data[k, 2] * x2
519
+ s1 += data[k, 3] * x0
520
+ s1 += data[k, 4] * x1
521
+ s1 += data[k, 5] * x2
522
+ s2 += data[k, 6] * x0
523
+ s2 += data[k, 7] * x1
524
+ s2 += data[k, 8] * x2
525
+ out[3 * i] = s0
526
+ out[3 * i + 1] = s1
527
+ out[3 * i + 2] = s2
528
+
529
+
530
+ @njit(parallel=True, cache=True)
531
+ def _block_places(indptr, indices, slot, b_indptr, b_indices, place):
532
+ """The place of each stored value of a CSR matrix among the values of its blocks
533
+ (nine per block of the block pattern ``b_indptr``, ``b_indices``, its rows sorted;
534
+ ``slot``: the place ``3 node + component`` of each unknown). Returns the number of
535
+ stored values without a block."""
536
+ missing = 0
537
+ for i in prange(indptr.size - 1):
538
+ node = slot[i] // 3
539
+ row = slot[i] - 3 * node
540
+ for k in range(indptr[i], indptr[i + 1]):
541
+ other = slot[indices[k]] // 3
542
+ lo, hi = b_indptr[node], b_indptr[node + 1]
543
+ while lo < hi:
544
+ mid = (lo + hi) // 2
545
+ if b_indices[mid] < other:
546
+ lo = mid + 1
547
+ else:
548
+ hi = mid
549
+ if lo < b_indptr[node + 1] and b_indices[lo] == other:
550
+ place[k] = 9 * lo + 3 * row + slot[indices[k]] - 3 * other
551
+ else:
552
+ place[k] = 0
553
+ missing += 1
554
+ return missing
555
+
556
+
557
+ @njit(parallel=True, cache=True)
558
+ def _fill_blocks(indptr, place, values, data):
559
+ """The stored values of a CSR matrix at their places in the values of its blocks
560
+ (``place``: :func:`_block_places`; two rows of the matrix never write at one place)."""
561
+ for p in prange(data.size):
562
+ data[p] = 0.0
563
+ for i in prange(indptr.size - 1):
564
+ for k in range(indptr[i], indptr[i + 1]):
565
+ data[place[k]] += values[k]
566
+
567
+
568
+ def _same(a: np.ndarray, b: np.ndarray) -> bool:
569
+ """Whether two arrays are one, or views of the same values in memory (SciPy gives
570
+ each matrix its own view of the arrays of a pattern that it is built on)."""
571
+ return a is b or (
572
+ a.shape == b.shape
573
+ and a.dtype == b.dtype
574
+ and a.strides == b.strides
575
+ and a.__array_interface__["data"][0] == b.__array_interface__["data"][0]
576
+ )
577
+
578
+
579
+ class NodeBlocks:
580
+ """A square matrix whose unknowns are components of the displacements of the nodes of
581
+ a mesh, by the 3 x 3 blocks of the couplings of its nodes, in double precision (block
582
+ CSR over the nodes that have an unknown: :attr:`indptr`, :attr:`indices`, sorted;
583
+ :attr:`data`, nine values per block, row by row). It is the form that the products
584
+ of the Krylov iterations read (:meth:`product`: one column index for nine values, the
585
+ three components of a neighbour read together; the same product to the bit as that
586
+ of the stored values taken in the order of their columns), and from which the sweeps
587
+ take their copy (:class:`Smoother`).
588
+
589
+ ``dofs``: the degree of freedom ``3 node + component`` of each unknown, in increasing
590
+ order. A component that is not an unknown (one that a condition holds) is an empty
591
+ row and column of its block: the blocks have :attr:`size` rows, the unknown ``i`` at
592
+ the row ``slot[i]``.
593
+
594
+ :meth:`refresh` takes the values of another matrix of the same pattern — the tangent
595
+ of the next Newton iteration of a loading path — at their places in the blocks, found
596
+ once."""
597
+
598
+ #: (as a SciPy matrix: the rows of the blocks are sorted)
599
+ has_sorted_indices = True
600
+
601
+ def __init__(self, matrix: sp.csr_matrix, dofs: np.ndarray):
602
+ by_nodes = _node_blocks(matrix, np.asarray(dofs))
603
+ if by_nodes is None:
604
+ raise ValueError("unknowns that are not in the order of their degrees of freedom")
605
+ blocks, self.slot = by_nodes
606
+ self.n = matrix.shape[0]
607
+ self.size = blocks.shape[0]
608
+ self.indptr, self.indices = blocks.indptr, blocks.indices
609
+ self.data = np.ascontiguousarray(blocks.data, dtype=np.float64).reshape(-1, 9)
610
+ self.cuts = _cuts(self.indptr)
611
+ self._pattern = None # the CSR pattern of the places of :meth:`refresh`
612
+ self._place = None
613
+
614
+ @classmethod
615
+ def of(cls, matrix, dofs) -> NodeBlocks | None:
616
+ """The blocks of a matrix, or none where they would not serve: a small matrix
617
+ (:data:`THREADED`), another format or precision, unknowns that are not in the
618
+ order of their degrees of freedom."""
619
+ if (
620
+ dofs is None
621
+ or matrix.format != "csr"
622
+ or matrix.nnz < THREADED
623
+ or matrix.data.dtype != np.float64
624
+ or matrix.shape[0] >= 2**31 - 1
625
+ ):
626
+ return None
627
+ dofs = np.asarray(dofs)
628
+ if dofs.shape != (matrix.shape[0],) or np.any(np.diff(dofs) <= 0):
629
+ return None
630
+ return cls(matrix, dofs)
631
+
632
+ def product(self, x: np.ndarray) -> np.ndarray:
633
+ """``A x``, on several threads."""
634
+ x = np.ascontiguousarray(x, dtype=np.float64)
635
+ if self.size == self.n:
636
+ out = np.empty(self.n)
637
+ _block_matvec(self.indptr, self.indices, self.data, self.cuts, x, out)
638
+ return out
639
+ wide = np.zeros(self.size)
640
+ wide[self.slot] = x
641
+ out = np.empty(self.size)
642
+ _block_matvec(self.indptr, self.indices, self.data, self.cuts, wide, out)
643
+ return out[self.slot]
644
+
645
+ def refresh(self, matrix: sp.csr_matrix) -> bool:
646
+ """Takes the values of another matrix of the same unknowns, whose stored values
647
+ all have a block here; returns false, and leaves the blocks as they are, for a
648
+ matrix with other stored values. The places of the stored values are found once
649
+ for the matrices that share the arrays of their pattern (which are kept, and
650
+ must not be changed in place)."""
651
+ if matrix.format != "csr" or matrix.shape[0] != self.n or matrix.data.dtype != np.float64:
652
+ return False
653
+ pattern = self._pattern
654
+ if (
655
+ pattern is None
656
+ or not _same(pattern[0], matrix.indptr)
657
+ or not _same(pattern[1], matrix.indices)
658
+ ):
659
+ kind = np.int32 if 9 * self.indices.size < 2**31 - 1 else np.int64
660
+ place = np.empty(matrix.indices.size, dtype=kind)
661
+ if _block_places(matrix.indptr, matrix.indices, self.slot, self.indptr, self.indices,
662
+ place): # fmt: skip
663
+ return False
664
+ self._pattern, self._place = (matrix.indptr, matrix.indices), place
665
+ _fill_blocks(matrix.indptr, self._place, matrix.data, self.data.reshape(-1))
666
+ return True
667
+
668
+
669
+ class Smoother:
670
+ """Gauss-Seidel sweeps on a CSR matrix: in the order of the unknowns on one thread
671
+ for a small matrix; for a large one (:data:`COLOURED`, fewer than 2^31 unknowns),
672
+ in the order of the colours of its unknowns (those of the graph of its couplings,
673
+ :func:`_graph`: no stored value couples two unknowns of a colour, on either side
674
+ of the diagonal), those of a colour relaxed in parallel, on a copy of the matrix
675
+ sorted by colour (:attr:`coloured`). The forward and the backward sweeps are the
676
+ adjoints of each other in both cases, and the result does not depend on the number
677
+ of threads.
678
+
679
+ With colours, :meth:`sorted`, :meth:`start`, :meth:`residual`, :meth:`relax` and
680
+ :meth:`unsorted` work on vectors in the order of the colours (a multigrid cycle
681
+ then sorts its right-hand side once, and its solution back once): :attr:`size`
682
+ values, the unknown ``i`` at the place ``place[i]``.
683
+
684
+ ``dofs``: the degree of freedom ``3 node + component`` of each unknown, for unknowns
685
+ that are components of the displacements of the nodes of a mesh, in increasing
686
+ order. The colours are then those of the nodes (a third of the colours: no stored
687
+ value couples two nodes of a colour), and the copy holds the 3 x 3 blocks of the
688
+ couplings of the nodes (:attr:`block` is 3): the sweep is the Gauss-Seidel sweep in
689
+ the order of the colours of the nodes, the three components of a node one after
690
+ the other. The vectors in the order of the colours have three places per node; the
691
+ place of a component that is not an unknown (a held one) stays zero. ``blocks``:
692
+ the blocks of the matrix, where they have been made already (:class:`NodeBlocks`);
693
+ :meth:`refresh` then gives the sweeps the values that these blocks have taken since
694
+ (the tangent of another Newton iteration: the same colours, the same places)."""
695
+
696
+ def __init__(self, matrix: sp.csr_matrix, dofs: np.ndarray | None = None,
697
+ blocks: NodeBlocks | None = None): # fmt: skip
698
+ self.matrix = matrix
699
+ self.coloured = False
700
+ self.block = 1
701
+ self._source = None # the blocks that the copy of the sweeps was taken from
702
+ n = matrix.shape[0]
703
+ if not (COLOURED <= matrix.nnz and n < 2**31 - 1 and matrix.data.dtype == np.float64):
704
+ return
705
+ if blocks is None:
706
+ blocks = NodeBlocks.of(matrix, dofs)
707
+ if blocks is not None:
708
+ self.size = blocks.size
709
+ nodes, self.starts = _colours(*_graph(blocks), self.size // 3)
710
+ *arrays, self._dest = _sorted_blocks(blocks.indptr, blocks.indices, blocks.data, nodes)
711
+ rank = np.empty(nodes.size, dtype=np.int64)
712
+ rank[nodes] = np.arange(nodes.size)
713
+ self.place = 3 * rank[blocks.slot // 3] + blocks.slot % 3
714
+ self.block = 3
715
+ self._source = blocks
716
+ else:
717
+ order, self.starts = _colours(*_graph(matrix), n)
718
+ arrays = _sorted_by_colour(matrix.indptr, matrix.indices, matrix.data, order)
719
+ self.size = n
720
+ self.place = np.empty(n, dtype=np.int64)
721
+ self.place[order] = np.arange(n)
722
+ self.lower, self.upper, self.diag = arrays[:3], arrays[3:6], arrays[6]
723
+ self.cuts = _cuts(self.upper[0])
724
+ self.coloured = True
725
+
726
+ def refresh(self, blocks: NodeBlocks) -> bool:
727
+ """The sweeps of the values that the blocks of the matrix have now (those that
728
+ the copy of the sweeps was taken from: false otherwise, and for sweeps that do
729
+ not work by nodes)."""
730
+ if self._source is None or blocks is not self._source:
731
+ return False
732
+ _refresh_blocks(blocks.data, self._dest, self.lower[2], self.upper[2], self.diag)
733
+ return True
734
+
735
+ def sorted(self, v: np.ndarray) -> np.ndarray:
736
+ """A vector in the order of the colours."""
737
+ out = np.zeros(self.size) if self.size != v.size else np.empty(self.size)
738
+ out[self.place] = v
739
+ return out
740
+
741
+ def unsorted(self, v: np.ndarray) -> np.ndarray:
742
+ """A vector of the order of the colours, in the order of the unknowns."""
743
+ return v[self.place]
744
+
745
+ def start(self, b: np.ndarray) -> np.ndarray:
746
+ """The forward sweep from zero (vectors in the order of the colours)."""
747
+ x = np.empty_like(b)
748
+ forward = _forward3 if self.block == 3 else _forward
749
+ forward(*self.lower, self.diag, self.starts, b, x)
750
+ return x
751
+
752
+ def residual(self, x: np.ndarray) -> np.ndarray:
753
+ """``b - A x`` for the ``x`` that :meth:`start` returns (in the order of the
754
+ colours): the part of the matrix above the diagonal only."""
755
+ out = np.empty_like(x)
756
+ if self.block == 3:
757
+ _upper3(*self.upper, self.diag, self.cuts, x, out)
758
+ else:
759
+ _upper(*self.upper, self.cuts, x, out)
760
+ return out
761
+
762
+ def relax(self, x: np.ndarray, b: np.ndarray, forward: bool) -> None:
763
+ """A sweep on vectors in the order of the colours, in place."""
764
+ relax = _relax3 if self.block == 3 else _relax
765
+ relax(*self.lower, *self.upper, self.diag, self.starts, x, b, forward)
766
+
767
+ def sweep(self, x: np.ndarray, b: np.ndarray, forward: bool) -> None:
768
+ """A sweep on vectors in the order of the unknowns, in place."""
769
+ if not self.coloured:
770
+ a = self.matrix
771
+ gauss_seidel(a.indptr, a.indices, a.data, x, b, forward)
772
+ return
773
+ xs = self.sorted(x)
774
+ self.relax(xs, self.sorted(b), forward)
775
+ x[:] = xs[self.place]
776
+
777
+
778
+ @njit(cache=True)
779
+ def _aggregate(indptr, indices, n):
780
+ """Standard aggregation of the graph of a CSR matrix: aggregate index per node."""
781
+ agg = np.full(n, -1, dtype=np.int64)
782
+ count = 0
783
+ for i in range(n): # pass 1: nodes whose neighbours are all free, with them
784
+ if agg[i] >= 0:
785
+ continue
786
+ free = True
787
+ for k in range(indptr[i], indptr[i + 1]):
788
+ if agg[indices[k]] >= 0:
789
+ free = False
790
+ break
791
+ if free:
792
+ agg[i] = count
793
+ for k in range(indptr[i], indptr[i + 1]):
794
+ agg[indices[k]] = count
795
+ count += 1
796
+ joined = agg.copy()
797
+ for i in range(n): # pass 2: join a neighbouring aggregate of pass 1
798
+ if agg[i] >= 0:
799
+ continue
800
+ for k in range(indptr[i], indptr[i + 1]):
801
+ j = indices[k]
802
+ if agg[j] >= 0:
803
+ joined[i] = agg[j]
804
+ break
805
+ agg = joined
806
+ for i in range(n): # pass 3: the rest, with their free neighbours
807
+ if agg[i] >= 0:
808
+ continue
809
+ agg[i] = count
810
+ for k in range(indptr[i], indptr[i + 1]):
811
+ j = indices[k]
812
+ if agg[j] < 0:
813
+ agg[j] = count
814
+ count += 1
815
+ return agg, count
816
+
817
+
818
+ @njit(cache=True)
819
+ def _fit_candidates(order, start, candidates):
820
+ """Orthonormal bases of the candidates on each aggregate (modified Gram-Schmidt).
821
+
822
+ ``order`` lists the nodes aggregate by aggregate (``start`` pointers). Returns the
823
+ column of each node's basis vectors (``q``: one row per node, ``k`` columns, the
824
+ ``kept`` first are used), the number of basis vectors per aggregate, and the
825
+ coarse candidates (rows of R, ``k`` per aggregate).
826
+ """
827
+ n, k = candidates.shape
828
+ n_agg = start.size - 1
829
+ q = np.zeros((n, k))
830
+ kept = np.zeros(n_agg, dtype=np.int64)
831
+ coarse = np.zeros((n_agg * k, k))
832
+ for a in range(n_agg):
833
+ lo, hi = start[a], start[a + 1]
834
+ m = hi - lo
835
+ basis = np.zeros((m, k))
836
+ r = np.zeros((k, k))
837
+ nk = 0
838
+ for c in range(k):
839
+ v = np.empty(m)
840
+ norm0 = 0.0
841
+ for t in range(m):
842
+ v[t] = candidates[order[lo + t], c]
843
+ norm0 += v[t] * v[t]
844
+ norm0 = np.sqrt(norm0)
845
+ for p in range(nk): # project out the kept vectors (twice, for stability)
846
+ for _ in range(2):
847
+ dot = 0.0
848
+ for t in range(m):
849
+ dot += basis[t, p] * v[t]
850
+ r[p, c] += dot
851
+ for t in range(m):
852
+ v[t] -= dot * basis[t, p]
853
+ norm = 0.0
854
+ for t in range(m):
855
+ norm += v[t] * v[t]
856
+ norm = np.sqrt(norm)
857
+ if norm > 1e-10 * norm0 and norm > 1e-300 and nk < m:
858
+ for t in range(m):
859
+ basis[t, nk] = v[t] / norm
860
+ r[nk, c] = norm
861
+ nk += 1
862
+ kept[a] = nk
863
+ for t in range(m):
864
+ for p in range(nk):
865
+ q[order[lo + t], p] = basis[t, p]
866
+ for p in range(nk):
867
+ for c in range(k):
868
+ coarse[a * k + p, c] = r[p, c]
869
+ return q, kept, coarse
870
+
871
+
872
+ def _tentative(matrix: sp.csr_matrix, candidates: np.ndarray):
873
+ """Tentative prolongator and coarse candidates of a matrix."""
874
+ n = matrix.shape[0]
875
+ agg, n_agg = _aggregate(matrix.indptr, matrix.indices, n)
876
+ order = np.argsort(agg, kind="stable")
877
+ start = np.searchsorted(agg[order], np.arange(n_agg + 1))
878
+ q, kept, coarse = _fit_candidates(order, start, np.ascontiguousarray(candidates))
879
+ k = candidates.shape[1]
880
+ offset = np.concatenate([[0], np.cumsum(kept)])
881
+ width = kept[agg] # basis vectors of each node's aggregate
882
+ rows = np.repeat(np.arange(n), width)
883
+ within = np.arange(rows.size) - np.repeat(np.cumsum(width) - width, width)
884
+ cols = offset[agg][rows] + within
885
+ vals = q[rows, within]
886
+ t = sp.csr_matrix((vals, (rows, cols)), shape=(n, int(offset[-1])))
887
+ keep_rows = (np.arange(k)[None, :] < kept[:, None]).ravel()
888
+ return t, coarse[keep_rows]
889
+
890
+
891
+ def _spectral_radius(matrix: sp.csr_matrix, inv_diag: np.ndarray) -> float:
892
+ """Largest eigenvalue of D^-1 A (symmetric form D^-1/2 A D^-1/2)."""
893
+ s = np.sqrt(inv_diag)
894
+ scaled = sp.diags(s) @ matrix @ sp.diags(s)
895
+ if scaled.shape[0] <= 50:
896
+ return float(np.max(np.abs(np.linalg.eigvalsh(scaled.toarray()))))
897
+ rng = np.random.default_rng(0)
898
+ try:
899
+ value = spla.eigsh(
900
+ scaled,
901
+ k=1,
902
+ which="LA",
903
+ tol=1e-3,
904
+ maxiter=200,
905
+ v0=rng.random(scaled.shape[0]),
906
+ return_eigenvectors=False,
907
+ )
908
+ return float(value[0])
909
+ except spla.ArpackNoConvergence as exc: # pragma: no cover - rare
910
+ if len(exc.eigenvalues):
911
+ return float(np.max(exc.eigenvalues))
912
+ return float(abs(scaled).sum(axis=1).max()) # Gershgorin bound
913
+
914
+
915
+ class SmoothedAggregation:
916
+ """Smoothed-aggregation hierarchy of a symmetric positive (semi-)definite matrix.
917
+
918
+ ``candidates`` (n x k) are the near-null modes (default: constants). Calling the
919
+ object applies one V-cycle from a zero initial guess. ``dofs``: the degree of
920
+ freedom ``3 node + component`` of each unknown, for unknowns that are components of
921
+ the nodes of a mesh (the sweeps of the first level then work by nodes, see
922
+ :class:`Smoother`); ``blocks``: the blocks of the matrix, where they have been made
923
+ already (:class:`NodeBlocks`; :meth:`refresh` then gives the sweeps of the first
924
+ level the values that they have taken since).
925
+ """
926
+
927
+ def __init__(self, matrix, candidates=None, *, max_coarse: int = 500, max_levels: int = 20,
928
+ dofs=None, blocks=None): # fmt: skip
929
+ a = sp.csr_matrix(matrix, dtype=np.float64)
930
+ a.sum_duplicates()
931
+ a.sort_indices()
932
+ n = a.shape[0]
933
+ b = np.ones((n, 1)) if candidates is None else np.asarray(candidates, dtype=float)
934
+ if b.ndim == 1:
935
+ b = b[:, None]
936
+ b = b[:, np.linalg.norm(b, axis=0) > 0] if b.shape[1] else np.ones((n, 1))
937
+ self.levels: list[dict] = []
938
+ while a.shape[0] > max_coarse and len(self.levels) < max_levels - 1:
939
+ diag = a.diagonal()
940
+ inv_diag = np.where(diag > 0, 1.0 / np.where(diag > 0, diag, 1.0), 0.0)
941
+ t, coarse_b = _tentative(a, b)
942
+ if t.shape[1] == 0 or t.shape[1] >= a.shape[0]:
943
+ break
944
+ rho = _spectral_radius(a, inv_diag)
945
+ omega = 4.0 / 3.0 / rho if rho > 0 else 0.0
946
+ p = (t - omega * (sp.diags(inv_diag) @ (a @ t))).tocsr()
947
+ r = p.T.tocsr()
948
+ first = not self.levels
949
+ smoother = Smoother(a, dofs if first else None, blocks if first else None)
950
+ self.levels.append({"A": a, "P": p, "R": r, "smoother": smoother})
951
+ a = (r @ a @ p).tocsr()
952
+ a.sum_duplicates()
953
+ a.sort_indices()
954
+ b = coarse_b
955
+ self.levels.append({"A": a})
956
+ self._coarse = self._pseudo_inverse(a)
957
+
958
+ def refresh(self, blocks: NodeBlocks) -> bool:
959
+ """The sweeps of the first level for the values that the blocks of the matrix
960
+ have now (see :meth:`Smoother.refresh`); the interpolations and the coarser
961
+ levels are kept. The cycle stays a symmetric positive definite preconditioner:
962
+ its coarse correction adds a positive term to the symmetric sweeps of the
963
+ matrix as it is."""
964
+ return len(self.levels) > 1 and self.levels[0]["smoother"].refresh(blocks)
965
+
966
+ @staticmethod
967
+ def _pseudo_inverse(a: sp.csr_matrix):
968
+ if a.shape[0] > 3000: # coarsening stalled: sparse factorization
969
+ lu = spla.splu(a.tocsc())
970
+ return spla.LinearOperator(a.shape, matvec=lu.solve)
971
+ dense = a.toarray()
972
+ dense = 0.5 * (dense + dense.T)
973
+ w, v = sla.eigh(dense)
974
+ cut = 1e-12 * max(float(np.abs(w).max()) if w.size else 0.0, 1e-300)
975
+ inv = np.where(np.abs(w) > cut, 1.0 / np.where(np.abs(w) > cut, w, 1.0), 0.0)
976
+ return (v * inv) @ v.T
977
+
978
+ def operator_complexity(self) -> float:
979
+ nnz = [lvl["A"].nnz for lvl in self.levels]
980
+ return float(sum(nnz) / nnz[0]) if nnz[0] else 1.0
981
+
982
+ def _cycle(self, level: int, b: np.ndarray) -> np.ndarray:
983
+ lvl = self.levels[level]
984
+ if level == len(self.levels) - 1:
985
+ if isinstance(self._coarse, np.ndarray):
986
+ return dense_product(self._coarse, b)
987
+ return self._coarse @ b
988
+ a, smoother = lvl["A"], lvl["smoother"]
989
+ if smoother.coloured: # (the cycle of this level in the order of the colours)
990
+ bs = smoother.sorted(b)
991
+ xs = smoother.start(bs)
992
+ residual = smoother.unsorted(smoother.residual(xs))
993
+ coarse = product(lvl["P"], self._cycle(level + 1, product(lvl["R"], residual)))
994
+ xs += smoother.sorted(coarse)
995
+ smoother.relax(xs, bs, False)
996
+ return smoother.unsorted(xs)
997
+ x = np.zeros_like(b)
998
+ smoother.sweep(x, b, True)
999
+ residual = b - product(a, x)
1000
+ x += product(lvl["P"], self._cycle(level + 1, product(lvl["R"], residual)))
1001
+ smoother.sweep(x, b, False)
1002
+ return x
1003
+
1004
+ def __call__(self, b: np.ndarray) -> np.ndarray:
1005
+ return self._cycle(0, np.ascontiguousarray(b, dtype=np.float64))