def test_apply_as_bilinear_form(): """ Testing u^T A v operation. """ # Testing assembled case. op = fe_op.make_from_data(np.array([[1.0, 5.0], [3.0, 3.0]]), fe_op.AssemblyType.ASSEMBLED) v = np.array([1.0, 1.0]) u = np.array([2.0, 0.5]) np_test.assert_almost_equal(fe_op.apply_as_linear_form(op, u, v), 15.0) # Testing lumped case. op = fe_op.make_from_data(np.array([1.0, 5.0]), fe_op.AssemblyType.LUMPED) np_test.assert_almost_equal(fe_op.apply_as_linear_form(op, u, v), 9.0 / 2.0)
def test_clone(): """ Testing cloning of finite element operators. """ # Testing assembled case. op0 = fe_op.make_from_data(np.diag(np.array([1.0, 1.0, 1.0])), fe_op.AssemblyType.ASSEMBLED) op1 = fe_op.clone(666.0, op0) np_test.assert_array_almost_equal(666.0 * op0.data.todense(), op1.data.todense()) # Testing lumped case. op0 = fe_op.make_from_data(np.array([1.0, 1.0, 1.0]), fe_op.AssemblyType.LUMPED) op1 = fe_op.clone(666.0, op0) np_test.assert_array_almost_equal(666.0 * op0.data, op1.data)
def test_inv(): """ Testing inplace inversion of finite element operators. """ dense_mat = np.array([[4., 2., 1.], [2., 4., 1.], [1., 1., 3.]]) # Testing assembled case. op = fe_op.make_from_data(dense_mat, fe_op.AssemblyType.ASSEMBLED) fe_op.inv(op) np_test.assert_array_almost_equal(op.data.todense(), np.linalg.inv(dense_mat)) # Testing lumped case. op = fe_op.make_from_data(np.array([1.0, 2.0, 3.0]), fe_op.AssemblyType.LUMPED) fe_op.inv(op) np_test.assert_array_almost_equal(op.data, [1.0, 0.5, 1.0 / 3.0])
def test_add_value(): """ Testing adding value at specific DoF index in finite element operator. """ # Testing assembled case. warnings.simplefilter('ignore', SparseEfficiencyWarning) op0 = fe_op.make_from_data(np.diag(np.array([1.0, 1.0])), fe_op.AssemblyType.ASSEMBLED) fe_op.add_value(op0, 666.0, 0, 1) np_test.assert_array_almost_equal(op0.data.todense(), np.array([[1.0, 666.0], [0.0, 1.0]])) warnings.resetwarnings() # Testing lumped case. op0 = fe_op.make_from_data(np.array([1.0, 1.0]), fe_op.AssemblyType.LUMPED) fe_op.add_value(op0, 665.0, 1, 1) np_test.assert_array_almost_equal(op0.data, np.array([1.0, 666.0]))
def test_linear_combination(): """ Testing combination of finite element operators. """ # Testing assembled case. op0 = fe_op.make_from_data(np.diag(np.array([1.0, 1.0, 1.0])), fe_op.AssemblyType.ASSEMBLED) op1 = fe_op.make_from_data(np.diag(np.array([2.0, 2.0, 2.0])), fe_op.AssemblyType.ASSEMBLED) op2 = fe_op.linear_combination(2.0, op0, 0.5, op1) np_test.assert_array_almost_equal(op2.data.todense(), np.diag(np.array([3.0, 3.0, 3.0]))) # Testing lumped case. op0 = fe_op.make_from_data(np.array([1.0, 1.0, 1.0]), fe_op.AssemblyType.LUMPED) op1 = fe_op.make_from_data(np.array([2.0, 2.0, 2.0]), fe_op.AssemblyType.LUMPED) op2 = fe_op.linear_combination(2.0, op0, 0.5, op1) np_test.assert_array_almost_equal(op2.data, [3.0, 3.0, 3.0]) # Testing lumped and assembled case. op0 = fe_op.make_from_data(np.array([1.0, 1.0, 1.0]), fe_op.AssemblyType.LUMPED) op1 = fe_op.make_from_data(np.diag(np.array([2.0, 2.0, 2.0])), fe_op.AssemblyType.ASSEMBLED) op2 = fe_op.linear_combination(2.0, op0, 0.5, op1) np_test.assert_array_almost_equal(op2.data.todense(), np.diag(np.array([3.0, 3.0, 3.0])))
def test_mlt(): """ Testing multiplication operation on finite element operators. """ # Testing assembled case. op = fe_op.make_from_data(np.diag(np.array([1.0, 2.0, 3.0])), fe_op.AssemblyType.ASSEMBLED) u = np.array([1.0, 1.0, 1.0]) v = np.array([666.0, 666.0, 666.0]) fe_op.mlt(op, u, v) np_test.assert_array_almost_equal(v, [1.0, 2.0, 3.0]) # Testing lumped case. op = fe_op.make_from_data(np.array([1.0, 2.0, 3.0]), fe_op.AssemblyType.LUMPED) u = np.array([1.0, 1.0, 1.0]) v = np.array([666.0, 666.0, 666.0]) fe_op.mlt(op, u, v) np_test.assert_array_almost_equal(v, [1.0, 2.0, 3.0])
def test_mlt_add(): """ Testing addition & multiplication operation on finite element operators. """ # Testing assembled case. op = fe_op.make_from_data(np.diag(np.array([1.0, 2.0, 3.0])), fe_op.AssemblyType.ASSEMBLED) u = np.array([1.0, 1.0, 1.0]) v = np.array([2.0, 3.0, 4.0]) fe_op.mlt_add(op, u, v, coef=2.0) np_test.assert_array_almost_equal(v, [4.0, 7.0, 10.0]) # Testing lumped case. op = fe_op.make_from_data(np.array([1.0, 2.0, 3.0]), fe_op.AssemblyType.LUMPED) u = np.array([1.0, 1.0, 1.0]) v = np.array([2.0, 3.0, 4.0]) fe_op.mlt_add(op, u, v) np_test.assert_array_almost_equal(v, [3.0, 5.0, 7.0])
def test_spectral_radius(): """ Testing computation of spectral radius. """ # Testing on diagonal matrices. m = 2.0 k = 4.0 Mop = fe_op.make_from_data(np.diag(np.array([m, m, m])), fe_op.AssemblyType.ASSEMBLED) Kop = fe_op.make_from_data(np.diag(np.array([k, k, k])), fe_op.AssemblyType.ASSEMBLED) radius = fe_op.spectral_radius(Mop, Kop) np_test.assert_almost_equal(radius, 2.0) # testing with varying mass values m = 2.0 k = 4.0 Mop = fe_op.make_from_data(np.diag(np.array([3 * m, 2 * m, m])), fe_op.AssemblyType.ASSEMBLED) Kop = fe_op.make_from_data(np.diag(np.array([k, k, k])), fe_op.AssemblyType.ASSEMBLED) radius = fe_op.spectral_radius(Mop, Kop) np_test.assert_almost_equal(radius, 2.0)
def test_apply_pseudo_elimination(): """ Tests pseudo elimination. """ # Testing assembled case. op0 = fe_op.make_from_data(np.array([[1.0, 5.0], [2.0, 3.0]]), fe_op.AssemblyType.ASSEMBLED) fe_op.apply_pseudo_elimination(op0, 1) np_test.assert_array_almost_equal(op0.data.todense(), np.array([[1.0, 0.0], [0.0, 1.0]])) # Testing assembled case. op0 = fe_op.make_from_data( np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]), fe_op.AssemblyType.ASSEMBLED) fe_op.apply_pseudo_elimination(op0, 1) np_test.assert_array_almost_equal( op0.data.todense(), np.array([[1.0, 0.0, 3.0], [0.0, 1.0, 0.0], [7.0, 0.0, 9.0]])) # Testing lumped case. op0 = fe_op.make_from_data(np.array([1.0, 5.0]), fe_op.AssemblyType.LUMPED) fe_op.apply_pseudo_elimination(op0, 1) np_test.assert_array_almost_equal(op0.data, np.array([1.0, 1.0]))
# Step 2: perform Newton iterative descent # ======================================== propag_builder = partial(elastic_propagator.Elastic, fe_space=fe_space, mass_assembly_type=fe_op.AssemblyType.LUMPED, stiffness_assembly_type=fe_op.AssemblyType.ASSEMBLED) k = stiffness_assembler.assemble_stiffness(fe_space) minv = mass_assembler.assemble_mass(fe_space, density=alpha, assembly_type=fe_op.AssemblyType.LUMPED) fe_op.inv(minv) minv.data = scipy.sparse.dia_matrix((minv.data, [0]), shape=k.data.shape) minv_k = fe_op.make_from_data(minv.data * k.data, assembly_type=fe_op.AssemblyType.ASSEMBLED) current_theta = THETA_INIT convergence_reached = False step = 0 while not convergence_reached: dthetaJ = JREGUL * current_theta d2thetaJ = JREGUL # Building the propagators for the current descent step current_beta = partial(beta, theta=current_theta) config_theta = configuration.Elastic(alpha=alpha, beta=current_beta, left_bc=left_bc)