def step(self, u_start: VectorMachine, t_start: float, t_stop: float) -> VectorMachine: """ Time integration routine :param u_start: approximate solution for the input time t_start :param t_start: time associated with the input approximate solution u_start :param t_stop: time to evolve the input approximate solution to :return: approximate solution at input time t_stop """ soli = self.run_getdp(u_start=u_start.get_values(), t_start=t_start, t_stop=t_stop) ret = VectorMachine(u_front_size=u_start.u_front_size, u_back_size=u_start.u_back_size, u_middle_size=u_start.u_middle_size) ret.set_values(values=soli['y'][-1], jl=soli['jl'][-1], ia=soli['ia'][-1], ib=soli['ib'][-1], ic=soli['ic'][-1], ua=soli['ua'][-1], ub=soli['ub'][-1], uc=soli['uc'][-1], tr=soli['tr'][-1]) return ret
def test_vector_machine_set_values(): """ Test the set_values() """ vector_machine = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_machine.set_values(values=np.array([1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3]), jl=1, ia=2, ib=3, ic=4, ua=5, ub=6, uc=7, tr=8) np.testing.assert_equal(vector_machine.u_front_size, 4) np.testing.assert_equal(vector_machine.u_middle_size, 5) np.testing.assert_equal(vector_machine.u_back_size, 6) np.testing.assert_equal(vector_machine.u_front, np.ones(4)) np.testing.assert_equal(vector_machine.u_middle, 2 * np.ones(5)) np.testing.assert_equal(vector_machine.u_back, 3 * np.ones(6)) np.testing.assert_equal(vector_machine.jl, 1) np.testing.assert_equal(vector_machine.ia, 2) np.testing.assert_equal(vector_machine.ib, 3) np.testing.assert_equal(vector_machine.ic, 4) np.testing.assert_equal(vector_machine.ua, 5) np.testing.assert_equal(vector_machine.ub, 6) np.testing.assert_equal(vector_machine.uc, 7) np.testing.assert_equal(vector_machine.tr, 8)
def test_vector_vector_machine_unpack(): """ Test get_values() """ vector_machine = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_machine.unpack( values=[np.array([1, 1, 1, 1]), np.array([2, 2, 2, 2, 2]), np.array([3, 3, 3, 3, 3, 3]), 1, 2, 3, 4, 5, 6, 7, 8]) np.testing.assert_equal(vector_machine.u_front_size, 4) np.testing.assert_equal(vector_machine.u_middle_size, 5) np.testing.assert_equal(vector_machine.u_back_size, 6) np.testing.assert_equal(vector_machine.u_front, np.ones(4)) np.testing.assert_equal(vector_machine.u_middle, 2 * np.ones(5)) np.testing.assert_equal(vector_machine.u_back, 3 * np.ones(6)) np.testing.assert_equal(vector_machine.jl, 1) np.testing.assert_equal(vector_machine.ia, 2) np.testing.assert_equal(vector_machine.ib, 3) np.testing.assert_equal(vector_machine.ic, 4) np.testing.assert_equal(vector_machine.ua, 5) np.testing.assert_equal(vector_machine.ub, 6) np.testing.assert_equal(vector_machine.uc, 7) np.testing.assert_equal(vector_machine.tr, 8)
def test_vector_vector_machine_cole_rand(): """ Test clone_rand() """ vector_machine = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_heat_1d_2pts_clone = vector_machine.clone_rand() np.testing.assert_equal(True, isinstance(vector_heat_1d_2pts_clone, VectorMachine)) np.testing.assert_equal(vector_machine.u_front_size, 4) np.testing.assert_equal(vector_machine.u_middle_size, 5) np.testing.assert_equal(vector_machine.u_back_size, 6) np.testing.assert_equal(vector_machine.u_front, np.zeros(4)) np.testing.assert_equal(vector_machine.u_middle, np.zeros(5)) np.testing.assert_equal(vector_machine.u_back, np.zeros(6)) np.testing.assert_equal(vector_machine.jl, 0) np.testing.assert_equal(vector_machine.ua, 0) np.testing.assert_equal(vector_machine.ub, 0) np.testing.assert_equal(vector_machine.uc, 0) np.testing.assert_equal(vector_machine.ia, 0) np.testing.assert_equal(vector_machine.ib, 0) np.testing.assert_equal(vector_machine.ic, 0) np.testing.assert_equal(vector_machine.tr, 0)
def test_vector_vector_machine_get_values(): """ Test get_values() """ vector_machine = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) np.testing.assert_equal(vector_machine.get_values(), np.zeros(15))
def __init__(self, grid: str, path_im3kw: str, path_getdp: str, imposed_speed: int = 1, nb_trelax: int = 2, analysis_type: int = 1, nb_max_iter: int = 60, relaxation_factor: float = 0.5, stop_criterion: float = 1e-6, nonlinear: bool = False, pwm: bool = False, pro_file: str = 'im_3kW.pro', verbose: bool = False, *args, **kwargs): """ Constructor :param nonlinear: Nonlinear or linear model :param pwm: pwm or sin rhs :param grid: mesh :param path_im3kw: path to im_3kW data :param path_getdp: path to getdp :param imposed_speed: imposed speed """ super(InductionMachine, self).__init__(*args, **kwargs) self.pro_path = path_im3kw + pro_file if not os.path.isfile(self.pro_path): raise Exception('Found no valid .pro file in', self.pro_path) self.getdp_path = path_getdp if not os.path.isfile(self.getdp_path): raise Exception('Getdp not found (http://getdp.info/)') self.nl = int(nonlinear) self.pwm = int(pwm) self.mesh = grid + '.msh' self.pre = grid + '.pre' self.further_unknowns_front = 8 self.further_unknowns_back = 15 cor_to_un, un_to_cor, boundary = pre_file(path_im3kw + self.pre) self.nx = len(un_to_cor) + self.further_unknowns_front + self.further_unknowns_back self.gopt = {'Verbose': int(verbose), 'TimeStep': self.t[1] - self.t[0], 'Executable': self.getdp_path, 'PreProcessing': '#1'} self.fopt = ['Flag_AnalysisType', analysis_type, 'Flag_NL', self.nl, 'Flag_ImposedSpeed', imposed_speed, 'Nb_max_iter', nb_max_iter, 'relaxation_factor', relaxation_factor, 'stop_criterion', stop_criterion, 'NbTrelax', nb_trelax, 'Flag_PWM', self.pwm] version_test = subprocess.run([self.gopt['Executable'], '--version'], stdout=PIPE, stderr=PIPE) if version_test.returncode: raise Exception('getdp not found.') self.vector_template = VectorMachine(u_front_size=self.further_unknowns_front, u_back_size=self.further_unknowns_back, u_middle_size=len(un_to_cor)) self.vector_t_start = VectorMachine(u_front_size=self.further_unknowns_front, u_back_size=self.further_unknowns_back, u_middle_size=len(un_to_cor))
def test_vector_vector_machine_pack(): """ Test get_values() """ vector_machine = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_machine.set_values(values=np.array( [1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3]), jl=1, ia=2, ib=3, ic=4, ua=5, ub=6, uc=7, tr=8) np.testing.assert_equal(vector_machine.pack(), [ np.array([1, 1, 1, 1]), np.array([2, 2, 2, 2, 2]), np.array([3, 3, 3, 3, 3, 3]), 1, 2, 3, 4, 5, 6, 7, 8 ]) import pickle try: pickle.dumps(vector_machine.pack()) except pickle.PicklingError: pickle_test = False pickle_test = True np.testing.assert_equal(pickle_test, True)
def test_grid_transfer_interpolation(): """ interpolation """ path = os.path.dirname(os.path.realpath(__file__)) if 'induction_machine' not in path: path += '/induction_machine/' else: path += '/' grid_transfer = GridTransferMachine(coarse_grid='im_3kW_4k', fine_grid='im_3kW_17k', path_meshes=path) vec = VectorMachine(u_front_size=2, u_middle_size=4449, u_back_size=2) res_vec = grid_transfer.interpolation(u=vec) np.testing.assert_equal(True, isinstance(res_vec, VectorMachine))
def test_vector_machine_constructor(): """ Test constructor """ vector_machine = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) np.testing.assert_equal(vector_machine.u_front_size, 4) np.testing.assert_equal(vector_machine.u_middle_size, 5) np.testing.assert_equal(vector_machine.u_back_size, 6) np.testing.assert_equal(vector_machine.u_front, np.zeros(4)) np.testing.assert_equal(vector_machine.u_middle, np.zeros(5)) np.testing.assert_equal(vector_machine.u_back, np.zeros(6)) np.testing.assert_equal(vector_machine.jl, 0) np.testing.assert_equal(vector_machine.ua, 0) np.testing.assert_equal(vector_machine.ub, 0) np.testing.assert_equal(vector_machine.uc, 0) np.testing.assert_equal(vector_machine.ia, 0) np.testing.assert_equal(vector_machine.ib, 0) np.testing.assert_equal(vector_machine.ic, 0) np.testing.assert_equal(vector_machine.tr, 0)
def test_vector_machine_sub(): """ Test __sub__ """ """ Test __add__ """ vector_machine_1 = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_machine_1.u_front = np.ones(4) vector_machine_1.u_middle = np.ones(5) vector_machine_1.u_back = np.ones(6) vector_machine_1.ua = 1 vector_machine_1.ub = 2 vector_machine_1.uc = 3 vector_machine_1.ia = 4 vector_machine_1.ib = 5 vector_machine_1.ic = 6 vector_machine_1.tr = 7 vector_machine_1.jl = 8 vector_machine_2 = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_machine_2.u_front = 2 * np.ones(4) vector_machine_2.u_middle = 3 * np.ones(5) vector_machine_2.u_back = 4 * np.ones(6) vector_machine_2.ua = 11 vector_machine_2.ub = 12 vector_machine_2.uc = 13 vector_machine_2.ia = 14 vector_machine_2.ib = 15 vector_machine_2.ic = 16 vector_machine_2.tr = 17 vector_machine_2.jl = 18 vector_machine_res = vector_machine_2 - vector_machine_1 np.testing.assert_equal(vector_machine_res.u_front, np.ones(4)) np.testing.assert_equal(vector_machine_res.u_middle, 2 * np.ones(5)) np.testing.assert_equal(vector_machine_res.u_back, 3 * np.ones(6)) np.testing.assert_equal(vector_machine_res.jl, 10) np.testing.assert_equal(vector_machine_res.ua, 10) np.testing.assert_equal(vector_machine_res.ub, 10) np.testing.assert_equal(vector_machine_res.uc, 10) np.testing.assert_equal(vector_machine_res.ia, 10) np.testing.assert_equal(vector_machine_res.ib, 10) np.testing.assert_equal(vector_machine_res.ic, 10) np.testing.assert_equal(vector_machine_res.tr, 10)
def test_vector_machine_norm(): """ Test norm() """ vector_machine = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_machine.u_front = 2 * np.ones(4) vector_machine.u_middle = 3 * np.ones(5) vector_machine.u_back = 4 * np.ones(6) vector_machine.ua = 11 vector_machine.ub = 12 vector_machine.uc = 13 vector_machine.ia = 14 vector_machine.ib = 15 vector_machine.ic = 16 vector_machine.tr = 17 vector_machine.jl = 18 np.testing.assert_equal(np.linalg.norm(np.array([2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4])), vector_machine.norm())
def test_vector_machine_add(): """ Test __add__ """ vector_machine_1 = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_machine_1.u_front = np.ones(4) vector_machine_1.u_middle = np.ones(5) vector_machine_1.u_back = np.ones(6) vector_machine_1.ua = 1 vector_machine_1.ub = 2 vector_machine_1.uc = 3 vector_machine_1.ia = 4 vector_machine_1.ib = 5 vector_machine_1.ic = 6 vector_machine_1.tr = 7 vector_machine_1.jl = 8 vector_machine_2 = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_machine_2.u_front = 2 * np.ones(4) vector_machine_2.u_middle = 3 * np.ones(5) vector_machine_2.u_back = 4 * np.ones(6) vector_machine_2.ua = 11 vector_machine_2.ub = 12 vector_machine_2.uc = 13 vector_machine_2.ia = 14 vector_machine_2.ib = 15 vector_machine_2.ic = 16 vector_machine_2.tr = 17 vector_machine_2.jl = 18 vector_machine_res = vector_machine_1 + vector_machine_2 np.testing.assert_equal(vector_machine_res.u_front, 3 * np.ones(4)) np.testing.assert_equal(vector_machine_res.u_middle, 4 * np.ones(5)) np.testing.assert_equal(vector_machine_res.u_back, 5 * np.ones(6)) np.testing.assert_equal(vector_machine_res.ua, 12) np.testing.assert_equal(vector_machine_res.ub, 14) np.testing.assert_equal(vector_machine_res.uc, 16) np.testing.assert_equal(vector_machine_res.ia, 18) np.testing.assert_equal(vector_machine_res.ib, 20) np.testing.assert_equal(vector_machine_res.ic, 22) np.testing.assert_equal(vector_machine_res.tr, 24) np.testing.assert_equal(vector_machine_res.jl, 26) vector_machine_res += vector_machine_1 np.testing.assert_equal(vector_machine_res.u_front, 4 * np.ones(4)) np.testing.assert_equal(vector_machine_res.u_middle, 5 * np.ones(5)) np.testing.assert_equal(vector_machine_res.u_back, 6 * np.ones(6)) np.testing.assert_equal(vector_machine_res.ua, 13) np.testing.assert_equal(vector_machine_res.ub, 16) np.testing.assert_equal(vector_machine_res.uc, 19) np.testing.assert_equal(vector_machine_res.ia, 22) np.testing.assert_equal(vector_machine_res.ib, 25) np.testing.assert_equal(vector_machine_res.ic, 28) np.testing.assert_equal(vector_machine_res.tr, 31) np.testing.assert_equal(vector_machine_res.jl, 34)
def test_vector_machine_mul(): """ Test __mul__ """ vector_machine_1 = VectorMachine(u_front_size=4, u_middle_size=5, u_back_size=6) vector_machine_1.u_front = np.ones(4) vector_machine_1.u_middle = np.ones(5) vector_machine_1.u_back = np.ones(6) vector_machine_1.ua = 1 vector_machine_1.ub = 2 vector_machine_1.uc = 3 vector_machine_1.ia = 4 vector_machine_1.ib = 5 vector_machine_1.ic = 6 vector_machine_1.tr = 7 vector_machine_1.jl = 8 fac = 5 vector_machine_res = vector_machine_1 * fac np.testing.assert_equal(vector_machine_res.u_front, np.ones(4) * fac) np.testing.assert_equal(vector_machine_res.u_middle, fac * np.ones(5)) np.testing.assert_equal(vector_machine_res.u_back, fac * np.ones(6)) np.testing.assert_equal(vector_machine_res.jl, 8 * fac) np.testing.assert_equal(vector_machine_res.ua, 1 * fac) np.testing.assert_equal(vector_machine_res.ub, 2 * fac) np.testing.assert_equal(vector_machine_res.uc, 3 * fac) np.testing.assert_equal(vector_machine_res.ia, 4 * fac) np.testing.assert_equal(vector_machine_res.ib, 5 * fac) np.testing.assert_equal(vector_machine_res.ic, 6 * fac) np.testing.assert_equal(vector_machine_res.tr, 7 * fac) fac = 7 vector_machine_res = fac * vector_machine_1 np.testing.assert_equal(vector_machine_res.u_front, np.ones(4) * fac) np.testing.assert_equal(vector_machine_res.u_middle, fac * np.ones(5)) np.testing.assert_equal(vector_machine_res.u_back, fac * np.ones(6)) np.testing.assert_equal(vector_machine_res.jl, 8 * fac) np.testing.assert_equal(vector_machine_res.ua, 1 * fac) np.testing.assert_equal(vector_machine_res.ub, 2 * fac) np.testing.assert_equal(vector_machine_res.uc, 3 * fac) np.testing.assert_equal(vector_machine_res.ia, 4 * fac) np.testing.assert_equal(vector_machine_res.ib, 5 * fac) np.testing.assert_equal(vector_machine_res.ic, 6 * fac) np.testing.assert_equal(vector_machine_res.tr, 7 * fac) fac2 = 7 vector_machine_res *= fac2 np.testing.assert_equal(vector_machine_res.u_front, np.ones(4) * fac * fac2) np.testing.assert_equal(vector_machine_res.u_middle, fac * fac2 * np.ones(5)) np.testing.assert_equal(vector_machine_res.u_back, fac * fac2 * np.ones(6)) np.testing.assert_equal(vector_machine_res.jl, 8 * fac * fac2) np.testing.assert_equal(vector_machine_res.ua, 1 * fac * fac2) np.testing.assert_equal(vector_machine_res.ub, 2 * fac * fac2) np.testing.assert_equal(vector_machine_res.uc, 3 * fac * fac2) np.testing.assert_equal(vector_machine_res.ia, 4 * fac * fac2) np.testing.assert_equal(vector_machine_res.ib, 5 * fac * fac2) np.testing.assert_equal(vector_machine_res.ic, 6 * fac * fac2) np.testing.assert_equal(vector_machine_res.tr, 7 * fac * fac2)