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,1241 @@
|
|
|
1
|
+
"""The FFT solvers of voxel images on the GPU (CuPy; see :mod:`morphogen.gpu`).
|
|
2
|
+
|
|
3
|
+
The conjugate gradients of the linear solver (:mod:`morphogen.homogenization.fft`) and
|
|
4
|
+
the iterations of the loading paths (:mod:`morphogen.homogenization.fft_nonlinear`) run
|
|
5
|
+
on the GPU, in double precision:
|
|
6
|
+
|
|
7
|
+
* the stiffness operators of the voxels are applied matrix-free by CUDA kernels, ports
|
|
8
|
+
of the Numba kernels of :mod:`morphogen.homogenization.kernels` (one thread per node,
|
|
9
|
+
so no atomics; the point stresses one thread per voxel), with the element matrices of
|
|
10
|
+
the materials or the tangents of the points;
|
|
11
|
+
* the Fourier preconditioner is cuFFT transforms and a kernel multiplying the blocks of
|
|
12
|
+
its inverse symbol, computed on the GPU;
|
|
13
|
+
* the dot products are reductions of CuPy, without cuBLAS; GMRES, for the full tangents
|
|
14
|
+
that are not symmetric, keeps its basis on the GPU (the dot products of a Gram-Schmidt
|
|
15
|
+
pass in one reduction over its rows, the vectors of its deflated restarts combined in
|
|
16
|
+
place) and its small Hessenberg matrix and harmonic Ritz vectors on the CPU, as the
|
|
17
|
+
space it recycles from a solve to the next along a path (GCRO-DR).
|
|
18
|
+
|
|
19
|
+
Along loading paths (:class:`VoxelPaths`) the arrays of the integration points stay on
|
|
20
|
+
the GPU: their measures of strain, the integration of their behaviours (the CUDA kernels
|
|
21
|
+
of :file:`behaviours.cu`, ports of the Numba kernels of von Mises plasticity, crystal
|
|
22
|
+
plasticity and the logarithmic strain space, one thread per point), the stresses
|
|
23
|
+
conjugate to B-bar and F-bar, the nodal forces and the tangent operator. The Newton
|
|
24
|
+
iterations, on the CPU, exchange with the GPU the nodal unknowns, the residuals and the
|
|
25
|
+
integrals of the stresses; the outputs of each step take the stresses, the measures and
|
|
26
|
+
the cumulated plastic strain of the points back.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
import logging
|
|
32
|
+
import math
|
|
33
|
+
import time
|
|
34
|
+
from functools import lru_cache, partial
|
|
35
|
+
|
|
36
|
+
import numpy as np
|
|
37
|
+
|
|
38
|
+
from morphogen.homogenization import behaviours as _vm
|
|
39
|
+
from morphogen.homogenization import finite as fin
|
|
40
|
+
from morphogen.homogenization.fft_nonlinear import _VoxelPaths
|
|
41
|
+
from morphogen.homogenization.kernels import CORNER_INDEX, CORNERS
|
|
42
|
+
from morphogen.homogenization.nonlinear import _asymmetry_of, _mfront_points
|
|
43
|
+
from morphogen.homogenization.solvers import (
|
|
44
|
+
_REORTHOGONALIZE,
|
|
45
|
+
GMRES_DEFLATE,
|
|
46
|
+
GMRES_RESTART,
|
|
47
|
+
GMRES_STAGNATION,
|
|
48
|
+
NEGLIGIBLE,
|
|
49
|
+
Recycling,
|
|
50
|
+
SolveInfo,
|
|
51
|
+
_deflation,
|
|
52
|
+
_gcro_dr,
|
|
53
|
+
_keep,
|
|
54
|
+
_stagnates,
|
|
55
|
+
)
|
|
56
|
+
from morphogen.stopping import checkpoint
|
|
57
|
+
|
|
58
|
+
log = logging.getLogger("morphogen")
|
|
59
|
+
|
|
60
|
+
_THREADS = 256
|
|
61
|
+
#: Threads of a block of the kernels of the behaviours. The points of a launch adapt to
|
|
62
|
+
#: its duration (about _LAUNCH_S seconds, between the bounds), so that each launch stays far
|
|
63
|
+
#: below the 2 s of the watchdog of the display drivers of Windows even in the steps whose
|
|
64
|
+
#: points take long (large increments, substeps)
|
|
65
|
+
_POINT_THREADS = 64
|
|
66
|
+
_CHUNK_MIN, _CHUNK_START, _CHUNK_MAX = 1 << 10, 1 << 14, 1 << 20
|
|
67
|
+
_LAUNCH_S = 0.2
|
|
68
|
+
|
|
69
|
+
_SOURCE = r"""
|
|
70
|
+
#include <cupy/complex.cuh>
|
|
71
|
+
|
|
72
|
+
__constant__ int CORNERS[8][3] = {CORNERS};
|
|
73
|
+
__constant__ int CORNER_INDEX[2][2][2] = {CORNER_INDEX};
|
|
74
|
+
|
|
75
|
+
__device__ __forceinline__ void element_nodes(long long e, int nx, int ny, int nz,
|
|
76
|
+
long long* nodes) {
|
|
77
|
+
int k = (int)(e % nz);
|
|
78
|
+
int j = (int)((e / nz) % ny);
|
|
79
|
+
int i = (int)(e / ((long long)ny * nz));
|
|
80
|
+
for (int q = 0; q < 8; ++q) {
|
|
81
|
+
int ii = (i + CORNERS[q][0]) % nx;
|
|
82
|
+
int jj = (j + CORNERS[q][1]) % ny;
|
|
83
|
+
int kk = (k + CORNERS[q][2]) % nz;
|
|
84
|
+
nodes[q] = ((long long)ii * ny + jj) * nz + kk;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// out = K w on the periodic grid: mats[g] is the element matrix (8 nd x 8 nd) of the
|
|
89
|
+
// voxels of group g (-1: no stiffness); one thread per node.
|
|
90
|
+
extern "C" __global__ void apply_operator(const int nx, const int ny, const int nz,
|
|
91
|
+
const int nd, const long long* group,
|
|
92
|
+
const double* mats, const double* w,
|
|
93
|
+
double* out) {
|
|
94
|
+
long long n = (long long)blockIdx.x * blockDim.x + threadIdx.x;
|
|
95
|
+
if (n >= (long long)nx * ny * nz) return;
|
|
96
|
+
int k = (int)(n % nz);
|
|
97
|
+
int j = (int)((n / nz) % ny);
|
|
98
|
+
int i = (int)(n / ((long long)ny * nz));
|
|
99
|
+
const int size = 8 * nd;
|
|
100
|
+
double acc[3] = {0.0, 0.0, 0.0};
|
|
101
|
+
for (int a = 0; a < 2; ++a) {
|
|
102
|
+
int ei = i - a >= 0 ? i - a : nx - 1;
|
|
103
|
+
for (int b = 0; b < 2; ++b) {
|
|
104
|
+
int ej = j - b >= 0 ? j - b : ny - 1;
|
|
105
|
+
for (int c = 0; c < 2; ++c) {
|
|
106
|
+
int ek = k - c >= 0 ? k - c : nz - 1;
|
|
107
|
+
long long g = group[((long long)ei * ny + ej) * nz + ek];
|
|
108
|
+
if (g < 0) continue;
|
|
109
|
+
int ln = CORNER_INDEX[a][b][c];
|
|
110
|
+
const double* mat = mats + g * size * size;
|
|
111
|
+
for (int q = 0; q < 8; ++q) {
|
|
112
|
+
int qi = ei + CORNERS[q][0]; if (qi == nx) qi = 0;
|
|
113
|
+
int qj = ej + CORNERS[q][1]; if (qj == ny) qj = 0;
|
|
114
|
+
int qk = ek + CORNERS[q][2]; if (qk == nz) qk = 0;
|
|
115
|
+
long long m = ((long long)qi * ny + qj) * nz + qk;
|
|
116
|
+
for (int c2 = 0; c2 < nd; ++c2) {
|
|
117
|
+
double wv = w[m * nd + c2];
|
|
118
|
+
for (int r = 0; r < nd; ++r)
|
|
119
|
+
acc[r] += mat[(ln * nd + r) * size + q * nd + c2] * wv;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
for (int r = 0; r < nd; ++r) out[n * nd + r] = acc[r];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Inverse of the symbol of a homogeneous operator (stencil blocks: 3 x 3 x 3 x nd x nd)
|
|
129
|
+
// at the frequencies of the real FFT of the grid (frequencies x nd x nd), zero at the
|
|
130
|
+
// zero frequency: the port of morphogen.homogenization.kernels.fourier_inverse.
|
|
131
|
+
extern "C" __global__ void fourier_inverse(const int nx, const int ny, const int nz,
|
|
132
|
+
const int nd, const double* blocks,
|
|
133
|
+
complex<double>* out) {
|
|
134
|
+
const int nzh = nz / 2 + 1;
|
|
135
|
+
long long f = (long long)blockIdx.x * blockDim.x + threadIdx.x;
|
|
136
|
+
if (f >= (long long)nx * ny * nzh) return;
|
|
137
|
+
int fz = (int)(f % nzh);
|
|
138
|
+
int fy = (int)((f / nzh) % ny);
|
|
139
|
+
int fx = (int)(f / ((long long)ny * nzh));
|
|
140
|
+
complex<double>* o = out + f * nd * nd;
|
|
141
|
+
if (fx == 0 && fy == 0 && fz == 0) {
|
|
142
|
+
for (int r = 0; r < nd * nd; ++r) o[r] = complex<double>(0.0, 0.0);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const double pi2 = 6.283185307179586476925286766559;
|
|
146
|
+
double tx = pi2 * fx / nx, ty = pi2 * fy / ny, tz = pi2 * fz / nz;
|
|
147
|
+
complex<double> px[3], py[3], pz[3];
|
|
148
|
+
for (int a = 0; a < 3; ++a) {
|
|
149
|
+
px[a] = complex<double>(cos((a - 1) * tx), sin((a - 1) * tx));
|
|
150
|
+
py[a] = complex<double>(cos((a - 1) * ty), sin((a - 1) * ty));
|
|
151
|
+
pz[a] = complex<double>(cos((a - 1) * tz), sin((a - 1) * tz));
|
|
152
|
+
}
|
|
153
|
+
complex<double> s[3][3];
|
|
154
|
+
for (int i = 0; i < 3; ++i)
|
|
155
|
+
for (int j = 0; j < 3; ++j) s[i][j] = complex<double>(0.0, 0.0);
|
|
156
|
+
for (int a = 0; a < 3; ++a)
|
|
157
|
+
for (int b = 0; b < 3; ++b)
|
|
158
|
+
for (int c = 0; c < 3; ++c) {
|
|
159
|
+
complex<double> phase = px[a] * py[b] * pz[c];
|
|
160
|
+
const double* blk = blocks + (((a * 3 + b) * 3 + c) * nd) * nd;
|
|
161
|
+
for (int i = 0; i < nd; ++i)
|
|
162
|
+
for (int j = 0; j < nd; ++j) s[i][j] += phase * blk[i * nd + j];
|
|
163
|
+
}
|
|
164
|
+
if (nd == 1) {
|
|
165
|
+
o[0] = complex<double>(1.0, 0.0) / s[0][0];
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
complex<double> c00 = s[1][1] * s[2][2] - s[1][2] * s[2][1];
|
|
169
|
+
complex<double> c01 = s[1][2] * s[2][0] - s[1][0] * s[2][2];
|
|
170
|
+
complex<double> c02 = s[1][0] * s[2][1] - s[1][1] * s[2][0];
|
|
171
|
+
complex<double> inv = complex<double>(1.0, 0.0)
|
|
172
|
+
/ (s[0][0] * c00 + s[0][1] * c01 + s[0][2] * c02);
|
|
173
|
+
o[0] = c00 * inv;
|
|
174
|
+
o[1] = (s[0][2] * s[2][1] - s[0][1] * s[2][2]) * inv;
|
|
175
|
+
o[2] = (s[0][1] * s[1][2] - s[0][2] * s[1][1]) * inv;
|
|
176
|
+
o[3] = c01 * inv;
|
|
177
|
+
o[4] = (s[0][0] * s[2][2] - s[0][2] * s[2][0]) * inv;
|
|
178
|
+
o[5] = (s[0][2] * s[1][0] - s[0][0] * s[1][2]) * inv;
|
|
179
|
+
o[6] = c02 * inv;
|
|
180
|
+
o[7] = (s[0][1] * s[2][0] - s[0][0] * s[2][1]) * inv;
|
|
181
|
+
o[8] = (s[0][0] * s[1][1] - s[0][1] * s[1][0]) * inv;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// out[i, f] = sum_j inv[f, i, j] hat[j, f] for complex numbers stored as pairs of
|
|
185
|
+
// doubles: the inverse symbol (frequencies x nd x nd), the fields component by component.
|
|
186
|
+
extern "C" __global__ void block_multiply(const long long n_freq, const int nd,
|
|
187
|
+
const double* inv, const double* hat,
|
|
188
|
+
double* out) {
|
|
189
|
+
long long f = (long long)blockIdx.x * blockDim.x + threadIdx.x;
|
|
190
|
+
if (f >= n_freq) return;
|
|
191
|
+
for (int i = 0; i < nd; ++i) {
|
|
192
|
+
double re = 0.0, im = 0.0;
|
|
193
|
+
for (int j = 0; j < nd; ++j) {
|
|
194
|
+
const double* m = inv + 2 * ((f * nd + i) * nd + j);
|
|
195
|
+
const double* h = hat + 2 * ((long long)j * n_freq + f);
|
|
196
|
+
re += m[0] * h[0] - m[1] * h[1];
|
|
197
|
+
im += m[0] * h[1] + m[1] * h[0];
|
|
198
|
+
}
|
|
199
|
+
out[2 * ((long long)i * n_freq + f)] = re;
|
|
200
|
+
out[2 * ((long long)i * n_freq + f) + 1] = im;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
__device__ __forceinline__ void gradient(const double ue[8][3], const double* gg, int g,
|
|
205
|
+
double* h) {
|
|
206
|
+
for (int r = 0; r < 9; ++r) h[r] = 0.0;
|
|
207
|
+
for (int a = 0; a < 8; ++a) {
|
|
208
|
+
double gx = gg[(g * 3 + 0) * 8 + a];
|
|
209
|
+
double gy = gg[(g * 3 + 1) * 8 + a];
|
|
210
|
+
double gz = gg[(g * 3 + 2) * 8 + a];
|
|
211
|
+
for (int i = 0; i < 3; ++i) {
|
|
212
|
+
double u = ue[a][i];
|
|
213
|
+
h[3 * i] += u * gx;
|
|
214
|
+
h[3 * i + 1] += u * gy;
|
|
215
|
+
h[3 * i + 2] += u * gz;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
__device__ __forceinline__ double det3(const double* f) {
|
|
221
|
+
return f[0] * (f[4] * f[8] - f[5] * f[7]) - f[1] * (f[3] * f[8] - f[5] * f[6])
|
|
222
|
+
+ f[2] * (f[3] * f[7] - f[4] * f[6]);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
__device__ __forceinline__ void inverse_transpose(const double* f, double det, double* o) {
|
|
226
|
+
double inv = 1.0 / det;
|
|
227
|
+
o[0] = (f[4] * f[8] - f[5] * f[7]) * inv;
|
|
228
|
+
o[1] = (f[5] * f[6] - f[3] * f[8]) * inv;
|
|
229
|
+
o[2] = (f[3] * f[7] - f[4] * f[6]) * inv;
|
|
230
|
+
o[3] = (f[2] * f[7] - f[1] * f[8]) * inv;
|
|
231
|
+
o[4] = (f[0] * f[8] - f[2] * f[6]) * inv;
|
|
232
|
+
o[5] = (f[1] * f[6] - f[0] * f[7]) * inv;
|
|
233
|
+
o[6] = (f[1] * f[5] - f[2] * f[4]) * inv;
|
|
234
|
+
o[7] = (f[2] * f[3] - f[0] * f[5]) * inv;
|
|
235
|
+
o[8] = (f[0] * f[4] - f[1] * f[3]) * inv;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Stress increments of the Gauss points (their conjugates with bbar) for the nodal
|
|
239
|
+
// displacements v plus the uniform gradient direction (9, row by row): the port of
|
|
240
|
+
// morphogen.homogenization.kernels.voxel_tangent_stress; one thread per voxel.
|
|
241
|
+
extern "C" __global__ void voxel_increments(const int nx, const int ny, const int nz,
|
|
242
|
+
const long long n_el, const long long* elem,
|
|
243
|
+
const double* v, const double* direction,
|
|
244
|
+
const double* tangent, const double* s,
|
|
245
|
+
const double* base, const double* gg,
|
|
246
|
+
const int finite, const int bbar,
|
|
247
|
+
double* out) {
|
|
248
|
+
long long t = (long long)blockIdx.x * blockDim.x + threadIdx.x;
|
|
249
|
+
if (t >= n_el) return;
|
|
250
|
+
long long nodes[8];
|
|
251
|
+
element_nodes(elem[t], nx, ny, nz, nodes);
|
|
252
|
+
double ue[8][3];
|
|
253
|
+
for (int a = 0; a < 8; ++a)
|
|
254
|
+
for (int c = 0; c < 3; ++c) ue[a][c] = v[3 * nodes[a] + c];
|
|
255
|
+
double dh[8][9];
|
|
256
|
+
for (int g = 0; g < 8; ++g) {
|
|
257
|
+
gradient(ue, gg, g, dh[g]);
|
|
258
|
+
for (int r = 0; r < 9; ++r) dh[g][r] += direction[r];
|
|
259
|
+
}
|
|
260
|
+
double ds[8][9];
|
|
261
|
+
if (!finite) {
|
|
262
|
+
double mean = 0.0;
|
|
263
|
+
for (int g = 0; g < 8; ++g) mean += (dh[g][0] + dh[g][4] + dh[g][8]) / 8.0;
|
|
264
|
+
double mean_p = 0.0;
|
|
265
|
+
for (int g = 0; g < 8; ++g) {
|
|
266
|
+
const double* h = dh[g];
|
|
267
|
+
double de[6] = {h[0], h[4], h[8], h[5] + h[7], h[6] + h[2], h[1] + h[3]};
|
|
268
|
+
if (bbar) {
|
|
269
|
+
double shift = (mean - de[0] - de[1] - de[2]) / 3.0;
|
|
270
|
+
for (int r = 0; r < 3; ++r) de[r] += shift;
|
|
271
|
+
}
|
|
272
|
+
long long p = 8 * t + g;
|
|
273
|
+
for (int r = 0; r < 6; ++r) {
|
|
274
|
+
double acc = 0.0;
|
|
275
|
+
for (int c = 0; c < 6; ++c) acc += tangent[(p * 6 + r) * 6 + c] * de[c];
|
|
276
|
+
ds[g][r] = acc;
|
|
277
|
+
}
|
|
278
|
+
mean_p += (ds[g][0] + ds[g][1] + ds[g][2]) / 24.0;
|
|
279
|
+
}
|
|
280
|
+
for (int g = 0; g < 8; ++g) {
|
|
281
|
+
long long p = 8 * t + g;
|
|
282
|
+
double shift = bbar ? mean_p - (ds[g][0] + ds[g][1] + ds[g][2]) / 3.0 : 0.0;
|
|
283
|
+
for (int r = 0; r < 6; ++r) out[p * 6 + r] = ds[g][r] + (r < 3 ? shift : 0.0);
|
|
284
|
+
}
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (!bbar) {
|
|
288
|
+
for (int g = 0; g < 8; ++g) {
|
|
289
|
+
long long p = 8 * t + g;
|
|
290
|
+
for (int r = 0; r < 9; ++r) {
|
|
291
|
+
double acc = 0.0;
|
|
292
|
+
for (int c = 0; c < 9; ++c) acc += tangent[(p * 9 + r) * 9 + c] * dh[g][c];
|
|
293
|
+
out[p * 9 + r] = acc;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
// F-bar: F_bar = alpha F, alpha = (J_bar / J)^(1/3)
|
|
299
|
+
double alpha[8], dalpha[8], tau[8], dtau[8];
|
|
300
|
+
double f[9], fit[9];
|
|
301
|
+
double mean_log = 0.0, mean_c = 0.0;
|
|
302
|
+
for (int g = 0; g < 8; ++g) {
|
|
303
|
+
long long p = 8 * t + g;
|
|
304
|
+
for (int r = 0; r < 9; ++r) f[r] = base[p * 9 + r] + (r % 4 == 0 ? 1.0 : 0.0);
|
|
305
|
+
double det = det3(f);
|
|
306
|
+
inverse_transpose(f, det, fit);
|
|
307
|
+
alpha[g] = log(det);
|
|
308
|
+
mean_log += alpha[g] / 8.0;
|
|
309
|
+
double c = 0.0;
|
|
310
|
+
for (int r = 0; r < 9; ++r) c += fit[r] * dh[g][r];
|
|
311
|
+
dalpha[g] = c;
|
|
312
|
+
mean_c += c / 8.0;
|
|
313
|
+
}
|
|
314
|
+
double mean_tau = 0.0, mean_dtau = 0.0;
|
|
315
|
+
for (int g = 0; g < 8; ++g) {
|
|
316
|
+
long long p = 8 * t + g;
|
|
317
|
+
double a = exp((mean_log - alpha[g]) / 3.0);
|
|
318
|
+
alpha[g] = a;
|
|
319
|
+
double da = a * (mean_c - dalpha[g]) / 3.0;
|
|
320
|
+
dalpha[g] = da;
|
|
321
|
+
double de[9];
|
|
322
|
+
for (int r = 0; r < 9; ++r) {
|
|
323
|
+
f[r] = base[p * 9 + r] + (r % 4 == 0 ? 1.0 : 0.0);
|
|
324
|
+
de[r] = a * dh[g][r] + da * f[r];
|
|
325
|
+
}
|
|
326
|
+
double pf = 0.0, dpf = 0.0, pdf = 0.0;
|
|
327
|
+
for (int r = 0; r < 9; ++r) {
|
|
328
|
+
double acc = 0.0;
|
|
329
|
+
for (int c = 0; c < 9; ++c) acc += tangent[(p * 9 + r) * 9 + c] * de[c];
|
|
330
|
+
ds[g][r] = acc;
|
|
331
|
+
pf += s[p * 9 + r] * f[r];
|
|
332
|
+
dpf += acc * f[r];
|
|
333
|
+
pdf += s[p * 9 + r] * dh[g][r];
|
|
334
|
+
}
|
|
335
|
+
tau[g] = a * pf / 3.0;
|
|
336
|
+
dtau[g] = (da * pf + a * (dpf + pdf)) / 3.0;
|
|
337
|
+
mean_tau += tau[g] / 8.0;
|
|
338
|
+
mean_dtau += dtau[g] / 8.0;
|
|
339
|
+
}
|
|
340
|
+
for (int g = 0; g < 8; ++g) {
|
|
341
|
+
long long p = 8 * t + g;
|
|
342
|
+
for (int r = 0; r < 9; ++r) f[r] = base[p * 9 + r] + (r % 4 == 0 ? 1.0 : 0.0);
|
|
343
|
+
inverse_transpose(f, det3(f), fit);
|
|
344
|
+
double a = alpha[g], da = dalpha[g];
|
|
345
|
+
double ct = mean_tau - tau[g], cdt = mean_dtau - dtau[g];
|
|
346
|
+
for (int i = 0; i < 3; ++i) {
|
|
347
|
+
for (int j = 0; j < 3; ++j) {
|
|
348
|
+
// d(F^-T)_ij = -(F^-T dF^T F^-T)_ij
|
|
349
|
+
double dfit = 0.0;
|
|
350
|
+
for (int k = 0; k < 3; ++k)
|
|
351
|
+
for (int m = 0; m < 3; ++m)
|
|
352
|
+
dfit -= fit[3 * i + k] * dh[g][3 * m + k] * fit[3 * m + j];
|
|
353
|
+
int r = 3 * i + j;
|
|
354
|
+
out[p * 9 + r] = da * s[p * 9 + r] + a * ds[g][r] + cdt * fit[r] + ct * dfit;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Nodal forces of the point stresses s (int B^T s; in finite strains int P . grad N),
|
|
361
|
+
// and the squared norm of the contributions of the points to each node: the port of
|
|
362
|
+
// morphogen.homogenization.kernels.voxel_forces; one thread per node.
|
|
363
|
+
extern "C" __global__ void voxel_forces(const int nx, const int ny, const int nz,
|
|
364
|
+
const long long* slot, const double* s,
|
|
365
|
+
const double weight, const double* gg,
|
|
366
|
+
const int finite, double* out, double* sq) {
|
|
367
|
+
long long node = (long long)blockIdx.x * blockDim.x + threadIdx.x;
|
|
368
|
+
if (node >= (long long)nx * ny * nz) return;
|
|
369
|
+
int k = (int)(node % nz);
|
|
370
|
+
int j = (int)((node / nz) % ny);
|
|
371
|
+
int i = (int)(node / ((long long)ny * nz));
|
|
372
|
+
double fx = 0.0, fy = 0.0, fz = 0.0, q = 0.0;
|
|
373
|
+
for (int a = 0; a < 2; ++a) {
|
|
374
|
+
int ei = i - a >= 0 ? i - a : nx - 1;
|
|
375
|
+
for (int b = 0; b < 2; ++b) {
|
|
376
|
+
int ej = j - b >= 0 ? j - b : ny - 1;
|
|
377
|
+
for (int c = 0; c < 2; ++c) {
|
|
378
|
+
int ek = k - c >= 0 ? k - c : nz - 1;
|
|
379
|
+
long long t = slot[((long long)ei * ny + ej) * nz + ek];
|
|
380
|
+
if (t < 0) continue;
|
|
381
|
+
int ln = CORNER_INDEX[a][b][c];
|
|
382
|
+
for (int g = 0; g < 8; ++g) {
|
|
383
|
+
long long p = 8 * t + g;
|
|
384
|
+
double gx = weight * gg[(g * 3 + 0) * 8 + ln];
|
|
385
|
+
double gy = weight * gg[(g * 3 + 1) * 8 + ln];
|
|
386
|
+
double gz = weight * gg[(g * 3 + 2) * 8 + ln];
|
|
387
|
+
double cx, cy, cz;
|
|
388
|
+
if (finite) {
|
|
389
|
+
const double* t9 = s + p * 9;
|
|
390
|
+
cx = t9[0] * gx + t9[1] * gy + t9[2] * gz;
|
|
391
|
+
cy = t9[3] * gx + t9[4] * gy + t9[5] * gz;
|
|
392
|
+
cz = t9[6] * gx + t9[7] * gy + t9[8] * gz;
|
|
393
|
+
} else {
|
|
394
|
+
const double* t6 = s + p * 6;
|
|
395
|
+
cx = gx * t6[0] + gz * t6[4] + gy * t6[5];
|
|
396
|
+
cy = gy * t6[1] + gz * t6[3] + gx * t6[5];
|
|
397
|
+
cz = gz * t6[2] + gy * t6[3] + gx * t6[4];
|
|
398
|
+
}
|
|
399
|
+
fx += cx; fy += cy; fz += cz;
|
|
400
|
+
q += cx * cx + cy * cy + cz * cz;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
out[3 * node] = fx;
|
|
406
|
+
out[3 * node + 1] = fy;
|
|
407
|
+
out[3 * node + 2] = fz;
|
|
408
|
+
sq[node] = q;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Measures of strain of the Gauss points of the voxels elem for the nodal fluctuation w and
|
|
412
|
+
// the macroscopic gradient macro (9, row by row): the port of
|
|
413
|
+
// morphogen.homogenization.kernels.voxel_measures (B-bar or F-bar with bbar, the gradients
|
|
414
|
+
// themselves into base in finite strains); one thread per voxel.
|
|
415
|
+
extern "C" __global__ void voxel_measures(const int nx, const int ny, const int nz,
|
|
416
|
+
const long long n_el, const long long* elem,
|
|
417
|
+
const double* w, const double* macro,
|
|
418
|
+
const double* gg, const int finite, const int bbar,
|
|
419
|
+
double* out, double* base) {
|
|
420
|
+
long long t = (long long)blockIdx.x * blockDim.x + threadIdx.x;
|
|
421
|
+
if (t >= n_el) return;
|
|
422
|
+
long long nodes[8];
|
|
423
|
+
element_nodes(elem[t], nx, ny, nz, nodes);
|
|
424
|
+
double ue[8][3];
|
|
425
|
+
for (int a = 0; a < 8; ++a)
|
|
426
|
+
for (int c = 0; c < 3; ++c) ue[a][c] = w[3 * nodes[a] + c];
|
|
427
|
+
double hs[8][9];
|
|
428
|
+
for (int g = 0; g < 8; ++g) {
|
|
429
|
+
gradient(ue, gg, g, hs[g]);
|
|
430
|
+
for (int r = 0; r < 9; ++r) hs[g][r] += macro[r];
|
|
431
|
+
}
|
|
432
|
+
if (!finite) {
|
|
433
|
+
double mean = 0.0;
|
|
434
|
+
for (int g = 0; g < 8; ++g) mean += hs[g][0] + hs[g][4] + hs[g][8];
|
|
435
|
+
mean /= 8.0;
|
|
436
|
+
for (int g = 0; g < 8; ++g) {
|
|
437
|
+
const double* h = hs[g];
|
|
438
|
+
double m[6] = {h[0], h[4], h[8], h[5] + h[7], h[6] + h[2], h[1] + h[3]};
|
|
439
|
+
if (bbar) {
|
|
440
|
+
double shift = (mean - m[0] - m[1] - m[2]) / 3.0;
|
|
441
|
+
for (int r = 0; r < 3; ++r) m[r] += shift;
|
|
442
|
+
}
|
|
443
|
+
long long p = 8 * t + g;
|
|
444
|
+
for (int r = 0; r < 6; ++r) out[p * 6 + r] = m[r];
|
|
445
|
+
}
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
bool valid = true;
|
|
449
|
+
double mean = 0.0;
|
|
450
|
+
double log_j[8];
|
|
451
|
+
for (int g = 0; g < 8; ++g) {
|
|
452
|
+
hs[g][0] += 1.0;
|
|
453
|
+
hs[g][4] += 1.0;
|
|
454
|
+
hs[g][8] += 1.0;
|
|
455
|
+
double det = det3(hs[g]);
|
|
456
|
+
if (!(det > 0.0)) {
|
|
457
|
+
valid = false;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
log_j[g] = log(det);
|
|
461
|
+
mean += log_j[g] / 8.0;
|
|
462
|
+
}
|
|
463
|
+
const double nan = __longlong_as_double(0x7ff8000000000000LL);
|
|
464
|
+
for (int g = 0; g < 8; ++g) {
|
|
465
|
+
long long p = 8 * t + g;
|
|
466
|
+
if (!valid) { // a voxel turned inside out: its behaviours fail
|
|
467
|
+
for (int r = 0; r < 9; ++r) {
|
|
468
|
+
out[p * 9 + r] = nan;
|
|
469
|
+
if (bbar) base[p * 9 + r] = nan;
|
|
470
|
+
}
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
double alpha = bbar ? exp((mean - log_j[g]) / 3.0) : 1.0;
|
|
474
|
+
for (int r = 0; r < 9; ++r) {
|
|
475
|
+
double f = hs[g][r];
|
|
476
|
+
double unit = r % 4 == 0 ? 1.0 : 0.0;
|
|
477
|
+
out[p * 9 + r] = alpha * f - unit;
|
|
478
|
+
if (bbar) base[p * 9 + r] = f - unit;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// The stresses of the points of n_el voxels replaced, in place, by those conjugate to the
|
|
484
|
+
// measures of B-bar or F-bar: the port of morphogen.homogenization.kernels.voxel_effective;
|
|
485
|
+
// one thread per voxel.
|
|
486
|
+
extern "C" __global__ void voxel_effective(const long long n_el, const int finite, double* s,
|
|
487
|
+
const double* base) {
|
|
488
|
+
long long t = (long long)blockIdx.x * blockDim.x + threadIdx.x;
|
|
489
|
+
if (t >= n_el) return;
|
|
490
|
+
if (!finite) {
|
|
491
|
+
double mean = 0.0;
|
|
492
|
+
for (int g = 0; g < 8; ++g) {
|
|
493
|
+
long long p = 8 * t + g;
|
|
494
|
+
mean += (s[p * 6] + s[p * 6 + 1] + s[p * 6 + 2]) / 24.0;
|
|
495
|
+
}
|
|
496
|
+
for (int g = 0; g < 8; ++g) {
|
|
497
|
+
long long p = 8 * t + g;
|
|
498
|
+
double shift = mean - (s[p * 6] + s[p * 6 + 1] + s[p * 6 + 2]) / 3.0;
|
|
499
|
+
for (int r = 0; r < 3; ++r) s[p * 6 + r] += shift;
|
|
500
|
+
}
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
double alpha[8], tau[8], f[9], fit[9];
|
|
504
|
+
double mean_log = 0.0;
|
|
505
|
+
for (int g = 0; g < 8; ++g) {
|
|
506
|
+
long long p = 8 * t + g;
|
|
507
|
+
for (int r = 0; r < 9; ++r) f[r] = base[p * 9 + r] + (r % 4 == 0 ? 1.0 : 0.0);
|
|
508
|
+
alpha[g] = log(det3(f));
|
|
509
|
+
mean_log += alpha[g] / 8.0;
|
|
510
|
+
}
|
|
511
|
+
double mean_tau = 0.0;
|
|
512
|
+
for (int g = 0; g < 8; ++g) {
|
|
513
|
+
long long p = 8 * t + g;
|
|
514
|
+
alpha[g] = exp((mean_log - alpha[g]) / 3.0);
|
|
515
|
+
double pf = 0.0;
|
|
516
|
+
for (int r = 0; r < 9; ++r)
|
|
517
|
+
pf += s[p * 9 + r] * (base[p * 9 + r] + (r % 4 == 0 ? 1.0 : 0.0));
|
|
518
|
+
tau[g] = alpha[g] * pf / 3.0;
|
|
519
|
+
mean_tau += tau[g] / 8.0;
|
|
520
|
+
}
|
|
521
|
+
for (int g = 0; g < 8; ++g) {
|
|
522
|
+
long long p = 8 * t + g;
|
|
523
|
+
for (int r = 0; r < 9; ++r) f[r] = base[p * 9 + r] + (r % 4 == 0 ? 1.0 : 0.0);
|
|
524
|
+
inverse_transpose(f, det3(f), fit);
|
|
525
|
+
for (int r = 0; r < 9; ++r)
|
|
526
|
+
s[p * 9 + r] = alpha[g] * s[p * 9 + r] + (mean_tau - tau[g]) * fit[r];
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
"""
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
def _cupy():
|
|
533
|
+
import cupy
|
|
534
|
+
|
|
535
|
+
return cupy
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
def _c_array(a) -> str:
|
|
539
|
+
"""C initializer of a nested integer array."""
|
|
540
|
+
a = np.asarray(a)
|
|
541
|
+
if a.ndim == 0:
|
|
542
|
+
return str(int(a))
|
|
543
|
+
return "{" + ", ".join(_c_array(x) for x in a) + "}"
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
@lru_cache(maxsize=1)
|
|
547
|
+
def _module():
|
|
548
|
+
cp = _cupy()
|
|
549
|
+
code = _SOURCE.replace("{CORNERS}", _c_array(CORNERS)).replace(
|
|
550
|
+
"{CORNER_INDEX}", _c_array(CORNER_INDEX)
|
|
551
|
+
)
|
|
552
|
+
return cp.RawModule(code=code)
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
def _launch(name: str, n: int, args) -> None:
|
|
556
|
+
if n > 0:
|
|
557
|
+
_module().get_function(name)(((n + _THREADS - 1) // _THREADS,), (_THREADS,), args)
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
@lru_cache(maxsize=8)
|
|
561
|
+
def _behaviours(ns: int, width: int):
|
|
562
|
+
"""The CUDA module of the behaviours (:file:`behaviours.cu`) for ``ns`` slip systems
|
|
563
|
+
in the arrays of the crystals and ``width`` internal variables per point, with the
|
|
564
|
+
constants of the Python modules."""
|
|
565
|
+
from importlib import resources
|
|
566
|
+
|
|
567
|
+
cp = _cupy()
|
|
568
|
+
constants = _vm.kernel_constants(ns, width)
|
|
569
|
+
defines = "".join(f"#define {name} {value!r}\n" for name, value in constants.items())
|
|
570
|
+
source = resources.files("morphogen.homogenization") / "behaviours.cu"
|
|
571
|
+
return cp.RawModule(code=defines + source.read_text(encoding="utf-8"))
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
@lru_cache(maxsize=1)
|
|
575
|
+
def _dot_kernel():
|
|
576
|
+
cp = _cupy()
|
|
577
|
+
return cp.ReductionKernel("float64 x, float64 y", "float64 z", "x * y", "a + b", "z = a",
|
|
578
|
+
"0", "morphogen_dot") # fmt: skip
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
def dot(a, b) -> float:
|
|
582
|
+
"""Dot product of two vectors on the GPU (a CuPy reduction, without cuBLAS)."""
|
|
583
|
+
return float(_dot_kernel()(a, b))
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
@lru_cache(maxsize=1)
|
|
587
|
+
def _updates():
|
|
588
|
+
"""The updates of the conjugate gradients, in place and fused."""
|
|
589
|
+
cp = _cupy()
|
|
590
|
+
step = cp.ElementwiseKernel("float64 alpha, float64 p, float64 ap", "float64 x, float64 r",
|
|
591
|
+
"x += alpha * p; r -= alpha * ap", "morphogen_cg_step") # fmt: skip
|
|
592
|
+
direction = cp.ElementwiseKernel("float64 beta, float64 z", "float64 p", "p = z + beta * p",
|
|
593
|
+
"morphogen_cg_direction") # fmt: skip
|
|
594
|
+
return step, direction
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
def conjugate_gradient(apply, precondition, rhs, tol, maxiter):
|
|
598
|
+
"""Preconditioned conjugate gradient from ``x = 0`` on the GPU, the algorithm of
|
|
599
|
+
:func:`morphogen.homogenization.solvers.conjugate_gradient` (:func:`minres` after a
|
|
600
|
+
direction of negative curvature)."""
|
|
601
|
+
cp = _cupy()
|
|
602
|
+
step, direction = _updates()
|
|
603
|
+
x = cp.zeros_like(rhs)
|
|
604
|
+
norm_b = math.sqrt(dot(rhs, rhs))
|
|
605
|
+
if norm_b == 0.0:
|
|
606
|
+
return x, 0, 0.0
|
|
607
|
+
r = rhs.copy()
|
|
608
|
+
z = precondition(r)
|
|
609
|
+
p = z.copy()
|
|
610
|
+
rz = dot(r, z)
|
|
611
|
+
res = 1.0
|
|
612
|
+
it = 0
|
|
613
|
+
for it in range(1, maxiter + 1):
|
|
614
|
+
checkpoint()
|
|
615
|
+
ap = apply(p)
|
|
616
|
+
pap = dot(p, ap)
|
|
617
|
+
if pap <= 0.0:
|
|
618
|
+
norm_r = math.sqrt(dot(r, r))
|
|
619
|
+
if pap < 0.0 and norm_r > 0.0:
|
|
620
|
+
if norm_r > norm_b: # an iterate worse than zero
|
|
621
|
+
x[:] = 0.0
|
|
622
|
+
r = rhs.copy()
|
|
623
|
+
norm_r = norm_b
|
|
624
|
+
dx, more, rel = minres(apply, precondition, r, tol * norm_b / norm_r,
|
|
625
|
+
maxiter - it) # fmt: skip
|
|
626
|
+
return x + dx, it + more, rel * norm_r / norm_b
|
|
627
|
+
break
|
|
628
|
+
alpha = rz / pap
|
|
629
|
+
step(alpha, p, ap, x, r)
|
|
630
|
+
res = math.sqrt(dot(r, r)) / norm_b
|
|
631
|
+
if res <= tol:
|
|
632
|
+
break
|
|
633
|
+
z = precondition(r)
|
|
634
|
+
rz_new = dot(r, z)
|
|
635
|
+
if rz_new <= 0.0: # the residual is round-off the preconditioner cancels
|
|
636
|
+
break
|
|
637
|
+
direction(rz_new / rz, z, p)
|
|
638
|
+
rz = rz_new
|
|
639
|
+
return x, it, res
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
@lru_cache(maxsize=1)
|
|
643
|
+
def _subtract_kernel():
|
|
644
|
+
"""``w -= basis[:k]^T coef[:k]`` on the GPU (the rows of the basis in their order), in
|
|
645
|
+
place: the second half of a Gram-Schmidt pass of :func:`gmres`."""
|
|
646
|
+
cp = _cupy()
|
|
647
|
+
return cp.ElementwiseKernel(
|
|
648
|
+
"raw float64 basis, raw float64 coef, int64 k, int64 n", "float64 w",
|
|
649
|
+
"double acc = w; for (long long j = 0; j < k; ++j) acc -= coef[j] * basis[j * n + i]; "
|
|
650
|
+
"w = acc;", "morphogen_gmres_subtract") # fmt: skip
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
@lru_cache(maxsize=1)
|
|
654
|
+
def _tangent_kernels():
|
|
655
|
+
"""The squared norms of ``C - C^T`` and of ``C`` for the tangent ``C`` of each point, and
|
|
656
|
+
its symmetrization in place as :func:`morphogen.homogenization.tensors.symmetrize` does
|
|
657
|
+
it (one thread per point)."""
|
|
658
|
+
cp = _cupy()
|
|
659
|
+
norms = cp.ElementwiseKernel(
|
|
660
|
+
"raw float64 t, int64 nc", "float64 skew, float64 full",
|
|
661
|
+
"long long o = i * nc * nc; double s = 0.0, c = 0.0; "
|
|
662
|
+
"for (long long r = 0; r < nc; ++r) for (long long q = 0; q < nc; ++q) { "
|
|
663
|
+
"double a = t[o + r * nc + q]; double d = a - t[o + q * nc + r]; c += a * a; "
|
|
664
|
+
"s += d * d; } skew = s; full = c;", "morphogen_tangent_norms") # fmt: skip
|
|
665
|
+
symmetrize = cp.ElementwiseKernel(
|
|
666
|
+
"int64 nc", "raw float64 t",
|
|
667
|
+
"long long o = i * nc * nc; for (long long r = 0; r < nc; ++r) "
|
|
668
|
+
"for (long long q = r + 1; q < nc; ++q) { "
|
|
669
|
+
"double m = 0.5 * (t[o + r * nc + q] + t[o + q * nc + r]); t[o + r * nc + q] = m; "
|
|
670
|
+
"t[o + q * nc + r] = m; }", "morphogen_symmetrize_points") # fmt: skip
|
|
671
|
+
return norms, symmetrize
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
@lru_cache(maxsize=64)
|
|
675
|
+
def _combine_kernel(cols: int):
|
|
676
|
+
"""``basis[:cols] = p^T basis[:rows]`` on the GPU, in place (``p``: rows x cols): one
|
|
677
|
+
thread per component of the vectors, its ``cols`` new values summed in the order of the
|
|
678
|
+
basis; the vectors that a deflated restart of :func:`gmres` keeps (and those that its
|
|
679
|
+
recycling keeps: up to ``2k + 3`` columns)."""
|
|
680
|
+
cp = _cupy()
|
|
681
|
+
return cp.ElementwiseKernel(
|
|
682
|
+
"raw float64 p, int64 rows, int64 n", "raw float64 basis",
|
|
683
|
+
f"double acc[{cols}]; for (int c = 0; c < {cols}; ++c) acc[c] = 0.0; "
|
|
684
|
+
"for (long long j = 0; j < rows; ++j) { double b = basis[j * n + i]; "
|
|
685
|
+
f"for (int c = 0; c < {cols}; ++c) acc[c] += p[j * {cols} + c] * b; }} "
|
|
686
|
+
f"for (int c = 0; c < {cols}; ++c) basis[c * n + i] = acc[c];",
|
|
687
|
+
f"morphogen_gmres_combine_{cols}") # fmt: skip
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
class _Device:
|
|
691
|
+
"""The operations on the vectors of the recycling of :func:`gmres` (GCRO-DR,
|
|
692
|
+
:func:`morphogen.homogenization.solvers._gcro_dr`) on the GPU: the reductions and the
|
|
693
|
+
kernels of its Gram-Schmidt passes and of its deflated restarts."""
|
|
694
|
+
|
|
695
|
+
@staticmethod
|
|
696
|
+
def norm(w) -> float:
|
|
697
|
+
return math.sqrt(dot(w, w))
|
|
698
|
+
|
|
699
|
+
@staticmethod
|
|
700
|
+
def zeros_like(a):
|
|
701
|
+
return _cupy().zeros_like(a)
|
|
702
|
+
|
|
703
|
+
@staticmethod
|
|
704
|
+
def empty(rows: int, n: int):
|
|
705
|
+
return _cupy().empty((rows, n))
|
|
706
|
+
|
|
707
|
+
@staticmethod
|
|
708
|
+
def dots(basis, k: int, w) -> np.ndarray:
|
|
709
|
+
return _cupy().asnumpy(_dot_kernel()(basis[:k], w, axis=1))
|
|
710
|
+
|
|
711
|
+
@staticmethod
|
|
712
|
+
def subtract(basis, k: int, coef, w) -> None:
|
|
713
|
+
cp = _cupy()
|
|
714
|
+
_subtract_kernel()(basis, cp.asarray(np.ascontiguousarray(coef, dtype=np.float64)),
|
|
715
|
+
np.int64(k), np.int64(w.size), w) # fmt: skip
|
|
716
|
+
|
|
717
|
+
@staticmethod
|
|
718
|
+
def combine(basis, rows: int, p) -> None:
|
|
719
|
+
cp = _cupy()
|
|
720
|
+
p = np.ascontiguousarray(p, dtype=np.float64)
|
|
721
|
+
n = int(basis.shape[1])
|
|
722
|
+
_combine_kernel(int(p.shape[1]))(cp.asarray(p), np.int64(rows), np.int64(n), basis,
|
|
723
|
+
size=n) # fmt: skip
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
_DEVICE = _Device()
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
def gmres(apply, precondition, rhs, tol, maxiter, restart: int = GMRES_RESTART,
|
|
730
|
+
deflate: int = GMRES_DEFLATE, recycle: Recycling | None = None,
|
|
731
|
+
stagnation: int = 0): # fmt: skip
|
|
732
|
+
"""GMRES with deflated restarts, preconditioned on the right on the GPU, the algorithm
|
|
733
|
+
of :func:`morphogen.homogenization.solvers.gmres`: the basis on the GPU (the dot
|
|
734
|
+
products of a Gram-Schmidt pass in one reduction over its rows, the vectors kept at a
|
|
735
|
+
deflated restart combined in place), the Hessenberg matrix, its factors and the harmonic
|
|
736
|
+
Ritz vectors on the CPU; ``recycle`` and ``stagnation`` as there, the recycled vectors
|
|
737
|
+
on the GPU."""
|
|
738
|
+
cp = _cupy()
|
|
739
|
+
subtract = _subtract_kernel()
|
|
740
|
+
x = cp.zeros_like(rhs)
|
|
741
|
+
norm_b = math.sqrt(dot(rhs, rhs))
|
|
742
|
+
if norm_b == 0.0:
|
|
743
|
+
return x, 0, 0.0
|
|
744
|
+
n = int(rhs.size)
|
|
745
|
+
m = max(1, min(int(restart), int(maxiter)))
|
|
746
|
+
k_max = min(int(deflate), m - 2)
|
|
747
|
+
if recycle is not None and recycle.u is not None and k_max > 0:
|
|
748
|
+
out = _gcro_dr(_DEVICE, apply, precondition, rhs, tol, maxiter, m, k_max, recycle,
|
|
749
|
+
stagnation) # fmt: skip
|
|
750
|
+
if out is not None:
|
|
751
|
+
return out
|
|
752
|
+
ends = [1.0]
|
|
753
|
+
basis = cp.empty((m + 1, n))
|
|
754
|
+
h = np.zeros((m + 1, m))
|
|
755
|
+
tri = np.zeros((m + 1, m))
|
|
756
|
+
cs, sn, g, c = np.zeros(m), np.zeros(m), np.zeros(m + 1), np.zeros(m + 1)
|
|
757
|
+
q0 = np.ones((1, 1))
|
|
758
|
+
beta, total, res = norm_b, 0, 1.0
|
|
759
|
+
s, deflated = 0, False
|
|
760
|
+
cp.divide(rhs, beta, out=basis[0])
|
|
761
|
+
c[0] = beta
|
|
762
|
+
while total < maxiter:
|
|
763
|
+
g[:] = 0.0
|
|
764
|
+
g[: s + 1] = q0.T @ c[: s + 1]
|
|
765
|
+
k = s
|
|
766
|
+
for j in range(s, min(m, s + maxiter - total)):
|
|
767
|
+
checkpoint()
|
|
768
|
+
w = apply(precondition(basis[j]))
|
|
769
|
+
h[:, j] = 0.0
|
|
770
|
+
before = math.sqrt(dot(w, w))
|
|
771
|
+
for _ in range(2):
|
|
772
|
+
coef = _dot_kernel()(basis[: j + 1], w, axis=1)
|
|
773
|
+
subtract(basis, coef, np.int64(j + 1), np.int64(n), w)
|
|
774
|
+
h[: j + 1, j] += cp.asnumpy(coef)
|
|
775
|
+
after = math.sqrt(dot(w, w))
|
|
776
|
+
if after > _REORTHOGONALIZE * before:
|
|
777
|
+
break
|
|
778
|
+
before = after
|
|
779
|
+
h[j + 1, j] = after
|
|
780
|
+
col = tri[:, j]
|
|
781
|
+
col[:] = h[:, j]
|
|
782
|
+
if s:
|
|
783
|
+
col[: s + 1] = q0.T @ col[: s + 1]
|
|
784
|
+
for i in range(s, j):
|
|
785
|
+
a, b = col[i], col[i + 1]
|
|
786
|
+
col[i] = cs[i] * a + sn[i] * b
|
|
787
|
+
col[i + 1] = cs[i] * b - sn[i] * a
|
|
788
|
+
gamma = math.hypot(col[j], after)
|
|
789
|
+
if not gamma > 0.0:
|
|
790
|
+
break
|
|
791
|
+
cs[j], sn[j] = col[j] / gamma, after / gamma
|
|
792
|
+
col[j], col[j + 1] = gamma, 0.0
|
|
793
|
+
g[j + 1] = -sn[j] * g[j]
|
|
794
|
+
g[j] *= cs[j]
|
|
795
|
+
k = j + 1
|
|
796
|
+
total += 1
|
|
797
|
+
res = abs(g[k]) / norm_b
|
|
798
|
+
if res <= tol or after == 0.0:
|
|
799
|
+
break
|
|
800
|
+
cp.divide(w, after, out=basis[j + 1])
|
|
801
|
+
if k == 0:
|
|
802
|
+
break
|
|
803
|
+
y = np.zeros(m + 1)
|
|
804
|
+
for i in range(k - 1, -1, -1):
|
|
805
|
+
y[i] = (g[i] - tri[i, i + 1 : k] @ y[i + 1 : k]) / tri[i, i]
|
|
806
|
+
combined = cp.zeros_like(rhs)
|
|
807
|
+
subtract(basis, cp.asarray(-y[:k]), np.int64(k), np.int64(n), combined)
|
|
808
|
+
x += precondition(combined)
|
|
809
|
+
if total >= maxiter or (res <= tol and not deflated):
|
|
810
|
+
break
|
|
811
|
+
previous = beta
|
|
812
|
+
full = res > tol and k == m and h[m, m - 1] > 0.0
|
|
813
|
+
if full and stagnation:
|
|
814
|
+
ends.append(res)
|
|
815
|
+
if _stagnates(ends, stagnation):
|
|
816
|
+
break
|
|
817
|
+
start = _deflation(h, c, y, m, k_max) if full and k_max > 0 else None
|
|
818
|
+
if start is not None:
|
|
819
|
+
p, hk, ck = start
|
|
820
|
+
beta = res * norm_b
|
|
821
|
+
if not beta < previous:
|
|
822
|
+
break
|
|
823
|
+
s = p.shape[1] - 1
|
|
824
|
+
_combine_kernel(s + 1)(cp.asarray(np.ascontiguousarray(p)), np.int64(m + 1),
|
|
825
|
+
np.int64(n), basis, size=n) # fmt: skip
|
|
826
|
+
h[:] = 0.0
|
|
827
|
+
h[: s + 1, :s] = hk
|
|
828
|
+
c[:] = 0.0
|
|
829
|
+
c[: s + 1] = ck
|
|
830
|
+
q0, r0 = np.linalg.qr(hk, mode="complete")
|
|
831
|
+
tri[:] = 0.0
|
|
832
|
+
tri[: s + 1, :s] = r0
|
|
833
|
+
deflated = True
|
|
834
|
+
continue
|
|
835
|
+
r = rhs - apply(x)
|
|
836
|
+
beta = math.sqrt(dot(r, r))
|
|
837
|
+
res = beta / norm_b
|
|
838
|
+
if res <= tol or not beta < previous:
|
|
839
|
+
break
|
|
840
|
+
if deflated and recycle is not None: # before the plain cycle overwrites them
|
|
841
|
+
_keep(_DEVICE, recycle, basis, h[: s + 1, :s])
|
|
842
|
+
cp.divide(r, beta, out=basis[0])
|
|
843
|
+
c[:] = 0.0
|
|
844
|
+
c[0] = beta
|
|
845
|
+
s, q0, deflated = 0, np.ones((1, 1)), False
|
|
846
|
+
if deflated and recycle is not None:
|
|
847
|
+
_keep(_DEVICE, recycle, basis, h[: s + 1, :s])
|
|
848
|
+
return x, total, res
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
def minres(apply, precondition, rhs, tol, maxiter):
|
|
852
|
+
"""Preconditioned MINRES on the GPU, the algorithm of
|
|
853
|
+
:func:`morphogen.homogenization.solvers.minres`."""
|
|
854
|
+
cp = _cupy()
|
|
855
|
+
x = cp.zeros_like(rhs)
|
|
856
|
+
norm_b = math.sqrt(dot(rhs, rhs))
|
|
857
|
+
if norm_b == 0.0:
|
|
858
|
+
return x, 0, 0.0
|
|
859
|
+
r = rhs.copy()
|
|
860
|
+
res = 1.0
|
|
861
|
+
total = 0
|
|
862
|
+
while total < maxiter:
|
|
863
|
+
dx, its = _minres_cycle(apply, precondition, r, tol / res, maxiter - total)
|
|
864
|
+
total += its
|
|
865
|
+
x += dx
|
|
866
|
+
r = rhs - apply(x)
|
|
867
|
+
res = math.sqrt(dot(r, r)) / norm_b
|
|
868
|
+
if res <= tol or its == 0:
|
|
869
|
+
break
|
|
870
|
+
return x, total, res
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
def _minres_cycle(apply, precondition, rhs, tol, maxiter):
|
|
874
|
+
"""One cycle of :func:`minres` from ``x = 0``: (x, iterations)."""
|
|
875
|
+
cp = _cupy()
|
|
876
|
+
x = cp.zeros_like(rhs)
|
|
877
|
+
r1 = rhs.copy()
|
|
878
|
+
y = precondition(r1)
|
|
879
|
+
beta1 = dot(r1, y)
|
|
880
|
+
if not beta1 > 0.0:
|
|
881
|
+
return x, 0
|
|
882
|
+
beta1 = math.sqrt(beta1)
|
|
883
|
+
oldb, beta, dbar, epsln, phibar, cs, sn = 0.0, beta1, 0.0, 0.0, beta1, -1.0, 0.0
|
|
884
|
+
w = cp.zeros_like(rhs)
|
|
885
|
+
w2 = cp.zeros_like(rhs)
|
|
886
|
+
r2 = r1
|
|
887
|
+
it = 0
|
|
888
|
+
for it in range(1, maxiter + 1):
|
|
889
|
+
checkpoint()
|
|
890
|
+
v = y / beta
|
|
891
|
+
y = apply(v)
|
|
892
|
+
if it >= 2:
|
|
893
|
+
y -= (beta / oldb) * r1
|
|
894
|
+
alfa = dot(v, y)
|
|
895
|
+
y -= (alfa / beta) * r2
|
|
896
|
+
r1 = r2
|
|
897
|
+
r2 = y
|
|
898
|
+
y = precondition(r2)
|
|
899
|
+
oldb = beta
|
|
900
|
+
beta = dot(r2, y)
|
|
901
|
+
if beta < 0.0: # a preconditioner that is not positive definite
|
|
902
|
+
break
|
|
903
|
+
beta = math.sqrt(beta)
|
|
904
|
+
oldeps = epsln
|
|
905
|
+
delta = cs * dbar + sn * alfa
|
|
906
|
+
gbar = sn * dbar - cs * alfa
|
|
907
|
+
epsln = sn * beta
|
|
908
|
+
dbar = -cs * beta
|
|
909
|
+
gamma = max(math.hypot(gbar, beta), 1e-300)
|
|
910
|
+
cs = gbar / gamma
|
|
911
|
+
sn = beta / gamma
|
|
912
|
+
phi = cs * phibar
|
|
913
|
+
phibar = sn * phibar
|
|
914
|
+
w1 = w2
|
|
915
|
+
w2 = w
|
|
916
|
+
w = (v - oldeps * w1 - delta * w2) / gamma
|
|
917
|
+
x += phi * w
|
|
918
|
+
if abs(phibar) <= tol * beta1 or beta == 0.0:
|
|
919
|
+
break
|
|
920
|
+
return x, it
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
class FourierPreconditioner:
|
|
924
|
+
"""The Fourier preconditioner of :class:`morphogen.homogenization.fft.
|
|
925
|
+
FourierPreconditioner` on the GPU, for the element matrix ``k_ref`` of the reference
|
|
926
|
+
medium: its inverse symbol computed on the GPU, the fields component by component,
|
|
927
|
+
cuFFT transforms of the grid."""
|
|
928
|
+
|
|
929
|
+
def __init__(self, shape, k_ref, nd: int):
|
|
930
|
+
from morphogen.homogenization.fft import _stencil
|
|
931
|
+
|
|
932
|
+
cp = _cupy()
|
|
933
|
+
self.shape = tuple(int(s) for s in shape)
|
|
934
|
+
self.nd = int(nd)
|
|
935
|
+
nx, ny, nz = self.shape
|
|
936
|
+
self.n_freq = nx * ny * (nz // 2 + 1)
|
|
937
|
+
stencil = _stencil(np.asarray(k_ref, dtype=float), self.nd)
|
|
938
|
+
blocks = cp.asarray(np.ascontiguousarray(stencil))
|
|
939
|
+
self.inverse = cp.empty((self.n_freq, self.nd, self.nd), dtype=np.complex128)
|
|
940
|
+
_launch("fourier_inverse", self.n_freq, (np.int32(nx), np.int32(ny), np.int32(nz),
|
|
941
|
+
np.int32(self.nd), blocks, self.inverse)) # fmt: skip
|
|
942
|
+
|
|
943
|
+
def __call__(self, r):
|
|
944
|
+
cp = _cupy()
|
|
945
|
+
nd, shape = self.nd, self.shape
|
|
946
|
+
field = cp.ascontiguousarray(r.reshape(-1, nd).T).reshape(nd, *shape)
|
|
947
|
+
hat = cp.ascontiguousarray(cp.fft.rfftn(field, axes=(1, 2, 3)))
|
|
948
|
+
out = cp.empty_like(hat)
|
|
949
|
+
_launch("block_multiply", self.n_freq, (np.int64(self.n_freq), np.int32(nd),
|
|
950
|
+
self.inverse, hat, out)) # fmt: skip
|
|
951
|
+
back = cp.fft.irfftn(out, s=shape, axes=(1, 2, 3))
|
|
952
|
+
return cp.ascontiguousarray(back.reshape(nd, -1).T).reshape(-1)
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
class LinearOperator:
|
|
956
|
+
"""The stiffness operator of the voxels of a linear FFT problem on the GPU (the
|
|
957
|
+
element matrices of its materials)."""
|
|
958
|
+
|
|
959
|
+
def __init__(self, problem):
|
|
960
|
+
cp = _cupy()
|
|
961
|
+
self.shape = problem.shape
|
|
962
|
+
self.nd = problem.nd
|
|
963
|
+
self.group = cp.asarray(np.ascontiguousarray(problem.mat, dtype=np.int64))
|
|
964
|
+
self.mats = cp.asarray(np.ascontiguousarray(problem.element_matrices, dtype=np.float64))
|
|
965
|
+
|
|
966
|
+
def __call__(self, w):
|
|
967
|
+
cp = _cupy()
|
|
968
|
+
nx, ny, nz = self.shape
|
|
969
|
+
out = cp.empty_like(w)
|
|
970
|
+
_launch("apply_operator", nx * ny * nz, (np.int32(nx), np.int32(ny), np.int32(nz),
|
|
971
|
+
np.int32(self.nd), self.group, self.mats, w, out)) # fmt: skip
|
|
972
|
+
return out
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
def solve_linear(problem, rhs, tol: float, maxiter: int):
|
|
976
|
+
"""Solution of the linear system of an FFT problem on the GPU: (x, iterations,
|
|
977
|
+
relative residual), ``x`` on the CPU. The operator and the preconditioner are kept
|
|
978
|
+
by the problem."""
|
|
979
|
+
cp = _cupy()
|
|
980
|
+
state = problem.__dict__.setdefault("_gpu", {})
|
|
981
|
+
if "operator" not in state:
|
|
982
|
+
state["operator"] = LinearOperator(problem)
|
|
983
|
+
k_ref = problem._reference_element()
|
|
984
|
+
state["precondition"] = FourierPreconditioner(problem.shape, k_ref, problem.nd)
|
|
985
|
+
x, it, res = conjugate_gradient(state["operator"], state["precondition"], cp.asarray(rhs),
|
|
986
|
+
tol, maxiter) # fmt: skip
|
|
987
|
+
return cp.asnumpy(x), it, res
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
class VoxelPaths(_VoxelPaths):
|
|
991
|
+
"""The discretization of the loading paths on the voxels
|
|
992
|
+
(:class:`~morphogen.homogenization.fft_nonlinear._VoxelPaths`) on the GPU: the arrays of
|
|
993
|
+
the points (measures of strain, stresses, tangents, internal variables) stay in its
|
|
994
|
+
memory, where the measures, the behaviours, the stresses conjugate to B-bar and F-bar,
|
|
995
|
+
the nodal forces and the products by the tangent operator are computed. The weights of
|
|
996
|
+
the points stay on the CPU (they are uniform)."""
|
|
997
|
+
|
|
998
|
+
def _buffers(self, n_pts: int, nc: int) -> None:
|
|
999
|
+
cp = _cupy()
|
|
1000
|
+
p = self.problem
|
|
1001
|
+
beh = p.behaviours
|
|
1002
|
+
self.elem_gpu = cp.asarray(np.ascontiguousarray(self.elem, dtype=np.int64))
|
|
1003
|
+
self.slot_gpu = cp.asarray(np.ascontiguousarray(self.slot, dtype=np.int64))
|
|
1004
|
+
self.g_gpu = cp.asarray(np.ascontiguousarray(self.g_gauss, dtype=np.float64))
|
|
1005
|
+
self.precondition = FourierPreconditioner(self.shape, self.reference, 3)
|
|
1006
|
+
self.work = cp.empty((n_pts, nc))
|
|
1007
|
+
# F-bar: the gradients of the points and the stresses of their behaviours
|
|
1008
|
+
keep = self.finite and self.bbar
|
|
1009
|
+
self.base = cp.zeros((n_pts if keep else 1, 9))
|
|
1010
|
+
self.stress = cp.zeros((n_pts if keep else 1, nc))
|
|
1011
|
+
self.zero = cp.zeros(9)
|
|
1012
|
+
self.still = cp.zeros(self.n_q)
|
|
1013
|
+
self.squares = cp.empty(int(np.prod(self.shape)))
|
|
1014
|
+
self.tangent = None
|
|
1015
|
+
self.phase_points = None # the points of each phase, for the asymmetry
|
|
1016
|
+
# the behaviours of the points
|
|
1017
|
+
self.points = tuple(cp.asarray(np.ascontiguousarray(a, dtype=np.int64))
|
|
1018
|
+
for a in (p.kind, p.point_mat, p.pmat)) # fmt: skip
|
|
1019
|
+
matrices, eigen, params, nk, systems, cp_par, cp_h, cp_n = beh.data
|
|
1020
|
+
if any(crystal.extended for crystal in beh.crystals):
|
|
1021
|
+
raise ValueError(
|
|
1022
|
+
"GPU: the twins and the cleavage planes of the crystals are integrated on the "
|
|
1023
|
+
"CPU only (device: cpu)"
|
|
1024
|
+
)
|
|
1025
|
+
# the rows of the kernels: von Mises's parameters and Hill's coefficients, or the
|
|
1026
|
+
# parameters of a porous point
|
|
1027
|
+
width = _vm.ROW_WIDTH
|
|
1028
|
+
params = np.pad(params, ((0, 0), (0, max(0, width - params.shape[1]))))[:, :width]
|
|
1029
|
+
dev = [cp.asarray(np.ascontiguousarray(a, dtype=np.float64))
|
|
1030
|
+
for a in (matrices, eigen, params, systems, cp_par, cp_h)] # fmt: skip
|
|
1031
|
+
self.data = (*dev[:3], np.int32(nk), *dev[3:],
|
|
1032
|
+
cp.asarray(np.ascontiguousarray(cp_n, dtype=np.int64))) # fmt: skip
|
|
1033
|
+
self.module = _behaviours(max(int(cp_par.shape[1]), 1), beh.width)
|
|
1034
|
+
self.failed = cp.zeros(1, dtype=np.uint32)
|
|
1035
|
+
self.chunk = _CHUNK_START
|
|
1036
|
+
|
|
1037
|
+
def arrays(self, n_pts: int, nc: int, width: int) -> tuple:
|
|
1038
|
+
cp = _cupy()
|
|
1039
|
+
return (cp.zeros((n_pts, nc)), np.full(n_pts, self.weight), cp.zeros((n_pts, nc)),
|
|
1040
|
+
cp.zeros((n_pts, nc, nc)), cp.zeros((n_pts, width)),
|
|
1041
|
+
cp.zeros((n_pts, width))) # fmt: skip
|
|
1042
|
+
|
|
1043
|
+
def host(self, *arrays) -> tuple:
|
|
1044
|
+
cp = _cupy()
|
|
1045
|
+
return tuple(cp.asnumpy(a) if isinstance(a, cp.ndarray) else a for a in arrays)
|
|
1046
|
+
|
|
1047
|
+
def measures(self, q, e, grads, wts) -> None:
|
|
1048
|
+
cp = _cupy()
|
|
1049
|
+
nx, ny, nz = self.shape
|
|
1050
|
+
n_el = int(self.elem.size)
|
|
1051
|
+
q_gpu = cp.asarray(np.ascontiguousarray(q, dtype=np.float64))
|
|
1052
|
+
macro = cp.asarray(np.ascontiguousarray(self._macro(e), dtype=np.float64))
|
|
1053
|
+
_launch("voxel_measures", n_el, (np.int32(nx), np.int32(ny), np.int32(nz),
|
|
1054
|
+
np.int64(n_el), self.elem_gpu, q_gpu, macro, self.g_gpu, np.int32(self.finite),
|
|
1055
|
+
np.int32(self.bbar), grads, self.base)) # fmt: skip
|
|
1056
|
+
wts.fill(self.weight)
|
|
1057
|
+
|
|
1058
|
+
def integrate(self, grads, dtemp, dt, old, new, stress, tangent, guess, ratio) -> int:
|
|
1059
|
+
"""The behaviours of the points on the GPU (:file:`behaviours.cu`), in launches of
|
|
1060
|
+
``chunk`` points that follow their durations, their tangents symmetrized as the
|
|
1061
|
+
problem says."""
|
|
1062
|
+
cp = _cupy()
|
|
1063
|
+
kernel = self.module.get_function("integrate_finite" if self.finite else "integrate_small")
|
|
1064
|
+
symmetric = bool(self.problem.symmetrize)
|
|
1065
|
+
self.failed.fill(0)
|
|
1066
|
+
n = int(grads.shape[0])
|
|
1067
|
+
first = 0
|
|
1068
|
+
while first < n:
|
|
1069
|
+
end = min(first + self.chunk, n)
|
|
1070
|
+
blocks = (end - first + _POINT_THREADS - 1) // _POINT_THREADS
|
|
1071
|
+
t0 = time.perf_counter()
|
|
1072
|
+
kernel((blocks,), (_POINT_THREADS,), (np.int64(first), np.int64(end), *self.points,
|
|
1073
|
+
*self.data, grads, np.float64(dtemp), np.float64(dt), old, new, stress,
|
|
1074
|
+
tangent, np.int32(guess), np.float64(ratio), np.int32(symmetric),
|
|
1075
|
+
self.failed)) # fmt: skip
|
|
1076
|
+
cp.cuda.get_current_stream().synchronize()
|
|
1077
|
+
elapsed = time.perf_counter() - t0
|
|
1078
|
+
if elapsed > 2.0 * _LAUNCH_S:
|
|
1079
|
+
self.chunk = max(self.chunk // 4, _CHUNK_MIN)
|
|
1080
|
+
elif elapsed < 0.5 * _LAUNCH_S and end - first == self.chunk:
|
|
1081
|
+
self.chunk = min(2 * self.chunk, _CHUNK_MAX)
|
|
1082
|
+
first = end
|
|
1083
|
+
failed = int(self.failed.get()[0])
|
|
1084
|
+
points = _mfront_points(self.problem)
|
|
1085
|
+
if points is not None and points.points.size:
|
|
1086
|
+
# MGIS integrates the points of MFront behaviours on the CPU
|
|
1087
|
+
idx = cp.asarray(points.points)
|
|
1088
|
+
nc = grads.shape[1]
|
|
1089
|
+
out = [cp.asnumpy(new[idx]), np.empty((idx.size, nc)), np.empty((idx.size, nc, nc))]
|
|
1090
|
+
failed += points.integrate_compact(cp.asnumpy(grads[idx]), dtemp, dt,
|
|
1091
|
+
cp.asnumpy(old[idx]), *out) # fmt: skip
|
|
1092
|
+
new[idx], stress[idx], tangent[idx] = (cp.asarray(a) for a in out)
|
|
1093
|
+
return failed
|
|
1094
|
+
|
|
1095
|
+
def stress_scale(self, sigma, wts) -> float:
|
|
1096
|
+
cp = _cupy()
|
|
1097
|
+
return self.weight * float(cp.sqrt((sigma * sigma).sum(axis=1)).sum())
|
|
1098
|
+
|
|
1099
|
+
def _nodal(self, s):
|
|
1100
|
+
"""Nodal forces of the point stresses ``s`` (on the GPU) and the norm of the
|
|
1101
|
+
contributions of the points before they cancel."""
|
|
1102
|
+
cp = _cupy()
|
|
1103
|
+
nx, ny, nz = self.shape
|
|
1104
|
+
out = cp.empty(self.n_q)
|
|
1105
|
+
_launch("voxel_forces", nx * ny * nz, (np.int32(nx), np.int32(ny), np.int32(nz),
|
|
1106
|
+
self.slot_gpu, s, np.float64(self.weight), self.g_gpu, np.int32(self.finite),
|
|
1107
|
+
out, self.squares)) # fmt: skip
|
|
1108
|
+
return out, math.sqrt(float(self.squares.sum()))
|
|
1109
|
+
|
|
1110
|
+
def _host_forces(self, s) -> np.ndarray:
|
|
1111
|
+
"""Nodal forces on the CPU, zero when they are round-off of uniform stresses."""
|
|
1112
|
+
cp = _cupy()
|
|
1113
|
+
out, scale = self._nodal(s)
|
|
1114
|
+
r = cp.asnumpy(out)
|
|
1115
|
+
if np.linalg.norm(r) <= NEGLIGIBLE * scale:
|
|
1116
|
+
r[:] = 0.0
|
|
1117
|
+
return r, scale
|
|
1118
|
+
|
|
1119
|
+
def forces(self, sigma, wts):
|
|
1120
|
+
cp = _cupy()
|
|
1121
|
+
if self.bbar:
|
|
1122
|
+
if self.finite:
|
|
1123
|
+
cp.copyto(self.stress, sigma)
|
|
1124
|
+
n_el = int(self.elem.size)
|
|
1125
|
+
_launch("voxel_effective", n_el, (np.int64(n_el), np.int32(self.finite), sigma,
|
|
1126
|
+
self.base)) # fmt: skip
|
|
1127
|
+
r, scale = self._host_forces(sigma)
|
|
1128
|
+
return r, self.weight * cp.asnumpy(sigma.sum(axis=0)), scale
|
|
1129
|
+
|
|
1130
|
+
def _increments(self, v, direction, tangent):
|
|
1131
|
+
nx, ny, nz = self.shape
|
|
1132
|
+
n_el = int(self.elem.size)
|
|
1133
|
+
_launch("voxel_increments", n_el, (np.int32(nx), np.int32(ny), np.int32(nz),
|
|
1134
|
+
np.int64(n_el), self.elem_gpu, v, direction, tangent, self.stress, self.base,
|
|
1135
|
+
self.g_gpu, np.int32(self.finite), np.int32(self.bbar), self.work)) # fmt: skip
|
|
1136
|
+
return self.work
|
|
1137
|
+
|
|
1138
|
+
def __call__(self, v):
|
|
1139
|
+
"""The tangent operator of the last linearization applied to the nodal
|
|
1140
|
+
displacements ``v`` (on the GPU)."""
|
|
1141
|
+
return self._nodal(self._increments(v, self.zero, self.tangent))[0]
|
|
1142
|
+
|
|
1143
|
+
def linearize(self, tangent, wts, first: bool, symmetric: bool | None = None):
|
|
1144
|
+
self.tangent = tangent
|
|
1145
|
+
symmetric = self.problem.symmetrize if symmetric is None else symmetric
|
|
1146
|
+
return KrylovSolver(self, self.tol, self.maxiter, symmetric, self.recycling)
|
|
1147
|
+
|
|
1148
|
+
def asymmetry(self, tangent, wts) -> float:
|
|
1149
|
+
"""The largest asymmetry of the tangents of the points of the phases
|
|
1150
|
+
(:func:`morphogen.homogenization.nonlinear.asymmetry`), their norms on the GPU."""
|
|
1151
|
+
cp = _cupy()
|
|
1152
|
+
n, nc = int(tangent.shape[0]), int(tangent.shape[1])
|
|
1153
|
+
if n == 0:
|
|
1154
|
+
return 0.0
|
|
1155
|
+
skew, full = cp.empty(n), cp.empty(n)
|
|
1156
|
+
_tangent_kernels()[0](tangent, np.int64(nc), skew, full)
|
|
1157
|
+
if self.phase_points is None:
|
|
1158
|
+
groups = np.asarray(self.problem.point_group)
|
|
1159
|
+
self.phase_points = [cp.asarray(np.flatnonzero(groups == g))
|
|
1160
|
+
for g in np.unique(groups)] # fmt: skip
|
|
1161
|
+
return max(_asymmetry_of(float(skew[p].sum()), float(full[p].sum()))
|
|
1162
|
+
for p in self.phase_points) # uniform weights # fmt: skip
|
|
1163
|
+
|
|
1164
|
+
def symmetrize(self, tangent) -> None:
|
|
1165
|
+
"""The tangents of the points replaced by their symmetric parts on the GPU, in place
|
|
1166
|
+
(to the bit those of the kernels that symmetrize them)."""
|
|
1167
|
+
n, nc = int(tangent.shape[0]), int(tangent.shape[1])
|
|
1168
|
+
_tangent_kernels()[1](np.int64(nc), tangent, size=n)
|
|
1169
|
+
|
|
1170
|
+
def macro_response(self, tangent, wts, v) -> np.ndarray:
|
|
1171
|
+
cp = _cupy()
|
|
1172
|
+
ds = self._increments(cp.asarray(np.ascontiguousarray(v, dtype=np.float64)), self.zero,
|
|
1173
|
+
tangent) # fmt: skip
|
|
1174
|
+
total = self.weight * cp.asnumpy(ds.sum(axis=0))
|
|
1175
|
+
return fin.macro_part(total) if self.finite else total
|
|
1176
|
+
|
|
1177
|
+
def coupling(self, tangent, wts, unknown):
|
|
1178
|
+
cp = _cupy()
|
|
1179
|
+
g = np.empty((self.n_q, unknown.size))
|
|
1180
|
+
kee = np.zeros((6, 6))
|
|
1181
|
+
for k, j in enumerate(unknown):
|
|
1182
|
+
unit = np.zeros(6)
|
|
1183
|
+
unit[j] = 1.0
|
|
1184
|
+
ds = self._increments(self.still, cp.asarray(self._macro(unit)), tangent)
|
|
1185
|
+
g[:, k] = self._host_forces(ds)[0]
|
|
1186
|
+
total = self.weight * cp.asnumpy(ds.sum(axis=0))
|
|
1187
|
+
kee[:, j] = fin.macro_part(total) if self.finite else total
|
|
1188
|
+
return g, kee
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
class KrylovSolver:
|
|
1192
|
+
"""Conjugate gradients of the tangent operator of :class:`VoxelPaths` on the GPU (GMRES
|
|
1193
|
+
if it is not ``symmetric``, recycling the space of ``recycle`` and stopped where it
|
|
1194
|
+
stagnates), with the interface of
|
|
1195
|
+
:class:`morphogen.homogenization.fft_nonlinear._KrylovSolver` (vectors on the CPU)."""
|
|
1196
|
+
|
|
1197
|
+
method = "fft-gpu"
|
|
1198
|
+
|
|
1199
|
+
def __init__(self, operator: VoxelPaths, tol: float, maxiter: int, symmetric: bool = True,
|
|
1200
|
+
recycle: Recycling | None = None): # fmt: skip
|
|
1201
|
+
self.operator = operator
|
|
1202
|
+
self.tol = float(tol)
|
|
1203
|
+
self.maxiter = int(maxiter)
|
|
1204
|
+
self.krylov = conjugate_gradient
|
|
1205
|
+
if not symmetric:
|
|
1206
|
+
self.krylov = partial(gmres, recycle=recycle, stagnation=GMRES_STAGNATION)
|
|
1207
|
+
if recycle is not None:
|
|
1208
|
+
recycle.renew()
|
|
1209
|
+
|
|
1210
|
+
def solve(self, rhs, x0=None, tol=None) -> tuple[np.ndarray, SolveInfo]:
|
|
1211
|
+
"""Solution of ``K x = rhs`` to the relative residual ``tol``, from ``x0`` if
|
|
1212
|
+
given; the mean of ``rhs`` (round-off) is removed."""
|
|
1213
|
+
cp = _cupy()
|
|
1214
|
+
t0 = time.perf_counter()
|
|
1215
|
+
b = np.asarray(rhs, dtype=float).reshape(-1, 3)
|
|
1216
|
+
b = (b - b.mean(axis=0)).ravel()
|
|
1217
|
+
n = b.size
|
|
1218
|
+
tol = self.tol if tol is None else float(tol)
|
|
1219
|
+
norm_b = float(np.linalg.norm(b))
|
|
1220
|
+
if norm_b == 0.0:
|
|
1221
|
+
return np.zeros(n), SolveInfo(self.method, n, 0, 0.0, 0.0, 0.0)
|
|
1222
|
+
b_gpu = cp.asarray(b)
|
|
1223
|
+
start = None if x0 is None or not np.any(x0) else cp.asarray(x0, dtype=np.float64)
|
|
1224
|
+
r = b_gpu if start is None else b_gpu - self.operator(start)
|
|
1225
|
+
norm_r = math.sqrt(dot(r, r))
|
|
1226
|
+
iterations, residual = 0, norm_r / norm_b
|
|
1227
|
+
if norm_r <= tol * norm_b:
|
|
1228
|
+
x = cp.zeros(n) if start is None else start
|
|
1229
|
+
else:
|
|
1230
|
+
op = self.operator
|
|
1231
|
+
x, iterations, rel = self.krylov(op, op.precondition, r, tol * norm_b / norm_r,
|
|
1232
|
+
self.maxiter) # fmt: skip
|
|
1233
|
+
residual = rel * norm_r / norm_b
|
|
1234
|
+
if start is not None:
|
|
1235
|
+
x += start
|
|
1236
|
+
if residual > 10 * tol:
|
|
1237
|
+
log.warning(
|
|
1238
|
+
"FFT solver stopped at relative residual %.2e (tolerance %.1e)", residual, tol
|
|
1239
|
+
)
|
|
1240
|
+
info = SolveInfo(self.method, n, iterations, residual, 0.0, time.perf_counter() - t0)
|
|
1241
|
+
return cp.asnumpy(x), info
|