def _load_from_file(self, filename): vsp.ClearVSPModel() vsp.ReadVSPFile(filename) for geom_id in vsp.FindGeoms(): geom_name_raw = vsp.GetGeomName(geom_id) geom_name, geom_idx = regex_listname.findall(geom_name_raw)[0] if geom_name not in self: if geom_idx: self[geom_name] = [] else: self[geom_name] = VspElement() if geom_idx != '': geom = self._update_list(self[geom_name], geom_idx) else: geom = self[geom_name] geom._id = geom_id for param_id in vsp.GetGeomParmIDs(geom_id): group_name_raw = vsp.GetParmDisplayGroupName(param_id) group_name, group_idx = regex_listname.findall( group_name_raw)[0] if group_name not in EXCLUDE_GROUPS: if group_name not in geom: if group_idx: geom[group_name] = [] else: geom[group_name] = VspElement() if group_idx != '': geom[group_name], group = self._update_list( geom[group_name], group_idx) else: group = geom[group_name] param = self._make_parameter(param_id) if param['name'] in group: raise ValueError("{} already in <{}:{}>".format( param.name, geom_name, group_name)) group[param['name']] = param
def __init__(self, vspFile, comm=MPI.COMM_WORLD, scale=1.0, comps=[], intersectedComps=None, debug=False): self.points = OrderedDict() self.pointSets = OrderedDict() self.updated = {} self.vspScale = scale self.comm = comm self.vspFile = vspFile self.debug = debug self.jac = None # Load in the VSP model vsp.ClearVSPModel() vsp.ReadVSPFile(vspFile) # Setup the export group set (0) with just the sets we want. self.exportSet = 9 # List of all componets returned from VSP. Note that this # order is important...it is the order the comps will be # written out in plot3d format. allComps = vsp.FindGeoms() # If we were not given comps, use all of them if comps == []: for c in allComps: comps.append(vsp.GetContainerName(c)) # First set the export flag for exportSet to False for everyone for comp in allComps: vsp.SetSetFlag(comp, self.exportSet, False) self.exportComps = [] for comp in allComps: # Check if this one is in our list: compName = vsp.GetContainerName(comp) if compName in comps: vsp.SetSetFlag(comp, self.exportSet, True) self.exportComps.append(compName) # Create a directory in which we will put the temporary files # we need. We *should* use something like tmepfile.mkdtemp() # but that behaves badly on pleiades. tmpDir = None if self.comm.rank == 0: tmpDir = './tmpDir_%d_%s' % (MPI.COMM_WORLD.rank, time.time()) print('Temp dir is: %s' % tmpDir) if not os.path.exists(tmpDir): os.makedirs(tmpDir) self.tmpDir = self.comm.bcast(tmpDir) # Initial list of DVs self.DVs = OrderedDict() # Run the update. This will also set the conn on the first pass self.conn = None self.pts0 = None self.cumSizes = None self.sizes = None if self.comm.rank == 0: self.pts0, self.conn, self.cumSizes, self.sizes = self._getUpdatedSurface( ) self.pts0 = self.comm.bcast(self.pts0) self.conn = self.comm.bcast(self.conn) self.cumSizes = self.comm.bcast(self.cumSizes) self.sizes = self.comm.bcast(self.sizes) self.pts = self.pts0.copy() # Finally process theintersection information. We had to wait # until all processors have the surface information. self.intersectComps = [] if intersectedComps is None: intersectedComps = [] for i in range(len(intersectedComps)): c = intersectedComps[i] # Get the index of each of the two comps: compIndexA = self.exportComps.index(c[0]) compIndexB = self.exportComps.index(c[1]) direction = c[2] dStar = c[3] extraComps = [] if len(c) == 5: for j in range(len(c[4])): extraComps.append(self.exportComps.index(c[4][j])) self.intersectComps.append( CompIntersection(compIndexA, compIndexB, extraComps, direction, dStar, self.pts, self.cumSizes, self.sizes, self.tmpDir))
vsp.PasteGeomClipboard(fuse_id) # make fuse parent # Set Name vsp.SetGeomName(pod_id, "Original_Pod") second_pod_id = vsp.FindGeom("Pod", 0) # Change Location and Symmetry vsp.SetParmVal(second_pod_id, "Sym_Planar_Flag", "Sym", 0) vsp.SetParmVal(second_pod_id, "Y_Location", "XForm", 0.0) vsp.SetParmVal(second_pod_id, "Z_Location", "XForm", 1.0) fname = "apitest1.vsp3" vsp.WriteVSPFile(fname) geoms = vsp.FindGeoms() print("All geoms in Vehicle.") print(geoms) errorMgr.PopErrorAndPrint(stdout) # ==== Use Case 2 ====# vsp.VSPRenew() errorMgr.PopErrorAndPrint(stdout) geoms = vsp.FindGeoms() print("All geoms in Vehicle.") print(geoms)
def UpdateGeometry(self, actions): # OpenVSP Script Part os.chdir("/home/simonx/Documents/Udacity/ML/Projects/capstone/OpenVSP") stdout = vsp.cvar.cstdout errorMgr = vsp.ErrorMgrSingleton_getInstance() # WingBodyTestCase vsp.VSPRenew() errorMgr.PopErrorAndPrint(stdout) # Add Wing WingBody = vsp.AddGeom("WING", "") # Insert A Couple More Sections InsertXSec(WingBody, 1, XS_FOUR_SERIES) InsertXSec(WingBody, 1, XS_FOUR_SERIES) InsertXSec(WingBody, 1, XS_FOUR_SERIES) # Cut The Original Section CutXSec(WingBody, 1) # Change Driver SetDriverGroup(WingBody, 1, AREA_WSECT_DRIVER, ROOTC_WSECT_DRIVER, TIPC_WSECT_DRIVER) SetParmVal(WingBody, "RotateAirfoilMatchDideralFlag", "WingGeom", 1.0) # Change Some Parameters 1st Section SetParmVal(WingBody, "Root_Chord", "XSec_1", 7.0) SetParmVal(WingBody, "Tip_Chord", "XSec_1", 3.0) SetParmVal(WingBody, "Area", "XSec_1", actions[0]) SetParmVal(WingBody, "Sweep", "XSec_1", actions[1]) # Because Sections Are Connected Change One Section At A Time Then Update Update() # Change Some Parameters 2nd Section SetParmVal(WingBody, "Tip_Chord", "XSec_2", 2.0) SetParmVal(WingBody, "Sweep", "XSec_2", 60.0) SetParmVal(WingBody, "Dihedral", "XSec_2", 30.0) Update() # Change Some Parameters 3rd Section SetParmVal(WingBody, "Sweep", "XSec_3", 60.0) SetParmVal(WingBody, "Dihedral", "XSec_3", 80.0) Update() # Change Airfoil SetParmVal(WingBody, "Camber", "XSecCurve_0", 0.02) Update() # print "All geoms in Vehicle." geoms = vsp.FindGeoms() print geoms # File basename baseName = "WingBody" csvName = baseName + "_Dege nGeom.csv" stlName = baseName + "_DegenGeom.stl" vspName = baseName + ".vsp3" # Set File Name SetComputationFileName(DEGEN_GEOM_CSV_TYPE, csvName) SetComputationFileName(CFD_STL_TYPE, stlName) WriteVSPFile(vspName) # ComputeDegenGeom( SET_ALL, DEGEN_GEOM_CSV_TYPE ); # ComputeCFDMesh( SET_ALL, CFD_STL_TYPE ); # Mesh # Check for errors num_err = errorMgr.GetNumTotalErrors() for i in range(0, num_err): err = errorMgr.PopLastError() print "error = ", err.m_ErrorString
def vsp_read(tag, units_type='SI',specified_network=None): """This reads an OpenVSP vehicle geometry and writes it into a SUAVE vehicle format. Includes wings, fuselages, and propellers. Assumptions: 1. OpenVSP vehicle is composed of conventionally shaped fuselages, wings, and propellers. 1a. OpenVSP fuselage: generally narrow at nose and tail, wider in center). 1b. Fuselage is designed in VSP as it appears in real life. That is, the VSP model does not rely on superficial elements such as canopies, stacks, or additional fuselages to cover up internal lofting oddities. 1c. This program will NOT account for multiple geometries comprising the fuselage. For example: a wingbox mounted beneath is a separate geometry and will NOT be processed. 2. Fuselage origin is located at nose. VSP file origin can be located anywhere, preferably at the forward tip of the vehicle or in front (to make all X-coordinates of vehicle positive). 3. Written for OpenVSP 3.21.1 Source: N/A Inputs: 1. A tag for an XML file in format .vsp3. 2. Units_type set to 'SI' (default) or 'Imperial' 3. User-specified network Outputs: Writes SUAVE vehicle with these geometries from VSP: (All values default to SI. Any other 2nd argument outputs Imperial.) Wings.Wing. (* is all keys) origin [m] in all three dimensions spans.projected [m] chords.root [m] chords.tip [m] aspect_ratio [-] sweeps.quarter_chord [radians] twists.root [radians] twists.tip [radians] thickness_to_chord [-] dihedral [radians] symmetric <boolean> tag <string> areas.reference [m^2] areas.wetted [m^2] Segments. tag <string> twist [radians] percent_span_location [-] .1 is 10% root_chord_percent [-] .1 is 10% dihedral_outboard [radians] sweeps.quarter_chord [radians] thickness_to_chord [-] airfoil <NACA 4-series, 6 series, or airfoil file> Fuselages.Fuselage. origin [m] in all three dimensions width [m] lengths. total [m] nose [m] tail [m] heights. maximum [m] at_quarter_length [m] at_three_quarters_length [m] effective_diameter [m] fineness.nose [-] ratio of nose section length to fuselage effective diameter fineness.tail [-] ratio of tail section length to fuselage effective diameter areas.wetted [m^2] tag <string> segment[]. (segments are in ordered container and callable by number) vsp.shape [point,circle,round_rect,general_fuse,fuse_file] vsp.xsec_id <10 digit string> percent_x_location percent_z_location height width length effective_diameter tag vsp.xsec_num <integer of fuselage segment quantity> vsp.xsec_surf_id <10 digit string> Propellers.Propeller. location[X,Y,Z] [radians] rotation[X,Y,Z] [radians] tip_radius [m] hub_radius [m] thrust_angle [radians] Properties Used: N/A """ vsp.ClearVSPModel() vsp.ReadVSPFile(tag) vsp_fuselages = [] vsp_wings = [] vsp_props = [] vsp_nacelles = [] vsp_nacelle_type = [] vsp_geoms = vsp.FindGeoms() geom_names = [] vehicle = SUAVE.Vehicle() vehicle.tag = tag if units_type == 'SI': units_type = 'SI' elif units_type == 'inches': units_type = 'inches' else: units_type = 'imperial' # The two for-loops below are in anticipation of an OpenVSP API update with a call for GETGEOMTYPE. # This print function allows user to enter VSP GeomID manually as first argument in vsp_read functions. print("VSP geometry IDs: ") # Label each geom type by storing its VSP geom ID. for geom in vsp_geoms: geom_name = vsp.GetGeomName(geom) geom_names.append(geom_name) print(str(geom_name) + ': ' + geom) # -------------------------------- # AUTOMATIC VSP ENTRY & PROCESSING # -------------------------------- for geom in vsp_geoms: geom_name = vsp.GetGeomName(geom) geom_type = vsp.GetGeomTypeName(str(geom)) if geom_type == 'Fuselage': vsp_fuselages.append(geom) if geom_type == 'Wing': vsp_wings.append(geom) if geom_type == 'Propeller': vsp_props.append(geom) if (geom_type == 'Stack') or (geom_type == 'BodyOfRevolution'): vsp_nacelle_type.append(geom_type) vsp_nacelles.append(geom) # -------------------------------------------------- # Read Fuselages # -------------------------------------------------- for fuselage_id in vsp_fuselages: sym_planar = vsp.GetParmVal(fuselage_id, 'Sym_Planar_Flag', 'Sym') # Check for symmetry sym_origin = vsp.GetParmVal(fuselage_id, 'Sym_Ancestor_Origin_Flag', 'Sym') if sym_planar == 2. and sym_origin == 1.: num_fus = 2 sym_flag = [1,-1] else: num_fus = 1 sym_flag = [1] for fux_idx in range(num_fus): # loop through fuselages on aircraft fuselage = read_vsp_fuselage(fuselage_id,fux_idx,sym_flag[fux_idx],units_type) vehicle.append_component(fuselage) # -------------------------------------------------- # Read Wings # -------------------------------------------------- for wing_id in vsp_wings: wing = read_vsp_wing(wing_id, units_type) vehicle.append_component(wing) # -------------------------------------------------- # Read Nacelles # -------------------------------------------------- for nac_id, nacelle_id in enumerate(vsp_nacelles): nacelle = read_vsp_nacelle(nacelle_id,vsp_nacelle_type[nac_id], units_type) vehicle.append_component(nacelle) # -------------------------------------------------- # Read Propellers/Rotors and assign to a network # -------------------------------------------------- # Initialize rotor network elements number_of_lift_rotor_engines = 0 number_of_propeller_engines = 0 lift_rotors = Data() propellers = Data() for prop_id in vsp_props: prop = read_vsp_propeller(prop_id,units_type) prop.tag = vsp.GetGeomName(prop_id) if prop.orientation_euler_angles[1] >= 70 * Units.degrees: lift_rotors.append(prop) number_of_lift_rotor_engines += 1 else: propellers.append(prop) number_of_propeller_engines += 1 if specified_network == None: # If no network specified, assign a network if number_of_lift_rotor_engines>0 and number_of_propeller_engines>0: net = Lift_Cruise() else: net = Battery_Propeller() else: net = specified_network # Create the rotor network if net.tag == "Lift_Cruise": # Lift + Cruise network for i in range(number_of_lift_rotor_engines): net.lift_rotors.append(lift_rotors[list(lift_rotors.keys())[i]]) net.number_of_lift_rotor_engines = number_of_lift_rotor_engines for i in range(number_of_propeller_engines): net.propellers.append(propellers[list(propellers.keys())[i]]) net.number_of_propeller_engines = number_of_propeller_engines elif net.tag == "Battery_Propeller": # Append all rotors as propellers for the battery propeller network for i in range(number_of_lift_rotor_engines): # Accounts for multicopter configurations net.propellers.append(lift_rotors[list(lift_rotors.keys())[i]]) for i in range(number_of_propeller_engines): net.propellers.append(propellers[list(propellers.keys())[i]]) net.number_of_propeller_engines = number_of_lift_rotor_engines + number_of_propeller_engines vehicle.networks.append(net) return vehicle
def set_sources(geometry): """This sets meshing sources in a way similar to the OpenVSP default. Some source values can also be optionally specified as below. Assumptions: None Source: https://github.com/OpenVSP/OpenVSP (with some modifications) Inputs: geometry. wings.*. (passed to add_segment_sources()) tag <string> Segments.*.percent_span_location [-] (.1 is 10%) Segments.*.root_chord_percent [-] (.1 is 10%) chords.root [m] chords.tip [m] vsp_mesh (optional) - This holds settings that are used in add_segment_sources fuselages.*. tag <string> vsp_mesh. (optional) length [m] radius [m] lengths.total (only used if vsp_mesh is not defined for the fuselage) Outputs: <tag>.stl Properties Used: N/A """ # Extract information on geometry type (for some reason it seems VSP doesn't have a simple # way to do this) comp_type_dict = dict() comp_dict = dict() for wing in geometry.wings: comp_type_dict[wing.tag] = 'wing' comp_dict[wing.tag] = wing for fuselage in geometry.fuselages: comp_type_dict[fuselage.tag] = 'fuselage' comp_dict[fuselage.tag] = fuselage # network sources have not been implemented #for network in geometry.networks: #comp_type_dict[network.tag] = 'turbojet' #comp_dict[network.tag] = network components = vsp.FindGeoms() # The default source values are (mostly) based on the OpenVSP scripts, wing for example: # https://github.com/OpenVSP/OpenVSP/blob/a5ac5302b320e8e318830663bb50ba0d4f2d6f64/src/geom_core/WingGeom.cpp for comp in components: comp_name = vsp.GetGeomName(comp) if comp_name not in comp_dict: continue comp_type = comp_type_dict[comp_name] # Nacelle sources are not implemented #if comp_name[0:8] == 'turbofan': #comp_type = comp_type_dict[comp_name[0:8]] #else: #comp_type = comp_type_dict[comp_name] if comp_type == 'wing': wing = comp_dict[comp_name] if len(wing.Segments) == 0: # check if segments exist num_secs = 1 use_base = True else: if wing.Segments[ 0].percent_span_location == 0.: # check if first segment starts at the root num_secs = len(wing.Segments) use_base = False else: num_secs = len(wing.Segments) + 1 use_base = True u_start = 0. base_root = wing.chords.root base_tip = wing.chords.tip for ii in range(0, num_secs): if (ii == 0) and (use_base == True): # create sources on root segment cr = base_root if len(wing.Segments) > 0: ct = base_root * wing.Segments[0].root_chord_percent seg = wing.Segments[ii] else: if 'vsp_mesh' in wing: custom_flag = True else: custom_flag = False ct = base_tip seg = wing # extract CFD source parameters if len(wing.Segments) == 0: wingtip_flag = True else: wingtip_flag = False add_segment_sources(comp, cr, ct, ii, u_start, num_secs, custom_flag, wingtip_flag, seg) elif (ii == 0) and (use_base == False): cr = base_root * wing.Segments[0].root_chord_percent if num_secs > 1: ct = base_root * wing.Segments[1].root_chord_percent else: ct = base_tip # extract CFD source parameters seg = wing.Segments[ii] if 'vsp_mesh' in wing.Segments[ii]: custom_flag = True else: custom_flag = False wingtip_flag = False add_segment_sources(comp, cr, ct, ii, u_start, num_secs, custom_flag, wingtip_flag, seg) elif ii < num_secs - 1: if use_base == True: jj = 1 else: jj = 0 cr = base_root * wing.Segments[ii - jj].root_chord_percent ct = base_root * wing.Segments[ii + 1 - jj].root_chord_percent seg = wing.Segments[ii - jj] if 'vsp_mesh' in wing.Segments[ii - jj]: custom_flag = True else: custom_flag = False wingtip_flag = False add_segment_sources(comp, cr, ct, ii, u_start, num_secs, custom_flag, wingtip_flag, seg) else: if use_base == True: jj = 1 else: jj = 0 cr = base_root * wing.Segments[ii - jj].root_chord_percent ct = base_tip seg = wing.Segments[ii - jj] if 'vsp_mesh' in wing.Segments[ii - jj]: custom_flag = True else: custom_flag = False wingtip_flag = True add_segment_sources(comp, cr, ct, ii, u_start, num_secs, custom_flag, wingtip_flag, seg) pass elif comp_type == 'fuselage': fuselage = comp_dict[comp_name] if 'vsp_mesh' in fuselage: len1 = fuselage.vsp_mesh.length rad1 = fuselage.vsp_mesh.radius else: len1 = 0.1 * 0.5 # not sure where VSP is getting this value rad1 = 0.2 * fuselage.lengths.total uloc = 0.0 wloc = 0.0 vsp.AddCFDSource(vsp.POINT_SOURCE, comp, 0, len1, rad1, uloc, wloc) uloc = 1.0 vsp.AddCFDSource(vsp.POINT_SOURCE, comp, 0, len1, rad1, uloc, wloc) pass
def vsp_read(tag, units_type='SI'): """This reads an OpenVSP vehicle geometry and writes it into a SUAVE vehicle format. Includes wings, fuselages, and propellers. Assumptions: 1. OpenVSP vehicle is composed of conventionally shaped fuselages, wings, and propellers. 1a. OpenVSP fuselage: generally narrow at nose and tail, wider in center). 1b. Fuselage is designed in VSP as it appears in real life. That is, the VSP model does not rely on superficial elements such as canopies, stacks, or additional fuselages to cover up internal lofting oddities. 1c. This program will NOT account for multiple geometries comprising the fuselage. For example: a wingbox mounted beneath is a separate geometry and will NOT be processed. 2. Fuselage origin is located at nose. VSP file origin can be located anywhere, preferably at the forward tip of the vehicle or in front (to make all X-coordinates of vehicle positive). 3. Written for OpenVSP 3.16.1 Source: N/A Inputs: 1. A tag for an XML file in format .vsp3. 2. Units_type set to 'SI' (default) or 'Imperial' Outputs: Writes SUAVE vehicle with these geometries from VSP: (All values default to SI. Any other 2nd argument outputs Imperial.) Wings.Wing. (* is all keys) origin [m] in all three dimensions spans.projected [m] chords.root [m] chords.tip [m] aspect_ratio [-] sweeps.quarter_chord [radians] twists.root [radians] twists.tip [radians] thickness_to_chord [-] dihedral [radians] symmetric <boolean> tag <string> areas.exposed [m^2] areas.reference [m^2] areas.wetted [m^2] Segments. tag <string> twist [radians] percent_span_location [-] .1 is 10% root_chord_percent [-] .1 is 10% dihedral_outboard [radians] sweeps.quarter_chord [radians] thickness_to_chord [-] airfoil <NACA 4-series, 6 series, or airfoil file> Fuselages.Fuselage. origin [m] in all three dimensions width [m] lengths. total [m] nose [m] tail [m] heights. maximum [m] at_quarter_length [m] at_three_quarters_length [m] effective_diameter [m] fineness.nose [-] ratio of nose section length to fuselage effective diameter fineness.tail [-] ratio of tail section length to fuselage effective diameter areas.wetted [m^2] tag <string> segment[]. (segments are in ordered container and callable by number) vsp.shape [point,circle,round_rect,general_fuse,fuse_file] vsp.xsec_id <10 digit string> percent_x_location percent_z_location height width length effective_diameter tag vsp.xsec_num <integer of fuselage segment quantity> vsp.xsec_surf_id <10 digit string> Propellers.Propeller. location[X,Y,Z] [radians] rotation[X,Y,Z] [radians] tip_radius [m] hub_radius [m] thrust_angle [radians] Properties Used: N/A """ vsp.ClearVSPModel() vsp.ReadVSPFile(tag) vsp_fuselages = [] vsp_wings = [] vsp_props = [] vsp_geoms = vsp.FindGeoms() geom_names = [] vehicle = SUAVE.Vehicle() vehicle.tag = tag if units_type == 'SI': units_type = 'SI' else: units_type = 'Imperial' # The two for-loops below are in anticipation of an OpenVSP API update with a call for GETGEOMTYPE. # This print function allows user to enter VSP GeomID manually as first argument in vsp_read functions. print("VSP geometry IDs: ") # Label each geom type by storing its VSP geom ID. (The API call for GETGEOMTYPE was not released as of 8/9/18, v 3.16.1) for geom in vsp_geoms: geom_name = vsp.GetGeomName(geom) geom_names.append(geom_name) print(str(geom_name) + ': ' + geom) # ----------------------------- # MANUAL VSP ENTRY & PROCESSING # ----------------------------- #fuselage = read_vsp_fuselage(fuselage_id, units_type=units_type) # Replace fuselage_id manually. #vehicle.append_component(fuselage) #wing = read_vsp_wing(wing_id, units_type=units_type) # Replace wing_id manually. #vehicle.append_component(wing) #prop = read_vsp_prop(prop_id, units_type=units_type) # Replace prop_id manually. #vehicle.append_component(prop) # -------------------------------- # AUTOMATIC VSP ENTRY & PROCESSING # -------------------------------- #for geom in vsp_geoms: #if vsp.GETGEOMTYPE(str(geom)) == 'FUSELAGE': #vsp_fuselages.append(geom) #if vsp.GETGEOMTYPE(str(geom)) == 'WING': #vsp_wings.append(geom) #if vsp.GETGEOMTYPE(str(geom)) == 'PROP': #vsp_props.append(geom) # Read VSP geoms and store in SUAVE components. #for vsp_fuselage in vsp_fuselages: #fuselage_id = vsp_fuselages[vsp_fuselage] #fuselage = read_vsp_fuselage(fuselage_id, units_type) #vehicle.append_component(fuselage) #for vsp_wing in vsp_wings: #wing_id = vsp_wings[vsp_wing] #wing = read_vsp_wing(wing_id, units_type) #vehicle.append_component(wing) #for vsp_prop in vsp_props: #prop_id = vsp_props[vsp_prop] #prop = read_vsp_prop(prop_id, units_type) #vehicle.append_component(prop) return vehicle