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,760 @@
1
+ """Compiled kernels of the trilinear voxel elements of the FFT solver.
2
+
3
+ The grid has ``(nx, ny, nz)`` voxels (elements), numbered ``e = (i * ny + j) * nz + k``.
4
+ Nodes are numbered the same way on the ``(nx + 1, ny + 1, nz + 1)`` node grid, or on
5
+ the ``(nx, ny, nz)`` grid when the mesh is periodic (opposite faces identified).
6
+ ``dof[node * nd + c]`` is the index of a degree of freedom, or -1 for an unused one.
7
+
8
+ Right-hand sides loop over nodes (each thread writes only the rows of its nodes, so no
9
+ atomics are needed); element sums loop over chunks of elements.
10
+
11
+ The kernels of the loading paths (:mod:`morphogen.homogenization.fft_nonlinear`) work on
12
+ the 8 Gauss points of the voxels that carry material: point ``8 t + g`` is Gauss point
13
+ ``g`` of voxel ``elem[t]``, and ``slot[e]`` is the position ``t`` of voxel ``e`` (-1 for a
14
+ void). Their measures of strain are the engineering strains (6, Voigt order) or, in
15
+ finite strains, the displacement gradients (9, row by row).
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import math
21
+
22
+ import numpy as np
23
+ from numba import njit, prange
24
+
25
+ from morphogen.mesh.voxel import HEX_CORNERS
26
+
27
+ CORNERS = np.ascontiguousarray(HEX_CORNERS, dtype=np.int64)
28
+ # CORNER_INDEX[a, b, c] = index of the corner with offsets (a, b, c)
29
+ CORNER_INDEX = np.zeros((2, 2, 2), dtype=np.int64)
30
+ for _q, (_a, _b, _c) in enumerate(HEX_CORNERS):
31
+ CORNER_INDEX[_a, _b, _c] = _q
32
+
33
+
34
+ @njit(cache=True, inline="always")
35
+ def _node_grid(nx, ny, nz, periodic):
36
+ if periodic:
37
+ return nx, ny, nz
38
+ return nx + 1, ny + 1, nz + 1
39
+
40
+
41
+ @njit(cache=True)
42
+ def _elements_around(i, j, k, nx, ny, nz, periodic, active, out_elem, out_local):
43
+ """Active elements containing node (i, j, k) and the local corner of the node."""
44
+ count = 0
45
+ for a in range(2):
46
+ ei = i - a
47
+ if periodic:
48
+ ei %= nx
49
+ elif ei < 0 or ei >= nx:
50
+ continue
51
+ for b in range(2):
52
+ ej = j - b
53
+ if periodic:
54
+ ej %= ny
55
+ elif ej < 0 or ej >= ny:
56
+ continue
57
+ for c in range(2):
58
+ ek = k - c
59
+ if periodic:
60
+ ek %= nz
61
+ elif ek < 0 or ek >= nz:
62
+ continue
63
+ e = (ei * ny + ej) * nz + ek
64
+ if active[e]:
65
+ out_elem[count] = e
66
+ out_local[count] = CORNER_INDEX[a, b, c]
67
+ count += 1
68
+ return count
69
+
70
+
71
+ @njit(parallel=True, cache=True)
72
+ def element_load(nx, ny, nz, periodic, mat, dof, nd, loads, n_free):
73
+ """Right-hand side -sum_e loads[mat_e] restricted to the free dofs (``loads[m]``: the
74
+ element load vector of material ``m``; ``mat`` -1: no stiffness)."""
75
+ gx, gy, gz = _node_grid(nx, ny, nz, periodic)
76
+ active = mat >= 0
77
+ f = np.zeros(n_free, dtype=np.float64)
78
+ for n in prange(gx * gy * gz):
79
+ has_free = False
80
+ for c in range(nd):
81
+ if dof[n * nd + c] >= 0:
82
+ has_free = True
83
+ if not has_free:
84
+ continue
85
+ k = n % gz
86
+ j = (n // gz) % gy
87
+ i = n // (gy * gz)
88
+ elem = np.empty(8, dtype=np.int64)
89
+ local = np.empty(8, dtype=np.int64)
90
+ n_el = _elements_around(i, j, k, nx, ny, nz, periodic, active, elem, local)
91
+ for t in range(n_el):
92
+ e = elem[t]
93
+ ln = local[t]
94
+ for c in range(nd):
95
+ r = dof[n * nd + c]
96
+ if r >= 0:
97
+ f[r] -= loads[mat[e], ln * nd + c]
98
+ return f
99
+
100
+
101
+ @njit(cache=True, inline="always")
102
+ def _element_nodes(e, nx, ny, nz, periodic, out):
103
+ k = e % nz
104
+ j = (e // nz) % ny
105
+ i = e // (ny * nz)
106
+ gx, gy, gz = _node_grid(nx, ny, nz, periodic)
107
+ for q in range(8):
108
+ ii = i + CORNERS[q, 0]
109
+ jj = j + CORNERS[q, 1]
110
+ kk = k + CORNERS[q, 2]
111
+ if periodic:
112
+ ii %= gx
113
+ jj %= gy
114
+ kk %= gz
115
+ out[q] = (ii * gy + jj) * gz + kk
116
+
117
+
118
+ N_ELASTIC = 27 # eps(6) sig(6) energy(1) eps^2(6) sig^2(6) energy^2(1) volume(1)
119
+ N_THERMAL = 15 # grad(3) flux(3) dissipation(1) grad^2(3) flux^2(3) dissipation^2(1) volume(1)
120
+
121
+
122
+ @njit(cache=True, inline="always")
123
+ def _stress(d, eps, sig):
124
+ for r in range(6):
125
+ s = 0.0
126
+ for c in range(6):
127
+ s += d[r, c] * eps[c]
128
+ sig[r] = s
129
+
130
+
131
+ @njit(parallel=True, cache=True)
132
+ def elastic_sums(nx, ny, nz, periodic, mat, stat, n_stat, dmat, eig, dt, w, u_loc, b_gauss, wt):
133
+ """Integrals over each phase (``stat``) of strains, stresses, energy and squares.
134
+
135
+ ``w`` holds the nodal fluctuation (3 per node), ``u_loc`` the affine part at the
136
+ corners of an element (identical for all elements), ``dmat[mat_e]`` the elasticity
137
+ matrix of element ``e`` (-1: void) and ``eig[mat_e]`` its thermal strain for a unit
138
+ change of temperature (``dt``: the change). Strains are tensor components, stresses
139
+ ``D (eps - dt eig)``.
140
+ """
141
+ n_el = nx * ny * nz
142
+ n_chunks = min(n_el, 256)
143
+ partial = np.zeros((n_chunks, n_stat, N_ELASTIC))
144
+ for ch in prange(n_chunks):
145
+ nodes = np.empty(8, dtype=np.int64)
146
+ ue = np.empty(24)
147
+ eps = np.empty(6)
148
+ mech = np.empty(6)
149
+ sig = np.empty(6)
150
+ start = ch * n_el // n_chunks
151
+ stop = (ch + 1) * n_el // n_chunks
152
+ for e in range(start, stop):
153
+ if mat[e] < 0:
154
+ continue
155
+ _element_nodes(e, nx, ny, nz, periodic, nodes)
156
+ for q in range(8):
157
+ for c in range(3):
158
+ ue[3 * q + c] = u_loc[3 * q + c] + w[3 * nodes[q] + c]
159
+ d = dmat[mat[e]]
160
+ acc = partial[ch, stat[e]]
161
+ for gp in range(8):
162
+ for r in range(6):
163
+ s = 0.0
164
+ for col in range(24):
165
+ s += b_gauss[gp, r, col] * ue[col]
166
+ eps[r] = s
167
+ mech[r] = s - dt * eig[mat[e], r]
168
+ _stress(d, mech, sig)
169
+ dens = 0.0
170
+ for r in range(6):
171
+ dens += sig[r] * mech[r]
172
+ for r in range(6):
173
+ et = eps[r] if r < 3 else 0.5 * eps[r]
174
+ acc[r] += wt * et
175
+ acc[6 + r] += wt * sig[r]
176
+ acc[13 + r] += wt * et * et
177
+ acc[19 + r] += wt * sig[r] * sig[r]
178
+ acc[12] += wt * dens
179
+ acc[25] += wt * dens * dens
180
+ acc[26] += wt
181
+ return partial.sum(axis=0)
182
+
183
+
184
+ @njit(parallel=True, cache=True)
185
+ def thermal_sums(nx, ny, nz, periodic, mat, stat, n_stat, kmat, t, t_loc, g_gauss, wt):
186
+ """Integrals over each phase of gradients, fluxes, dissipation and squares
187
+ (``kmat[mat_e]``: conductivity tensor of element ``e``)."""
188
+ n_el = nx * ny * nz
189
+ n_chunks = min(n_el, 256)
190
+ partial = np.zeros((n_chunks, n_stat, N_THERMAL))
191
+ for ch in prange(n_chunks):
192
+ nodes = np.empty(8, dtype=np.int64)
193
+ te = np.empty(8)
194
+ grad = np.empty(3)
195
+ start = ch * n_el // n_chunks
196
+ stop = (ch + 1) * n_el // n_chunks
197
+ for e in range(start, stop):
198
+ if mat[e] < 0:
199
+ continue
200
+ _element_nodes(e, nx, ny, nz, periodic, nodes)
201
+ for q in range(8):
202
+ te[q] = t_loc[q] + t[nodes[q]]
203
+ acc = partial[ch, stat[e]]
204
+ kk = kmat[mat[e]]
205
+ for gp in range(8):
206
+ for d in range(3):
207
+ s = 0.0
208
+ for q in range(8):
209
+ s += g_gauss[gp, d, q] * te[q]
210
+ grad[d] = s
211
+ dens = 0.0
212
+ for d in range(3):
213
+ flux = -(kk[d, 0] * grad[0] + kk[d, 1] * grad[1] + kk[d, 2] * grad[2])
214
+ dens -= flux * grad[d]
215
+ acc[d] += wt * grad[d]
216
+ acc[3 + d] += wt * flux
217
+ acc[7 + d] += wt * grad[d] * grad[d]
218
+ acc[10 + d] += wt * flux * flux
219
+ acc[6] += wt * dens
220
+ acc[13] += wt * dens * dens
221
+ acc[14] += wt
222
+ return partial.sum(axis=0)
223
+
224
+
225
+ @njit(parallel=True, cache=True)
226
+ def elastic_fields(nx, ny, nz, periodic, mat, dmat, eig, dt, w, u_loc, b_mean, b_gauss):
227
+ """Element means of the strain (tensor components), stress and energy density
228
+ ``sigma : (eps - dt eig)`` (NaN where inactive)."""
229
+ n_el = nx * ny * nz
230
+ strain = np.full((n_el, 6), np.nan)
231
+ stress = np.full((n_el, 6), np.nan)
232
+ energy = np.full(n_el, np.nan)
233
+ for e in prange(n_el):
234
+ if mat[e] < 0:
235
+ continue
236
+ nodes = np.empty(8, dtype=np.int64)
237
+ _element_nodes(e, nx, ny, nz, periodic, nodes)
238
+ ue = np.empty(24)
239
+ for q in range(8):
240
+ for c in range(3):
241
+ ue[3 * q + c] = u_loc[3 * q + c] + w[3 * nodes[q] + c]
242
+ d = dmat[mat[e]]
243
+ eps = np.zeros(6)
244
+ mech = np.empty(6)
245
+ sig = np.empty(6)
246
+ for r in range(6):
247
+ s = 0.0
248
+ for col in range(24):
249
+ s += b_mean[r, col] * ue[col]
250
+ eps[r] = s
251
+ mech[r] = s - dt * eig[mat[e], r]
252
+ _stress(d, mech, sig)
253
+ for r in range(6):
254
+ stress[e, r] = sig[r]
255
+ strain[e, r] = eps[r] if r < 3 else 0.5 * eps[r]
256
+ dens = 0.0
257
+ for gp in range(8):
258
+ for r in range(6):
259
+ s = 0.0
260
+ for col in range(24):
261
+ s += b_gauss[gp, r, col] * ue[col]
262
+ mech[r] = s - dt * eig[mat[e], r]
263
+ _stress(d, mech, sig)
264
+ for r in range(6):
265
+ dens += sig[r] * mech[r]
266
+ energy[e] = dens / 8.0
267
+ return strain, stress, energy
268
+
269
+
270
+ @njit(parallel=True, cache=True)
271
+ def thermal_fields(nx, ny, nz, periodic, mat, kmat, t, t_loc, g_mean, g_gauss):
272
+ """Element means of the temperature gradient, flux and dissipation ``grad T . K grad
273
+ T`` (NaN where inactive)."""
274
+ n_el = nx * ny * nz
275
+ grad = np.full((n_el, 3), np.nan)
276
+ flux = np.full((n_el, 3), np.nan)
277
+ dissipation = np.full(n_el, np.nan)
278
+ for e in prange(n_el):
279
+ if mat[e] < 0:
280
+ continue
281
+ nodes = np.empty(8, dtype=np.int64)
282
+ _element_nodes(e, nx, ny, nz, periodic, nodes)
283
+ te = np.empty(8)
284
+ for q in range(8):
285
+ te[q] = t_loc[q] + t[nodes[q]]
286
+ kk = kmat[mat[e]]
287
+ g = np.empty(3)
288
+ for d in range(3):
289
+ s = 0.0
290
+ for q in range(8):
291
+ s += g_mean[d, q] * te[q]
292
+ g[d] = s
293
+ grad[e, d] = s
294
+ for d in range(3):
295
+ flux[e, d] = -(kk[d, 0] * g[0] + kk[d, 1] * g[1] + kk[d, 2] * g[2])
296
+ dens = 0.0
297
+ for gp in range(8):
298
+ for d in range(3):
299
+ s = 0.0
300
+ for q in range(8):
301
+ s += g_gauss[gp, d, q] * te[q]
302
+ g[d] = s
303
+ for d in range(3):
304
+ dens += g[d] * (kk[d, 0] * g[0] + kk[d, 1] * g[1] + kk[d, 2] * g[2])
305
+ dissipation[e] = dens / 8.0
306
+ return grad, flux, dissipation
307
+
308
+
309
+ # -- loading paths: the Gauss points of the voxels -----------------------------------------
310
+ #
311
+ # ``bbar``: the volumetric part of the measures is averaged over each voxel, which keeps
312
+ # the trilinear elements from locking under isochoric (plastic) flow. In small strains
313
+ # (B-bar, Hughes 1980) the trace of the strain at each point is replaced by its mean over
314
+ # the voxel; in finite strains (F-bar, de Souza Neto et al. 1996) the gradient of each
315
+ # point is scaled, ``F_bar = (J_bar / J)^(1/3) F``, to the geometric mean ``J_bar`` of the
316
+ # Jacobians of the voxel. The forces are the derivatives of the energy of these measures,
317
+ # so that the tangent stays symmetric: the stresses of the points are replaced by their
318
+ # conjugates (:func:`voxel_effective`), ``s + p_bar 1`` in small strains (the deviator of
319
+ # the point, the mean pressure of the voxel) and ``alpha P + (tau_bar - tau) F^-T`` in
320
+ # finite strains (``alpha = (J_bar / J)^(1/3)``, ``tau = alpha P : F / 3``).
321
+
322
+
323
+ @njit(cache=True, inline="always")
324
+ def _gradient(ue, g_gauss, g, h):
325
+ """Displacement gradient (3 x 3) at Gauss point ``g`` of a voxel whose corners move
326
+ by ``ue`` (8 x 3), into ``h``."""
327
+ for i in range(3):
328
+ for j in range(3):
329
+ h[i, j] = 0.0
330
+ for a in range(8):
331
+ gx = g_gauss[g, 0, a]
332
+ gy = g_gauss[g, 1, a]
333
+ gz = g_gauss[g, 2, a]
334
+ for i in range(3):
335
+ u = ue[a, i]
336
+ h[i, 0] += u * gx
337
+ h[i, 1] += u * gy
338
+ h[i, 2] += u * gz
339
+
340
+
341
+ @njit(cache=True, inline="always")
342
+ def _det3(f):
343
+ return (
344
+ f[0, 0] * (f[1, 1] * f[2, 2] - f[1, 2] * f[2, 1])
345
+ - f[0, 1] * (f[1, 0] * f[2, 2] - f[1, 2] * f[2, 0])
346
+ + f[0, 2] * (f[1, 0] * f[2, 1] - f[1, 1] * f[2, 0])
347
+ )
348
+
349
+
350
+ @njit(cache=True, inline="always")
351
+ def _inverse_transpose(f, det, out):
352
+ """``F^-T`` (the cofactor matrix over the determinant)."""
353
+ inv = 1.0 / det
354
+ out[0, 0] = (f[1, 1] * f[2, 2] - f[1, 2] * f[2, 1]) * inv
355
+ out[0, 1] = (f[1, 2] * f[2, 0] - f[1, 0] * f[2, 2]) * inv
356
+ out[0, 2] = (f[1, 0] * f[2, 1] - f[1, 1] * f[2, 0]) * inv
357
+ out[1, 0] = (f[0, 2] * f[2, 1] - f[0, 1] * f[2, 2]) * inv
358
+ out[1, 1] = (f[0, 0] * f[2, 2] - f[0, 2] * f[2, 0]) * inv
359
+ out[1, 2] = (f[0, 1] * f[2, 0] - f[0, 0] * f[2, 1]) * inv
360
+ out[2, 0] = (f[0, 1] * f[1, 2] - f[0, 2] * f[1, 1]) * inv
361
+ out[2, 1] = (f[0, 2] * f[1, 0] - f[0, 0] * f[1, 2]) * inv
362
+ out[2, 2] = (f[0, 0] * f[1, 1] - f[0, 1] * f[1, 0]) * inv
363
+
364
+
365
+ @njit(cache=True, inline="always")
366
+ def _engineering(h, out):
367
+ """Engineering strains (Voigt order) of the symmetric part of a gradient ``h``."""
368
+ out[0] = h[0, 0]
369
+ out[1] = h[1, 1]
370
+ out[2] = h[2, 2]
371
+ out[3] = h[1, 2] + h[2, 1]
372
+ out[4] = h[2, 0] + h[0, 2]
373
+ out[5] = h[0, 1] + h[1, 0]
374
+
375
+
376
+ @njit(parallel=True, cache=True)
377
+ def voxel_measures(nx, ny, nz, elem, w, macro, g_gauss, finite, bbar, out, base):
378
+ """Measures of strain at the Gauss points of the voxels ``elem`` of the periodic grid
379
+ (into ``out``, points x 6 or 9): the gradient of the nodal fluctuation ``w`` (3 per
380
+ node) plus the macroscopic gradient ``macro`` (9, row by row; symmetric in small
381
+ strains). With ``bbar``, the measures of B-bar or F-bar (see above), and in finite
382
+ strains the gradients themselves into ``base``. A voxel turned inside out gets NaN
383
+ measures (its behaviours then fail)."""
384
+ n_el = elem.size
385
+ n_chunks = min(max(n_el, 1), 512)
386
+ for ch in prange(n_chunks):
387
+ nodes = np.empty(8, dtype=np.int64)
388
+ ue = np.empty((8, 3))
389
+ hs = np.empty((8, 3, 3))
390
+ m = np.empty(6)
391
+ log_j = np.empty(8)
392
+ for t in range(ch * n_el // n_chunks, (ch + 1) * n_el // n_chunks):
393
+ _element_nodes(elem[t], nx, ny, nz, True, nodes)
394
+ for a in range(8):
395
+ for c in range(3):
396
+ ue[a, c] = w[3 * nodes[a] + c]
397
+ for g in range(8):
398
+ _gradient(ue, g_gauss, g, hs[g])
399
+ for i in range(3):
400
+ for j in range(3):
401
+ hs[g, i, j] += macro[3 * i + j]
402
+ if not finite:
403
+ mean = 0.0
404
+ for g in range(8):
405
+ mean += hs[g, 0, 0] + hs[g, 1, 1] + hs[g, 2, 2]
406
+ mean /= 8.0
407
+ for g in range(8):
408
+ _engineering(hs[g], m)
409
+ if bbar:
410
+ shift = (mean - m[0] - m[1] - m[2]) / 3.0
411
+ for r in range(3):
412
+ m[r] += shift
413
+ p = 8 * t + g
414
+ for r in range(6):
415
+ out[p, r] = m[r]
416
+ continue
417
+ valid = True
418
+ mean = 0.0
419
+ for g in range(8):
420
+ for i in range(3):
421
+ hs[g, i, i] += 1.0 # F
422
+ det = _det3(hs[g])
423
+ if not det > 0.0:
424
+ valid = False
425
+ break
426
+ log_j[g] = math.log(det)
427
+ mean += log_j[g] / 8.0
428
+ for g in range(8):
429
+ p = 8 * t + g
430
+ if not valid:
431
+ for r in range(9):
432
+ out[p, r] = np.nan
433
+ if bbar:
434
+ base[p, r] = np.nan
435
+ continue
436
+ alpha = math.exp((mean - log_j[g]) / 3.0) if bbar else 1.0
437
+ for i in range(3):
438
+ for j in range(3):
439
+ f = hs[g, i, j]
440
+ out[p, 3 * i + j] = alpha * f - (1.0 if i == j else 0.0)
441
+ if bbar:
442
+ base[p, 3 * i + j] = f - (1.0 if i == j else 0.0)
443
+
444
+
445
+ @njit(parallel=True, cache=True)
446
+ def voxel_effective(n_el, finite, s, base):
447
+ """The stresses of the points of ``n_el`` voxels replaced, in place, by those
448
+ conjugate to the measures of B-bar or F-bar (see above): ``s`` holds the stresses of
449
+ the behaviours (points x 6, or x 9 first Piola-Kirchhoff stresses), ``base`` the
450
+ gradients of the points in finite strains."""
451
+ n_chunks = min(max(n_el, 1), 512)
452
+ for ch in prange(n_chunks):
453
+ f = np.empty((3, 3))
454
+ fit = np.empty((3, 3))
455
+ alpha = np.empty(8)
456
+ tau = np.empty(8)
457
+ for t in range(ch * n_el // n_chunks, (ch + 1) * n_el // n_chunks):
458
+ if not finite:
459
+ mean = 0.0
460
+ for g in range(8):
461
+ p = 8 * t + g
462
+ mean += (s[p, 0] + s[p, 1] + s[p, 2]) / 24.0
463
+ for g in range(8):
464
+ p = 8 * t + g
465
+ shift = mean - (s[p, 0] + s[p, 1] + s[p, 2]) / 3.0
466
+ for r in range(3):
467
+ s[p, r] += shift
468
+ continue
469
+ mean_log = 0.0
470
+ for g in range(8):
471
+ p = 8 * t + g
472
+ for i in range(3):
473
+ for j in range(3):
474
+ f[i, j] = base[p, 3 * i + j] + (1.0 if i == j else 0.0)
475
+ alpha[g] = math.log(_det3(f))
476
+ mean_log += alpha[g] / 8.0
477
+ mean_tau = 0.0
478
+ for g in range(8):
479
+ p = 8 * t + g
480
+ alpha[g] = math.exp((mean_log - alpha[g]) / 3.0)
481
+ pf = 0.0
482
+ for i in range(3):
483
+ for j in range(3):
484
+ pf += s[p, 3 * i + j] * (base[p, 3 * i + j] + (1.0 if i == j else 0.0))
485
+ tau[g] = alpha[g] * pf / 3.0
486
+ mean_tau += tau[g] / 8.0
487
+ for g in range(8):
488
+ p = 8 * t + g
489
+ for i in range(3):
490
+ for j in range(3):
491
+ f[i, j] = base[p, 3 * i + j] + (1.0 if i == j else 0.0)
492
+ _inverse_transpose(f, _det3(f), fit)
493
+ for i in range(3):
494
+ for j in range(3):
495
+ k = 3 * i + j
496
+ s[p, k] = alpha[g] * s[p, k] + (mean_tau - tau[g]) * fit[i, j]
497
+
498
+
499
+ @njit(parallel=True, cache=True)
500
+ def voxel_tangent_stress(nx, ny, nz, elem, v, direction, tangent, s, base, g_gauss, finite,
501
+ bbar, out): # fmt: skip
502
+ """Increments of the stresses of the points (of their conjugates with ``bbar``) for
503
+ the nodal displacements ``v`` (3 per node) plus the uniform gradient ``direction`` (9,
504
+ row by row; symmetric in small strains), with the tangents ``tangent`` of the points
505
+ (points x n x n), into ``out`` (points x n). In finite strains with ``bbar``, ``s``
506
+ holds the first Piola-Kirchhoff stresses of the behaviours and ``base`` the gradients
507
+ of the points (the derivatives of the F-bar measures depend on them)."""
508
+ n_el = elem.size
509
+ n_chunks = min(max(n_el, 1), 512)
510
+ for ch in prange(n_chunks):
511
+ nodes = np.empty(8, dtype=np.int64)
512
+ ue = np.empty((8, 3))
513
+ dh = np.empty((8, 3, 3))
514
+ de = np.empty(9)
515
+ ds = np.empty((8, 9))
516
+ f = np.empty((3, 3))
517
+ fit = np.empty((3, 3))
518
+ alpha = np.empty(8)
519
+ dalpha = np.empty(8)
520
+ tau = np.empty(8)
521
+ dtau = np.empty(8)
522
+ for t in range(ch * n_el // n_chunks, (ch + 1) * n_el // n_chunks):
523
+ _element_nodes(elem[t], nx, ny, nz, True, nodes)
524
+ for a in range(8):
525
+ for c in range(3):
526
+ ue[a, c] = v[3 * nodes[a] + c]
527
+ for g in range(8):
528
+ _gradient(ue, g_gauss, g, dh[g])
529
+ for i in range(3):
530
+ for j in range(3):
531
+ dh[g, i, j] += direction[3 * i + j]
532
+ if not finite:
533
+ mean = 0.0
534
+ for g in range(8):
535
+ mean += (dh[g, 0, 0] + dh[g, 1, 1] + dh[g, 2, 2]) / 8.0
536
+ mean_p = 0.0
537
+ for g in range(8):
538
+ _engineering(dh[g], de)
539
+ if bbar:
540
+ shift = (mean - de[0] - de[1] - de[2]) / 3.0
541
+ for r in range(3):
542
+ de[r] += shift
543
+ p = 8 * t + g
544
+ for r in range(6):
545
+ acc = 0.0
546
+ for c in range(6):
547
+ acc += tangent[p, r, c] * de[c]
548
+ ds[g, r] = acc
549
+ mean_p += (ds[g, 0] + ds[g, 1] + ds[g, 2]) / 24.0
550
+ for g in range(8):
551
+ p = 8 * t + g
552
+ shift = mean_p - (ds[g, 0] + ds[g, 1] + ds[g, 2]) / 3.0 if bbar else 0.0
553
+ for r in range(6):
554
+ out[p, r] = ds[g, r] + (shift if r < 3 else 0.0)
555
+ continue
556
+ if not bbar:
557
+ for g in range(8):
558
+ p = 8 * t + g
559
+ for r in range(9):
560
+ acc = 0.0
561
+ for c in range(9):
562
+ acc += tangent[p, r, c] * dh[g, c // 3, c % 3]
563
+ out[p, r] = acc
564
+ continue
565
+ # F-bar: F_bar = alpha F, alpha = (J_bar / J)^(1/3)
566
+ mean_log = 0.0
567
+ mean_c = 0.0
568
+ for g in range(8):
569
+ p = 8 * t + g
570
+ for i in range(3):
571
+ for j in range(3):
572
+ f[i, j] = base[p, 3 * i + j] + (1.0 if i == j else 0.0)
573
+ det = _det3(f)
574
+ _inverse_transpose(f, det, fit)
575
+ alpha[g] = math.log(det)
576
+ mean_log += alpha[g] / 8.0
577
+ c = 0.0 # d ln J = F^-T : dF
578
+ for i in range(3):
579
+ for j in range(3):
580
+ c += fit[i, j] * dh[g, i, j]
581
+ dalpha[g] = c
582
+ mean_c += c / 8.0
583
+ mean_tau = 0.0
584
+ mean_dtau = 0.0
585
+ for g in range(8):
586
+ p = 8 * t + g
587
+ a = math.exp((mean_log - alpha[g]) / 3.0)
588
+ alpha[g] = a
589
+ da = a * (mean_c - dalpha[g]) / 3.0
590
+ dalpha[g] = da
591
+ # dF_bar = alpha dF + dalpha F, dP = A : dF_bar
592
+ for i in range(3):
593
+ for j in range(3):
594
+ fij = base[p, 3 * i + j] + (1.0 if i == j else 0.0)
595
+ de[3 * i + j] = a * dh[g, i, j] + da * fij
596
+ pf = 0.0
597
+ dpf = 0.0
598
+ pdf = 0.0
599
+ for r in range(9):
600
+ acc = 0.0
601
+ for c in range(9):
602
+ acc += tangent[p, r, c] * de[c]
603
+ ds[g, r] = acc
604
+ fr = base[p, r] + (1.0 if r % 4 == 0 else 0.0)
605
+ pf += s[p, r] * fr
606
+ dpf += acc * fr
607
+ pdf += s[p, r] * dh[g, r // 3, r % 3]
608
+ tau[g] = a * pf / 3.0
609
+ dtau[g] = (da * pf + a * (dpf + pdf)) / 3.0
610
+ mean_tau += tau[g] / 8.0
611
+ mean_dtau += dtau[g] / 8.0
612
+ for g in range(8):
613
+ p = 8 * t + g
614
+ for i in range(3):
615
+ for j in range(3):
616
+ f[i, j] = base[p, 3 * i + j] + (1.0 if i == j else 0.0)
617
+ _inverse_transpose(f, _det3(f), fit)
618
+ a = alpha[g]
619
+ da = dalpha[g]
620
+ ct = mean_tau - tau[g]
621
+ cdt = mean_dtau - dtau[g]
622
+ for i in range(3):
623
+ for j in range(3):
624
+ # d(F^-T)_ij = -(F^-T dF^T F^-T)_ij
625
+ dfit = 0.0
626
+ for k in range(3):
627
+ for m in range(3):
628
+ dfit -= fit[i, k] * dh[g, m, k] * fit[m, j]
629
+ r = 3 * i + j
630
+ out[p, r] = da * s[p, r] + a * ds[g, r] + cdt * fit[i, j] + ct * dfit
631
+
632
+
633
+ @njit(parallel=True, cache=True)
634
+ def voxel_forces(nx, ny, nz, slot, s, weight, g_gauss, finite, out):
635
+ """Nodal forces of the stresses ``s`` at the Gauss points (``int B^T s``; in finite
636
+ strains ``int P . grad N`` of the first Piola-Kirchhoff stresses), into ``out`` (3 per
637
+ node of the periodic grid; ``weight``: the weight of a Gauss point). Loops over nodes,
638
+ so no atomics are needed. Returns the norm of the contributions of the points before
639
+ they cancel."""
640
+ n = nx * ny * nz
641
+ norm2 = 0.0
642
+ for node in prange(n):
643
+ k = node % nz
644
+ j = (node // nz) % ny
645
+ i = node // (ny * nz)
646
+ fx = 0.0
647
+ fy = 0.0
648
+ fz = 0.0
649
+ sq = 0.0
650
+ for a in range(2):
651
+ ei = i - a if i - a >= 0 else nx - 1
652
+ for b in range(2):
653
+ ej = j - b if j - b >= 0 else ny - 1
654
+ for c in range(2):
655
+ ek = k - c if k - c >= 0 else nz - 1
656
+ t = slot[(ei * ny + ej) * nz + ek]
657
+ if t < 0:
658
+ continue
659
+ ln = CORNER_INDEX[a, b, c]
660
+ for g in range(8):
661
+ p = 8 * t + g
662
+ gx = weight * g_gauss[g, 0, ln]
663
+ gy = weight * g_gauss[g, 1, ln]
664
+ gz = weight * g_gauss[g, 2, ln]
665
+ if finite:
666
+ cx = s[p, 0] * gx + s[p, 1] * gy + s[p, 2] * gz
667
+ cy = s[p, 3] * gx + s[p, 4] * gy + s[p, 5] * gz
668
+ cz = s[p, 6] * gx + s[p, 7] * gy + s[p, 8] * gz
669
+ else:
670
+ cx = gx * s[p, 0] + gz * s[p, 4] + gy * s[p, 5]
671
+ cy = gy * s[p, 1] + gz * s[p, 3] + gx * s[p, 5]
672
+ cz = gz * s[p, 2] + gy * s[p, 3] + gx * s[p, 4]
673
+ fx += cx
674
+ fy += cy
675
+ fz += cz
676
+ sq += cx * cx + cy * cy + cz * cz
677
+ out[3 * node] = fx
678
+ out[3 * node + 1] = fy
679
+ out[3 * node + 2] = fz
680
+ norm2 += sq
681
+ return math.sqrt(norm2)
682
+
683
+
684
+ @njit(parallel=True, cache=True)
685
+ def voxel_centres(nx, ny, nz, w, out):
686
+ """Mean of the nodal fluctuation ``w`` (3 per node) over the corners of every voxel:
687
+ its value at the centre (voxels x 3)."""
688
+ for e in prange(nx * ny * nz):
689
+ nodes = np.empty(8, dtype=np.int64)
690
+ _element_nodes(e, nx, ny, nz, True, nodes)
691
+ for c in range(3):
692
+ acc = 0.0
693
+ for a in range(8):
694
+ acc += w[3 * nodes[a] + c]
695
+ out[e, c] = acc / 8.0
696
+
697
+
698
+ # -- the Fourier preconditioner ------------------------------------------------------------
699
+
700
+
701
+ @njit(parallel=True, cache=True)
702
+ def fourier_inverse(nx, ny, nz, blocks, out):
703
+ """Inverse of the symbol of a homogeneous operator at the frequencies of the real FFT
704
+ of the periodic grid, into ``out`` (nx x ny x (nz // 2 + 1) x nd x nd, complex): the
705
+ stencil ``blocks`` (3 x 3 x 3 x nd x nd, node offsets -1, 0, 1) summed with its
706
+ phases, then inverted (nd = 1 or 3, by cofactors); zero at the zero frequency (the
707
+ fluctuation has a zero mean)."""
708
+ nzh = nz // 2 + 1
709
+ nd = blocks.shape[3]
710
+ n_freq = nx * ny * nzh
711
+ n_chunks = min(max(n_freq, 1), 512)
712
+ for ch in prange(n_chunks):
713
+ s = np.empty((3, 3), dtype=np.complex128)
714
+ px = np.empty(3, dtype=np.complex128)
715
+ py = np.empty(3, dtype=np.complex128)
716
+ pz = np.empty(3, dtype=np.complex128)
717
+ for f in range(ch * n_freq // n_chunks, (ch + 1) * n_freq // n_chunks):
718
+ fz = f % nzh
719
+ fy = (f // nzh) % ny
720
+ fx = f // (ny * nzh)
721
+ if fx == 0 and fy == 0 and fz == 0:
722
+ for i in range(nd):
723
+ for j in range(nd):
724
+ out[fx, fy, fz, i, j] = 0.0
725
+ continue
726
+ tx = 2.0 * math.pi * fx / nx
727
+ ty = 2.0 * math.pi * fy / ny
728
+ tz = 2.0 * math.pi * fz / nz
729
+ for a in range(3):
730
+ px[a] = complex(math.cos((a - 1) * tx), math.sin((a - 1) * tx))
731
+ py[a] = complex(math.cos((a - 1) * ty), math.sin((a - 1) * ty))
732
+ pz[a] = complex(math.cos((a - 1) * tz), math.sin((a - 1) * tz))
733
+ for i in range(nd):
734
+ for j in range(nd):
735
+ s[i, j] = 0.0
736
+ for a in range(3):
737
+ for b in range(3):
738
+ pab = px[a] * py[b]
739
+ for c in range(3):
740
+ phase = pab * pz[c]
741
+ for i in range(nd):
742
+ for j in range(nd):
743
+ s[i, j] += phase * blocks[a, b, c, i, j]
744
+ if nd == 1:
745
+ out[fx, fy, fz, 0, 0] = 1.0 / s[0, 0]
746
+ continue
747
+ c00 = s[1, 1] * s[2, 2] - s[1, 2] * s[2, 1]
748
+ c01 = s[1, 2] * s[2, 0] - s[1, 0] * s[2, 2]
749
+ c02 = s[1, 0] * s[2, 1] - s[1, 1] * s[2, 0]
750
+ inv = 1.0 / (s[0, 0] * c00 + s[0, 1] * c01 + s[0, 2] * c02)
751
+ o = out[fx, fy, fz]
752
+ o[0, 0] = c00 * inv
753
+ o[0, 1] = (s[0, 2] * s[2, 1] - s[0, 1] * s[2, 2]) * inv
754
+ o[0, 2] = (s[0, 1] * s[1, 2] - s[0, 2] * s[1, 1]) * inv
755
+ o[1, 0] = c01 * inv
756
+ o[1, 1] = (s[0, 0] * s[2, 2] - s[0, 2] * s[2, 0]) * inv
757
+ o[1, 2] = (s[0, 2] * s[1, 0] - s[0, 0] * s[1, 2]) * inv
758
+ o[2, 0] = c02 * inv
759
+ o[2, 1] = (s[0, 1] * s[2, 0] - s[0, 0] * s[2, 1]) * inv
760
+ o[2, 2] = (s[0, 0] * s[1, 1] - s[0, 1] * s[1, 0]) * inv