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

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
.commfile, 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.

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:
- Can it be done cleanly, without one material group per element?
- Does it give the same answer as a reference SIMP code?
- How does it behave at a size that needs a cloud machine?
Results at a glance
| Case | Elements | DOF (incl. clamped) | Iterations | Final compliance | Instance | Wall time | Cost |
|---|---|---|---|---|---|---|---|
| A โ validation 60ร20ร4 (top3d reference, numpy) | 4,800 | 19,215 | 198 | 2416.8 | workstation, 2 cores | 181 s | โ |
| A โ validation 60ร20ร4 (code_aster) | 4,800 | 19,215 | 146 | 2417.7 | 4 vCPU highcpu | 213 s | โฌ0.058 |
| C โ fine mesh 240ร80ร16 (code_aster) | 307,200 | 995,571 | 200 (limit) | 6923.8 | 16 vCPU standard | 5.5 h | โฌ9.09 |
The 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**3of a density near zero) stopped the run with a floating-point exception reported as a crash of code_aster. Wrap the numpy part incode_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 toLR, 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

| top3d reference (numpy) | code_aster 17.3.17 (cloudHPC) | |
|---|---|---|
| Compliance, iteration 1 | 28354.7799 | 28354.7799 |
| Max. relative difference, iterations 1โ40 | 2.1e-11 | |
| Iterations to convergence (change < 0.01) | 198 | 146 |
| Final compliance | 2416.84 | 2417.75 (0.04 %) |
| Cells on the same side of ฯ = 0.5 | 99.8 % | |

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

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.

| Case A (60ร20ร4) | Case C (240ร80ร16) | |
|---|---|---|
| Elements / DOF | 4,800 / 19,215 | 307,200 / 995,571 |
| Filter radius | 1.5 elements | 6 elements (same physical size) |
| Iterations | 146 (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 / cost | 213 s / โฌ0.058 | 5.5 h / โฌ9.09 |


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 %.

| Instance (n2d, regular) | RAM | OpenMP threads | Peak memory (GiB, VmPeak) | MUMPS mode | s / iteration | cost / iteration |
|---|---|---|---|---|---|---|
| 8 vCPU standard | 32 GB | 4 | 8 GiB | out-of-core | 199 | โฌ0.044 |
| 16 vCPU highcpu | 16 GB | 8 | 9 GiB | out-of-core | 367 | โฌ0.131 |
| 16 vCPU standard | 64 GB | 8 | 39 GiB | in-core | 99 | โฌ0.044 |
| 32 vCPU highcpu | 32 GB | 16 | 11 GiB | out-of-core | 194 | โฌ0.138 |
| 64 vCPU highcpu | 64 GB | 32 | 44 GiB | in-core | 97 | โฌ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.
| Run | Instance | Billed | Cost |
|---|---|---|---|
| A โ validation 60ร20ร4 (full run) | 4 vCPU highcpu | 0.7 vCPU-h | โฌ0.06 |
| B โ sizing 240ร80ร16, 3 iterations | 32 vCPU highcpu | 9.7 vCPU-h | โฌ0.77 |
| Scaling, 3 iterations | 8 vCPU standard | 2.4 vCPU-h | โฌ0.24 |
| Scaling, repeat | 8 vCPU standard | 2.5 vCPU-h | โฌ0.25 |
| Scaling, 3 iterations | 16 vCPU highcpu | 7.6 vCPU-h | โฌ0.61 |
| Scaling, repeat | 16 vCPU highcpu | 7.4 vCPU-h | โฌ0.59 |
| Scaling, 3 iterations | 16 vCPU standard | 3.9 vCPU-h | โฌ0.39 |
| Scaling, 3 iterations | 64 vCPU highcpu | 17.4 vCPU-h | โฌ1.39 |
| C โ first attempt, stopped at it. 31 (out-of-core) | 32 vCPU highcpu | 64.2 vCPU-h | โฌ5.13 |
| C โ production 240ร80ร16, 200 iterations | 16 vCPU standard | 90.9 vCPU-h | โฌ9.09 |
| Total for this post | 207 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
- Download the case files (
simp.comm,simp.exportfor cases A and C) and set the size at the top ofsimp.comm:NELX, NELY, NELZ,VOLFRAC,PENAL,RMIN,MAXLOOP. - Upload the two files into one storage folder and launch codeAster-17.3.17_mpi. One MPI process is used (the
.commhas noNB_SOUS_DOMAINE), and MUMPS uses OpenMP threads = vCPU / 2. - 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. - Results:
history.json(compliance, volume, change and timing per iteration),snapshots.npz(density every 2 iterations, for animations) andresult.med(final density + displacement, open it in ParaView or SALOME and use a threshold at 0.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
- 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
- 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
- O. Sigmund, “A 99 line topology optimization code written in Matlab”, Struct. Multidisc. Optim. 21, 120โ127 (2001). doi:10.1007/s001580050176
- M. P. Bendsรธe, “Optimal shape design as a material distribution problem”, Structural Optimization 1, 193โ202 (1989). doi:10.1007/BF01650949
- 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).
- beso โ Python topology optimization with CalculiX: https://github.com/calculix/beso
- 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