def test_debug_plot(self): if show_plots: self.roots = ut.find_roots(self.char_eq, self.n_roots, self.grid, rtol=self.rtol, show_plot=show_plots)
def test_rtol(self): roots = ut.find_roots(self.char_eq, self.n_roots, self.grid, self.rtol, show_plot=show_plots) self.assertGreaterEqual(np.log10(min(np.abs(np.diff(roots)))), self.rtol)
def test_n_dim_func(self): grid = np.array([list(range(10)), list(range(10))]) roots = ut.find_roots(self.univar_eq, self.n_roots, grid, self.rtol, show_plot=show_plots) print(roots)
def test_cmplx_func(self): grid = [np.arange(-10, 10), np.arange(-5, 5)] roots = ut.find_roots(self.cmplx_eq, 3, grid, -1, show_plot=show_plots, complex=True) self.assertTrue( np.allclose([self.cmplx_eq(root) for root in roots], [0] * len(roots))) print(roots)
def test_cmplx_func(self): grid = [np.arange(-10, 10), np.arange(-5, 5)] roots = ut.find_roots(self.cmplx_eq, 3, grid, -1, show_plot=show_plots, complex=True) self.assertTrue(np.allclose([self.cmplx_eq(root) for root in roots], [0]*len(roots))) print(roots)
def test_in_area(self): roots = ut.find_roots(self.char_eq, self.n_roots, self.grid, self.rtol) for root in roots: self.assertTrue(root >= 0.)
def test_enough_roots(self): roots = ut.find_roots(self.char_eq, self.n_roots, self.grid, self.rtol) self.assertEqual(len(roots), self.n_roots)
def test_in_fact_roots(self): roots = ut.find_roots(self.char_eq, self.n_roots, self.grid, self.rtol) for root in roots: self.assertAlmostEqual(self.char_eq(root), 0)
def test_modal(self): order = 8 def char_eq(w): return w * (np.sin(w) + self.params.m * w * np.cos(w)) def phi_k_factory(freq, derivative_order=0): def eig_func(z): return np.cos(freq * z) - self.params.m * freq * np.sin(freq * z) def eig_func_dz(z): return -freq * (np.sin(freq * z) + self.params.m * freq * np.cos(freq * z)) def eig_func_ddz(z): return freq ** 2 * (-np.cos(freq * z) + self.params.m * freq * np.sin(freq * z)) if derivative_order == 0: return eig_func elif derivative_order == 1: return eig_func_dz elif derivative_order == 2: return eig_func_ddz else: raise ValueError # create eigenfunctions eig_frequencies = ut.find_roots(char_eq, n_roots=order, grid=np.arange(0, 1e3, 2), rtol=-2) print("eigenfrequencies:") print eig_frequencies # create eigen function vectors class SWMFunctionVector(cr.ComposedFunctionVector): """ String With Mass Function Vector, necessary due to manipulated scalar product """ @property def func(self): return self.members["funcs"][0] @property def scalar(self): return self.members["scalars"][0] eig_vectors = [] for n in range(order): eig_vectors.append(SWMFunctionVector(cr.Function(phi_k_factory(eig_frequencies[n]), derivative_handles=[ phi_k_factory(eig_frequencies[n], der_order) for der_order in range(1, 3)], domain=self.dz.bounds, nonzero=self.dz.bounds), phi_k_factory(eig_frequencies[n])(0))) # normalize eigen vectors norm_eig_vectors = [cr.normalize_function(vec) for vec in eig_vectors] norm_eig_funcs = np.array([vec.func for vec in norm_eig_vectors]) register_base("norm_eig_funcs", norm_eig_funcs, overwrite=True) norm_eig_funcs[0](1) # debug print eigenfunctions if 0: func_vals = [] for vec in eig_vectors: func_vals.append(np.vectorize(vec.func)(self.dz)) norm_func_vals = [] for func in norm_eig_funcs: norm_func_vals.append(np.vectorize(func)(self.dz)) clrs = ["r", "g", "b", "c", "m", "y", "k", "w"] for n in range(1, order + 1, len(clrs)): pw_phin_k = pg.plot(title="phin_k for k in [{0}, {1}]".format(n, min(n + len(clrs), order))) for k in range(len(clrs)): if k + n > order: break pw_phin_k.plot(x=np.array(self.dz), y=norm_func_vals[n + k - 1], pen=clrs[k]) app.exec_() # create terms of weak formulation terms = [ph.IntegralTerm(ph.Product(ph.FieldVariable("norm_eig_funcs", order=(2, 0)), ph.TestFunction("norm_eig_funcs")), self.dz.bounds, scale=-1), ph.ScalarTerm(ph.Product( ph.FieldVariable("norm_eig_funcs", order=(2, 0), location=0), ph.TestFunction("norm_eig_funcs", location=0)), scale=-1), ph.ScalarTerm(ph.Product(ph.Input(self.u), ph.TestFunction("norm_eig_funcs", location=1))), ph.ScalarTerm( ph.Product(ph.FieldVariable("norm_eig_funcs", location=1), ph.TestFunction("norm_eig_funcs", order=1, location=1)), scale=-1), ph.ScalarTerm(ph.Product(ph.FieldVariable("norm_eig_funcs", location=0), ph.TestFunction("norm_eig_funcs", order=1, location=0))), ph.IntegralTerm(ph.Product(ph.FieldVariable("norm_eig_funcs"), ph.TestFunction("norm_eig_funcs", order=2)), self.dz.bounds)] modal_pde = sim.WeakFormulation(terms, name="swm_lib-modal") eval_data = sim.simulate_system(modal_pde, self.ic, self.dt, self.dz, der_orders=(2, 0)) # display results if show_plots: win = vis.PgAnimatedPlot(eval_data[0:2], title="modal approx and derivative") win2 = vis.PgSurfacePlot(eval_data[0]) app.exec_() # test for correct transition self.assertTrue(np.isclose(eval_data[0].output_data[-1, 0], self.y_end, atol=1e-3))
def test_modal(self): order = 8 def char_eq(w): return w * (np.sin(w) + self.params.m * w * np.cos(w)) def phi_k_factory(freq, derivative_order=0): def eig_func(z): return np.cos( freq * z) - self.params.m * freq * np.sin(freq * z) def eig_func_dz(z): return -freq * (np.sin(freq * z) + self.params.m * freq * np.cos(freq * z)) def eig_func_ddz(z): return freq**2 * (-np.cos(freq * z) + self.params.m * freq * np.sin(freq * z)) if derivative_order == 0: return eig_func elif derivative_order == 1: return eig_func_dz elif derivative_order == 2: return eig_func_ddz else: raise ValueError # create eigenfunctions eig_frequencies = ut.find_roots(char_eq, n_roots=order, grid=np.arange(0, 1e3, 2), rtol=-2) print("eigenfrequencies:") print(eig_frequencies) # create eigen function vectors class SWMFunctionVector(cr.ComposedFunctionVector): """ String With Mass Function Vector, necessary due to manipulated scalar product """ @property def func(self): return self.members["funcs"][0] @property def scalar(self): return self.members["scalars"][0] eig_vectors = [] for n in range(order): eig_vectors.append( SWMFunctionVector( cr.Function(phi_k_factory(eig_frequencies[n]), derivative_handles=[ phi_k_factory(eig_frequencies[n], der_order) for der_order in range(1, 3) ], domain=self.dz.bounds, nonzero=self.dz.bounds), phi_k_factory(eig_frequencies[n])(0))) # normalize eigen vectors norm_eig_vectors = [cr.normalize_function(vec) for vec in eig_vectors] norm_eig_funcs = np.array([vec.func for vec in norm_eig_vectors]) register_base("norm_eig_funcs", norm_eig_funcs, overwrite=True) norm_eig_funcs[0](1) # debug print eigenfunctions if 0: func_vals = [] for vec in eig_vectors: func_vals.append(np.vectorize(vec.func)(self.dz)) norm_func_vals = [] for func in norm_eig_funcs: norm_func_vals.append(np.vectorize(func)(self.dz)) clrs = ["r", "g", "b", "c", "m", "y", "k", "w"] for n in range(1, order + 1, len(clrs)): pw_phin_k = pg.plot(title="phin_k for k in [{0}, {1}]".format( n, min(n + len(clrs), order))) for k in range(len(clrs)): if k + n > order: break pw_phin_k.plot(x=np.array(self.dz), y=norm_func_vals[n + k - 1], pen=clrs[k]) app.exec_() # create terms of weak formulation terms = [ ph.IntegralTerm(ph.Product( ph.FieldVariable("norm_eig_funcs", order=(2, 0)), ph.TestFunction("norm_eig_funcs")), self.dz.bounds, scale=-1), ph.ScalarTerm(ph.Product( ph.FieldVariable("norm_eig_funcs", order=(2, 0), location=0), ph.TestFunction("norm_eig_funcs", location=0)), scale=-1), ph.ScalarTerm( ph.Product(ph.Input(self.u), ph.TestFunction("norm_eig_funcs", location=1))), ph.ScalarTerm(ph.Product( ph.FieldVariable("norm_eig_funcs", location=1), ph.TestFunction("norm_eig_funcs", order=1, location=1)), scale=-1), ph.ScalarTerm( ph.Product( ph.FieldVariable("norm_eig_funcs", location=0), ph.TestFunction("norm_eig_funcs", order=1, location=0))), ph.IntegralTerm( ph.Product(ph.FieldVariable("norm_eig_funcs"), ph.TestFunction("norm_eig_funcs", order=2)), self.dz.bounds) ] modal_pde = sim.WeakFormulation(terms, name="swm_lib-modal") eval_data = sim.simulate_system(modal_pde, self.ic, self.dt, self.dz, der_orders=(2, 0)) # display results if show_plots: win = vis.PgAnimatedPlot(eval_data[0:2], title="modal approx and derivative") win2 = vis.PgSurfacePlot(eval_data[0]) app.exec_() # test for correct transition self.assertTrue( np.isclose(eval_data[0].output_data[-1, 0], self.y_end, atol=1e-3))