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.
- morphogen/__init__.py +60 -0
- morphogen/__main__.py +7 -0
- morphogen/_platform.py +55 -0
- morphogen/_public.py +153 -0
- morphogen/_utils.py +80 -0
- morphogen/_version.py +1 -0
- morphogen/analysis/__init__.py +174 -0
- morphogen/analysis/anisotropy.py +135 -0
- morphogen/analysis/basic.py +38 -0
- morphogen/analysis/cells.py +54 -0
- morphogen/analysis/connectivity.py +156 -0
- morphogen/analysis/correlation.py +195 -0
- morphogen/analysis/covariance.py +80 -0
- morphogen/analysis/fractal.py +54 -0
- morphogen/analysis/measures.py +188 -0
- morphogen/analysis/morphology.py +265 -0
- morphogen/analysis/particles.py +160 -0
- morphogen/analysis/percolation.py +2034 -0
- morphogen/analysis/power_laws.py +143 -0
- morphogen/analysis/random_sets.py +351 -0
- morphogen/analysis/stereology.py +289 -0
- morphogen/analysis/surface.py +107 -0
- morphogen/analysis/topology.py +92 -0
- morphogen/automata/__init__.py +74 -0
- morphogen/automata/engine.py +211 -0
- morphogen/automata/growth.py +686 -0
- morphogen/automata/lattice.py +143 -0
- morphogen/automata/reaction.py +255 -0
- morphogen/automata/rules.py +353 -0
- morphogen/automata/tilings.py +1715 -0
- morphogen/cli.py +3526 -0
- morphogen/config.py +2167 -0
- morphogen/contact/__init__.py +113 -0
- morphogen/contact/experiments.py +368 -0
- morphogen/contact/hertz.py +104 -0
- morphogen/contact/kernels.py +827 -0
- morphogen/contact/periodic.py +278 -0
- morphogen/contact/problem.py +578 -0
- morphogen/contact/surfaces.py +310 -0
- morphogen/coupling/__init__.py +43 -0
- morphogen/coupling/ageing.py +143 -0
- morphogen/coupling/diffusion.py +226 -0
- morphogen/coupling/fenicsx.py +484 -0
- morphogen/coupling/oxidation.py +447 -0
- morphogen/coupling/plates.py +247 -0
- morphogen/design/__init__.py +51 -0
- morphogen/design/csp.py +616 -0
- morphogen/design/frames.py +277 -0
- morphogen/design/search.py +287 -0
- morphogen/design/tiles.py +336 -0
- morphogen/distributions.py +312 -0
- morphogen/domain.py +203 -0
- morphogen/dynamics/__init__.py +179 -0
- morphogen/dynamics/model.py +638 -0
- morphogen/dynamics/openradioss.py +643 -0
- morphogen/dynamics/results.py +515 -0
- morphogen/fracture/__init__.py +170 -0
- morphogen/fracture/bulk.py +349 -0
- morphogen/fracture/cohesive.py +1614 -0
- morphogen/fracture/ductile.py +557 -0
- morphogen/fracture/lefm.py +1168 -0
- morphogen/fracture/mixed.py +1178 -0
- morphogen/fracture/periodic.py +733 -0
- morphogen/fracture/phasefield.py +714 -0
- morphogen/fracture/phasefield_finite.py +368 -0
- morphogen/fracture/surface.py +572 -0
- morphogen/fracture/toughness.py +566 -0
- morphogen/gpu.py +108 -0
- morphogen/gui/__init__.py +8 -0
- morphogen/gui/api.py +1927 -0
- morphogen/gui/desktop.py +387 -0
- morphogen/gui/icon.png +0 -0
- morphogen/gui/index.html +1909 -0
- morphogen/gui/server.py +229 -0
- morphogen/gui/tools/__init__.py +629 -0
- morphogen/gui/tools/compare.py +149 -0
- morphogen/gui/tools/contact.py +391 -0
- morphogen/gui/tools/couplings.py +1085 -0
- morphogen/gui/tools/crackfront.py +245 -0
- morphogen/gui/tools/design.py +224 -0
- morphogen/gui/tools/dynamics.py +824 -0
- morphogen/gui/tools/fracture.py +2586 -0
- morphogen/gui/tools/interop.py +1654 -0
- morphogen/gui/tools/learning.py +595 -0
- morphogen/gui/tools/mechanics.py +3881 -0
- morphogen/gui/tools/nonlocal_damage.py +1321 -0
- morphogen/gui/tools/phasefield.py +215 -0
- morphogen/gui/tools/physics.py +597 -0
- morphogen/gui/tools/stability.py +1756 -0
- morphogen/gui/tools/statistics.py +1408 -0
- morphogen/gui/viewer.js +1100 -0
- morphogen/homogenization/__init__.py +210 -0
- morphogen/homogenization/acoustics.py +566 -0
- morphogen/homogenization/amg.py +1005 -0
- morphogen/homogenization/apparent.py +1046 -0
- morphogen/homogenization/behaviours.cu +2285 -0
- morphogen/homogenization/behaviours.py +944 -0
- morphogen/homogenization/beremin.py +263 -0
- morphogen/homogenization/bifurcation.py +1085 -0
- morphogen/homogenization/bloch.py +507 -0
- morphogen/homogenization/bounds.py +226 -0
- morphogen/homogenization/core.py +458 -0
- morphogen/homogenization/coupled.py +1135 -0
- morphogen/homogenization/crystal.py +2967 -0
- morphogen/homogenization/dilatation.py +454 -0
- morphogen/homogenization/elements.py +157 -0
- morphogen/homogenization/fatigue.py +768 -0
- morphogen/homogenization/fe.py +1739 -0
- morphogen/homogenization/fenicsx.py +307 -0
- morphogen/homogenization/fft.py +499 -0
- morphogen/homogenization/fft_nonlinear.py +391 -0
- morphogen/homogenization/finite.py +613 -0
- morphogen/homogenization/flow.py +361 -0
- morphogen/homogenization/frames.py +1125 -0
- morphogen/homogenization/gpu.py +1241 -0
- morphogen/homogenization/harmonic.py +427 -0
- morphogen/homogenization/identification.py +579 -0
- morphogen/homogenization/kernels.py +760 -0
- morphogen/homogenization/laws.py +2061 -0
- morphogen/homogenization/materials.py +1080 -0
- morphogen/homogenization/mfront.py +589 -0
- morphogen/homogenization/networks.py +880 -0
- morphogen/homogenization/nonlinear.py +2612 -0
- morphogen/homogenization/ntfa.py +577 -0
- morphogen/homogenization/paths.py +476 -0
- morphogen/homogenization/photonic.py +467 -0
- morphogen/homogenization/poroelastic.py +212 -0
- morphogen/homogenization/porous.py +1120 -0
- morphogen/homogenization/postprocess.py +290 -0
- morphogen/homogenization/solvers.py +1140 -0
- morphogen/homogenization/tensors.py +240 -0
- morphogen/homogenization/voxel_fe.py +457 -0
- morphogen/homogenization/xfem.py +1016 -0
- morphogen/homogenization/zmat.py +1258 -0
- morphogen/iga/__init__.py +48 -0
- morphogen/iga/analysis.py +508 -0
- morphogen/iga/basis.py +214 -0
- morphogen/iga/geometry.py +501 -0
- morphogen/iga/homogenization.py +219 -0
- morphogen/iga/optimization.py +700 -0
- morphogen/iga/shapes.py +331 -0
- morphogen/image.py +128 -0
- morphogen/io/__init__.py +246 -0
- morphogen/io/amitex.py +741 -0
- morphogen/io/ebsd.py +881 -0
- morphogen/io/export.py +196 -0
- morphogen/io/geometry.py +158 -0
- morphogen/io/medit.py +147 -0
- morphogen/io/meshes.py +844 -0
- morphogen/io/metaimage.py +97 -0
- morphogen/io/mfront.py +1104 -0
- morphogen/io/native.py +57 -0
- morphogen/io/radioss.py +764 -0
- morphogen/io/solvers.py +371 -0
- morphogen/io/tables.py +278 -0
- morphogen/io/tiff.py +66 -0
- morphogen/io/vtk.py +701 -0
- morphogen/io/xdmf.py +200 -0
- morphogen/kernels/__init__.py +5 -0
- morphogen/kernels/annealing.py +192 -0
- morphogen/kernels/cells.py +69 -0
- morphogen/kernels/collective.py +87 -0
- morphogen/kernels/force_biased.py +595 -0
- morphogen/kernels/implicit.py +145 -0
- morphogen/kernels/leaves.py +132 -0
- morphogen/kernels/packing.py +304 -0
- morphogen/kernels/raster.py +279 -0
- morphogen/kernels/rsa.py +371 -0
- morphogen/kernels/union.py +135 -0
- morphogen/kernels/watershed.py +190 -0
- morphogen/laser/__init__.py +234 -0
- morphogen/laser/architectures.py +375 -0
- morphogen/laser/bending.py +216 -0
- morphogen/laser/cells.py +1694 -0
- morphogen/laser/design.py +940 -0
- morphogen/laser/figures.py +227 -0
- morphogen/laser/heat.py +804 -0
- morphogen/laser/materials.py +691 -0
- morphogen/laser/mechanics.py +1670 -0
- morphogen/laser/metallurgy.py +615 -0
- morphogen/laser/patterns.py +828 -0
- morphogen/laser/periodic.py +392 -0
- morphogen/laser/process.py +362 -0
- morphogen/laser/run.py +534 -0
- morphogen/laser/solid.py +216 -0
- morphogen/laser/specimen.py +200 -0
- morphogen/laser/structures.py +708 -0
- morphogen/learning/__init__.py +81 -0
- morphogen/learning/behaviour.py +398 -0
- morphogen/learning/data.py +328 -0
- morphogen/learning/learned.cpp +668 -0
- morphogen/learning/logarithmic.py +202 -0
- morphogen/learning/mfront.py +409 -0
- morphogen/learning/networks.py +767 -0
- morphogen/learning/run.py +212 -0
- morphogen/learning/standard.py +814 -0
- morphogen/learning/training.py +235 -0
- morphogen/mesh/__init__.py +29 -0
- morphogen/mesh/conforming.py +1423 -0
- morphogen/mesh/curved.py +600 -0
- morphogen/mesh/exact.py +1717 -0
- morphogen/mesh/octree.py +350 -0
- morphogen/mesh/polycrystal.py +1041 -0
- morphogen/mesh/polyhedra.py +677 -0
- morphogen/mesh/regularize.py +570 -0
- morphogen/mesh/surface.py +97 -0
- morphogen/mesh/tetra.py +512 -0
- morphogen/mesh/voxel.py +124 -0
- morphogen/micromorphic/__init__.py +31 -0
- morphogen/micromorphic/damage.py +359 -0
- morphogen/micromorphic/ductile.py +534 -0
- morphogen/micromorphic/finite.py +220 -0
- morphogen/micromorphic/microfoam.py +787 -0
- morphogen/micromorphic/space.py +341 -0
- morphogen/models/__init__.py +195 -0
- morphogen/models/affine.py +384 -0
- morphogen/models/automata.py +592 -0
- morphogen/models/base.py +500 -0
- morphogen/models/boolean.py +310 -0
- morphogen/models/bouligand.py +331 -0
- morphogen/models/branching.py +241 -0
- morphogen/models/combination.py +430 -0
- morphogen/models/deadleaves.py +766 -0
- morphogen/models/design.py +289 -0
- morphogen/models/ebsd.py +206 -0
- morphogen/models/fibers.py +1476 -0
- morphogen/models/foam.py +594 -0
- morphogen/models/gaussian.py +314 -0
- morphogen/models/growth.py +459 -0
- morphogen/models/hardcore.py +317 -0
- morphogen/models/hierarchical.py +269 -0
- morphogen/models/honeycomb.py +749 -0
- morphogen/models/implicit.py +177 -0
- morphogen/models/laminate.py +470 -0
- morphogen/models/lattice.py +351 -0
- morphogen/models/lattice_field.py +345 -0
- morphogen/models/levelset_foam.py +563 -0
- morphogen/models/nacre.py +411 -0
- morphogen/models/nested.py +924 -0
- morphogen/models/osteons.py +374 -0
- morphogen/models/pieces.py +167 -0
- morphogen/models/planar.py +126 -0
- morphogen/models/reconstruction.py +203 -0
- morphogen/models/scale_rich.py +1409 -0
- morphogen/models/scale_rich_3d.py +1616 -0
- morphogen/models/tessellation.py +735 -0
- morphogen/models/tiling_field.py +302 -0
- morphogen/models/tpms.py +197 -0
- morphogen/models/transparent.py +406 -0
- morphogen/models/union.py +141 -0
- morphogen/optimization/__init__.py +43 -0
- morphogen/optimization/cell.py +295 -0
- morphogen/optimization/filters.py +115 -0
- morphogen/optimization/mma.py +226 -0
- morphogen/optimization/run.py +219 -0
- morphogen/optimization/topology.py +632 -0
- morphogen/orientations.py +568 -0
- morphogen/parallel.py +599 -0
- morphogen/pipeline.py +978 -0
- morphogen/primitives.py +188 -0
- morphogen/results.py +564 -0
- morphogen/rve/__init__.py +73 -0
- morphogen/rve/report.py +627 -0
- morphogen/rve/scaling.py +363 -0
- morphogen/rve/study.py +1054 -0
- morphogen/rve/windows.py +345 -0
- morphogen/seeding.py +51 -0
- morphogen/settings.py +118 -0
- morphogen/stopping.py +48 -0
- morphogen/symmetry.py +384 -0
- morphogen/templates/activator_inhibitor.yaml +22 -0
- morphogen/templates/aggregation.yaml +23 -0
- morphogen/templates/boolean_cylinders.yaml +20 -0
- morphogen/templates/boolean_spheres.yaml +22 -0
- morphogen/templates/bouligand.yaml +28 -0
- morphogen/templates/cellular_automaton.yaml +25 -0
- morphogen/templates/coated_spheres.yaml +26 -0
- morphogen/templates/contact_fibers.yaml +27 -0
- morphogen/templates/cox_nanocomposite.yaml +22 -0
- morphogen/templates/cross_linked_fibers.yaml +26 -0
- morphogen/templates/curved_arcs.yaml +28 -0
- morphogen/templates/curved_fibers.yaml +24 -0
- morphogen/templates/dead_leaves.yaml +22 -0
- morphogen/templates/delaunay_network.yaml +22 -0
- morphogen/templates/dense_packing.yaml +22 -0
- morphogen/templates/diffusion.yaml +33 -0
- morphogen/templates/ebsd.yaml +34 -0
- morphogen/templates/ebsd_map.ctf +3087 -0
- morphogen/templates/fracture_composite.yaml +42 -0
- morphogen/templates/fracture_rubber.yaml +47 -0
- morphogen/templates/gaussian_excursion.yaml +17 -0
- morphogen/templates/graded_lattice.yaml +24 -0
- morphogen/templates/hardcore_fibers.yaml +22 -0
- morphogen/templates/hardcore_spheres.yaml +22 -0
- morphogen/templates/hierarchical_lattice.yaml +22 -0
- morphogen/templates/homogenization_acoustic_foam.yaml +36 -0
- morphogen/templates/homogenization_beremin.yaml +50 -0
- morphogen/templates/homogenization_cleavage.yaml +51 -0
- morphogen/templates/homogenization_cleavage_band.yaml +55 -0
- morphogen/templates/homogenization_contact.yaml +43 -0
- morphogen/templates/homogenization_crack_closure.yaml +49 -0
- morphogen/templates/homogenization_crystal_plasticity.yaml +50 -0
- morphogen/templates/homogenization_crystal_plasticity_fft.yaml +52 -0
- morphogen/templates/homogenization_crystallization.yaml +30 -0
- morphogen/templates/homogenization_debonding.yaml +43 -0
- morphogen/templates/homogenization_detwinning.yaml +58 -0
- morphogen/templates/homogenization_ductile_damage.yaml +47 -0
- morphogen/templates/homogenization_fatigue.yaml +40 -0
- morphogen/templates/homogenization_grain_fracture.yaml +56 -0
- morphogen/templates/homogenization_gyroid.yaml +30 -0
- morphogen/templates/homogenization_intergranular.yaml +43 -0
- morphogen/templates/homogenization_interphase.yaml +42 -0
- morphogen/templates/homogenization_mixed_loading.yaml +33 -0
- morphogen/templates/homogenization_ntfa.yaml +56 -0
- morphogen/templates/homogenization_piezoelectric.yaml +27 -0
- morphogen/templates/homogenization_polycrystal.yaml +30 -0
- morphogen/templates/homogenization_polycrystal_inclusions.yaml +30 -0
- morphogen/templates/homogenization_poroelastic.yaml +26 -0
- morphogen/templates/homogenization_rubber.yaml +40 -0
- morphogen/templates/homogenization_slip_cleavage.yaml +50 -0
- morphogen/templates/homogenization_spheres.yaml +31 -0
- morphogen/templates/homogenization_transport.yaml +29 -0
- morphogen/templates/homogenization_twinning.yaml +54 -0
- morphogen/templates/homogenization_viscoelastic.yaml +34 -0
- morphogen/templates/homogenization_viscoplastic.yaml +42 -0
- morphogen/templates/homogenization_voronoi.yaml +26 -0
- morphogen/templates/homogenization_xfem.yaml +23 -0
- morphogen/templates/hyperuniform_cells.yaml +24 -0
- morphogen/templates/laguerre.yaml +18 -0
- morphogen/templates/laser_bending.yaml +35 -0
- morphogen/templates/laser_cells.yaml +38 -0
- morphogen/templates/laser_corrugated.yaml +36 -0
- morphogen/templates/level_set_foam.yaml +29 -0
- morphogen/templates/menger_sponge.yaml +17 -0
- morphogen/templates/nacre.yaml +25 -0
- morphogen/templates/nucleation_growth.yaml +25 -0
- morphogen/templates/optimize_auxetic.yaml +29 -0
- morphogen/templates/optimize_auxetic_3d.yaml +27 -0
- morphogen/templates/optimize_bulk.yaml +23 -0
- morphogen/templates/optimize_conductivity.yaml +20 -0
- morphogen/templates/osteons.yaml +28 -0
- morphogen/templates/penetrable_spheres.yaml +21 -0
- morphogen/templates/poisson_fibers.yaml +21 -0
- morphogen/templates/random_laminate.yaml +26 -0
- morphogen/templates/random_lattice.yaml +27 -0
- morphogen/templates/random_walk_fibers.yaml +26 -0
- morphogen/templates/reaction_diffusion.yaml +22 -0
- morphogen/templates/reconstruction.yaml +22 -0
- morphogen/templates/rve_cellular_automaton.yaml +23 -0
- morphogen/templates/rve_crystal_plasticity_fft.yaml +51 -0
- morphogen/templates/rve_multiphysics.yaml +43 -0
- morphogen/templates/rve_poisson_fibers.yaml +22 -0
- morphogen/templates/rve_poisson_fibers_fe.yaml +33 -0
- morphogen/templates/rve_voronoi_windows.yaml +20 -0
- morphogen/templates/scale_rich_network.yaml +29 -0
- morphogen/templates/scale_rich_plates.yaml +30 -0
- morphogen/templates/sequential_dead_leaves.yaml +25 -0
- morphogen/templates/strut_lattice.yaml +21 -0
- morphogen/templates/tile_assembly.yaml +28 -0
- morphogen/templates/tpms.yaml +19 -0
- morphogen/templates/trabecular_bone.yaml +29 -0
- morphogen/templates/transparent_dead_leaves.yaml +23 -0
- morphogen/templates/vascular_tree.yaml +23 -0
- morphogen/templates/voronoi.yaml +20 -0
- morphogen/templates/wood.yaml +34 -0
- morphogen/templates/workflow.yaml +56 -0
- morphogen/texture.py +665 -0
- morphogen/theory.py +162 -0
- morphogen/viz/__init__.py +12 -0
- morphogen/viz/export.py +244 -0
- morphogen/viz/figures.py +707 -0
- morphogen/viz/png.py +148 -0
- morphogen/viz/sections.py +696 -0
- morphogen/viz/surfaces.py +1352 -0
- morphogen/viz/video.py +904 -0
- morphogen/viz/workflow.py +488 -0
- morphogen-1.0.0.dist-info/METADATA +2316 -0
- morphogen-1.0.0.dist-info/RECORD +381 -0
- morphogen-1.0.0.dist-info/WHEEL +4 -0
- morphogen-1.0.0.dist-info/entry_points.txt +5 -0
- morphogen-1.0.0.dist-info/licenses/LICENSE +674 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""The logarithmic strain of finite strain data and the stresses conjugate to it, for the
|
|
2
|
+
learned laws of ``strain="logarithmic"`` (Numba kernels).
|
|
3
|
+
|
|
4
|
+
Such a law sees the logarithmic strain ``H = 1/2 ln C`` (``C = I + 2 E``) of the
|
|
5
|
+
Green-Lagrange strain ``E`` of its data, the space of the Hencky points of morphoGen
|
|
6
|
+
(Miehe, Apel & Lambrecht 2002), and gives the stress ``T`` conjugate to it; the second
|
|
7
|
+
Piola-Kirchhoff stress follows from ``S : dE = T : dH``,
|
|
8
|
+
|
|
9
|
+
S = J^T T, dS/dE = J^T (dT/dH) J + T : d2H/dE2, J = dH/dE (6 x 6),
|
|
10
|
+
|
|
11
|
+
with ``J`` and the geometric term ``T : d2H/dE2`` from the first and second divided
|
|
12
|
+
differences of ``1/2 ln`` in the eigenbasis of ``C`` (Daleckii-Krein; those of
|
|
13
|
+
:mod:`morphogen.homogenization.tensors`, exact for equal eigenvalues). ``H`` is an
|
|
14
|
+
isotropic function of ``C``: ``H(R C R^T) = R H(C) R^T``. The strains and stresses are
|
|
15
|
+
normalized by the scales of a model (``x = E / s``, ``h = H / s``), Voigt components with
|
|
16
|
+
engineering shears for the strains.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import math
|
|
22
|
+
|
|
23
|
+
import numpy as np
|
|
24
|
+
from numba import njit
|
|
25
|
+
|
|
26
|
+
from morphogen.homogenization.tensors import (
|
|
27
|
+
eigh3,
|
|
28
|
+
half_log,
|
|
29
|
+
log_divided,
|
|
30
|
+
log_divided2,
|
|
31
|
+
strain_voigt,
|
|
32
|
+
stress_tensor,
|
|
33
|
+
)
|
|
34
|
+
from morphogen.learning.data import PAIRS
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@njit(cache=True)
|
|
38
|
+
def _logarithmic(x, scale, h, jac, q, mu, dq):
|
|
39
|
+
"""The logarithmic strain ``H = 1/2 ln C`` (``C = I + 2 E``) of a normalized
|
|
40
|
+
Green-Lagrange strain ``x = E / scale`` (Voigt, engineering shears), normalized alike
|
|
41
|
+
(``h``), and its derivative ``jac = dH/dE`` (6 x 6): ``J_ij = w_i sum_ab D_i,ab f[c_a,
|
|
42
|
+
c_b] D_j,ab`` in the eigenbasis of ``C`` (eigenvalues ``mu`` of ``C - I``, vectors the
|
|
43
|
+
columns of ``q``; ``f[.,.]`` the divided differences of ``1/2 ln``), ``D_j = Q^T dC/dE_j
|
|
44
|
+
Q`` kept in ``dq`` (``w_i`` 1/2 for the normal components, 1 for the shears). Returns 1
|
|
45
|
+
where ``C`` is not positive definite."""
|
|
46
|
+
a = np.empty((3, 3))
|
|
47
|
+
for k in range(6):
|
|
48
|
+
i, j = PAIRS[k]
|
|
49
|
+
v = scale * x[k] * (2.0 if k < 3 else 1.0) # C - I = 2 E
|
|
50
|
+
a[i, j] = v
|
|
51
|
+
a[j, i] = v
|
|
52
|
+
eigh3(a, mu, q)
|
|
53
|
+
for c in range(3):
|
|
54
|
+
if not (mu[c] > -1.0 and math.isfinite(mu[c])):
|
|
55
|
+
return 1
|
|
56
|
+
for i in range(3):
|
|
57
|
+
for j in range(3):
|
|
58
|
+
s = 0.0
|
|
59
|
+
for c in range(3):
|
|
60
|
+
s += q[i, c] * q[j, c] * half_log(mu[c])
|
|
61
|
+
a[i, j] = s
|
|
62
|
+
strain_voigt(a, h)
|
|
63
|
+
for k in range(6):
|
|
64
|
+
h[k] /= scale
|
|
65
|
+
f1 = np.empty((3, 3))
|
|
66
|
+
for c in range(3):
|
|
67
|
+
for d in range(c, 3):
|
|
68
|
+
f1[c, d] = log_divided(mu[c], mu[d])
|
|
69
|
+
f1[d, c] = f1[c, d]
|
|
70
|
+
for k in range(6):
|
|
71
|
+
m, n = PAIRS[k]
|
|
72
|
+
for c in range(3):
|
|
73
|
+
for d in range(3):
|
|
74
|
+
if k < 3:
|
|
75
|
+
dq[k, c, d] = 2.0 * q[m, c] * q[m, d]
|
|
76
|
+
else:
|
|
77
|
+
dq[k, c, d] = q[m, c] * q[n, d] + q[n, c] * q[m, d]
|
|
78
|
+
for i in range(6):
|
|
79
|
+
for j in range(i, 6):
|
|
80
|
+
s = 0.0
|
|
81
|
+
for c in range(3):
|
|
82
|
+
for d in range(3):
|
|
83
|
+
s += dq[i, c, d] * f1[c, d] * dq[j, c, d]
|
|
84
|
+
jac[i, j] = s * (0.5 if i < 3 else 1.0)
|
|
85
|
+
jac[j, i] = s * (0.5 if j < 3 else 1.0)
|
|
86
|
+
return 0
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@njit(cache=True)
|
|
90
|
+
def _geometric(t, q, mu, dq, scale, out):
|
|
91
|
+
"""Adds to ``out`` (6 x 6) the normalized ``T : d2H/dE2`` of the normalized stress ``t``
|
|
92
|
+
conjugate to the logarithmic strain (the Hessian of ``T . H(E)`` at fixed ``T``):
|
|
93
|
+
``2 sum_abc f[c_a, c_b, c_c] T_ca D_j,ab D_k,bc`` in the eigenbasis of ``C`` (the second
|
|
94
|
+
divided differences of ``1/2 ln``, Daleckii-Krein), times ``scale``."""
|
|
95
|
+
tt = np.empty((3, 3))
|
|
96
|
+
stress_tensor(t, tt)
|
|
97
|
+
tq = np.empty((3, 3))
|
|
98
|
+
for a in range(3):
|
|
99
|
+
for b in range(3):
|
|
100
|
+
s = 0.0
|
|
101
|
+
for i in range(3):
|
|
102
|
+
for j in range(3):
|
|
103
|
+
s += q[i, a] * tt[i, j] * q[j, b]
|
|
104
|
+
tq[a, b] = s
|
|
105
|
+
f2 = np.empty((3, 3, 3))
|
|
106
|
+
for a in range(3): # symmetric in its arguments: 10 values
|
|
107
|
+
for b in range(a, 3):
|
|
108
|
+
for c in range(b, 3):
|
|
109
|
+
v = log_divided2(mu[a], mu[b], mu[c])
|
|
110
|
+
f2[a, b, c] = v
|
|
111
|
+
f2[a, c, b] = v
|
|
112
|
+
f2[b, a, c] = v
|
|
113
|
+
f2[b, c, a] = v
|
|
114
|
+
f2[c, a, b] = v
|
|
115
|
+
f2[c, b, a] = v
|
|
116
|
+
r = np.empty((6, 3, 3))
|
|
117
|
+
for j in range(6):
|
|
118
|
+
for b in range(3):
|
|
119
|
+
for c in range(3):
|
|
120
|
+
s = 0.0
|
|
121
|
+
for a in range(3):
|
|
122
|
+
s += f2[a, b, c] * tq[c, a] * dq[j, a, b]
|
|
123
|
+
r[j, b, c] = s
|
|
124
|
+
for j in range(6):
|
|
125
|
+
for k in range(j, 6):
|
|
126
|
+
s = 0.0
|
|
127
|
+
for b in range(3):
|
|
128
|
+
for c in range(3):
|
|
129
|
+
s += r[j, b, c] * dq[k, b, c]
|
|
130
|
+
out[j, k] += 2.0 * scale * s
|
|
131
|
+
if k != j:
|
|
132
|
+
out[k, j] += 2.0 * scale * s
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@njit(cache=True)
|
|
136
|
+
def _conjugate(jac, t, kmat, y, out):
|
|
137
|
+
"""The stress conjugate to the Green-Lagrange strain of the stress ``t`` conjugate to
|
|
138
|
+
``H``, ``y = J^T t``, and ``out = J^T K J`` of the tangent ``K = dT/dH``."""
|
|
139
|
+
tmp = np.empty((6, 6))
|
|
140
|
+
for i in range(6):
|
|
141
|
+
s = 0.0
|
|
142
|
+
for k in range(6):
|
|
143
|
+
s += jac[k, i] * t[k]
|
|
144
|
+
y[i] = s
|
|
145
|
+
for j in range(6):
|
|
146
|
+
s = 0.0
|
|
147
|
+
for k in range(6):
|
|
148
|
+
s += kmat[i, k] * jac[k, j]
|
|
149
|
+
tmp[i, j] = s
|
|
150
|
+
for i in range(6):
|
|
151
|
+
for j in range(6):
|
|
152
|
+
s = 0.0
|
|
153
|
+
for k in range(6):
|
|
154
|
+
s += jac[k, i] * tmp[k, j]
|
|
155
|
+
out[i, j] = s
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
@njit(cache=True)
|
|
159
|
+
def _symmetrize(c):
|
|
160
|
+
for r in range(6):
|
|
161
|
+
for k in range(r):
|
|
162
|
+
s = 0.5 * (c[r, k] + c[k, r])
|
|
163
|
+
c[r, k] = s
|
|
164
|
+
c[k, r] = s
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@njit(cache=True)
|
|
168
|
+
def _measures(x, scale):
|
|
169
|
+
"""The normalized logarithmic strains (n x 6) and their derivatives ``dH/dE`` (n x 6 x
|
|
170
|
+
6) of normalized Green-Lagrange strains ``x`` (n x 6); NaN where ``C`` is not positive
|
|
171
|
+
definite."""
|
|
172
|
+
n = x.shape[0]
|
|
173
|
+
h, jac = np.empty((n, 6)), np.empty((n, 6, 6))
|
|
174
|
+
q, mu, dq = np.empty((3, 3)), np.empty(3), np.empty((6, 3, 3))
|
|
175
|
+
for p in range(n):
|
|
176
|
+
if _logarithmic(x[p], scale, h[p], jac[p], q, mu, dq):
|
|
177
|
+
h[p] = np.nan
|
|
178
|
+
jac[p] = np.nan
|
|
179
|
+
return h, jac
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
@njit(cache=True)
|
|
183
|
+
def _pullback(x, t, k, scale, symmetric):
|
|
184
|
+
"""The normalized second Piola-Kirchhoff stresses ``J^T t`` (n x 6) and their tangents
|
|
185
|
+
``J^T k J + T : d2H/dE2`` (n x 6 x 6, normalized) of normalized Green-Lagrange strains
|
|
186
|
+
``x`` (n x 6), of the normalized stresses ``t`` conjugate to their logarithmic strains
|
|
187
|
+
(n x 6) and of the tangents ``k = dt/dh`` of a law (n x 6 x 6), symmetrized with
|
|
188
|
+
``symmetric`` (a law of a potential); NaN where ``C`` is not positive definite."""
|
|
189
|
+
n = x.shape[0]
|
|
190
|
+
y, tangent = np.empty((n, 6)), np.empty((n, 6, 6))
|
|
191
|
+
h, jac, q, mu = np.empty(6), np.empty((6, 6)), np.empty((3, 3)), np.empty(3)
|
|
192
|
+
dq = np.empty((6, 3, 3))
|
|
193
|
+
for p in range(n):
|
|
194
|
+
if _logarithmic(x[p], scale, h, jac, q, mu, dq):
|
|
195
|
+
y[p] = np.nan
|
|
196
|
+
tangent[p] = np.nan
|
|
197
|
+
continue
|
|
198
|
+
_conjugate(jac, t[p], k[p], y[p], tangent[p])
|
|
199
|
+
_geometric(t[p], q, mu, dq, scale, tangent[p])
|
|
200
|
+
if symmetric:
|
|
201
|
+
_symmetrize(tangent[p])
|
|
202
|
+
return y, tangent
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
"""MFront sources of the learned laws: a material whose ``learned`` names a trained model
|
|
2
|
+
(:class:`~morphogen.learning.behaviour.LearnedBehaviour`) becomes an MFront behaviour, for
|
|
3
|
+
the codes that MFront serves (AMITEX_FFTP and Cast3M through its ``umat`` interface,
|
|
4
|
+
Abaqus, Code_Aster, MGIS), as :mod:`morphogen.io.mfront` writes morphoGen's own
|
|
5
|
+
behaviours: :func:`~morphogen.io.mfront.mfront_source`, ``morphogen mfront`` and the
|
|
6
|
+
AMITEX and Abaqus exports take the learned materials like the others.
|
|
7
|
+
|
|
8
|
+
The trained parameters are constants of the source (their shortest decimal forms, which
|
|
9
|
+
read back to the bit) and the forward pass of the model and its consistent tangent are
|
|
10
|
+
morphoGen's C++ port of the models (``learned.cpp``: :class:`~morphogen.learning.networks.
|
|
11
|
+
FeedForward`, :class:`~morphogen.learning.networks.Potential`, the gated recurrent unit of
|
|
12
|
+
:class:`~morphogen.learning.networks.Recurrent` and the Newton solve of the incremental
|
|
13
|
+
potential of :class:`~morphogen.learning.standard.Standard`, line by line), in a behaviour
|
|
14
|
+
of the ``Default`` DSL:
|
|
15
|
+
|
|
16
|
+
* small strains: the strain and the stress of the model;
|
|
17
|
+
* finite strains: the laws of the logarithmic strain (``strain="logarithmic"``) with
|
|
18
|
+
``@StrainMeasure Hencky``, whose strain is ``1/2 ln C`` and stress the ``T`` conjugate to
|
|
19
|
+
it, the very ``T`` of the model (MFront turns it to the Cauchy stress and its tangent
|
|
20
|
+
``dT/dH`` to the operator of the interface); the laws of the Green-Lagrange strain with
|
|
21
|
+
``@StrainMeasure GreenLagrange`` (``E`` and the second Piola-Kirchhoff stress);
|
|
22
|
+
* the state of the recurrent network (its ``hidden`` values) and the internal variables of
|
|
23
|
+
the standard model: an array of auxiliary state variables (``LearnedState``), zero at the
|
|
24
|
+
natural state as in morphoGen; the strain at the start of a step, which the recurrent
|
|
25
|
+
network takes, is MFront's (``eto``), the strain that morphoGen keeps in its internal
|
|
26
|
+
variables;
|
|
27
|
+
* thermal expansion: the stress-free thermal strain ``alpha (T + dT - 293.15)`` of the
|
|
28
|
+
material (its tensor in the axes of the material) taken out of the strain the model sees,
|
|
29
|
+
that of the Hencky points in finite strains (``1/2 ln C - alpha dT``), as
|
|
30
|
+
:mod:`morphogen.learning.behaviour` does; MFront's reference temperature, 293.15 K, is
|
|
31
|
+
morphoGen's natural state. A law of the Green-Lagrange strain that expands is refused
|
|
32
|
+
(its split of the Hencky points, ``E_m = (exp(2 H_m) - I) / 2``, is not written);
|
|
33
|
+
* orientations: a law in the axes of its material (``oriented``, the default) is an
|
|
34
|
+
orthotropic behaviour (``@OrthotropicBehaviour``), which the solver gives in the axes of
|
|
35
|
+
each grain (MGIS through morphoGen turns the strains, the umat interface takes the axes
|
|
36
|
+
V1, V2, Abaqus its ``*ORIENTATION``; in finite strains, the first six state variables in
|
|
37
|
+
Abaqus); a law in the axes of the sample is isotropic (not turned). A law in the axes of
|
|
38
|
+
the sample with an anisotropic expansion, which the orientations of the grains would
|
|
39
|
+
turn, is refused.
|
|
40
|
+
|
|
41
|
+
Order of the components at each interface: TFEL's symmetric tensors (11, 22, 33, then
|
|
42
|
+
``sqrt(2)`` times 12, 13, 23) in the behaviour (``eto``, ``sig``, ``Dt``); morphoGen's
|
|
43
|
+
Voigt order (11, 22, 33, 23, 31, 12, the engineering shears of the strains) in the model
|
|
44
|
+
and its C++ port: the strain goes to the model as ``e[VOIGT[i]] = c_i eto[i]``, the stress
|
|
45
|
+
comes back as ``sig[i] = c_i s[VOIGT[i]]`` and the tangent as ``Dt(i, j) = c_i c_j
|
|
46
|
+
K[VOIGT[i]][VOIGT[j]]``, ``VOIGT = (0, 1, 2, 5, 4, 3)`` and ``c = (1, 1, 1, sqrt(2),
|
|
47
|
+
sqrt(2), sqrt(2))``, as in the sources of morphoGen's porous kernel.
|
|
48
|
+
|
|
49
|
+
The tangents: symmetric for the potential and the standard model
|
|
50
|
+
(``@ProvidesSymmetricTangentOperator``), not for the perceptron and the recurrent network
|
|
51
|
+
(``@ProvidesTangentOperator``). MGIS through morphoGen reads that declaration (in the
|
|
52
|
+
header MFront generates, :func:`morphogen.homogenization.mfront.declared_symmetric`): its
|
|
53
|
+
solvers symmetrize the tangents of the first for the conjugate gradients and pass those
|
|
54
|
+
of the others whole, to GMRES in the volume elements and to Newton's method at a material
|
|
55
|
+
point, as they do for morphoGen's own learned points. A step of the standard model that
|
|
56
|
+
stops without converging keeps the last iterate of Newton's method, as morphoGen's points
|
|
57
|
+
do; a stress or a tangent that is not finite fails the integration.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
from __future__ import annotations
|
|
61
|
+
|
|
62
|
+
import re
|
|
63
|
+
from importlib import resources
|
|
64
|
+
|
|
65
|
+
import numpy as np
|
|
66
|
+
|
|
67
|
+
from morphogen._version import __version__
|
|
68
|
+
|
|
69
|
+
#: The strain measures of the learned laws in finite strains, by the option ``strain`` of
|
|
70
|
+
#: their models
|
|
71
|
+
MEASURES = {"logarithmic": "Hencky", "green-lagrange": "GreenLagrange"}
|
|
72
|
+
_SECTION = re.compile(r"^// ---- (\w+)\s*$", re.M)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _real(value) -> str:
|
|
76
|
+
return repr(float(value))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _values(values, per_line: int = 6) -> str:
|
|
80
|
+
"""A C array initializer of reals, ``per_line`` values a line."""
|
|
81
|
+
values = [_real(v) for v in np.ravel(values)] or ["0.0"]
|
|
82
|
+
rows = [", ".join(values[k : k + per_line]) for k in range(0, len(values), per_line)]
|
|
83
|
+
return "{\n " + ",\n ".join(rows) + "}"
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _reals(name: str, values) -> str:
|
|
87
|
+
values = np.ravel(np.asarray(values, dtype=float))
|
|
88
|
+
return f"constexpr double {name}[{max(values.size, 1)}] = {_values(values)};"
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _ints(name: str, values) -> str:
|
|
92
|
+
values = [int(v) for v in values] or [0]
|
|
93
|
+
return f"constexpr int {name}[{len(values)}] = {{{', '.join(map(str, values))}}};"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _flag(name: str, value) -> str:
|
|
97
|
+
return f"constexpr bool {name} = {'true' if value else 'false'};"
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def learned_model(material, kinematics: str = "small"):
|
|
101
|
+
"""The model of the learned law of a material, checked for an MFront source of the
|
|
102
|
+
kinematics: trained on data of its measure (small strains, or the Green-Lagrange
|
|
103
|
+
measures in finite strains), its expansion one that the source takes (a law of the
|
|
104
|
+
Green-Lagrange strain does not expand; a law in the axes of the sample has an isotropic
|
|
105
|
+
expansion)."""
|
|
106
|
+
from morphogen.learning.behaviour import learned_law
|
|
107
|
+
|
|
108
|
+
if kinematics not in ("small", "finite"):
|
|
109
|
+
raise ValueError("kinematics: 'small' or 'finite'")
|
|
110
|
+
finite = kinematics == "finite"
|
|
111
|
+
try:
|
|
112
|
+
law = learned_law(material.learned, finite=finite)
|
|
113
|
+
except ValueError as exc:
|
|
114
|
+
raise ValueError(f"mfront: {exc}") from None
|
|
115
|
+
model = law.model
|
|
116
|
+
if material.expands:
|
|
117
|
+
if finite and not model.logarithmic:
|
|
118
|
+
raise ValueError(
|
|
119
|
+
"mfront: a learned law of the Green-Lagrange strain that expands (its "
|
|
120
|
+
"thermal split, that of the Hencky points, is not exported: train the law "
|
|
121
|
+
"of the logarithmic strain, strain='logarithmic')"
|
|
122
|
+
)
|
|
123
|
+
if isinstance(material.expansion, tuple) and not material.learned.oriented:
|
|
124
|
+
raise ValueError(
|
|
125
|
+
"mfront: an anisotropic expansion of a learned law in the axes of the sample "
|
|
126
|
+
"(the orientations of the grains turn the expansion, not the law)"
|
|
127
|
+
)
|
|
128
|
+
return model
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def learned_strain_measure(material, kinematics: str = "small") -> str | None:
|
|
132
|
+
"""The strain measure of the MFront source of a learned material: none in small
|
|
133
|
+
strains; ``Hencky`` (a law of the logarithmic strain) or ``GreenLagrange`` in finite
|
|
134
|
+
strains."""
|
|
135
|
+
model = learned_model(material, kinematics)
|
|
136
|
+
if kinematics == "small":
|
|
137
|
+
return None
|
|
138
|
+
return MEASURES["logarithmic" if model.logarithmic else "green-lagrange"]
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _expansion(material) -> np.ndarray:
|
|
142
|
+
"""The thermal expansion of the material in the axes of its law (morphoGen's Voigt
|
|
143
|
+
components, engineering shears: the thermal strain of a unit change of temperature)."""
|
|
144
|
+
from morphogen.learning.data import _voigt
|
|
145
|
+
|
|
146
|
+
if not material.expands:
|
|
147
|
+
return np.zeros(6)
|
|
148
|
+
a = np.asarray(material.expansion_tensor(), dtype=float)
|
|
149
|
+
return _voigt(0.5 * (a + a.T), engineering=True)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _constants(model) -> list[str]:
|
|
153
|
+
"""The C++ constants of a model: its scales, sizes and trained parameters (the values
|
|
154
|
+
that its NumPy and Numba functions compute with)."""
|
|
155
|
+
s = model._scales()
|
|
156
|
+
lines = [
|
|
157
|
+
"// the scales of the training set: x = strain / SCALE_STRAIN, y = stress / SCALE_STRESS",
|
|
158
|
+
f"constexpr double SCALE_STRAIN = {_real(s.strain)};",
|
|
159
|
+
f"constexpr double SCALE_STRESS = {_real(s.stress)};",
|
|
160
|
+
f"constexpr double SCALE_INCREMENT = {_real(s.increment)};",
|
|
161
|
+
f"constexpr double SCALE_TIME = {_real(s.time)};",
|
|
162
|
+
f"constexpr int N_STATE = {model.n_state};",
|
|
163
|
+
]
|
|
164
|
+
p = model.params
|
|
165
|
+
if model.kind == "feedforward":
|
|
166
|
+
sizes = list(model.options["hidden"])
|
|
167
|
+
n = len(sizes)
|
|
168
|
+
lines += [
|
|
169
|
+
f"constexpr int N_LAYERS = {n};",
|
|
170
|
+
_ints("SIZES", sizes),
|
|
171
|
+
f"constexpr int WIDEST = {max(6, *sizes)};",
|
|
172
|
+
"// the hidden layers (W_k: out x in, row by row), the output and the linear term",
|
|
173
|
+
_reals("W", np.concatenate([p[f"W{k}"].ravel() for k in range(n)])),
|
|
174
|
+
_reals("B", np.concatenate([p[f"b{k}"] for k in range(n)])),
|
|
175
|
+
_reals("WOUT", p["Wout"]),
|
|
176
|
+
_reals("BOUT", p["bout"]),
|
|
177
|
+
_reals("LIN", p["L"]),
|
|
178
|
+
]
|
|
179
|
+
elif model.kind == "potential":
|
|
180
|
+
sizes = list(model.options["hidden"])
|
|
181
|
+
n = len(sizes)
|
|
182
|
+
w, wout, amat = model._weights()
|
|
183
|
+
offsets = np.concatenate([[0], np.cumsum(sizes)[:-1]])
|
|
184
|
+
wpos = [w[k].ravel() for k in range(1, n)]
|
|
185
|
+
# the offsets of the W_k in WPOS (none for the first layer)
|
|
186
|
+
woffsets = [0, 0, *np.cumsum([v.size for v in wpos])[:-1]] if n > 1 else [0]
|
|
187
|
+
g0 = model._gradient(np.zeros((1, 6)))[0][0]
|
|
188
|
+
lines += [
|
|
189
|
+
f"constexpr int N_LAYERS = {n};",
|
|
190
|
+
_ints("SIZES", sizes),
|
|
191
|
+
_ints("OFFSETS", offsets),
|
|
192
|
+
f"constexpr int TOTAL = {sum(sizes)};",
|
|
193
|
+
f"constexpr int WIDEST = {max(6, *sizes)};",
|
|
194
|
+
"// U_k (units x 6) and b_k of the layers; W_k = softplus(V_k) >= 0 of the layers",
|
|
195
|
+
"// k > 0 (units x units below), w = softplus(v) >= 0, A = L L^T, dW/dx (0)",
|
|
196
|
+
_reals("U", np.concatenate([p[f"U{k}"].ravel() for k in range(n)])),
|
|
197
|
+
_reals("BIAS", np.concatenate([p[f"b{k}"] for k in range(n)])),
|
|
198
|
+
_reals("WPOS", np.concatenate(wpos) if wpos else []),
|
|
199
|
+
_ints("WOFFSETS", woffsets[:n]),
|
|
200
|
+
_reals("WOUT", wout),
|
|
201
|
+
_reals("AMAT", amat),
|
|
202
|
+
_reals("G0", g0),
|
|
203
|
+
]
|
|
204
|
+
elif model.kind == "recurrent":
|
|
205
|
+
lines += [
|
|
206
|
+
f"constexpr int HIDDEN = {model.options['hidden']};",
|
|
207
|
+
f"constexpr int N_INPUTS = {model.n_inputs};",
|
|
208
|
+
_flag("RATE", model.options["rate"]),
|
|
209
|
+
_flag("LOGARITHMIC", model.logarithmic),
|
|
210
|
+
"// the gates (W_i: inputs x 3 hidden, W_h: hidden x 3 hidden; reset, update, new)",
|
|
211
|
+
"// and the output y = V h + Q x + c",
|
|
212
|
+
_reals("WI", p["Wi"]),
|
|
213
|
+
_reals("WH", p["Wh"]),
|
|
214
|
+
_reals("BG", p["b"]),
|
|
215
|
+
_reals("BN", p["bn"]),
|
|
216
|
+
_reals("V", p["V"]),
|
|
217
|
+
_reals("Q", p["Q"]),
|
|
218
|
+
_reals("C", p["c"]),
|
|
219
|
+
]
|
|
220
|
+
elif model.kind == "standard":
|
|
221
|
+
from morphogen.learning import standard
|
|
222
|
+
|
|
223
|
+
u, b, w, amat, g0, _, bm, gm, pm, cm, vm, delta, plastic, viscous, _, _ = (
|
|
224
|
+
model._values()) # fmt: skip
|
|
225
|
+
m = model.n_state
|
|
226
|
+
lines += [
|
|
227
|
+
f"constexpr int M = {m};",
|
|
228
|
+
f"constexpr int D = {6 + m};",
|
|
229
|
+
f"constexpr int NH = {b.size};",
|
|
230
|
+
f"constexpr int N_NORMS = {bm.shape[0]};",
|
|
231
|
+
f"constexpr int N_RIDGES = {cm.shape[0]};",
|
|
232
|
+
f"constexpr double DELTA = {_real(delta)};",
|
|
233
|
+
_flag("PLASTIC", plastic),
|
|
234
|
+
_flag("VISCOUS", viscous),
|
|
235
|
+
f"constexpr double NEWTON_TOL = {_real(standard.NEWTON_TOL)};",
|
|
236
|
+
f"constexpr int NEWTON_MAX = {int(standard.NEWTON_MAX)};",
|
|
237
|
+
f"constexpr double ARMIJO = {_real(standard._ARMIJO)};",
|
|
238
|
+
"// the free energy: U (ridges x D), b, w = softplus >= 0, A = L L^T, dW/dz (0)",
|
|
239
|
+
_reals("U", u),
|
|
240
|
+
_reals("BIAS", b),
|
|
241
|
+
_reals("WR", w),
|
|
242
|
+
_reals("A", amat),
|
|
243
|
+
_reals("G0", g0),
|
|
244
|
+
"// the dissipation: B_j (norms x M x M), B_j^T B_j, P = softplus >= 0 (ridges x",
|
|
245
|
+
"// norms), C (ridges x M), the viscous V = M M^T (M x M)",
|
|
246
|
+
_reals("BM", bm),
|
|
247
|
+
_reals("GM", gm),
|
|
248
|
+
_reals("P", pm),
|
|
249
|
+
_reals("CM", cm),
|
|
250
|
+
_reals("VM", vm),
|
|
251
|
+
]
|
|
252
|
+
else:
|
|
253
|
+
raise ValueError(f"mfront: no source of a {model.kind!r} model")
|
|
254
|
+
return lines
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def _port(kind: str) -> str:
|
|
258
|
+
"""The common part of morphoGen's C++ port of the learned laws and the section of the
|
|
259
|
+
model of ``kind`` (``learned.cpp``)."""
|
|
260
|
+
text = (resources.files("morphogen.learning") / "learned.cpp").read_text(encoding="utf-8")
|
|
261
|
+
parts = _SECTION.split(text)
|
|
262
|
+
sections = dict(zip(parts[1::2], parts[2::2], strict=True))
|
|
263
|
+
return "".join([parts[0], "// ---- common", sections["common"], f"// ---- {kind}",
|
|
264
|
+
sections[kind]]).strip("\n") # fmt: skip
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def _describe(model, material) -> str:
|
|
268
|
+
"""The model, its options and its file, for the description of a source."""
|
|
269
|
+
options = model.options
|
|
270
|
+
if model.kind in ("feedforward", "potential"):
|
|
271
|
+
what = f"{model.kind} {'-'.join(str(h) for h in options['hidden'])}"
|
|
272
|
+
elif model.kind == "recurrent":
|
|
273
|
+
what = f"recurrent (GRU) of {options['hidden']} states" + (", rate" if options["rate"]
|
|
274
|
+
else "") # fmt: skip
|
|
275
|
+
else:
|
|
276
|
+
terms = [f"{options['internal']} internal variables", f"{options['hidden']} ridges"]
|
|
277
|
+
terms += ["rate-independent dissipation"] if options["plastic"] else []
|
|
278
|
+
terms += ["viscous dissipation"] if options["rate"] else []
|
|
279
|
+
what = f"standard ({', '.join(terms)})"
|
|
280
|
+
path = model.path
|
|
281
|
+
where = f" of {path.name}" if path is not None else ""
|
|
282
|
+
turned = "in the axes of the material" if material.learned.oriented else "isotropic"
|
|
283
|
+
return f"{what}{where}, {turned}"
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def learned_source(material, name: str, *, kinematics: str = "small") -> str:
|
|
287
|
+
"""The MFront source of the behaviour ``name`` of a material with a learned law (see the
|
|
288
|
+
module): its model's parameters as constants, its forward pass and tangent by
|
|
289
|
+
morphoGen's C++ port, in small strains or in finite strains (Hencky's measure for the
|
|
290
|
+
laws of the logarithmic strain, Green-Lagrange's for the others)."""
|
|
291
|
+
from morphogen.io.mfront import ABAQUS_AXES
|
|
292
|
+
|
|
293
|
+
model = learned_model(material, kinematics)
|
|
294
|
+
measure = learned_strain_measure(material, kinematics)
|
|
295
|
+
oriented = material.learned.oriented
|
|
296
|
+
n_state = model.n_state
|
|
297
|
+
alpha = _expansion(material)
|
|
298
|
+
symmetric = model.kind in ("potential", "standard")
|
|
299
|
+
if measure is None:
|
|
300
|
+
space = "in small strains"
|
|
301
|
+
elif measure == "Hencky":
|
|
302
|
+
space = ("in finite strains, of the logarithmic strain 1/2 ln C (Hencky), its stress "
|
|
303
|
+
"the conjugate T") # fmt: skip
|
|
304
|
+
else:
|
|
305
|
+
space = ("in finite strains, of the Green-Lagrange strain, its stress the second "
|
|
306
|
+
"Piola-Kirchhoff stress") # fmt: skip
|
|
307
|
+
terms = [_describe(model, material)]
|
|
308
|
+
if material.expands:
|
|
309
|
+
terms.append("thermal expansion " + ", ".join(f"{v:g}" for v in alpha))
|
|
310
|
+
space_name = f"morphogen_{name}"
|
|
311
|
+
lines = [
|
|
312
|
+
"@DSL Default;",
|
|
313
|
+
f"@Behaviour {name};",
|
|
314
|
+
"@Author morphoGen;",
|
|
315
|
+
"@Description {",
|
|
316
|
+
f' "morphoGen {__version__}: a learned law {space}: {"; ".join(terms)}; its forward '
|
|
317
|
+
"pass and consistent tangent by morphoGen's C++ port of the model (learned.cpp), "
|
|
318
|
+
'the trained parameters constants of the source."',
|
|
319
|
+
"}",
|
|
320
|
+
"@ModellingHypothesis Tridimensional;",
|
|
321
|
+
]
|
|
322
|
+
if oriented:
|
|
323
|
+
lines.append("@OrthotropicBehaviour;")
|
|
324
|
+
if measure is not None:
|
|
325
|
+
lines.append(ABAQUS_AXES)
|
|
326
|
+
if measure is not None:
|
|
327
|
+
lines.append(f"@StrainMeasure {measure};")
|
|
328
|
+
lines.append("@ProvidesSymmetricTangentOperator;" if symmetric
|
|
329
|
+
else "@ProvidesTangentOperator;") # fmt: skip
|
|
330
|
+
if n_state:
|
|
331
|
+
lines += [f"@AuxiliaryStateVariable real lv[{n_state}];",
|
|
332
|
+
'lv.setEntryName("LearnedState");'] # fmt: skip
|
|
333
|
+
lines += [
|
|
334
|
+
"",
|
|
335
|
+
"@Includes{",
|
|
336
|
+
"// the learned law: its scales, sizes and trained parameters, then morphoGen's C++",
|
|
337
|
+
"// port of its model (MFront includes this block in several headers of one",
|
|
338
|
+
"// translation unit)",
|
|
339
|
+
f"#ifndef {space_name.upper()}_LEARNED",
|
|
340
|
+
f"#define {space_name.upper()}_LEARNED",
|
|
341
|
+
"#include <cmath>",
|
|
342
|
+
f"namespace {space_name} {{",
|
|
343
|
+
*_constants(model),
|
|
344
|
+
"// morphoGen's components (11 22 33 23 31 12) of TFEL's (11 22 33 12 13 23)",
|
|
345
|
+
"constexpr int VOIGT[6] = {0, 1, 2, 5, 4, 3};",
|
|
346
|
+
"// the thermal strain of a unit change of temperature, in the axes of the law",
|
|
347
|
+
"// (morphoGen's components, engineering shears)",
|
|
348
|
+
_reals("EXPANSION", alpha),
|
|
349
|
+
_port(model.kind),
|
|
350
|
+
f"}} // end of namespace {space_name}",
|
|
351
|
+
"#endif",
|
|
352
|
+
"}",
|
|
353
|
+
"",
|
|
354
|
+
"@Integrator{",
|
|
355
|
+
f" namespace ml = {space_name};",
|
|
356
|
+
" // TFEL's components of tensors: sqrt(2) times the shears 12, 13, 23; the model's:",
|
|
357
|
+
" // morphoGen's 23, 31, 12, the engineering shears of the strains",
|
|
358
|
+
" const auto r2 = std::sqrt(2.);",
|
|
359
|
+
" const StrainStensor e = eto + deto;",
|
|
360
|
+
" double e1[6], e0[6], s6[6], k6[36];",
|
|
361
|
+
" for (int i = 0; i != 6; ++i) {",
|
|
362
|
+
" const auto c = i < 3 ? 1 : r2;",
|
|
363
|
+
" e1[ml::VOIGT[i]] = c * e[i];",
|
|
364
|
+
" e0[ml::VOIGT[i]] = c * eto[i];",
|
|
365
|
+
" }",
|
|
366
|
+
]
|
|
367
|
+
if material.expands:
|
|
368
|
+
lines += [
|
|
369
|
+
" // the mechanical strain: morphoGen's thermal strain alpha (T - 293.15) out, at",
|
|
370
|
+
" // the end and at the start of the step (logarithmic in finite strains)",
|
|
371
|
+
" for (int r = 0; r != 6; ++r) {",
|
|
372
|
+
" e1[r] -= ml::EXPANSION[r] * (T + dT - 293.15);",
|
|
373
|
+
" e0[r] -= ml::EXPANSION[r] * (T - 293.15);",
|
|
374
|
+
" }",
|
|
375
|
+
]
|
|
376
|
+
if n_state:
|
|
377
|
+
lines += [
|
|
378
|
+
" double s0[ml::N_STATE], s1[ml::N_STATE];",
|
|
379
|
+
" for (int r = 0; r != ml::N_STATE; ++r) {",
|
|
380
|
+
" s0[r] = lv[r];",
|
|
381
|
+
" }",
|
|
382
|
+
" if (ml::law(e1, e0, s0, dt, s6, k6, s1) != 0) {",
|
|
383
|
+
" return false;",
|
|
384
|
+
" }",
|
|
385
|
+
" for (int r = 0; r != ml::N_STATE; ++r) {",
|
|
386
|
+
" lv[r] = s1[r];",
|
|
387
|
+
" }",
|
|
388
|
+
]
|
|
389
|
+
else:
|
|
390
|
+
lines += [
|
|
391
|
+
" if (ml::law(e1, e0, nullptr, dt, s6, k6, nullptr) != 0) {",
|
|
392
|
+
" return false;",
|
|
393
|
+
" }",
|
|
394
|
+
]
|
|
395
|
+
lines += [
|
|
396
|
+
" for (int i = 0; i != 6; ++i) {",
|
|
397
|
+
" sig[i] = (i < 3 ? 1 : r2) * s6[ml::VOIGT[i]];",
|
|
398
|
+
" }",
|
|
399
|
+
" if (computeTangentOperator_) {",
|
|
400
|
+
" for (int i = 0; i != 6; ++i) {",
|
|
401
|
+
" for (int j = 0; j != 6; ++j) {",
|
|
402
|
+
" const auto c = (i < 3 ? 1 : r2) * (j < 3 ? 1 : r2);",
|
|
403
|
+
" Dt(i, j) = c * k6[6 * ml::VOIGT[i] + ml::VOIGT[j]];",
|
|
404
|
+
" }",
|
|
405
|
+
" }",
|
|
406
|
+
" }",
|
|
407
|
+
"}",
|
|
408
|
+
]
|
|
409
|
+
return "\n".join(lines) + "\n"
|