<commit_before>import copy
import numpy as np

from openmdao.api import Component, Group, ParallelGroup

from hawc2_inputreader import HAWC2InputReader
from hawc2_inputwriter import HAWC2SInputWriter
from hawc2_wrapper import HAWC2Wrapper
from hawc2_output import HAWC2SOutputCompact
from hawc2_geomIDO import HAWC2GeometryBuilder


class HAWC2SWorkflow(Component):
    """
    OpenMDAO component to run the HAWC2S workflow.

    parameters
    ----------
    config: dict
        Configuration dictionary. It has to contain the following entries:
        |* 'with_structure': bool to add structural properties to the parameters
        |* 'with_geom': bool to add the blade geometry to the parameters
        |* 'master_file': str with the name of the master file.
        |* 'aerodynamic_sections': int of the number of aerodynamic sections.
        |* 'HAWC2SOutputs': dict of the outputs required. It has to include two
            dictionaries 'rotor' and 'blade' with the list of rotor sensor and
            blade sensors.

        |* 'HAWC2SInputWriter': dict for initialization of HAWC2SInputWriter
            parameters.
        |* 'HAWC2Wrapper': dict for initialization of HAWC2Wrapper parameters.
        |* 'HAWC2GeometryBuilder': dict for initialization of
            HAWC2GeometryBuilder parameters

    case_id: str
        Name of the HAWC2s case to create and run. If case_id contains the word
        "user" then it is assumed that the user is providing the operational
        points. Therefore "case" has to be a dictionary.

    case: list/dict
        This variable can be a list or a dictionary. If the operational points
        need to be computed "case" is a list with the wind speeds to evaluate.
        If more than one wind speed is given the wind speeds evaluated are
        actually the first, the last, and those obtained with a linspace of the
        same length. If the operational points do not need to be computed
        "case" is a dictionary with keys "wsp", "rpm", and "pitch". Each
        parameter is a list containing wind speed, rotorspeed and pitch angle
        respectively.

    """
    def __init__(self, config, case_id, case, cs_size, pfsize):
        super(HAWC2SWorkflow, self).__init__()
        self.with_structure = config['with_structure']
        self.with_geom = config['with_geom']
        self.with_tsr = config['with_tsr']
        self.with_ctr_tuning = 0
        self.with_freq_placement = 0

        self.reader = HAWC2InputReader(config['master_file'])
        self.reader.execute()

        self.writer = HAWC2SInputWriter(**config['HAWC2SInputWriter'])
        self.writer.vartrees = copy.copy(self.reader.vartrees)
        self.writer.case_id = case_id
        self.writer.vartrees.aero.aerosections = config['aerodynamic_sections']

        nws = self._check_cases(self.writer.vartrees, case_id, case)

        self.wrapper = HAWC2Wrapper(**config['HAWC2Wrapper'])
        self.wrapper.case_id = case_id
        naes = config['aerodynamic_sections']-2

        self.output = HAWC2SOutputCompact(config['HAWC2SOutputs'])
        self.output.case_id = case_id
        self.output.commands = self.reader.vartrees.h2s.commands

        n = len(config['HAWC2SOutputs']['rotor'])
        self.add_output('outputs_rotor', shape=[nws, n])
        n = len(config['HAWC2SOutputs']['blade'])
        self.add_output('outputs_blade', shape=[nws, n*naes])

        if self.with_tsr:
            self.add_param('tsr', 0.)

        if self.with_structure:
            self.add_param('blade_beam_structure', shape=cs_size)

        if self.with_geom:
            self.geom = HAWC2GeometryBuilder(**config['HAWC2GeometryBuilder'])
            self.geom.c12axis_init = self.reader.vartrees.main_bodies.blade1.c12axis.copy()
            self.geom_var = ['s', 'x', 'y', 'z', 'rot_x', 'rot_y', 'rot_z',
                             'chord', 'rthick', 'p_le']
            for v in self.geom_var:
                self.add_param(v, shape=[pfsize])
            self.add_param('blade_length', 0.)

    def solve_nonlinear(self, params, unknowns, resids):

        if self.with_tsr:
            self.writer.vartrees.dlls.risoe_controller.dll_init.designTSR = \
                params['tsr']

        if self.with_geom:
            for v in self.geom_var:
                setattr(self.geom.bladegeom, v, params[v])
            self.geom.blade_length = params['blade_length']
            self.geom.execute()

            self.writer.vartrees.blade_ae = self.geom.blade_ae
            self.writer.vartrees.main_bodies.blade1.c12axis = self.geom.c12axis

        if self.with_structure:
            body = self.writer.vartrees.main_bodies.blade1
            if self.with_geom:
                blade_length = params['blade_length']
            else:
                blade_length = body.c12axis[-1, 2]

            self._array2hawc2beamstructure(blade_length, body,
                                           params['blade_beam_structure'])

        if self.with_ctr_tuning:
            pass
        if self.with_freq_placement:
            pass

        self.writer.execute()
        self.wrapper.compute()
        self.output.execute()

        unknowns['outputs_rotor'] = self.output.outputs_rotor
        unknowns['outputs_blade'] = self.output.outputs_blade

    def _array2hawc2beamstructure(self, blade_length, body, body_st):

        bset = body.body_set[1] - 1
        if body.st_input_type is 0:
            body.beam_structure[bset].s = body_st[:, 0]*blade_length
            body.beam_structure[bset].dm = body_st[:, 1]
            body.beam_structure[bset].x_cg = body_st[:, 2]
            body.beam_structure[bset].y_cg = body_st[:, 3]
            body.beam_structure[bset].ri_x = body_st[:, 4]
            body.beam_structure[bset].ri_y = body_st[:, 5]
            body.beam_structure[bset].x_sh = body_st[:, 6]
            body.beam_structure[bset].y_sh = body_st[:, 7]<commit_msg>fixing blade length and ae file update<commit_after>import copy
import numpy as np

from openmdao.api import Component, Group, ParallelGroup

from hawc2_inputreader import HAWC2InputReader
from hawc2_inputwriter import HAWC2SInputWriter
from hawc2_wrapper import HAWC2Wrapper
from hawc2_output import HAWC2SOutputCompact
from hawc2_geomIDO import HAWC2GeometryBuilder


class HAWC2SWorkflow(Component):
    """
    OpenMDAO component to run the HAWC2S workflow.

    parameters
    ----------
    config: dict
        Configuration dictionary. It has to contain the following entries:
        |* 'with_structure': bool to add structural properties to the parameters
        |* 'with_geom': bool to add the blade geometry to the parameters
        |* 'master_file': str with the name of the master file.
        |* 'aerodynamic_sections': int of the number of aerodynamic sections.
        |* 'HAWC2SOutputs': dict of the outputs required. It has to include two
            dictionaries 'rotor' and 'blade' with the list of rotor sensor and
            blade sensors.

        |* 'HAWC2SInputWriter': dict for initialization of HAWC2SInputWriter
            parameters.
        |* 'HAWC2Wrapper': dict for initialization of HAWC2Wrapper parameters.
        |* 'HAWC2GeometryBuilder': dict for initialization of
            HAWC2GeometryBuilder parameters

    case_id: str
        Name of the HAWC2s case to create and run. If case_id contains the word
        "user" then it is assumed that the user is providing the operational
        points. Therefore "case" has to be a dictionary.

    case: list/dict
        This variable can be a list or a dictionary. If the operational points
        need to be computed "case" is a list with the wind speeds to evaluate.
        If more than one wind speed is given the wind speeds evaluated are
        actually the first, the last, and those obtained with a linspace of the
        same length. If the operational points do not need to be computed
        "case" is a dictionary with keys "wsp", "rpm", and "pitch". Each
        parameter is a list containing wind speed, rotorspeed and pitch angle
        respectively.

    """
    def __init__(self, config, case_id, case, cs_size, pfsize):
        super(HAWC2SWorkflow, self).__init__()
        self.with_structure = config['with_structure']
        self.with_geom = config['with_geom']
        self.with_tsr = config['with_tsr']
        self.with_ctr_tuning = 0
        self.with_freq_placement = 0

        self.reader = HAWC2InputReader(config['master_file'])
        self.reader.execute()

        self.writer = HAWC2SInputWriter(**config['HAWC2SInputWriter'])
        self.writer.vartrees = copy.copy(self.reader.vartrees)
        self.writer.case_id = case_id
        self.writer.vartrees.aero.aerosections = config['aerodynamic_sections']

        nws = self._check_cases(self.writer.vartrees, case_id, case)

        self.wrapper = HAWC2Wrapper(**config['HAWC2Wrapper'])
        self.wrapper.case_id = case_id
        naes = config['aerodynamic_sections']-2

        self.output = HAWC2SOutputCompact(config['HAWC2SOutputs'])
        self.output.case_id = case_id
        self.output.commands = self.reader.vartrees.h2s.commands

        n = len(config['HAWC2SOutputs']['rotor'])
        self.add_output('outputs_rotor', shape=[nws, n])
        n = len(config['HAWC2SOutputs']['blade'])
        self.add_output('outputs_blade', shape=[nws, n*naes])

        if self.with_tsr:
            self.add_param('tsr', 0.)

        if self.with_structure:
            self.add_param('blade_beam_structure', shape=cs_size)

        self.geom = HAWC2GeometryBuilder(**config['HAWC2GeometryBuilder'])
        self.geom.c12axis_init = self.reader.vartrees.main_bodies.blade1.c12axis.copy()

        if self.with_geom:
            self.geom_var = ['s', 'x', 'y', 'z', 'rot_x', 'rot_y', 'rot_z',
                             'chord', 'rthick', 'p_le']
            for v in self.geom_var:
                self.add_param(v, shape=[pfsize])
            self.add_param('blade_length', 0.)
            self.geom.interp_from_htc = False
        else:
            self.geom.interp_from_htc = True

    def solve_nonlinear(self, params, unknowns, resids):

        if self.with_tsr:
            self.writer.vartrees.dlls.risoe_controller.dll_init.designTSR = \
                params['tsr']

        if self.with_geom:
            blade_length = params['blade_length']
            for v in self.geom_var:
                setattr(self.geom.bladegeom, v, params[v])
        else:
            blade_length = self.writer.vartrees.main_bodies.blade1.c12axis[-1, 2]

        self.geom.blade_length = blade_length
        self.geom.execute()

        self.writer.vartrees.blade_ae = self.geom.blade_ae
        self.writer.vartrees.main_bodies.blade1.c12axis = self.geom.c12axis

        if self.with_structure:
            body = self.writer.vartrees.main_bodies.blade1
            self._array2hawc2beamstructure(blade_length, body,
                                           params['blade_beam_structure'])

        if self.with_ctr_tuning:
            pass
        if self.with_freq_placement:
            pass

        self.writer.execute()
        self.wrapper.compute()
        self.output.execute()

        unknowns['outputs_rotor'] = self.output.outputs_rotor
        unknowns['outputs_blade'] = self.output.outputs_blade

    def _array2hawc2beamstructure(self, blade_length, body, body_st):

        bset = body.body_set[1] - 1
        if body.st_input_type is 0:
            body.beam_structure[bset].s = body_st[:, 0]*blade_length
            body.beam_structure[bset].dm = body_st[:, 1]
            body.beam_structure[bset].x_cg = body_st[:, 2]
            body.beam_structure[bset].y_cg = body_st[:, 3]
            body.beam_structure[bset].ri_x = body_st[:, 4]
            body.beam_structure[bset].ri_y = body_st[:, 5]
            body.beam_structure[bset].x_sh = body_st[:, 6]
            body.beam_structure[bset].y_sh = body_st[:, 7]
{'fraction_non_alphanumeric': 0.0952977532782093, 'fraction_numerical': 0.011442412093877892, 'mean_word_length': 2.5052693208430914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2259289', 'n_tokens_mistral': 3768, 'n_tokens_neox': 3623, 'n_words': 884}