class AirfoilCFD(ExplicitComponent):

    def setup(self):
        ######################
        ### needed Objects ###
        self.bzFoil = BPAirfoil()
        self.air = Airfoil(None)


        #####################
        ### openMDAO init ###
        ### INPUTS

        self.add_input('r_le', val=-0.05, desc='nose radius')
        self.add_input('beta_te', val=0.1, desc='thickness angle trailing edge')
        self.add_input('x_t', val=0.3, desc='dickenruecklage')
        self.add_input('y_t', val=0.1, desc='max thickness')

        self.add_input('gamma_le', val=0.5, desc='camber angle leading edge')
        self.add_input('x_c', val=0.5, desc='woelbungsruecklage')
        self.add_input('y_c', val=0.1, desc='max camber')
        self.add_input('alpha_te', val=-0.1, desc='camber angle trailing edge')
        #self.add_input('z_te', val=0., desc='camber trailing edge')

        # bezier parameters
        self.add_input('b_8', val=0.05, desc='')
        self.add_input('b_15', val=0.75, desc='')
        self.add_input('b_0', val=0.1, desc='')
        self.add_input('b_2', val=0.25, desc='')
        self.add_input('b_17', val=0.9, desc='')

        ###only for visualiziation
        self.add_input('offsetFront', val=0.1, desc='...')
        self.add_input('cabin_height', val=1.0, desc='...')
        self.add_input('angle', val=1.0, desc='...')

        ### OUTPUTS
        self.add_output('c_d', val=.2)
        self.add_output('c_l', val=.2)
        self.add_output('c_m', val=.2)
        #self.add_output('y_t', val=0.1, desc='max thickness')

        #self.add_output('cabin_height', val=cabinHeigth)
        #self.add_output('angle', val=0.)
        #self.add_output('offsetFront', val=.1)

        self.declare_partials('*', '*', method='fd')
        self.executionCounter = 0

    """
    def fit_cabin(self, xFront, angle):
        top, buttom = self.bzFoil.get_cooridnates_top_buttom(500)
        if self.bzFoil.valid == False:
            return False
        xBack = xFront + cabinLength  # inputs['length']
        self.air.set_coordinates(top, buttom)
        self.air.rotate(angle)
        yMinButtom = max(self.air.get_buttom_y(xFront), self.air.get_buttom_y(xBack))
        yMaxTop = min(self.air.get_top_y(xFront), self.air.get_top_y(xBack))
        height = yMaxTop - yMinButtom
        return height
        #outputs['cabin_height'] = height
    """

    def compute(self, inputs, outputs):
        error = False
        self.bzFoil.r_le = inputs['r_le']
        self.bzFoil.beta_te = inputs['beta_te']
        self.bzFoil.x_t = inputs['x_t']
        self.bzFoil.y_t = inputs['y_t']

        self.bzFoil.gamma_le = inputs['gamma_le']
        self.bzFoil.x_c = inputs['x_c']
        self.bzFoil.y_c = inputs['y_c']
        self.bzFoil.alpha_te = inputs['alpha_te']
        #self.bzFoil.z_te = inputs['z_te']

        self.bzFoil.b_8 = inputs['b_8']
        self.bzFoil.b_15 = inputs['b_15']
        self.bzFoil.b_0 = inputs['b_0']
        self.bzFoil.b_2 = inputs['b_2']
        self.bzFoil.b_17 = inputs['b_17']

        projectName = PROJECT_NAME_PREFIX + '_%09d' % self.executionCounter
        cfd = CFDrun(projectName)

        airFoilCoords = self.bzFoil.generate_airfoil(500,
                                                     show_plot=False,
                                                     save_plot_path=WORKING_DIR+'/'+projectName+'/airfoil.png',
                                                     param_dump_file=WORKING_DIR+'/'+projectName+'/airfoil.txt')

        # check if bz is valid
        if self.bzFoil.valid:
            print('valid bz')
            ### use cabin fit optimization to find y_t
            #cabinFit.cabinHeigth = cabinHeigth
            #cabinFit.cabinLength = cabinLength
            #cabinFit.bzFoil = self.bzFoil
            #y_t, height, angle, offsetFront = cabinFit.run_cabin_opti(show_plot=False)
            #self.bzFoil.y_t = y_t

            #outputs['cabin_height'] = height
            #outputs['y_t'] = y_t
            #outputs['angle'] = angle
            #outputs['offsetFront'] = offsetFront
            #print('new cabin_height= ' + str(outputs['cabin_height']))


        if not self.bzFoil.valid:
            #raise AnalysisError('AirfoilCFD: invalid BPAirfoil')
            print('ERROR: AirfoilCFD, invalid BPAirfoil')
            self.bzFoil.save_parameters_to_file(
                WORKING_DIR + '/bz_error_' + datetime.now().strftime('%Y-%m-%d_%H_%M_%S') + '.txt')
            error = True
        else:

            self.bzFoil.plot_airfoil_with_cabin(inputs['offsetFront'],
                                                cabinLength,
                                                inputs['cabin_height'],
                                                inputs['angle'],
                                                show_plot=False,
                                                save_plot_path=WORKING_DIR + '/' + projectName + '/airfoil_cabin.png')

            ### now we do cfd
            top, buttom = self.bzFoil.get_cooridnates_top_buttom(500)
            cfd.set_airfoul_coords(top, buttom)

            cfd.c2d.pointsInNormalDir = 80
            cfd.c2d.pointNrAirfoilSurface = 200
            cfd.c2d.reynoldsNum = REYNOLD
            cfd.construct2d_generate_mesh(scale=SCALE, plot=False)
            cfd.su2_fix_mesh()
            cfd.su2_solve(config)
            results = cfd.su2_parse_iteration_result()
            cfd.clean_up()

            if float(results['CD']) <= 0. or float(results['CD']) > 100.:
                #raise AnalysisError('AirfoilCFD: c_d is out of range (cfd failed)')
                print('ERROR: AirfoilCFD, c_d is out of range (cfd failed)')
                error = True

            outputs['c_d'] = results['CD']
            outputs['c_l'] = results['CL']
            outputs['c_m'] = results['CMz']
            print('c_l= ' + str(outputs['c_l']))
            print('c_d= ' + str(outputs['c_d']))
            print('c_m= ' + str(outputs['c_m']))
            print('c_l/c_d= ' + str(results['CL/CD']))
            print('cfdIterations= ' + str(results['Iteration']))
            write_to_log(str(self.executionCounter) + ','
                         + datetime.now().strftime('%H:%M:%S') + ','
                         + str(outputs['c_l']) + ','
                         + str(outputs['c_d']) + ','
                         + str(outputs['c_m']) + ','
                         + str(results['CL/CD']) + ','
                         + str(results['Iteration']) + ','
                         + str(inputs['cabin_height']) + ','
                         + str(inputs['offsetFront']) + ','
                         + str(inputs['angle']) + ','
                         + str(inputs['r_le']) + ','
                         + str(inputs['beta_te']) + ','
                         + str(inputs['x_t']) + ','
                         + str(inputs['y_t']) + ','
                         + str(inputs['gamma_le']) + ','
                         + str(inputs['x_c']) + ','
                         + str(inputs['y_c']) + ','
                         + str(inputs['alpha_te']) + ','
                         + str(self.bzFoil.z_te) + ','#+ str(inputs['z_te']) + ','
                         + str(inputs['b_8']) + ','
                         + str(inputs['b_15']) + ','
                         + str(inputs['b_0']) + ','
                         + str(inputs['b_17']) + ','
                         + str(inputs['b_2']))
            globBzFoil = self.bzFoil

        #workaround since raising an error seems to crash the optimization
        if error:
            outputs['c_d'] = 999.
            outputs['c_l'] = 0.
            outputs['c_m'] = 0.
        self.executionCounter += 1
Beispiel #2
0
class AirfoilCFD(ExplicitComponent):

    def setup(self):
        ######################
        ### needed Objects ###
        self.bzFoil = BPAirfoil()


        #####################
        ### openMDAO init ###
        ### INPUTS

        self.add_input('r_le', val=-0.05, desc='nose radius')
        self.add_input('beta_te', val=0.1, desc='thickness angle trailing edge')
        #self.add_input('dz_te', val=0., desc='thickness trailing edge')
        self.add_input('x_t', val=0.3, desc='dickenruecklage')
        self.add_input('y_t', val=0.1, desc='max thickness')

        self.add_input('gamma_le', val=0.5, desc='camber angle leading edge')
        self.add_input('x_c', val=0.5, desc='woelbungsruecklage')
        self.add_input('y_c', val=0.1, desc='max camber')
        self.add_input('alpha_te', val=-0.1, desc='camber angle trailing edge')
        self.add_input('z_te', val=0., desc='camber trailing edge')

        # bezier parameters
        self.add_input('b_8', val=0.05, desc='')
        self.add_input('b_15', val=0.75, desc='')
        self.add_input('b_0', val=0.1, desc='')
        self.add_input('b_2', val=0.25, desc='')
        self.add_input('b_17', val=0.9, desc='')

        # just for plotin
        self.add_input('offsetFront', val=0.1, desc='...')
        self.add_input('angle', val=.0, desc='...')
        self.add_input('cabin_height', val=.0, desc='...')

        ### OUTPUTS
        self.add_output('c_d', val=.2)
        self.add_output('c_l', val=.2)
        self.add_output('c_m', val=.2)

        self.declare_partials('*', '*', method='fd')
        self.executionCounter = 0

    def compute(self, inputs, outputs):
        error = False
        self.bzFoil.r_le = inputs['r_le']
        self.bzFoil.beta_te = inputs['beta_te']
        #self.bzFoil.dz_te = inputs['dz_te']
        self.bzFoil.x_t = inputs['x_t']
        self.bzFoil.y_t = inputs['y_t']

        self.bzFoil.gamma_le = inputs['gamma_le']
        self.bzFoil.x_c = inputs['x_c']
        self.bzFoil.y_c = inputs['y_c']
        self.bzFoil.alpha_te = inputs['alpha_te']
        self.bzFoil.z_te = inputs['z_te']

        self.bzFoil.b_8 = inputs['b_8']
        self.bzFoil.b_15 = inputs['b_15']
        self.bzFoil.b_0 = inputs['b_0']
        self.bzFoil.b_2 = inputs['b_2']
        self.bzFoil.b_17 = inputs['b_17']

        projectName = PROJECT_NAME_PREFIX + '_%09d' % self.executionCounter
        cfd = CFDrun(projectName)

        airFoilCoords = self.bzFoil.generate_airfoil(500,
                                                     show_plot=False,
                                                     save_plot_path=WORKING_DIR+'/'+projectName+'/airfoil.png',
                                                     param_dump_file=WORKING_DIR+'/'+projectName+'/airfoil.txt')
        self.bzFoil.plot_airfoil_with_cabin(inputs['offsetFront'],
                                            cabinLength,
                                            inputs['cabin_height'],
                                            inputs['angle'],
                                            show_plot=False,
                                            save_plot_path=WORKING_DIR+'/'+projectName+'/airfoil_cabin.png')
        if not self.bzFoil.valid:
            #raise AnalysisError('AirfoilCFD: invalid BPAirfoil')
            print('ERROR: AirfoilCFD, invalid BPAirfoil')
            error = True
        else:
            top, buttom = self.bzFoil.get_cooridnates_top_buttom(500)
            #self.air.set_coordinates(top, buttom)
            cfd.set_airfoul_coords(top, buttom)

            cfd.c2d.pointsInNormalDir = 80
            cfd.c2d.pointNrAirfoilSurface = 200
            cfd.c2d.reynoldsNum = REYNOLD
            cfd.construct2d_generate_mesh(scale=SCALE, plot=False)
            cfd.su2_fix_mesh()
            cfd.su2_solve(config)
            #totalCL, totalCD, totalCM, totalE = cfd.su2_parse_results()
            results = cfd.su2_parse_iteration_result()
            cfd.clean_up()

            if float(results['CD']) <= 0. or float(results['CD']) > 100.:
                #raise AnalysisError('AirfoilCFD: c_d is out of range (cfd failed)')
                print('ERROR: AirfoilCFD, c_d is out of range (cfd failed)')
                error = True

            outputs['c_d'] = results['CD']
            outputs['c_l'] = results['CL']
            outputs['c_m'] = results['CMz']
            print('c_l= ' + str(outputs['c_l']))
            print('c_d= ' + str(outputs['c_d']))
            print('c_m= ' + str(outputs['c_m']))
            print('c_l/c_d= ' + str(results['CL/CD']))
            print('cfdIterations= ' + str(results['Iteration']))
            write_to_log(str(self.executionCounter) + ','
                         + datetime.now().strftime('%H:%M:%S') + ','
                         + str(outputs['c_l']) + ','
                         + str(outputs['c_d']) + ','
                         + str(outputs['c_m']) + ','
                         + str(results['CL/CD']) + ','
                         + str(results['Iteration']) + ','
                         + str(inputs['cabin_height']) + ','
                         + str(inputs['offsetFront']) + ','
                         + str(inputs['angle']) + ','
                         + str(inputs['r_le']) + ','
                         + str(inputs['beta_te']) + ','
                         + str(inputs['x_t']) + ','
                         + str(inputs['y_t']) + ','
                         + str(inputs['gamma_le']) + ','
                         + str(inputs['x_c']) + ','
                         + str(inputs['y_c']) + ','
                         + str(inputs['alpha_te']) + ','
                         + str(inputs['z_te']) + ','
                         + str(inputs['b_8']) + ','
                         + str(inputs['b_15']) + ','
                         + str(inputs['b_0']) + ','
                         + str(inputs['b_17']) + ','
                         + str(inputs['b_2']))

        #workaround since raising an error seems to crash the optimization
        if error:
            outputs['c_d'] = 999.
            outputs['c_l'] = 0.
            outputs['c_m'] = 0.
        self.executionCounter += 1
Beispiel #3
0
class AirfoilCFD(ExplicitComponent):
    def setup(self):
        ######################
        ### needed Objects ###
        self.bzFoil = BPAirfoil()
        self.air = Airfoil(None)

        #####################
        ### openMDAO init ###
        ### INPUTS

        self.add_input('r_le', val=-0.05, desc='nose radius')
        self.add_input('beta_te',
                       val=0.1,
                       desc='thickness angle trailing edge')
        #self.add_input('dz_te', val=0., desc='thickness trailing edge')
        self.add_input('x_t', val=0.3, desc='dickenruecklage')
        #self.add_input('y_t', val=0.1, desc='max thickness')

        self.add_input('gamma_le', val=0.5, desc='camber angle leading edge')
        self.add_input('x_c', val=0.5, desc='woelbungsruecklage')
        self.add_input('y_c', val=0.1, desc='max camber')
        self.add_input('alpha_te', val=-0.1, desc='camber angle trailing edge')
        #self.add_input('z_te', val=0., desc='camber trailing edge')

        # bezier parameters
        self.add_input('b_8', val=0.05, desc='')
        self.add_input('b_15', val=0.75, desc='')
        self.add_input('b_0', val=0.1, desc='')
        self.add_input('b_2', val=0.25, desc='')
        self.add_input('b_17', val=0.9, desc='')

        # just for plotin
        #self.add_input('offsetFront', val=0.1, desc='...')
        #self.add_input('angle', val=.0, desc='...')
        #self.add_input('cabin_height', val=.0, desc='...')

        ### OUTPUTS
        self.add_output('c_d', val=.2)
        self.add_output('c_l', val=.2)
        self.add_output('c_m', val=.2)
        self.add_output('y_t', val=0.1, desc='max thickness')

        self.add_output('cabin_height', val=cabinHeigth)
        self.add_output('angle', val=.0, desc='...')
        self.add_output('offsetFront', val=0.1, desc='...')

        self.declare_partials('*', '*', method='fd')
        self.executionCounter = 0
        self.prevAngle = -0.239
        self.prevOffsetFront = 0.117

    def calc_max_cabin_height(self, xFront, angle):
        top, buttom = self.bzFoil.get_cooridnates_top_buttom(100)
        if self.bzFoil.valid == False:
            return False
        xBack = xFront + cabinLength  # inputs['length']
        self.air.set_coordinates(top, buttom)
        self.air.rotate(angle)
        yMinButtom = max(self.air.get_buttom_y(xFront),
                         self.air.get_buttom_y(xBack))
        yMaxTop = min(self.air.get_top_y(xFront), self.air.get_top_y(xBack))
        height = yMaxTop - yMinButtom
        return height
        #outputs['cabin_height'] = height

    def calc_min_y_t(self, offset_front, angle):
        self.bzFoil.y_t = 0.07
        init_height = self.calc_max_cabin_height(offset_front, angle)
        if self.bzFoil.valid:
            iterCounter = 0
            height = init_height
            while (abs(height - cabinHeigth) > 1e-6):
                self.bzFoil.y_t += cabinHeigth - height
                height = self.calc_max_cabin_height(offset_front, angle)
                if height == False or self.bzFoil.y_t > 100 or self.bzFoil.y_t < 0.:
                    self.bzFoil.y_t = 9999.
                    return -1.
                iterCounter += 1
                #print('height: ' + str(height) + '\t' + str(self.bzFoil.y_t))
            #print('calc_min_y_t: needed iterations= ' + str(iterCounter))
            return height
        #workaround for invalid airfoil
        self.bzFoil.y_t = 9999.
        return -1.

    def optimize_cabin_angle(self, offset_front, angle):
        iterCounter = 0
        stepWidth = .01
        iterStopStepWidth = 1e-3

        height = self.calc_min_y_t(offset_front, angle)
        act_min_y_t = self.bzFoil.y_t
        while (abs(stepWidth) > iterStopStepWidth):

            height = self.calc_min_y_t(offset_front, angle + stepWidth)
            plus_min_y_t = self.bzFoil.y_t
            if plus_min_y_t < act_min_y_t:
                angle = angle + stepWidth
                act_min_y_t = plus_min_y_t
            else:
                height = self.calc_min_y_t(offset_front, angle - stepWidth)
                minus_min_y_t = self.bzFoil.y_t
                if minus_min_y_t < act_min_y_t:
                    angle = angle - stepWidth
                    act_min_y_t = minus_min_y_t
                else:
                    stepWidth = stepWidth / 10
            iterCounter += 1
            print('new angle: ' + str(angle) + '\t' + str(self.bzFoil.y_t))
        print('optimize_cabin_angle: needed iterations= ' + str(iterCounter))
        return angle

    def optimize_cabin_front_offset(self, offset_front, angle):
        iterCounter = 0
        stepWidth = .01
        iterStopStepWidth = 1e-3
        newAngle = 0  #angle
        act_angle = self.optimize_cabin_angle(offset_front, newAngle)
        act_min_y_t = self.bzFoil.y_t
        while (abs(stepWidth) > iterStopStepWidth):

            plus_angle = self.optimize_cabin_angle(offset_front + stepWidth,
                                                   newAngle)
            plus_min_y_t = self.bzFoil.y_t
            if plus_min_y_t < act_min_y_t:
                offset_front = offset_front + stepWidth
                newAngle = plus_angle
                act_min_y_t = plus_min_y_t
            else:
                minus_angle = self.optimize_cabin_angle(
                    offset_front - stepWidth, newAngle)
                minus_min_y_t = self.bzFoil.y_t
                if minus_min_y_t < act_min_y_t:
                    offset_front = offset_front - stepWidth
                    newAngle = minus_angle
                    act_min_y_t = minus_min_y_t
                else:
                    stepWidth = stepWidth / 10
                    newAngle = act_angle
            iterCounter += 1
            print('new offset_front: ' + str(offset_front))
        print('optimize_cabin_angle: needed iterations= ' + str(iterCounter))
        return offset_front, newAngle

    def compute(self, inputs, outputs):
        error = False
        self.bzFoil.r_le = inputs['r_le']
        self.bzFoil.beta_te = inputs['beta_te']
        #self.bzFoil.dz_te = inputs['dz_te']
        self.bzFoil.x_t = inputs['x_t']
        #self.bzFoil.y_t = inputs['y_t']

        self.bzFoil.gamma_le = inputs['gamma_le']
        self.bzFoil.x_c = inputs['x_c']
        self.bzFoil.y_c = inputs['y_c']
        self.bzFoil.alpha_te = inputs['alpha_te']
        self.bzFoil.z_te = 0  #inputs['z_te']

        self.bzFoil.b_8 = inputs['b_8']
        self.bzFoil.b_15 = inputs['b_15']
        self.bzFoil.b_0 = inputs['b_0']
        self.bzFoil.b_2 = inputs['b_2']
        self.bzFoil.b_17 = inputs['b_17']

        projectName = PROJECT_NAME_PREFIX + '_%09d' % self.executionCounter
        cfd = CFDrun(projectName)

        airFoilCoords = self.bzFoil.generate_airfoil(
            500,
            show_plot=False,
            save_plot_path=WORKING_DIR + '/' + projectName + '/airfoil.png',
            param_dump_file=WORKING_DIR + '/' + projectName + '/airfoil.txt')

        # check how high the cabin can be
        outputs['offsetFront'], outputs[
            'angle'] = self.optimize_cabin_front_offset(
                self.prevOffsetFront, self.prevAngle)
        self.prevAngle = outputs['angle']
        outputs['cabin_height'] = self.calc_min_y_t(outputs['offsetFront'],
                                                    outputs['angle'])

        #if self.bzFoil.valid:
        outputs['y_t'] = self.bzFoil.y_t
        print('new cabin_height= ' + str(outputs['cabin_height']))

        if not self.bzFoil.valid:
            #raise AnalysisError('AirfoilCFD: invalid BPAirfoil')
            print('ERROR: AirfoilCFD, invalid BPAirfoil')
            self.bzFoil.save_parameters_to_file(
                WORKING_DIR + '/bz_error_' +
                datetime.now().strftime('%Y-%m-%d_%H_%M_%S') + '.txt')
            error = True
        else:
            self.bzFoil.plot_airfoil_with_cabin(outputs['offsetFront'],
                                                cabinLength,
                                                outputs['cabin_height'],
                                                outputs['angle'],
                                                show_plot=True,
                                                save_plot_path=WORKING_DIR +
                                                '/' + projectName +
                                                '/airfoil_cabin.png')

            ### now we do cfd
            top, buttom = self.bzFoil.get_cooridnates_top_buttom(500)
            cfd.set_airfoul_coords(top, buttom)

            cfd.c2d.pointsInNormalDir = 80
            cfd.c2d.pointNrAirfoilSurface = 200
            cfd.c2d.reynoldsNum = REYNOLD
            cfd.construct2d_generate_mesh(scale=SCALE, plot=False)
            cfd.su2_fix_mesh()
            cfd.su2_solve(config)
            #totalCL, totalCD, totalCM, totalE = cfd.su2_parse_results()
            results = cfd.su2_parse_iteration_result()
            cfd.clean_up()

            if float(results['CD']) <= 0. or float(results['CD']) > 100.:
                #raise AnalysisError('AirfoilCFD: c_d is out of range (cfd failed)')
                print('ERROR: AirfoilCFD, c_d is out of range (cfd failed)')
                error = True

            outputs['c_d'] = results['CD']
            outputs['c_l'] = results['CL']
            outputs['c_m'] = results['CMz']
            print('c_l= ' + str(outputs['c_l']))
            print('c_d= ' + str(outputs['c_d']))
            print('c_m= ' + str(outputs['c_m']))
            print('c_l/c_d= ' + str(results['CL/CD']))
            print('cfdIterations= ' + str(results['Iteration']))
            write_to_log(
                str(self.executionCounter) + ',' +
                datetime.now().strftime('%H:%M:%S') + ',' +
                str(outputs['c_l']) + ',' + str(outputs['c_d']) + ',' +
                str(outputs['c_m']) + ',' + str(results['CL/CD']) + ',' +
                str(results['Iteration']) + ',' +
                str(outputs['cabin_height']) + ',' +
                str(outputs['offsetFront']) + ',' + str(outputs['angle']) +
                ',' + str(inputs['r_le']) + ',' + str(inputs['beta_te']) +
                ',' + str(inputs['x_t']) + ',' + str(outputs['y_t']) + ',' +
                str(inputs['gamma_le']) + ',' + str(inputs['x_c']) + ',' +
                str(inputs['y_c']) + ',' + str(inputs['alpha_te']) + ',' +
                str(self.bzFoil.z_te) + ',' + str(inputs['b_8']) + ',' +
                str(inputs['b_15']) + ',' + str(inputs['b_0']) + ',' +
                str(inputs['b_17']) + ',' + str(inputs['b_2']))

        #workaround since raising an error seems to crash the optimization
        if error:
            outputs['c_d'] = 999.
            outputs['c_l'] = 0.
            outputs['c_m'] = 0.
        self.executionCounter += 1