04 CFD shape‐optimization example - LucieKubickova/CFDNNetAdapt GitHub Wiki
CFD shape-optimization example
This section describes the CFD-based shape-optimization example from
example/convDifShapeOptim/. It couples
CFDNNetAdapt to OpenFOAM simulations of a single-phase ejector.
Physical problem and objectives
The case models a single-phase ejector with an inlet, suction, converging part, mixing tube, and diffuser. The geometry is controlled by 14 parameters consisting of:
- $
x$ and $y$ coordinates of two control points defining the shape of the converging part; - $
x$ and $y$ coordinates of four control points defining the shape of the diffuser; - length of the converging part ($
\ell_\mathrm{C}$); and - length of the diffuser ($
\ell_\mathrm{D}$).
Candidate geometries are rejected when control-point x-coordinates are not strictly increasing or when
$$ \ell_\mathrm{C}+\ell_\mathrm{D}\leq\ell_\mathrm{max}, $$
where $\ell_\mathrm{max}$ is a predefined maximal value.
The optimized objectives include the ejector total length
$$ \ell_\mathrm{t}=\ell_\mathrm{C}+\ell_\mathrm{D}, $$
and the ejector energy efficiency calculated from flow rates $\dot{Q}$ and
pressure values $p$ as
$$ e_\mathrm{eff} = \frac{Q_\mathrm{suction}(p_\mathrm{D}-p_\mathrm{suction})} {Q_\mathrm{inlet}(p_\mathrm{inlet}-p_\mathrm{D})}. $$
The optimization maximizes $e_\mathrm{eff}$ and minimizes $\ell_\mathrm{t}$.
The energy efficiency is calculated by the custom OpenFOAM utility
calcEnergyEfficiencyV1.C.
The utility has to be compiled before running the optimization:
cd example/convDifShapeOptim/02_ofCodes
source /opt/openfoam8/etc/bashrc
./Allwmake
Running the optimization
Full optimization run:
cd example/convDifShapeOptim
python Allrun.py
Allrun.py settings
Allrun.py configures a
CFDNNetAdaptV3 run as follows:
nPars = 14-- number of decision variablesnObjs = 2-- number of objectivesnOuts = 1-- number of surrogate outputsnSam = 2000-- initial sample countdeltaNSams = [500]-- sample count increments over iterationsnNN = 10-- number of neural networks generated in each iterationminN = 2-- minimum number of neurons in a hidden layermaxN = 20-- maximum number of neurons in a hidden layernHidLay = 3-- number of hidden layersdRN = 0-- decrement of variability in hidden layer sizetol = 5e-2-- optimization toleranceiMax = 200-- maximum number of iterationsnComps = 8-- number points chosen for verificationnSeeds = 5-- number of seeds for DNN trainingtrainPro = 75-- training percentage splitvalPro = 15-- validation percentage splittestPro = 10-- testing percentage splitkMax = 10000-- maximum number of iterations in DNN trainingrEStop = 1e-5-- relative error for stopping DNN trainingpopSize = 500-- population size of MOEA algorithmoffSize = 500-- offspring size of MOEA algorithmnGens = 30-- number of MOEA generations
Geometry generation and OpenFOAM execution
Scripts for generation of CFD simulations are stored in
01_pyCodes/.
caseConstructor.py
copies the base case, generates blockMeshDict, and copies the current
geometry scripts into the generated case. The base case
10_baseCase/ supplies the
boundary conditions, turbulence model, solver settings, and field files.
The CFD simulation is run by executing Allrun-serialAll, which:
- sources OpenFOAM v8;
- generates the mesh by running
blockMesh; - checks the mesh by running
checkMesh; - evaluates the simulation via
simpleFoam; and - calculates the energy efficiency with
calcEnergyEfficiencyV1.
Simulations with failed mesh generation, missing computation, failed residual checks, or missing/invalid energy-efficiency output are rejected and not included in the optimization.
Outputs
Allrun.py saves data to 01_algoRuns in a dedicated run_* folder,
including a log.out file and folders for each optimization step step_*.
The log.out file stores data about:
- used settings;
- architectures of all tested DNNs;
- architecture of the best-found DNN;
- points chosen for verification with DNN-predicted values and values calculated by CFD; and
- verification errors.
Each step_* folder includes:
- weights and metadata of each trained DNN; and
- MOEA results from each DNN-based optimization.
Individual CFD simulations are not stored by default, but the simulation
folder is cleared after each iteration. To save the CFD simulations, set
cleanSims = False in Allrun.py. However, when running more iterations of
CFDNNetAdapt, the saved simulations may be overwritten.
Visualization of results
postProcess.py plots results
in the objective space, including:
- the NSGA-II sample data; and
- the solutions predicted by the best DNNs found in each iteration.
The resulting figure objSpacePlot.png is saved to the run folder.
