Exemplo n.º 1
0
    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
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
    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