Topology optimization with code_aster: a 100-line SIMP loop, validated against top3d and run on cloudHPC

Published by Ruggero Poletto on

SIMP topology optimization of a 3D cantilever with code_aster, 307,200 elements: density iso-surface over 200 iterations, then the final design coloured by von Mises stress

TL;DR

  • code_aster has no topology optimization module, but its Python API is enough to build one: a SIMP loop (density filter + Optimality Criteria) in about 100 lines of .comm file, with code_aster doing every finite-element solve and numpy doing the filter and the update.
  • Validated against top3d (Liu & Tovar 2014) on the 60ร—20ร—4 cantilever: the compliance agrees to round-off (~1e-11) for the first 40 iterations, and the final designs differ by 0.04 % in compliance, with 99.8 % of the cells on the same side of ฯ = 0.5.
  • The same file scaled to 307k elements (~1.0 M DOF) with only the size changed: 200 iterations in 5.5 h on 16 vCPU, for โ‚ฌ9.09, with the same members as the coarse mesh (95.8 % of cells agree).
  • Memory beats cores: with enough RAM for MUMPS to stay in-core (~40 GB at 1 M DOF), 16 vCPU standard runs an iteration in 99 s; with 16โ€“32 GB it silently goes out-of-core and takes 194โ€“367 s. 64 vCPU were no faster than 16.
  • Files to run it yourself are at the end: a single simp.comm + simp.export, no mesh file needed.
SIMP topology optimization of a 3D cantilever with code_aster, 307,200 elements: density iso-surface over 200 iterations, then the final design coloured by von Mises stress

Why topology optimization in code_aster

Topology optimization finds where material should go: you give it a design space, loads, supports and a material budget, and it returns the stiffest layout. Commercial FEA suites (OptiStruct, Abaqus/Tosca, ANSYS) have it built in. In the open-source world the options are mostly educational codes (the 99-line and 88-line MATLAB codes, top3d, FEniCS and Python ports) or BESO for CalculiX (beso, FreeCAD integration).

code_aster, the EDF solver we run on cloudHPC, has no topology optimization operator. But a code_aster 17 study is a Python script, and fields can be read and written from Python as plain arrays. That is all a density-based method needs: solve, read the strain energy of each element, update the densities, solve again. We wanted to know three things:

  1. Can it be done cleanly, without one material group per element?
  2. Does it give the same answer as a reference SIMP code?
  3. How does it behave at a size that needs a cloud machine?

Results at a glance

CaseElementsDOF (incl. clamped)IterationsFinal complianceInstanceWall timeCost
A โ€“ validation 60ร—20ร—4 (top3d reference, numpy)4,80019,2151982416.8workstation, 2 cores181 sโ€“
A โ€“ validation 60ร—20ร—4 (code_aster)4,80019,2151462417.74 vCPU highcpu213 sโ‚ฌ0.058
C โ€“ fine mesh 240ร—80ร—16 (code_aster)307,200995,571200 (limit)6923.816 vCPU standard5.5 hโ‚ฌ9.09

The benchmark

Setup sketch of the 3D cantilever benchmark

We used the default example of top3d (Liu & Tovar 2014), the 3D version of the classic 88-line code:

  • design domain of unit hexahedra, nelx ร— nely ร— nelz = 60 ร— 20 ร— 4 (case A) and the same domain at 4ร— the resolution, 240 ร— 80 ร— 16 (case C);
  • face x = 0 clamped; unit downward nodal loads on every node of the edge x = L, y = 0;
  • E0 = 1, Emin = 1e-9, ฮฝ = 0.3, SIMP penalty p = 3, volume fraction 0.3;
  • density filter radius rmin = 1.5 elements (case A) and 6 elements in case C, so that the filter has the same physical size and the two designs can be compared;
  • Optimality Criteria update with move limit 0.2, stop when the largest density change is below 0.01 (max 200 iterations).

As the reference we wrote a line-by-line numpy port of top3d (same element matrix, same filter, same OC bisection). Because code_aster’s 8-node hexahedron (HEXA8, 2ร—2ร—2 Gauss points) has exactly the same stiffness matrix as top3d’s analytic lk_H8, the two codes should give the same numbers, not just similar ones. That makes it a strict test.

How the code_aster implementation works

The whole optimizer is one .comm file. The key ideas:

1. One Young’s modulus per element without one material per element. The obvious route โ€” one AFFE_MATERIAU group per element, or per density bin โ€” does not scale. Instead the modulus is a function of the external state variable NEUT1:

fE  = DEFI_FONCTION(NOM_PARA='NEUT1', VALE=(0., 0., 1., 1.))      # E = NEUT1
mat = DEFI_MATERIAU(ELAS_FO=_F(E=fE, NU=DEFI_CONSTANTE(VALE=0.3)))
efield = CREA_CHAMP(TYPE_CHAM='ELEM_NEUT_R', OPERATION='AFFE', MODELE=model,
                    PROL_ZERO='OUI', AFFE=_F(TOUT='OUI', NOM_CMP='X1', VALE=1.0))

At every iteration numpy writes E = Emin + x^p (E0 โˆ’ Emin) into the cell field with efield.setValues(...) and the material field is rebuilt with AFFE_VARC(NOM_VARC='NEUT1', CHAM_GD=efield). We use an ELEM_NEUT_R field (one value per cell); NEUT_R fields carry 30 components per cell, so the array passed to setValues is cells ร— 30 with the value in the first column.

2. Sensitivities come from the element strain energy. For minimum compliance the problem is self-adjoint, so no adjoint solve is needed:

res  = MECA_STATIQUE(MODELE=model, CHAM_MATER=chm, EXCIT=_F(CHARGE=load))
res  = CALC_CHAMP(reuse=res, RESULTAT=res, ENERGIE=('EPOT_ELEM',))
epot = np.array(res.getField('EPOT_ELEM', 1).getValues())
c    = 2 * epot.sum()                        # compliance
dc   = -p * (E0 - Emin) * x**(p-1) * 2 * epot / E

3. Filter and update in numpy. On a structured grid the cone filter of top3d is a 3D convolution, done with numpy.fft (no scipy needed, and memory stays small even with rmin = 6, where a sparse filter matrix would need ~900 entries per element). The OC update with bisection on the volume constraint is the textbook one.

4. The mesh is built inside the .comm with MEDCoupling (MEDCouplingCMesh โ†’ Mesh.buildFromMedCouplingMesh), so a case is just two files and the cell order is known (i fastest, then j, then k), which is what the FFT filter needs.

What went wrong on the way

  • Floating-point traps. code_aster enables hardware floating-point exceptions, so a harmless underflow inside numpy (x**3 of a density near zero) stopped the run with a floating-point exception reported as a crash of code_aster. Wrap the numpy part in code_aster.Utilities.ExecutionParameter.disable_fpe().
  • MUMPS singularity checks. With Emin = 1e-9 the stiffness matrix spans nine orders of magnitude; the solve is fine, but the default singularity detection must be relaxed (NPREC=-1, RESI_RELA=-1).
  • Low-rank MUMPS (ACCELERATION='LR+') was not available in the conda-forge build we used for local tests: code_aster fell back to LR, and a 6-iteration test was still running after 10 minutes, against ~1 min with full rank. We kept the default full-rank factorization and did not test it further on cloudHPC.
  • A bug in our own reference. The first comparison drifted from iteration 2. The cause was in the numpy reference, not in code_aster: the KD-tree already returned the self pair and we added the diagonal of the filter matrix a second time. Matching to 1e-11 makes this kind of error impossible to miss.

Case A โ€“ validation against top3d

Compliance history and relative difference, code_aster vs top3d

top3d reference (numpy)code_aster 17.3.17 (cloudHPC)
Compliance, iteration 128354.779928354.7799
Max. relative difference, iterations 1โ€“402.1e-11
Iterations to convergence (change < 0.01)198146
Final compliance2416.842417.75 (0.04 %)
Cells on the same side of ฯ = 0.599.8 %
Final designs: top3d reference vs code_aster

What we learned. The two codes are numerically the same algorithm: identical to ~1e-11 for 40 iterations. After that the difference peaks at a few 1e-3 (iteration ~70) and settles at ~1e-5โ€“1e-4: the OC loop is an iteration on a non-convex problem, and round-off differences (different solvers, summation order, FFT vs sparse filter) are amplified once the design starts to pick between nearly equivalent members. The final layouts have the same members (99.8 % of cells agree), and the compliance differs by 0.04 %. The run converged in 146 iterations on cloudHPC and in 150 on our workstation, which is the same effect.

Case C โ€“ 307k elements on one 16-vCPU machine

Final design, 240ร—80ร—16, iso-surface ฯ = 0.5

The same .comm file, with only NELX, NELY, NELZ = 240, 80, 16 and RMIN = 6 changed, gives a model of 307,200 elements and 995,571 DOF (1,003,833 equations with the Lagrange multipliers of the clamp). It ran on 16 vCPU standard (64 GB, MUMPS in-core, peak 39 GiB) at 99 s per iteration.

Compliance and largest density change per iteration, case C

Case A (60ร—20ร—4)Case C (240ร—80ร—16)
Elements / DOF4,800 / 19,215307,200 / 995,571
Filter radius1.5 elements6 elements (same physical size)
Iterations146 (converged, change < 0.01)200 (iteration limit)
Compliance change over the last 50 iterationsโ€“0.16 %
Cells with 0.1 < ฯ < 0.9 (“grey”)38 %31 %
Same solid/void layout as case A (fine mesh averaged on the coarse grid)95.8 %
Wall time / cost213 s / โ‚ฌ0.0585.5 h / โ‚ฌ9.09
Coarse vs fine design

Final design coloured by von Mises stress in the material

Stress check. From the final displacement field (result.med) we computed the von Mises stress the solid material sees (ฯƒ = D0ยทฮต, E0 = 1, normalised units, averaged over the 8 Gauss points of each element); the compliance recomputed from those displacements matches the run (6923.8). In the solid cells (ฯ > 0.5) the median is 0.33 and the 99th percentile 0.53: the members carry an almost uniform stress, as expected from a minimum-compliance design. The peaks (up to 2.2) sit at the clamped corners and at the loaded edge, where point loads and fixed supports give singular stresses. This is a post-processing check: the optimization itself has no stress constraint.

What we learned.

  • Same structure at 64ร— the elements. With the filter radius scaled to the same physical size, the fine design has the same members as the coarse one (95.8 % of cells agree after averaging; the rest are member edges). This is what the density filter is for: the design depends on the filter size, not on the mesh. The fine mesh draws the members with clean, closed cross-sections; on the coarse mesh some members are only 1โ€“2 elements thick and the ฯ = 0.5 iso-surface cuts them.
  • Compliance is not comparable between A and C. Case C is a larger domain with 17 loaded nodes instead of 5, so the absolute value is different. Compare the designs, not the numbers.
  • It did not meet the 0.01 change criterion in 200 iterations. The compliance was flat (0.16 % over the last 50 iterations) and the layout barely changed (0.3 % of the cells crossed ฯ = 0.5 between iterations 150 and 200), but the largest change per iteration stayed at ~0.1: OC with move limit 0.2 keeps a few boundary cells flipping. In practice: stop on the compliance change, or add continuation on the penalty and a projection (Heaviside) step if you need a crisp 0/1 design. With a density filter and no projection, a grey band around each member is expected: 31 % of the cells have 0.1 < ฯ < 0.9 (case A: 38 %).
  • At 64ร— the elements, one 16-vCPU machine runs ~36 iterations per hour. 200 iterations took 5.5 h. 83 % of each iteration is MECA_STATIQUE, 16 s the strain energy; the numpy filter (FFT on a 250ร—90ร—26 padded grid) and the OC bisection take 0.6 s per iteration.

Run times, scalability and costs

Each SIMP iteration is one static solve of a 1.0 M DOF system with MUMPS (direct solver, one MPI process, OpenMP threads = vCPU / 2), plus the strain energy and a few numpy operations. On the production run, MECA_STATIQUE (assembly + MUMPS) took 83 % of each iteration, the strain energy (CALC_CHAMP) about 16 s and the numpy filter + OC update only 0.6 s. We timed iterations 2โ€“3 of the 240ร—80ร—16 case on five machine configurations and repeated the two that looked odd. The repeats matched within 1.2 %.

Time and cost per SIMP iteration vs vCPU, in-core and out-of-core MUMPS

Instance (n2d, regular)RAMOpenMP threadsPeak memory (GiB, VmPeak)MUMPS modes / iterationcost / iteration
8 vCPU standard32 GB48 GiBout-of-core199โ‚ฌ0.044
16 vCPU highcpu16 GB89 GiBout-of-core367โ‚ฌ0.131
16 vCPU standard64 GB839 GiBin-core99โ‚ฌ0.044
32 vCPU highcpu32 GB1611 GiBout-of-core194โ‚ฌ0.138
64 vCPU highcpu64 GB3244 GiBin-core97โ‚ฌ0.139

What we learned: for a big direct solve, buy memory before cores. code_aster’s default GESTION_MEMOIRE='AUTO' lets MUMPS write the factors to disk when they do not fit in RAM. The run does not fail and we found no explicit message about it in the .mess, but each iteration is 2โ€“4ร— slower. You can spot it from the memory peak: about 40 GB when the 1 M DOF factorization is in memory, only 8โ€“11 GB on the slow machines. (We infer the out-of-core switch from these peaks and timings; the RAM of the machine, not the thread count, separates the fast runs from the slow ones.) Once in-core, more threads help little: 64 highcpu vCPU are no faster than 16 standard vCPU (97 vs 99 s) and cost 3.2ร— more per iteration. The 16-vCPU highcpu machine was the worst of all: same threads as the 16 standard, a quarter of the RAM, 3.7ร— slower.

This is also why our production run moved. We first launched case C on 32 vCPU highcpu, following our usual “start on highcpu” rule for FEA. It ran out-of-core at 195โ€“230 s per iteration. After the scaling runs we stopped it at iteration 31 (โ‚ฌ5.13 billed) and relaunched on 16 vCPU standard, which finished the whole optimization in 5.5 h for โ‚ฌ9.09.

RunInstanceBilledCost
A โ€“ validation 60ร—20ร—4 (full run)4 vCPU highcpu0.7 vCPU-hโ‚ฌ0.06
B โ€“ sizing 240ร—80ร—16, 3 iterations32 vCPU highcpu9.7 vCPU-hโ‚ฌ0.77
Scaling, 3 iterations8 vCPU standard2.4 vCPU-hโ‚ฌ0.24
Scaling, repeat8 vCPU standard2.5 vCPU-hโ‚ฌ0.25
Scaling, 3 iterations16 vCPU highcpu7.6 vCPU-hโ‚ฌ0.61
Scaling, repeat16 vCPU highcpu7.4 vCPU-hโ‚ฌ0.59
Scaling, 3 iterations16 vCPU standard3.9 vCPU-hโ‚ฌ0.39
Scaling, 3 iterations64 vCPU highcpu17.4 vCPU-hโ‚ฌ1.39
C โ€“ first attempt, stopped at it. 31 (out-of-core)32 vCPU highcpu64.2 vCPU-hโ‚ฌ5.13
C โ€“ production 240ร—80ร—16, 200 iterations16 vCPU standard90.9 vCPU-hโ‚ฌ9.09
Total for this post207 vCPU-hโ‚ฌ18.52

Billed time includes machine boot, result upload and the end-of-run checks (about 7โ€“11 min per run), which is why 3-iteration runs cost more than their solve time suggests.

How to run it on cloudHPC

  1. Download the case files (simp.comm, simp.export for cases A and C) and set the size at the top of simp.comm: NELX, NELY, NELZ, VOLFRAC, PENAL, RMIN, MAXLOOP.
  2. Upload the two files into one storage folder and launch codeAster-17.3.17_mpi. One MPI process is used (the .comm has no NB_SOUS_DOMAINE), and MUMPS uses OpenMP threads = vCPU / 2.
  3. Choose the machine from the memory peak: MUMPS needs ~40 GB in-core for 1 M DOF (8-node hexahedra). Check the memory peak in the .mess (VmPeak): if it is far below that and the run is slow, MUMPS went out-of-core. For ~1 M DOF use 16 vCPU standard (64 GB); for the 60ร—20ร—4 case 4 vCPU highcpu is plenty.
  4. Results: history.json (compliance, volume, change and timing per iteration), snapshots.npz (density every 2 iterations, for animations) and result.med (final density + displacement, open it in ParaView or SALOME and use a threshold at 0.5).
  5. Runs longer than ~3 h: use a regular instance. The loop keeps its state in memory, so a spot preemption restarts from iteration 1.

To use your own geometry you need two changes: an unstructured mesh (read with LIRE_MAILLAGE) and a filter that works on cell centroids with volume weights (a KD-tree or a Helmholtz PDE filter solved by code_aster itself). The rest of the loop stays the same.

FAQ

Does code_aster have built-in topology optimization? No. code_aster has parameter identification (MACR_RECAL) but no topology optimization operator. Through its Python API you can build a SIMP loop around MECA_STATIQUE, as shown here.

How do you assign a different Young’s modulus to every element in code_aster? Make E a function of the external state variable NEUT1 (ELAS_FO + DEFI_FONCTION(NOM_PARA='NEUT1')), create an ELEM_NEUT_R field and set its values from numpy, then pass it with AFFE_VARC(NOM_VARC='NEUT1').

How are the sensitivities computed? For compliance minimization, from the element strain energy EPOT_ELEM: dC/dฯe = โˆ’p ฯe^(pโˆ’1) (E0 โˆ’ Emin) ยท 2ยทEPOTe / Ee. No adjoint solve is needed.

Is code_aster SIMP accurate compared with reference codes? On the top3d cantilever, code_aster and a numpy port of top3d agree to ~1e-11 for 40 iterations, and the final compliance differs by 0.04 %.

What about stress constraints, multiple load cases or other objectives? Multiple load cases are easy: sum the sensitivities of each case. Compliant mechanisms or eigenfrequencies need an adjoint or modal sensitivities, and stress constraints need aggregation (p-norm) and an MMA update instead of OC: same loop structure, more work.

Is there an open-source alternative ready to use? For CalculiX, beso (Bi-directional Evolutionary Structural Optimization) is mature and integrated in FreeCAD. For learning, top3d, the 88-line code and the FEniCS 55-line code are the references.

References

  1. K. Liu, A. Tovar, “An efficient 3D topology optimization code written in Matlab”, Structural and Multidisciplinary Optimization 50, 1175โ€“1196 (2014). doi:10.1007/s00158-014-1107-x
  2. E. Andreassen, A. Clausen, M. Schevenels, B. S. Lazarov, O. Sigmund, “Efficient topology optimization in MATLAB using 88 lines of code”, Struct. Multidisc. Optim. 43, 1โ€“16 (2011). doi:10.1007/s00158-010-0594-7
  3. O. Sigmund, “A 99 line topology optimization code written in Matlab”, Struct. Multidisc. Optim. 21, 120โ€“127 (2001). doi:10.1007/s001580050176
  4. M. P. Bendsรธe, “Optimal shape design as a material distribution problem”, Structural Optimization 1, 193โ€“202 (1989). doi:10.1007/BF01650949
  5. A. Gupta, R. Chowdhury, A. Chakrabarti, T. Rabczuk, “A 55-line code for large-scale parallel topology optimization in 2D and 3D”, arXiv:2012.08208 (2020).
  6. beso โ€“ Python topology optimization with CalculiX: https://github.com/calculix/beso
  7. code_aster documentation: U4.43.03 (AFFE_MATERIAU / AFFE_VARC), U4.81.04 (CALC_CHAMP), U2.08.03 (linear solvers), https://code-aster.org

CloudHPC is a HPC provider to run engineering simulations on the cloud. CloudHPC provides from 1 to 224 vCPUs for each process in several configuration of HPC infrastructure - both multi-thread and multi-core. Current software ranges includes several CAE, CFD, FEA, FEM software among which OpenFOAM, FDS, Blender and several others.

New users benefit of a FREE trial of 300 vCPU/Hours to be used on the platform in order to test the platform, all each features and verify if it is suitable for their needs