def J(self, x, grad): # x is ln(nu) # write out nut_no.dat so that it can be read in nu = exp(x) temp = self.path; temp += "nut_no.dat" file_io.write_field(temp, self.x_no, self.y_no, nu) # solve flow at this nu f = open('runopt.out','a') f.write(''.join(['Running primal from ',self.restart,'...\n'])) f.close() run_ifdual.runpri(self.path, self.restart, self.niter, self.np, self.ctolpri) f = open('runopt.out','a') f.write('Done running primal\n') f.close() src = ''.join([self.path,'ifDual.out']) dst = ''.join([self.path,'ifDual_Pri.step',str(self.count),'.out']) shutil.copy(src,dst) # read in objective function value J = file_io.read_J(self.path) # calculate gradient - call adjoint if grad.size > 0: f = open('runopt.out','a') f.write(''.join(['Running adjoint from ',self.restart,'...\n'])) f.close() run_ifdual.runadj(self.path, self.restart, self.niter, self.np, self.ctoladj) f = open('runopt.out','a') f.write('Done running adjoint\n') f.close() # read in the gradient temp = self.path; temp += "grad.dat" nno, self.x_no, self.y_no, grad[:] = file_io.read_field(temp) # move files around f = open('runopt.out','a') f.write('Writing files for step %d\n' % self.count ) f.close() src = ''.join([self.path,'J.dat']) dst = ''.join([self.path,'J.step',str(self.count),'.dat']) shutil.copy(src,dst) src = ''.join([self.path,'nut_no.dat']) dst = ''.join([self.path,'nut_no.step',str(self.count),'.dat']) shutil.copy(src,dst) src = ''.join([self.path,'grad.dat']) dst = ''.join([self.path,'grad.step',str(self.count),'.dat']) shutil.copy(src,dst) src = ''.join([self.path,'restart.out']) dst = ''.join([self.path,'restart.step',str(self.count),'.out']) shutil.copy(src,dst) src = ''.join([self.path,'ifDual.out']) dst = ''.join([self.path,'ifDual_Adj.step',str(self.count),'.out']) shutil.copy(src,dst) f = open('runopt.out','a') f.write('Iter: %d\t' % self.count) f.write('J = %.10e\n' % J) f.close() self.count+=1 return J
def lbfgs(self, maxeval, nu0, tol): n = nu0.size o = nlopt.opt(nlopt.LD_LBFGS, n) # tolerance on objective function value o.set_ftol_rel(tol) o.set_maxeval(maxeval) o.set_min_objective(self.J) # read in the baseline RANS viscosity for bounds temp = self.path; temp += "nut_noRANS.dat" j1, j2, j3, nu_base = file_io.read_field(temp) # constrain mu to be greater than 0.75 * baseline # o.set_lower_bounds(log(0.10*nu_base)) # constrain mu to be less than 1.5 * baseline # o.set_upper_bounds(log(10.0*nu_base)) # call the optimizer - pass in initial viscosity f = o.optimize(log(nu0)) minf = o.last_optimum_value() res = o.last_optimize_result() return f, minf, res
def lbfgs(self, maxeval, nu0, tol): n = nu0.size o = nlopt.opt(nlopt.LD_LBFGS, n) # tolerance on objective function value o.set_ftol_rel(tol) o.set_maxeval(maxeval) o.set_min_objective(self.J) # read in the baseline RANS viscosity for bounds temp = self.path temp += "nut_noRANS.dat" j1, j2, j3, nu_base = file_io.read_field(temp) # constrain mu to be greater than 0.75 * baseline # o.set_lower_bounds(log(0.10*nu_base)) # constrain mu to be less than 1.5 * baseline # o.set_upper_bounds(log(10.0*nu_base)) # call the optimizer - pass in initial viscosity f = o.optimize(log(nu0)) minf = o.last_optimum_value() res = o.last_optimize_result() return f, minf, res
import pylab from numpy import * from numpy.linalg import * from opt import opt import file_io path = "reg_test/" restart = "restart.out" niter = 1 # max ifDual iterations # read in initial turbulent viscosity temp = path temp += "nut_no.dat" nno, x_no, y_no, nu0 = file_io.read_field(temp) # create an instance of opt o = opt(x_no, y_no, niter, restart, path) tol = 1e-9 nopt = 200 # number of optimization iterations x_opt, J_opt, res = o.lbfgs(nopt, nu0, tol) nu_opt = exp(x_opt) print J_opt # write out mu_opt temp = path temp += "nu_opt" file_io.write_field(temp, x_no, y_no, nu_opt)
def J(self, x, grad): # x is ln(nu) # write out nut_no.dat so that it can be read in nu = exp(x) temp = self.path temp += "nut_no.dat" file_io.write_field(temp, self.x_no, self.y_no, nu) # solve flow at this nu f = open('runopt.out', 'a') f.write(''.join(['Running primal from ', self.restart, '...\n'])) f.close() run_ifdual.runpri(self.path, self.restart, self.niter, self.np, self.ctolpri) f = open('runopt.out', 'a') f.write('Done running primal\n') f.close() src = ''.join([self.path, 'ifDual.out']) dst = ''.join([self.path, 'ifDual_Pri.step', str(self.count), '.out']) shutil.copy(src, dst) # read in objective function value J = file_io.read_J(self.path) # calculate gradient - call adjoint if grad.size > 0: f = open('runopt.out', 'a') f.write(''.join(['Running adjoint from ', self.restart, '...\n'])) f.close() run_ifdual.runadj(self.path, self.restart, self.niter, self.np, self.ctoladj) f = open('runopt.out', 'a') f.write('Done running adjoint\n') f.close() # read in the gradient temp = self.path temp += "grad.dat" nno, self.x_no, self.y_no, grad[:] = file_io.read_field(temp) # move files around f = open('runopt.out', 'a') f.write('Writing files for step %d\n' % self.count) f.close() src = ''.join([self.path, 'J.dat']) dst = ''.join([self.path, 'J.step', str(self.count), '.dat']) shutil.copy(src, dst) src = ''.join([self.path, 'nut_no.dat']) dst = ''.join([self.path, 'nut_no.step', str(self.count), '.dat']) shutil.copy(src, dst) src = ''.join([self.path, 'grad.dat']) dst = ''.join([self.path, 'grad.step', str(self.count), '.dat']) shutil.copy(src, dst) src = ''.join([self.path, 'restart.out']) dst = ''.join([self.path, 'restart.step', str(self.count), '.out']) shutil.copy(src, dst) src = ''.join([self.path, 'ifDual.out']) dst = ''.join([self.path, 'ifDual_Adj.step', str(self.count), '.out']) shutil.copy(src, dst) f = open('runopt.out', 'a') f.write('Iter: %d\t' % self.count) f.write('J = %.10e\n' % J) f.close() self.count += 1 return J