def __init__(self, geo=-1, elec=-1, simu=-1, init_dict=None): """Constructor of the class. Can be use in two ways : - __init__ (arg1 = 1, arg3 = 5) every parameters have name and default values for Matrix, None will initialise the property with an empty Matrix for pyleecan type, None will call the default constructor - __init__ (init_dict = d) d must be a dictionnary wiht every properties as keys ndarray or list can be given for Vector and Matrix object or dict can be given for pyleecan Object""" if geo == -1: geo = OutGeo() if elec == -1: elec = OutElec() if simu == -1: simu = Simulation() if init_dict is not None: # Initialisation by dict check_init_dict(init_dict, ["geo", "elec", "simu"]) # Overwrite default value with init_dict content if "geo" in list(init_dict.keys()): geo = init_dict["geo"] if "elec" in list(init_dict.keys()): elec = init_dict["elec"] if "simu" in list(init_dict.keys()): simu = init_dict["simu"] # Initialisation by argument self.parent = None # geo can be None, a OutGeo object or a dict if isinstance(geo, dict): self.geo = OutGeo(init_dict=geo) else: self.geo = geo # elec can be None, a OutElec object or a dict if isinstance(elec, dict): self.elec = OutElec(init_dict=elec) else: self.elec = elec # simu can be None, a Simulation object or a dict if isinstance(simu, dict): # Call the correct constructor according to the dict load_dict = {"Simu1": Simu1, "Simulation": Simulation} obj_class = simu.get("__class__") if obj_class is None: self.simu = Simulation(init_dict=simu) elif obj_class in list(load_dict.keys()): self.simu = load_dict[obj_class](init_dict=simu) else: # Avoid generation error or wrong modification in json raise InitUnKnowClassError( "Unknow class name in init_dict for simu") else: self.simu = simu # The class is frozen, for now it's impossible to add new properties self._freeze()
def test_InCurrent_Ok(self): """Check that the input current can return a correct output """ test_obj = Simulation(machine=M3) output = Output(simu=test_obj) time = ImportGenVectLin(0, 1, 16) angle = ImportGenVectLin(0, 2 * pi, 20) Is = ImportGenMatrixSin(is_transpose=True) Is.init_vector(f=[2, 2, 2], A=[2, 2, 2], Phi=[pi / 2, 0, -pi / 2], N=16, Tf=1) S = sqrt(2) Is_exp = transpose( array([ [2, S, 0, -S, -2, -S, 0, S, 2, S, 0, -S, -2, -S, 0, S], [0, S, 2, S, 0, -S, -2, -S, 0, S, 2, S, 0, -S, -2, -S], [-2, -S, 0, S, 2, S, 0, -S, -2, -S, 0, S, 2, S, 0, -S], ])) Ir = ImportGenMatrixSin(is_transpose=True) Ir.init_vector(f=[2, 2], A=[2, 2], Phi=[0, -pi / 2], N=16, Tf=1) Ir_exp = transpose( array([ [0, S, 2, S, 0, -S, -2, -S, 0, S, 2, S, 0, -S, -2, -S], [-2, -S, 0, S, 2, S, 0, -S, -2, -S, 0, S, 2, S, 0, -S], ])) angle_rotor = ImportGenVectLin(0, 2 * pi, 16) Nr = ImportMatrixVal(value=ones(16) * 10) test_obj.input = InCurrent(time=time, angle=angle, Is=Is, Ir=Ir, angle_rotor=angle_rotor, Nr=Nr) test_obj.input.gen_input() assert_array_almost_equal(output.elec.time, linspace(0, 1, 16)) assert_array_almost_equal(output.elec.angle, linspace(0, 2 * pi, 20)) assert_array_almost_equal(output.elec.Is, Is_exp) assert_array_almost_equal(output.elec.Ir, Ir_exp) assert_array_almost_equal(output.elec.angle_rotor, linspace(0, 2 * pi, 16)) assert_array_almost_equal(output.elec.Nr, ones(16) * 10)
def test_InCurrentDQ_Ok(self): """Check that the input current can return a correct output """ test_obj = Simulation(machine=M1) output = Output(simu=test_obj) time = ImportGenVectLin(0, 1, 7) angle = ImportGenVectLin(0, 2 * pi, 20) Is = ImportMatrixVal(value=transpose( array([ [2, 2, 2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0], ]))) Is_exp = transpose( array([ [2, 1, -1, -2, -1, 1, 2], [-1, 1, 2, 1, -1, -2, -1], [-1, -2, -1, 1, 2, 1, -1], ])) zp = M1.stator.get_pole_pair_number() angle_rotor_initial = M1.comp_initial_angle() angle_rotor_exp = linspace(0, 2 * pi / zp, 7) + angle_rotor_initial Nr = ImportMatrixVal(value=ones(7) * 60 / zp) test_obj.input = InCurrentDQ(time=time, angle=angle, Is=Is, Ir=None, angle_rotor=None, Nr=Nr, angle_rotor_initial=angle_rotor_initial, rot_dir=1) test_obj.input.gen_input() assert_array_almost_equal(output.elec.time, linspace(0, 1, 7)) assert_array_almost_equal(output.elec.angle, linspace(0, 2 * pi, 20)) assert_array_almost_equal(output.elec.Is, Is_exp) assert_array_almost_equal(output.elec.angle_rotor, angle_rotor_exp) assert_array_almost_equal(output.elec.Nr, ones(7) * 60 / zp)
def setUp(self): self.simu = Simulation() self.out = Output(simu=self.simu) self.mesh = Mesh() self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3) self.mesh.node = NodeMat() self.mesh.node.add_node(np.array([0, 0])) self.mesh.node.add_node(np.array([1, 0])) self.mesh.node.add_node(np.array([0, 1])) self.mesh.node.add_node(np.array([1, 1])) self.mesh.node.add_node(np.array([2, 0])) self.mesh.add_element(np.array([0, 1, 2]), "Triangle3", group=int(3)) self.mesh.add_element(np.array([1, 2, 3]), "Triangle3", group=int(3)) self.mesh.add_element(np.array([4, 1, 3]), "Triangle3", group=int(2))
class Output(FrozenClass): """Main Output object: gather all the outputs of all the modules""" VERSION = 1 # cf Methods.Output.Output.getter.get_BH_stator get_BH_stator = get_BH_stator # cf Methods.Output.Output.getter.get_BH_rotor get_BH_rotor = get_BH_rotor # cf Methods.Output.Output.getter.get_path_result get_path_result = get_path_result # cf Methods.Output.Output.getter.get_angle_rotor get_angle_rotor = get_angle_rotor # cf Methods.Output.Output.plot.Magnetic.plot_B_space plot_B_space = plot_B_space # save method is available in all object save = save def __init__(self, simu=-1, path_res="", geo=-1, elec=-1, mag=-1, post=-1, init_dict=None): """Constructor of the class. Can be use in two ways : - __init__ (arg1 = 1, arg3 = 5) every parameters have name and default values for Matrix, None will initialise the property with an empty Matrix for pyleecan type, None will call the default constructor - __init__ (init_dict = d) d must be a dictionnary wiht every properties as keys ndarray or list can be given for Vector and Matrix object or dict can be given for pyleecan Object""" if simu == -1: simu = Simulation() if geo == -1: geo = OutGeo() if elec == -1: elec = OutElec() if mag == -1: mag = OutMag() if post == -1: post = OutPost() if init_dict is not None: # Initialisation by dict check_init_dict(init_dict, ["simu", "path_res", "geo", "elec", "mag", "post"]) # Overwrite default value with init_dict content if "simu" in list(init_dict.keys()): simu = init_dict["simu"] if "path_res" in list(init_dict.keys()): path_res = init_dict["path_res"] if "geo" in list(init_dict.keys()): geo = init_dict["geo"] if "elec" in list(init_dict.keys()): elec = init_dict["elec"] if "mag" in list(init_dict.keys()): mag = init_dict["mag"] if "post" in list(init_dict.keys()): post = init_dict["post"] # Initialisation by argument self.parent = None # simu can be None, a Simulation object or a dict if isinstance(simu, dict): # Call the correct constructor according to the dict load_dict = {"Simu1": Simu1, "Simulation": Simulation} obj_class = simu.get("__class__") if obj_class is None: self.simu = Simulation(init_dict=simu) elif obj_class in list(load_dict.keys()): self.simu = load_dict[obj_class](init_dict=simu) else: # Avoid generation error or wrong modification in json raise InitUnKnowClassError( "Unknow class name in init_dict for simu") else: self.simu = simu self.path_res = path_res # geo can be None, a OutGeo object or a dict if isinstance(geo, dict): self.geo = OutGeo(init_dict=geo) else: self.geo = geo # elec can be None, a OutElec object or a dict if isinstance(elec, dict): self.elec = OutElec(init_dict=elec) else: self.elec = elec # mag can be None, a OutMag object or a dict if isinstance(mag, dict): self.mag = OutMag(init_dict=mag) else: self.mag = mag # post can be None, a OutPost object or a dict if isinstance(post, dict): self.post = OutPost(init_dict=post) else: self.post = post # The class is frozen, for now it's impossible to add new properties self._freeze() def __str__(self): """Convert this objet in a readeable string (for print)""" Output_str = "" if self.parent is None: Output_str += "parent = None " + linesep else: Output_str += "parent = " + str(type( self.parent)) + " object" + linesep Output_str += "simu = " + str(self.simu.as_dict()) + linesep + linesep Output_str += 'path_res = "' + str(self.path_res) + '"' + linesep Output_str += "geo = " + str(self.geo.as_dict()) + linesep + linesep Output_str += "elec = " + str(self.elec.as_dict()) + linesep + linesep Output_str += "mag = " + str(self.mag.as_dict()) + linesep + linesep Output_str += "post = " + str(self.post.as_dict()) return Output_str def __eq__(self, other): """Compare two objects (skip parent)""" if type(other) != type(self): return False if other.simu != self.simu: return False if other.path_res != self.path_res: return False if other.geo != self.geo: return False if other.elec != self.elec: return False if other.mag != self.mag: return False if other.post != self.post: return False return True def as_dict(self): """Convert this objet in a json seriable dict (can be use in __init__) """ Output_dict = dict() if self.simu is None: Output_dict["simu"] = None else: Output_dict["simu"] = self.simu.as_dict() Output_dict["path_res"] = self.path_res if self.geo is None: Output_dict["geo"] = None else: Output_dict["geo"] = self.geo.as_dict() if self.elec is None: Output_dict["elec"] = None else: Output_dict["elec"] = self.elec.as_dict() if self.mag is None: Output_dict["mag"] = None else: Output_dict["mag"] = self.mag.as_dict() if self.post is None: Output_dict["post"] = None else: Output_dict["post"] = self.post.as_dict() # The class name is added to the dict fordeserialisation purpose Output_dict["__class__"] = "Output" return Output_dict def _set_None(self): """Set all the properties to None (except pyleecan object)""" if self.simu is not None: self.simu._set_None() self.path_res = None if self.geo is not None: self.geo._set_None() if self.elec is not None: self.elec._set_None() if self.mag is not None: self.mag._set_None() if self.post is not None: self.post._set_None() def _get_simu(self): """getter of simu""" return self._simu def _set_simu(self, value): """setter of simu""" check_var("simu", value, "Simulation") self._simu = value if self._simu is not None: self._simu.parent = self # Simulation object that generated the Output # Type : Simulation simu = property( fget=_get_simu, fset=_set_simu, doc=u"""Simulation object that generated the Output""", ) def _get_path_res(self): """getter of path_res""" return self._path_res def _set_path_res(self, value): """setter of path_res""" check_var("path_res", value, "str") self._path_res = value # Path to the folder to same the results # Type : str path_res = property( fget=_get_path_res, fset=_set_path_res, doc=u"""Path to the folder to same the results""", ) def _get_geo(self): """getter of geo""" return self._geo def _set_geo(self, value): """setter of geo""" check_var("geo", value, "OutGeo") self._geo = value if self._geo is not None: self._geo.parent = self # Geometry output # Type : OutGeo geo = property(fget=_get_geo, fset=_set_geo, doc=u"""Geometry output""") def _get_elec(self): """getter of elec""" return self._elec def _set_elec(self, value): """setter of elec""" check_var("elec", value, "OutElec") self._elec = value if self._elec is not None: self._elec.parent = self # Electrical module output # Type : OutElec elec = property(fget=_get_elec, fset=_set_elec, doc=u"""Electrical module output""") def _get_mag(self): """getter of mag""" return self._mag def _set_mag(self, value): """setter of mag""" check_var("mag", value, "OutMag") self._mag = value if self._mag is not None: self._mag.parent = self # Magnetic module output # Type : OutMag mag = property(fget=_get_mag, fset=_set_mag, doc=u"""Magnetic module output""") def _get_post(self): """getter of post""" return self._post def _set_post(self, value): """setter of post""" check_var("post", value, "OutPost") self._post = value if self._post is not None: self._post.parent = self # Post-Processing settings # Type : OutPost post = property(fget=_get_post, fset=_set_post, doc=u"""Post-Processing settings""")
def __init__( self, simu=-1, path_res="", geo=-1, elec=-1, mag=-1, struct=-1, post=-1, init_dict=None, ): """Constructor of the class. Can be use in two ways : - __init__ (arg1 = 1, arg3 = 5) every parameters have name and default values for Matrix, None will initialise the property with an empty Matrix for pyleecan type, None will call the default constructor - __init__ (init_dict = d) d must be a dictionnary wiht every properties as keys ndarray or list can be given for Vector and Matrix object or dict can be given for pyleecan Object""" if simu == -1: simu = Simulation() if geo == -1: geo = OutGeo() if elec == -1: elec = OutElec() if mag == -1: mag = OutMag() if struct == -1: struct = OutStruct() if post == -1: post = OutPost() if init_dict is not None: # Initialisation by dict check_init_dict( init_dict, ["simu", "path_res", "geo", "elec", "mag", "struct", "post"]) # Overwrite default value with init_dict content if "simu" in list(init_dict.keys()): simu = init_dict["simu"] if "path_res" in list(init_dict.keys()): path_res = init_dict["path_res"] if "geo" in list(init_dict.keys()): geo = init_dict["geo"] if "elec" in list(init_dict.keys()): elec = init_dict["elec"] if "mag" in list(init_dict.keys()): mag = init_dict["mag"] if "struct" in list(init_dict.keys()): struct = init_dict["struct"] if "post" in list(init_dict.keys()): post = init_dict["post"] # Initialisation by argument self.parent = None # simu can be None, a Simulation object or a dict if isinstance(simu, dict): # Check that the type is correct (including daughter) class_name = simu.get("__class__") if class_name not in ["Simulation", "Simu1"]: raise InitUnKnowClassError("Unknow class name " + class_name + " in init_dict for simu") # Dynamic import to call the correct constructor module = __import__("pyleecan.Classes." + class_name, fromlist=[class_name]) class_obj = getattr(module, class_name) self.simu = class_obj(init_dict=simu) else: self.simu = simu self.path_res = path_res # geo can be None, a OutGeo object or a dict if isinstance(geo, dict): self.geo = OutGeo(init_dict=geo) else: self.geo = geo # elec can be None, a OutElec object or a dict if isinstance(elec, dict): self.elec = OutElec(init_dict=elec) else: self.elec = elec # mag can be None, a OutMag object or a dict if isinstance(mag, dict): self.mag = OutMag(init_dict=mag) else: self.mag = mag # struct can be None, a OutStruct object or a dict if isinstance(struct, dict): self.struct = OutStruct(init_dict=struct) else: self.struct = struct # post can be None, a OutPost object or a dict if isinstance(post, dict): self.post = OutPost(init_dict=post) else: self.post = post # The class is frozen, for now it's impossible to add new properties self._freeze()
M1 = Machine() M1.stator = LamSlotWind() M1.stator.winding.qs = 3 # Winding rotor only M2 = Machine() M2.rotor = LamSlotWind() M2.rotor.winding.qs = 2 # Winding rotor + stator M3 = Machine() M3.stator = LamSlotWind() M3.stator.winding.qs = 3 M3.rotor = LamSlotWind() M3.rotor.winding.qs = 2 # Wrong time test_obj = Simulation() test_obj.input = InCurrent(time=None) InCurrent_Error_test.append({ "test_obj": test_obj, "exp": "ERROR: InCurrent.time missing" }) test_obj = Simulation() test_obj.input = InCurrent(time=time_wrong) InCurrent_Error_test.append({ "test_obj": test_obj, "exp": "ERROR: InCurrent.time should be a vector, (10, 2) shape found", }) # Wrong angle test_obj = Simulation()
class Output(FrozenClass): """Main Output object: gather all the outputs of all the modules""" VERSION = 1 # save method is available in all object save = save def __init__(self, geo=-1, elec=-1, simu=-1, init_dict=None): """Constructor of the class. Can be use in two ways : - __init__ (arg1 = 1, arg3 = 5) every parameters have name and default values for Matrix, None will initialise the property with an empty Matrix for pyleecan type, None will call the default constructor - __init__ (init_dict = d) d must be a dictionnary wiht every properties as keys ndarray or list can be given for Vector and Matrix object or dict can be given for pyleecan Object""" if geo == -1: geo = OutGeo() if elec == -1: elec = OutElec() if simu == -1: simu = Simulation() if init_dict is not None: # Initialisation by dict check_init_dict(init_dict, ["geo", "elec", "simu"]) # Overwrite default value with init_dict content if "geo" in list(init_dict.keys()): geo = init_dict["geo"] if "elec" in list(init_dict.keys()): elec = init_dict["elec"] if "simu" in list(init_dict.keys()): simu = init_dict["simu"] # Initialisation by argument self.parent = None # geo can be None, a OutGeo object or a dict if isinstance(geo, dict): self.geo = OutGeo(init_dict=geo) else: self.geo = geo # elec can be None, a OutElec object or a dict if isinstance(elec, dict): self.elec = OutElec(init_dict=elec) else: self.elec = elec # simu can be None, a Simulation object or a dict if isinstance(simu, dict): # Call the correct constructor according to the dict load_dict = {"Simu1": Simu1, "Simulation": Simulation} obj_class = simu.get("__class__") if obj_class is None: self.simu = Simulation(init_dict=simu) elif obj_class in list(load_dict.keys()): self.simu = load_dict[obj_class](init_dict=simu) else: # Avoid generation error or wrong modification in json raise InitUnKnowClassError( "Unknow class name in init_dict for simu") else: self.simu = simu # The class is frozen, for now it's impossible to add new properties self._freeze() def __str__(self): """Convert this objet in a readeable string (for print)""" Output_str = "" if self.parent is None: Output_str += "parent = None " + linesep else: Output_str += "parent = " + str(type( self.parent)) + " object" + linesep Output_str += "geo = " + str(self.geo.as_dict()) + linesep + linesep Output_str += "elec = " + str(self.elec.as_dict()) + linesep + linesep Output_str += "simu = " + str(self.simu.as_dict()) return Output_str def __eq__(self, other): """Compare two objects (skip parent)""" if type(other) != type(self): return False if other.geo != self.geo: return False if other.elec != self.elec: return False if other.simu != self.simu: return False return True def as_dict(self): """Convert this objet in a json seriable dict (can be use in __init__) """ Output_dict = dict() if self.geo is None: Output_dict["geo"] = None else: Output_dict["geo"] = self.geo.as_dict() if self.elec is None: Output_dict["elec"] = None else: Output_dict["elec"] = self.elec.as_dict() if self.simu is None: Output_dict["simu"] = None else: Output_dict["simu"] = self.simu.as_dict() # The class name is added to the dict fordeserialisation purpose Output_dict["__class__"] = "Output" return Output_dict def _set_None(self): """Set all the properties to None (except pyleecan object)""" if self.geo is not None: self.geo._set_None() if self.elec is not None: self.elec._set_None() if self.simu is not None: self.simu._set_None() def _get_geo(self): """getter of geo""" return self._geo def _set_geo(self, value): """setter of geo""" check_var("geo", value, "OutGeo") self._geo = value if self._geo is not None: self._geo.parent = self # Geometry output # Type : OutGeo geo = property(fget=_get_geo, fset=_set_geo, doc=u"""Geometry output""") def _get_elec(self): """getter of elec""" return self._elec def _set_elec(self, value): """setter of elec""" check_var("elec", value, "OutElec") self._elec = value if self._elec is not None: self._elec.parent = self # Electrical module output # Type : OutElec elec = property(fget=_get_elec, fset=_set_elec, doc=u"""Electrical module output""") def _get_simu(self): """getter of simu""" return self._simu def _set_simu(self, value): """setter of simu""" check_var("simu", value, "Simulation") self._simu = value if self._simu is not None: self._simu.parent = self # Simulation object that generated the Output # Type : Simulation simu = property( fget=_get_simu, fset=_set_simu, doc=u"""Simulation object that generated the Output""", )