Optimization

As mentioned earlier R will be used for the optimization. Basically we need to access our scripts within R. It looks like:

library(GA)
setwd("/home/bjorn/optimization_tutorial/bend_geometry")
func <- function(x){
a <- system2("./run.sh",args = c(x),wait=TRUE,stdout=TRUE)
y <- as.numeric(a)
}
GA <- ga(type="real-valued",
fitness = function(x) - func(x),
lower=c(430,430,-100,1700,270,1600,330,1600),upper=c(600,600,50,2400,500,2100,500,2100),
popSize=50,maxiter=1000,run=100,
optim=TRUE)
optimArgs = list(method = "L-BFGS-B",
poptim = 0.05,
pressel = 0.5,
control = list(fnscale = -1, maxit = 100))

This is saved in a R-script which you run like “Rscript genetic.R”

So the entire simulation will be accessed via the “run.sh” script:

!/bin/sh
./1_create_geometry.sh $1 $2 $3 $4 $5 $6 $7 $8 &> geom.log
./2_mesh.sh &> mesh.log
if [ -d "mesh_tmp/constant/polyMesh" ]; then
./3_simulate.sh &> sim.log
fi
RES=./4_post.rb
echo $1 $2 $3 $4 $5 $6 $7 $8 $RES>> run.log
echo $RES
rm -r run_last &> /dev/null
cp -r run_tmp run_last &> /dev/null

The “4_post.rb” script is made to take care of simulations gone wrong. It will just give a high back pressure value if something goes wrong:

!/usr/bin/ruby
require 'open3'
res_all=Open3.popen3("./post_pressure.sh")
res=res_all[1].read
res_size=res.size
if res_size > 2
if res.to_f > 0 && res.to_f < 1000.0
puts res
else
puts 30.0
end
else
puts 30.0
end

I’ve already ran this optimization for a couple of weeks. The improvement in back pressure has evolved like this:

Back pressure on the y-axis and simulation number on the x-axis. It will be interesting to see the convergence speed for other optimization algorithms. Or test switching to neldermead at some point?

This approach is very useful and I use it all the time.

Actually R created a progress plot as well:

Do you just want to see if the optimized bend is any better. Download the stl files and checkout for yourself.