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,295 @@
|
|
|
1
|
+
"""Homogenized properties of a periodic voxel cell with graded moduli, and their
|
|
2
|
+
sensitivities.
|
|
3
|
+
|
|
4
|
+
Every voxel ``e`` is a trilinear element of the base material with its stiffness (or
|
|
5
|
+
conductance) scaled by ``s_e``: ``K = sum_e s_e K0_e``. For each unit macroscopic strain
|
|
6
|
+
``E^i`` (temperature gradient in conduction), the periodic fluctuation ``w^i`` solves
|
|
7
|
+
``K w^i = -sum_e s_e K0_e u_aff^i`` (FFT-preconditioned conjugate gradients, see
|
|
8
|
+
:mod:`morphogen.homogenization.fft`; previous solutions are reused as initial guesses).
|
|
9
|
+
With ``u^i = u_aff^i + w^i``, the homogenized tensor and its derivatives are the
|
|
10
|
+
mutual energies
|
|
11
|
+
|
|
12
|
+
C_ij = (1 / V) sum_e s_e u^i_e . K0 u^j_e, dC_ij / ds_e = (1 / V) u^i_e . K0 u^j_e
|
|
13
|
+
|
|
14
|
+
(the problem is self-adjoint: no adjoint solve is needed; Sigmund 1994, Andreassen and
|
|
15
|
+
Andreasen 2014). Elastic tensors use the Voigt order 11, 22, 33, 23, 31, 12 with
|
|
16
|
+
engineering shear strains.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import time
|
|
22
|
+
|
|
23
|
+
import numpy as np
|
|
24
|
+
from numba import njit, prange
|
|
25
|
+
|
|
26
|
+
from morphogen.homogenization import kernels
|
|
27
|
+
from morphogen.homogenization.elements import hex_element
|
|
28
|
+
from morphogen.homogenization.fft import FourierPreconditioner
|
|
29
|
+
from morphogen.homogenization.materials import Material
|
|
30
|
+
from morphogen.homogenization.solvers import NEGLIGIBLE, dot, norm
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@njit(parallel=True, cache=True)
|
|
34
|
+
def apply_scaled(nx, ny, nz, nd, scale, mat, w, out):
|
|
35
|
+
"""out = sum_e scale_e K0 w_e on the periodic node grid (loop over nodes)."""
|
|
36
|
+
corners = kernels.CORNERS
|
|
37
|
+
size = 8 * nd
|
|
38
|
+
for n in prange(nx * ny * nz):
|
|
39
|
+
k = n % nz
|
|
40
|
+
j = (n // nz) % ny
|
|
41
|
+
i = n // (ny * nz)
|
|
42
|
+
acc = np.zeros(3)
|
|
43
|
+
we = np.empty(size)
|
|
44
|
+
for a in range(2):
|
|
45
|
+
ei = i - a if i - a >= 0 else nx - 1
|
|
46
|
+
for b in range(2):
|
|
47
|
+
ej = j - b if j - b >= 0 else ny - 1
|
|
48
|
+
for c in range(2):
|
|
49
|
+
ek = k - c if k - c >= 0 else nz - 1
|
|
50
|
+
s = scale[(ei * ny + ej) * nz + ek]
|
|
51
|
+
if s == 0.0:
|
|
52
|
+
continue
|
|
53
|
+
ln = kernels.CORNER_INDEX[a, b, c]
|
|
54
|
+
for q in range(8):
|
|
55
|
+
qi = ei + corners[q, 0]
|
|
56
|
+
qj = ej + corners[q, 1]
|
|
57
|
+
qk = ek + corners[q, 2]
|
|
58
|
+
if qi == nx:
|
|
59
|
+
qi = 0
|
|
60
|
+
if qj == ny:
|
|
61
|
+
qj = 0
|
|
62
|
+
if qk == nz:
|
|
63
|
+
qk = 0
|
|
64
|
+
m = (qi * ny + qj) * nz + qk
|
|
65
|
+
for c2 in range(nd):
|
|
66
|
+
we[q * nd + c2] = w[m * nd + c2]
|
|
67
|
+
for r in range(nd):
|
|
68
|
+
row = ln * nd + r
|
|
69
|
+
t = 0.0
|
|
70
|
+
for col in range(size):
|
|
71
|
+
t += mat[row, col] * we[col]
|
|
72
|
+
acc[r] += s * t
|
|
73
|
+
for r in range(nd):
|
|
74
|
+
out[n * nd + r] = acc[r]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@njit(parallel=True, cache=True)
|
|
78
|
+
def scaled_load(nx, ny, nz, nd, scale, v, out):
|
|
79
|
+
"""out = -sum_e scale_e v (v: element vector K0 u_aff, the same in every voxel)."""
|
|
80
|
+
for n in prange(nx * ny * nz):
|
|
81
|
+
k = n % nz
|
|
82
|
+
j = (n // nz) % ny
|
|
83
|
+
i = n // (ny * nz)
|
|
84
|
+
for r in range(nd):
|
|
85
|
+
out[n * nd + r] = 0.0
|
|
86
|
+
for a in range(2):
|
|
87
|
+
ei = i - a if i - a >= 0 else nx - 1
|
|
88
|
+
for b in range(2):
|
|
89
|
+
ej = j - b if j - b >= 0 else ny - 1
|
|
90
|
+
for c in range(2):
|
|
91
|
+
ek = k - c if k - c >= 0 else nz - 1
|
|
92
|
+
s = scale[(ei * ny + ej) * nz + ek]
|
|
93
|
+
if s == 0.0:
|
|
94
|
+
continue
|
|
95
|
+
ln = kernels.CORNER_INDEX[a, b, c]
|
|
96
|
+
for r in range(nd):
|
|
97
|
+
out[n * nd + r] -= s * v[ln * nd + r]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@njit(parallel=True, cache=True)
|
|
101
|
+
def element_energies(nx, ny, nz, nd, mat, w, uaff, out):
|
|
102
|
+
"""out[p, e] = u^i_e . K0 u^j_e for the pairs p = (i, j) of load cases (upper
|
|
103
|
+
triangle, row by row); ``w[i]`` are the fluctuations, ``uaff[i]`` the affine element
|
|
104
|
+
vectors."""
|
|
105
|
+
n_cases = w.shape[0]
|
|
106
|
+
size = 8 * nd
|
|
107
|
+
corners = kernels.CORNERS
|
|
108
|
+
for e in prange(nx * ny * nz):
|
|
109
|
+
k = e % nz
|
|
110
|
+
j = (e // nz) % ny
|
|
111
|
+
i = e // (ny * nz)
|
|
112
|
+
u = np.empty((n_cases, size))
|
|
113
|
+
for q in range(8):
|
|
114
|
+
qi = i + corners[q, 0]
|
|
115
|
+
qj = j + corners[q, 1]
|
|
116
|
+
qk = k + corners[q, 2]
|
|
117
|
+
if qi == nx:
|
|
118
|
+
qi = 0
|
|
119
|
+
if qj == ny:
|
|
120
|
+
qj = 0
|
|
121
|
+
if qk == nz:
|
|
122
|
+
qk = 0
|
|
123
|
+
m = (qi * ny + qj) * nz + qk
|
|
124
|
+
for c in range(n_cases):
|
|
125
|
+
for d in range(nd):
|
|
126
|
+
u[c, q * nd + d] = uaff[c, q * nd + d] + w[c, m * nd + d]
|
|
127
|
+
ku = np.empty((n_cases, size))
|
|
128
|
+
for c in range(n_cases):
|
|
129
|
+
for r in range(size):
|
|
130
|
+
t = 0.0
|
|
131
|
+
for col in range(size):
|
|
132
|
+
t += mat[r, col] * u[c, col]
|
|
133
|
+
ku[c, r] = t
|
|
134
|
+
p = 0
|
|
135
|
+
for a in range(n_cases):
|
|
136
|
+
for b in range(a, n_cases):
|
|
137
|
+
t = 0.0
|
|
138
|
+
for r in range(size):
|
|
139
|
+
t += u[a, r] * ku[b, r]
|
|
140
|
+
out[p, e] = t
|
|
141
|
+
p += 1
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _pcg(apply, precondition, rhs, x0, tol, maxiter):
|
|
145
|
+
"""Preconditioned conjugate gradient from ``x0``; relative residual to ``rhs``."""
|
|
146
|
+
norm_b = norm(rhs)
|
|
147
|
+
if norm_b == 0.0:
|
|
148
|
+
return np.zeros_like(rhs), 0, 0.0
|
|
149
|
+
x = x0.copy()
|
|
150
|
+
r = rhs - apply(x)
|
|
151
|
+
res = norm(r) / norm_b
|
|
152
|
+
if res <= tol:
|
|
153
|
+
return x, 0, res
|
|
154
|
+
z = precondition(r)
|
|
155
|
+
p = z.copy()
|
|
156
|
+
rz = dot(r, z)
|
|
157
|
+
it = 0
|
|
158
|
+
for it in range(1, maxiter + 1): # noqa: B007
|
|
159
|
+
ap = apply(p)
|
|
160
|
+
pap = dot(p, ap)
|
|
161
|
+
if pap <= 0.0:
|
|
162
|
+
break
|
|
163
|
+
alpha = rz / pap
|
|
164
|
+
x += alpha * p
|
|
165
|
+
r -= alpha * ap
|
|
166
|
+
res = norm(r) / norm_b
|
|
167
|
+
if res <= tol:
|
|
168
|
+
break
|
|
169
|
+
z = precondition(r)
|
|
170
|
+
rz_new = dot(r, z)
|
|
171
|
+
if rz_new <= 0.0: # the residual is round-off the preconditioner cancels
|
|
172
|
+
break
|
|
173
|
+
p *= rz_new / rz
|
|
174
|
+
p += z
|
|
175
|
+
rz = rz_new
|
|
176
|
+
return x, it, res
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
ELASTIC_CASES = 6
|
|
180
|
+
THERMAL_CASES = 3
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class PeriodicCell:
|
|
184
|
+
"""Periodic cell of ``shape`` voxels of edge ``spacing`` made of a base material whose
|
|
185
|
+
moduli are scaled voxel by voxel.
|
|
186
|
+
|
|
187
|
+
``physics`` is ``elastic`` (stiffness tensor 6 x 6) or ``thermal`` (conductivity
|
|
188
|
+
tensor 3 x 3). ``cases`` selects the unit loadings solved (default: all); the tensor
|
|
189
|
+
entries between unsolved cases are NaN.
|
|
190
|
+
"""
|
|
191
|
+
|
|
192
|
+
def __init__(
|
|
193
|
+
self,
|
|
194
|
+
shape,
|
|
195
|
+
spacing=(1.0, 1.0, 1.0),
|
|
196
|
+
*,
|
|
197
|
+
physics: str = "elastic",
|
|
198
|
+
material: Material | None = None,
|
|
199
|
+
cases=None,
|
|
200
|
+
tol: float = 1e-7,
|
|
201
|
+
maxiter: int | None = None,
|
|
202
|
+
):
|
|
203
|
+
if physics not in ("elastic", "thermal"):
|
|
204
|
+
raise ValueError("physics: elastic or thermal")
|
|
205
|
+
self.shape = tuple(int(n) for n in shape)
|
|
206
|
+
if len(self.shape) != 3 or min(self.shape) < 1:
|
|
207
|
+
raise ValueError("shape: three positive numbers of voxels")
|
|
208
|
+
self.spacing = tuple(float(h) for h in np.broadcast_to(spacing, (3,)))
|
|
209
|
+
self.physics = physics
|
|
210
|
+
self.material = material or Material(young=1.0, poisson=0.3, conductivity=1.0)
|
|
211
|
+
el = hex_element(self.spacing)
|
|
212
|
+
self.element = el
|
|
213
|
+
n_all = ELASTIC_CASES if physics == "elastic" else THERMAL_CASES
|
|
214
|
+
self.cases = tuple(range(n_all)) if cases is None else tuple(sorted(set(cases)))
|
|
215
|
+
if not self.cases or min(self.cases) < 0 or max(self.cases) >= n_all:
|
|
216
|
+
raise ValueError(f"cases: indices among 0..{n_all - 1}")
|
|
217
|
+
self.nd = 3 if physics == "elastic" else 1
|
|
218
|
+
if physics == "elastic":
|
|
219
|
+
lam, mu = self.material.lame
|
|
220
|
+
self.ka, self.kb = el.k_lambda, el.k_mu
|
|
221
|
+
self.coef = (lam, mu)
|
|
222
|
+
self.k0 = np.ascontiguousarray(lam * el.k_lambda + mu * el.k_mu)
|
|
223
|
+
basis = np.eye(6)
|
|
224
|
+
self.uaff = np.array([el.affine_displacement(basis[c]) for c in self.cases])
|
|
225
|
+
else:
|
|
226
|
+
k = self.material.conductivity
|
|
227
|
+
if k is None:
|
|
228
|
+
raise ValueError("thermal problems need the conductivity of the material")
|
|
229
|
+
self.ka, self.kb = el.k_thermal, np.zeros_like(el.k_thermal)
|
|
230
|
+
self.coef = (k, 0.0)
|
|
231
|
+
self.k0 = np.ascontiguousarray(k * el.k_thermal)
|
|
232
|
+
self.uaff = np.array([el.affine_temperature(np.eye(3)[c]) for c in self.cases])
|
|
233
|
+
self.n_voxels = int(np.prod(self.shape))
|
|
234
|
+
self.volume = self.n_voxels * float(np.prod(self.spacing))
|
|
235
|
+
self.tol = float(tol)
|
|
236
|
+
self.maxiter = maxiter or 20 * max(self.shape) + 1000
|
|
237
|
+
self._w = np.zeros((len(self.cases), self.nd * self.n_voxels))
|
|
238
|
+
self.iterations = 0
|
|
239
|
+
self.solve_time = 0.0
|
|
240
|
+
|
|
241
|
+
@property
|
|
242
|
+
def n_cases(self) -> int:
|
|
243
|
+
return len(self.cases)
|
|
244
|
+
|
|
245
|
+
def pairs(self) -> list[tuple[int, int]]:
|
|
246
|
+
"""Pairs of solved cases, in the order of the energies (upper triangle)."""
|
|
247
|
+
return [(a, b) for a in range(self.n_cases) for b in range(a, self.n_cases)]
|
|
248
|
+
|
|
249
|
+
def solve(self, scale) -> tuple[np.ndarray, np.ndarray]:
|
|
250
|
+
"""Homogenized tensor and the mutual energies ``(n_pairs, n_voxels)``, whose
|
|
251
|
+
products with ``1 / V`` are the derivatives ``dC_ij / ds_e``."""
|
|
252
|
+
s = np.ascontiguousarray(np.asarray(scale, dtype=float).reshape(-1))
|
|
253
|
+
if s.size != self.n_voxels or np.any(s < 0):
|
|
254
|
+
raise ValueError("scale: one non-negative value per voxel")
|
|
255
|
+
nx, ny, nz = self.shape
|
|
256
|
+
nd = self.nd
|
|
257
|
+
t0 = time.perf_counter()
|
|
258
|
+
active = s > 0
|
|
259
|
+
smin = float(s[active].min()) if active.any() else 1.0
|
|
260
|
+
sref = 0.5 * (smin + float(s.max(initial=1.0)))
|
|
261
|
+
pre = FourierPreconditioner(
|
|
262
|
+
self.shape, self.ka, self.kb, sref * self.coef[0], sref * self.coef[1], nd
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
def apply(v):
|
|
266
|
+
out = np.empty_like(v)
|
|
267
|
+
apply_scaled(nx, ny, nz, nd, s, self.k0, v, out)
|
|
268
|
+
return out
|
|
269
|
+
|
|
270
|
+
rhs = np.empty(nd * self.n_voxels)
|
|
271
|
+
for c in range(self.n_cases):
|
|
272
|
+
load = self.k0 @ self.uaff[c]
|
|
273
|
+
scaled_load(nx, ny, nz, nd, s, load, rhs)
|
|
274
|
+
if norm(rhs) <= NEGLIGIBLE * norm(s) * norm(load):
|
|
275
|
+
# the element loads cancel out (e.g. shear of a laminate): no fluctuation
|
|
276
|
+
w, it = np.zeros_like(rhs), 0
|
|
277
|
+
else:
|
|
278
|
+
w, it, _ = _pcg(apply, pre, rhs, self._w[c], self.tol, self.maxiter)
|
|
279
|
+
w = w.reshape(-1, nd)
|
|
280
|
+
self._w[c] = (w - w.mean(axis=0)).reshape(-1) # zero-mean fluctuation
|
|
281
|
+
self.iterations += it
|
|
282
|
+
energies = np.empty((len(self.pairs()), self.n_voxels))
|
|
283
|
+
element_energies(nx, ny, nz, nd, self.k0, self._w, self.uaff, energies)
|
|
284
|
+
n_all = ELASTIC_CASES if self.physics == "elastic" else THERMAL_CASES
|
|
285
|
+
tensor = np.full((n_all, n_all), np.nan)
|
|
286
|
+
for p, (a, b) in enumerate(self.pairs()):
|
|
287
|
+
value = float(s @ energies[p]) / self.volume
|
|
288
|
+
tensor[self.cases[a], self.cases[b]] = tensor[self.cases[b], self.cases[a]] = value
|
|
289
|
+
self.solve_time += time.perf_counter() - t0
|
|
290
|
+
return tensor, energies
|
|
291
|
+
|
|
292
|
+
def derivative(self, energies: np.ndarray, i: int, j: int) -> np.ndarray:
|
|
293
|
+
"""dC_ij / ds_e for all voxels (i, j: case indices of the full tensor)."""
|
|
294
|
+
a, b = sorted((self.cases.index(i), self.cases.index(j)))
|
|
295
|
+
return energies[self.pairs().index((a, b))] / self.volume
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""Regularization of density designs: filter, projection and symmetry.
|
|
2
|
+
|
|
3
|
+
* Density filter (Bruns and Tortorelli 2001, Bourdin 2001): weighted mean of the design
|
|
4
|
+
over a ball of radius ``r`` with weights ``r - distance`` (linear hat), periodic. It
|
|
5
|
+
sets a minimal length scale and removes checkerboards.
|
|
6
|
+
* Heaviside projection (Wang, Lazarov and Sigmund 2011) of the filtered densities:
|
|
7
|
+
``(tanh(beta eta) + tanh(beta (x - eta))) / (tanh(beta eta) + tanh(beta (1 - eta)))``,
|
|
8
|
+
sharper as ``beta`` grows (continuation).
|
|
9
|
+
* Symmetry: projection on the designs invariant by the mirror planes of the cell
|
|
10
|
+
(orthotropic), and by the permutations of the axes (cubic; square in 2D).
|
|
11
|
+
|
|
12
|
+
The three operators are linear (filter, symmetry) or diagonal (projection), with
|
|
13
|
+
their transposes applied to sensitivities.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import itertools
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
from morphogen.automata.lattice import kernel_sum, offsets
|
|
23
|
+
|
|
24
|
+
SYMMETRIES = ("none", "orthotropic", "cubic")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class DensityFilter:
|
|
28
|
+
"""Periodic linear-hat filter of radius ``radius`` voxels (planar for nz = 1)."""
|
|
29
|
+
|
|
30
|
+
def __init__(self, shape, radius: float):
|
|
31
|
+
self.shape = tuple(int(n) for n in shape)
|
|
32
|
+
self.radius = float(radius)
|
|
33
|
+
dims = 2 if self.shape[2] == 1 else 3
|
|
34
|
+
if self.radius < 1.0:
|
|
35
|
+
self.offsets = np.zeros((1, 3), dtype=np.int64)
|
|
36
|
+
self.weights = np.ones(1)
|
|
37
|
+
else:
|
|
38
|
+
self.offsets = offsets("euclidean", self.radius, dims=dims, include_center=True)
|
|
39
|
+
dist = np.linalg.norm(self.offsets, axis=1)
|
|
40
|
+
self.weights = np.maximum(0.0, self.radius - dist)
|
|
41
|
+
self.weights = self.weights / self.weights.sum()
|
|
42
|
+
|
|
43
|
+
def __call__(self, x: np.ndarray) -> np.ndarray:
|
|
44
|
+
if len(self.weights) == 1:
|
|
45
|
+
return np.asarray(x, dtype=float).copy()
|
|
46
|
+
return kernel_sum(
|
|
47
|
+
np.asarray(x, dtype=float).reshape(self.shape), self.offsets, self.weights
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
def transpose(self, g: np.ndarray) -> np.ndarray:
|
|
51
|
+
"""Transpose of the filter (the kernel mirrored: the same kernel)."""
|
|
52
|
+
if len(self.weights) == 1:
|
|
53
|
+
return np.asarray(g, dtype=float).copy()
|
|
54
|
+
return kernel_sum(
|
|
55
|
+
np.asarray(g, dtype=float).reshape(self.shape), -self.offsets, self.weights
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def heaviside(x, beta: float, eta: float = 0.5):
|
|
60
|
+
"""Smoothed Heaviside projection (identity-like for small beta)."""
|
|
61
|
+
x = np.asarray(x, dtype=float)
|
|
62
|
+
if beta <= 0:
|
|
63
|
+
return x.copy()
|
|
64
|
+
den = np.tanh(beta * eta) + np.tanh(beta * (1.0 - eta))
|
|
65
|
+
return (np.tanh(beta * eta) + np.tanh(beta * (x - eta))) / den
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def heaviside_derivative(x, beta: float, eta: float = 0.5):
|
|
69
|
+
"""The derivative of the smoothed Heaviside projection (:func:`heaviside`) with respect to the
|
|
70
|
+
density, of sharpness ``beta`` and threshold ``eta``; 1 without projection (``beta`` ≤ 0)."""
|
|
71
|
+
x = np.asarray(x, dtype=float)
|
|
72
|
+
if beta <= 0:
|
|
73
|
+
return np.ones_like(x)
|
|
74
|
+
den = np.tanh(beta * eta) + np.tanh(beta * (1.0 - eta))
|
|
75
|
+
return beta * (1.0 - np.tanh(beta * (x - eta)) ** 2) / den
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class Symmetry:
|
|
79
|
+
"""Projection on the designs invariant by a symmetry group of the cell."""
|
|
80
|
+
|
|
81
|
+
def __init__(self, shape, kind: str = "none"):
|
|
82
|
+
if kind not in SYMMETRIES:
|
|
83
|
+
raise ValueError(f"symmetry: one of {SYMMETRIES}")
|
|
84
|
+
self.shape = tuple(int(n) for n in shape)
|
|
85
|
+
self.kind = kind
|
|
86
|
+
planar = self.shape[2] == 1
|
|
87
|
+
axes = (0, 1) if planar else (0, 1, 2)
|
|
88
|
+
if kind == "cubic":
|
|
89
|
+
n = {self.shape[a] for a in axes}
|
|
90
|
+
if len(n) != 1:
|
|
91
|
+
raise ValueError("cubic symmetry needs a cubic (square in 2D) grid")
|
|
92
|
+
self.ops = []
|
|
93
|
+
if kind == "none":
|
|
94
|
+
return
|
|
95
|
+
perms = list(itertools.permutations(axes)) if kind == "cubic" else [axes]
|
|
96
|
+
for perm in perms:
|
|
97
|
+
for flips in itertools.product((False, True), repeat=len(axes)):
|
|
98
|
+
self.ops.append((perm, flips))
|
|
99
|
+
|
|
100
|
+
def __call__(self, x: np.ndarray) -> np.ndarray:
|
|
101
|
+
x = np.asarray(x, dtype=float).reshape(self.shape)
|
|
102
|
+
if not self.ops:
|
|
103
|
+
return x.copy()
|
|
104
|
+
acc = np.zeros(self.shape)
|
|
105
|
+
planar = self.shape[2] == 1
|
|
106
|
+
for perm, flips in self.ops:
|
|
107
|
+
full = tuple(perm) + ((2,) if planar else ())
|
|
108
|
+
y = np.transpose(x, full)
|
|
109
|
+
for a, f in enumerate(flips):
|
|
110
|
+
if f:
|
|
111
|
+
y = np.flip(y, axis=a)
|
|
112
|
+
acc += y
|
|
113
|
+
return acc / len(self.ops)
|
|
114
|
+
|
|
115
|
+
transpose = __call__ # an orthogonal projection is self-adjoint
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""Method of moving asymptotes (Svanberg 1987, 2002).
|
|
2
|
+
|
|
3
|
+
Each iteration replaces the problem
|
|
4
|
+
|
|
5
|
+
minimize f0(x) subject to fi(x) <= 0 (i = 1..m), xmin <= x <= xmax
|
|
6
|
+
|
|
7
|
+
by a convex separable approximation built on moving asymptotes ``low < x < upp`` and
|
|
8
|
+
solves it by a primal-dual interior-point method, as in Svanberg's notes "MMA and GCMMA,
|
|
9
|
+
two methods for nonlinear optimization" (2007), with artificial variables ``y_i`` of
|
|
10
|
+
cost ``c_i y_i + d_i y_i^2 / 2`` that make every subproblem feasible.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class MMA:
|
|
19
|
+
"""Optimizer state: call :meth:`update` with the current point, objective and
|
|
20
|
+
constraint values and gradients; it returns the next point."""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
n: int,
|
|
25
|
+
m: int,
|
|
26
|
+
xmin,
|
|
27
|
+
xmax,
|
|
28
|
+
*,
|
|
29
|
+
move: float = 0.5,
|
|
30
|
+
asyinit: float = 0.5,
|
|
31
|
+
asyincr: float = 1.2,
|
|
32
|
+
asydecr: float = 0.7,
|
|
33
|
+
c: float = 1000.0,
|
|
34
|
+
):
|
|
35
|
+
self.n, self.m = int(n), int(m)
|
|
36
|
+
if self.m < 1 or self.m >= self.n:
|
|
37
|
+
raise ValueError("MMA here needs 1 <= m < n constraints")
|
|
38
|
+
self.xmin = np.broadcast_to(np.asarray(xmin, dtype=float), (self.n,)).copy()
|
|
39
|
+
self.xmax = np.broadcast_to(np.asarray(xmax, dtype=float), (self.n,)).copy()
|
|
40
|
+
self.move = float(move)
|
|
41
|
+
self.asyinit, self.asyincr, self.asydecr = asyinit, asyincr, asydecr
|
|
42
|
+
self.a0 = 1.0
|
|
43
|
+
self.a = np.zeros(self.m)
|
|
44
|
+
self.c = np.full(self.m, float(c))
|
|
45
|
+
self.d = np.ones(self.m)
|
|
46
|
+
self.iteration = 0
|
|
47
|
+
self.xold1 = None
|
|
48
|
+
self.xold2 = None
|
|
49
|
+
self.low = None
|
|
50
|
+
self.upp = None
|
|
51
|
+
|
|
52
|
+
def reset(self) -> None:
|
|
53
|
+
"""Forget the asymptotes after a change of the problem (a continuation step):
|
|
54
|
+
asymptotes tightened by past oscillations would hold the design in place."""
|
|
55
|
+
self.iteration = 0
|
|
56
|
+
self.xold1 = None
|
|
57
|
+
self.xold2 = None
|
|
58
|
+
|
|
59
|
+
def update(self, x, f0, df0, fval, dfdx) -> np.ndarray:
|
|
60
|
+
x = np.asarray(x, dtype=float).copy()
|
|
61
|
+
df0 = np.asarray(df0, dtype=float).reshape(self.n)
|
|
62
|
+
fval = np.atleast_1d(np.asarray(fval, dtype=float))
|
|
63
|
+
dfdx = np.asarray(dfdx, dtype=float).reshape(self.m, self.n)
|
|
64
|
+
self.iteration += 1
|
|
65
|
+
span = self.xmax - self.xmin
|
|
66
|
+
if self.iteration <= 2 or self.xold2 is None:
|
|
67
|
+
self.low = x - self.asyinit * span
|
|
68
|
+
self.upp = x + self.asyinit * span
|
|
69
|
+
else:
|
|
70
|
+
sign = (x - self.xold1) * (self.xold1 - self.xold2)
|
|
71
|
+
factor = np.ones(self.n)
|
|
72
|
+
factor[sign > 0] = self.asyincr
|
|
73
|
+
factor[sign < 0] = self.asydecr
|
|
74
|
+
self.low = x - factor * (self.xold1 - self.low)
|
|
75
|
+
self.upp = x + factor * (self.upp - self.xold1)
|
|
76
|
+
self.low = np.clip(self.low, x - 10.0 * span, x - 0.01 * span)
|
|
77
|
+
self.upp = np.clip(self.upp, x + 0.01 * span, x + 10.0 * span)
|
|
78
|
+
albefa = 0.1
|
|
79
|
+
alpha = np.maximum.reduce(
|
|
80
|
+
[self.xmin, self.low + albefa * (x - self.low), x - self.move * span]
|
|
81
|
+
)
|
|
82
|
+
beta = np.minimum.reduce(
|
|
83
|
+
[self.xmax, self.upp - albefa * (self.upp - x), x + self.move * span]
|
|
84
|
+
)
|
|
85
|
+
xmami = np.maximum(span, 1e-5)
|
|
86
|
+
ux1 = self.upp - x
|
|
87
|
+
xl1 = x - self.low
|
|
88
|
+
ux2, xl2 = ux1 * ux1, xl1 * xl1
|
|
89
|
+
raa0 = 1e-5
|
|
90
|
+
p0 = np.maximum(df0, 0.0)
|
|
91
|
+
q0 = np.maximum(-df0, 0.0)
|
|
92
|
+
pq0 = 0.001 * (p0 + q0) + raa0 / xmami
|
|
93
|
+
p0 = (p0 + pq0) * ux2
|
|
94
|
+
q0 = (q0 + pq0) * xl2
|
|
95
|
+
p = np.maximum(dfdx, 0.0)
|
|
96
|
+
q = np.maximum(-dfdx, 0.0)
|
|
97
|
+
pq = 0.001 * (p + q) + raa0 / xmami
|
|
98
|
+
p = (p + pq) * ux2
|
|
99
|
+
q = (q + pq) * xl2
|
|
100
|
+
b = p @ (1.0 / ux1) + q @ (1.0 / xl1) - fval
|
|
101
|
+
xnew = _subsolve(
|
|
102
|
+
self.m,
|
|
103
|
+
self.n,
|
|
104
|
+
self.low,
|
|
105
|
+
self.upp,
|
|
106
|
+
alpha,
|
|
107
|
+
beta,
|
|
108
|
+
p0,
|
|
109
|
+
q0,
|
|
110
|
+
p,
|
|
111
|
+
q,
|
|
112
|
+
self.a0,
|
|
113
|
+
self.a,
|
|
114
|
+
b,
|
|
115
|
+
self.c,
|
|
116
|
+
self.d,
|
|
117
|
+
)
|
|
118
|
+
self.xold2 = self.xold1
|
|
119
|
+
self.xold1 = x
|
|
120
|
+
return xnew
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _subsolve(
|
|
124
|
+
m, n, low, upp, alpha, beta, p0, q0, p, q, a0, a, b, c, d, epsimin: float = 1e-7
|
|
125
|
+
) -> np.ndarray:
|
|
126
|
+
"""Primal-dual Newton solution of the MMA subproblem (Svanberg's subsolv)."""
|
|
127
|
+
een = np.ones(n)
|
|
128
|
+
eem = np.ones(m)
|
|
129
|
+
epsi = 1.0
|
|
130
|
+
x = 0.5 * (alpha + beta)
|
|
131
|
+
y = eem.copy()
|
|
132
|
+
z = 1.0
|
|
133
|
+
lam = eem.copy()
|
|
134
|
+
xsi = np.maximum(een / (x - alpha), een)
|
|
135
|
+
eta = np.maximum(een / (beta - x), een)
|
|
136
|
+
mu = np.maximum(eem, 0.5 * c)
|
|
137
|
+
zet = 1.0
|
|
138
|
+
s = eem.copy()
|
|
139
|
+
|
|
140
|
+
def residual(x, y, z, lam, xsi, eta, mu, zet, s, epsi):
|
|
141
|
+
ux1 = upp - x
|
|
142
|
+
xl1 = x - low
|
|
143
|
+
plam = p0 + p.T @ lam
|
|
144
|
+
qlam = q0 + q.T @ lam
|
|
145
|
+
gvec = p @ (1.0 / ux1) + q @ (1.0 / xl1)
|
|
146
|
+
dpsidx = plam / ux1**2 - qlam / xl1**2
|
|
147
|
+
return np.concatenate(
|
|
148
|
+
[
|
|
149
|
+
dpsidx - xsi + eta,
|
|
150
|
+
c + d * y - mu - lam,
|
|
151
|
+
[a0 - zet - a @ lam],
|
|
152
|
+
gvec - a * z - y + s - b,
|
|
153
|
+
xsi * (x - alpha) - epsi,
|
|
154
|
+
eta * (beta - x) - epsi,
|
|
155
|
+
mu * y - epsi,
|
|
156
|
+
[zet * z - epsi],
|
|
157
|
+
lam * s - epsi,
|
|
158
|
+
]
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
while epsi > epsimin:
|
|
162
|
+
res = residual(x, y, z, lam, xsi, eta, mu, zet, s, epsi)
|
|
163
|
+
resnorm = np.linalg.norm(res)
|
|
164
|
+
resmax = np.max(np.abs(res))
|
|
165
|
+
inner = 0
|
|
166
|
+
while resmax > 0.9 * epsi and inner < 200:
|
|
167
|
+
inner += 1
|
|
168
|
+
ux1 = upp - x
|
|
169
|
+
xl1 = x - low
|
|
170
|
+
ux2, xl2 = ux1**2, xl1**2
|
|
171
|
+
ux3, xl3 = ux1 * ux2, xl1 * xl2
|
|
172
|
+
plam = p0 + p.T @ lam
|
|
173
|
+
qlam = q0 + q.T @ lam
|
|
174
|
+
gvec = p @ (1.0 / ux1) + q @ (1.0 / xl1)
|
|
175
|
+
gg = p / ux2 - q / xl2 # (m, n)
|
|
176
|
+
dpsidx = plam / ux2 - qlam / xl2
|
|
177
|
+
delx = dpsidx - epsi / (x - alpha) + epsi / (beta - x)
|
|
178
|
+
dely = c + d * y - lam - epsi / y
|
|
179
|
+
delz = a0 - a @ lam - epsi / z
|
|
180
|
+
dellam = gvec - a * z - y - b + epsi / lam
|
|
181
|
+
diagx = 2.0 * (plam / ux3 + qlam / xl3) + xsi / (x - alpha) + eta / (beta - x)
|
|
182
|
+
diagy = d + mu / y
|
|
183
|
+
diaglamyi = s / lam + 1.0 / diagy
|
|
184
|
+
blam = dellam + dely / diagy - gg @ (delx / diagx)
|
|
185
|
+
alam = np.diag(diaglamyi) + (gg / diagx) @ gg.T
|
|
186
|
+
aa = np.zeros((m + 1, m + 1))
|
|
187
|
+
aa[:m, :m] = alam
|
|
188
|
+
aa[:m, m] = a
|
|
189
|
+
aa[m, :m] = a
|
|
190
|
+
aa[m, m] = -zet / z
|
|
191
|
+
sol = np.linalg.solve(aa, np.concatenate([blam, [delz]]))
|
|
192
|
+
dlam, dz = sol[:m], sol[m]
|
|
193
|
+
dx = -delx / diagx - (gg.T @ dlam) / diagx
|
|
194
|
+
dy = -dely / diagy + dlam / diagy
|
|
195
|
+
dxsi = -xsi + epsi / (x - alpha) - xsi * dx / (x - alpha)
|
|
196
|
+
deta = -eta + epsi / (beta - x) + eta * dx / (beta - x)
|
|
197
|
+
dmu = -mu + epsi / y - mu * dy / y
|
|
198
|
+
dzet = -zet + epsi / z - zet * dz / z
|
|
199
|
+
ds = -s + epsi / lam - s * dlam / lam
|
|
200
|
+
xx = np.concatenate([y, [z], lam, xsi, eta, mu, [zet], s])
|
|
201
|
+
dxx = np.concatenate([dy, [dz], dlam, dxsi, deta, dmu, [dzet], ds])
|
|
202
|
+
stmxx = np.max(-1.01 * dxx / xx)
|
|
203
|
+
stmalfa = np.max(-1.01 * dx / (x - alpha))
|
|
204
|
+
stmbeta = np.max(1.01 * dx / (beta - x))
|
|
205
|
+
step = 1.0 / max(stmxx, stmalfa, stmbeta, 1.0)
|
|
206
|
+
old = (x, y, z, lam, xsi, eta, mu, zet, s)
|
|
207
|
+
newnorm = 2.0 * resnorm
|
|
208
|
+
tries = 0
|
|
209
|
+
while newnorm > resnorm and tries < 50:
|
|
210
|
+
tries += 1
|
|
211
|
+
x = old[0] + step * dx
|
|
212
|
+
y = old[1] + step * dy
|
|
213
|
+
z = old[2] + step * dz
|
|
214
|
+
lam = old[3] + step * dlam
|
|
215
|
+
xsi = old[4] + step * dxsi
|
|
216
|
+
eta = old[5] + step * deta
|
|
217
|
+
mu = old[6] + step * dmu
|
|
218
|
+
zet = old[7] + step * dzet
|
|
219
|
+
s = old[8] + step * ds
|
|
220
|
+
res = residual(x, y, z, lam, xsi, eta, mu, zet, s, epsi)
|
|
221
|
+
newnorm = np.linalg.norm(res)
|
|
222
|
+
step *= 0.5
|
|
223
|
+
resnorm = newnorm
|
|
224
|
+
resmax = np.max(np.abs(res))
|
|
225
|
+
epsi *= 0.1
|
|
226
|
+
return x
|