def interpolation(self, u: VectorHeat1D) -> VectorHeat1D: """ Interpolate input vector u using linear interpolation. Note: In the 1d heat equation example, we consider homogeneous Dirichlet BCs in space. The Heat1D vector class only stores interior points. :param u: approximate solution vector :return: input solution vector u interpolated to a fine grid """ # Get values at interior points sol = u.get_values() # Create array for interpolated values ret_array = np.zeros(int(len(sol) * 2 + 1)) # Linear interpolation for i in range(len(sol)): ret_array[i * 2] += 1 / 2 * sol[i] ret_array[i * 2 + 1] += sol[i] ret_array[i * 2 + 2] += 1 / 2 * sol[i] # Create and return a VectorHeat1D object with interpolated values ret = VectorHeat1D(len(ret_array)) ret.set_values(ret_array) return ret
def test_vector_heat_1d_sub(): """ Test __sub__ """ vector_heat_1d_1 = VectorHeat1D(5) vector_heat_1d_1.values = np.ones(5) vector_heat_1d_2 = VectorHeat1D(5) vector_heat_1d_2.values = 2 * np.ones(5) vector_heat_1d_res = vector_heat_1d_2 - vector_heat_1d_1 np.testing.assert_equal(vector_heat_1d_res.values, np.ones(5))
def test_vector_heat_1d_add(): """ Test __add__ """ vector_heat_1d_1 = VectorHeat1D(5) vector_heat_1d_1.values = np.ones(5) vector_heat_1d_2 = VectorHeat1D(5) vector_heat_1d_2.values = 2 * np.ones(5) vector_heat_1d_res = vector_heat_1d_1 + vector_heat_1d_2 np.testing.assert_equal(vector_heat_1d_res.values, 3 * np.ones(5))
def test_vector_heat_1d_norm(): """ Test norm() """ vector_heat_1d = VectorHeat1D(5) vector_heat_1d.values = np.array([1, 2, 3, 4, 5]) np.testing.assert_equal(np.linalg.norm(np.array([1, 2, 3, 4, 5])), vector_heat_1d.norm())
def test_vector_heat_1d_set_values(): """ Test the set_values() """ vector_heat_1d = VectorHeat1D(2) vector_heat_1d.set_values(np.array([1, 2])) np.testing.assert_equal(vector_heat_1d.values, np.array([1, 2]))
def test_vector_heat_1d_constructor(): """ Test constructor """ vector_heat_1d = VectorHeat1D(3) np.testing.assert_equal(vector_heat_1d.values[0], 0) np.testing.assert_equal(vector_heat_1d.values[1], 0) np.testing.assert_equal(vector_heat_1d.values[2], 0)
def test_vector_heat_1d_clone_rand(): """ Test clone_rand() """ vector_heat_1d = VectorHeat1D(2) vector_heat_1d_clone = vector_heat_1d.clone_rand() np.testing.assert_equal(True, isinstance(vector_heat_1d_clone, VectorHeat1D)) np.testing.assert_equal(len(vector_heat_1d_clone.values), 2)
def test_vector_heat_1d_mul(): """ Test __mul__ """ vector_heat_1d_1 = VectorHeat1D(5) vector_heat_1d_1.values = np.ones(5) vector_heat_1d_res = vector_heat_1d_1 * 3 np.testing.assert_equal(vector_heat_1d_res.values, np.ones(5) * 3) vector_heat_1d_res = 5 * vector_heat_1d_1 np.testing.assert_equal(vector_heat_1d_res.values, np.ones(5) * 5) vector_heat_1d_res *= 7 np.testing.assert_equal(vector_heat_1d_res.values, np.ones(5) * 35)
def restriction(self, u: VectorHeat1D) -> VectorHeat1D: """ Restrict input vector u using standard full weighting restriction. Note: In the 1d heat equation example, we consider homogeneous Dirichlet BCs in space. The Heat1D vector class only stores interior points. :param u: approximate solution vector :return: input solution vector u restricted to a coarse grid """ # Get values at interior points sol = u.get_values() # Create array for restricted values ret_array = np.zeros(int((len(sol) - 1) / 2)) # Full weighting restriction for i in range(len(ret_array)): ret_array[i] = sol[2 * i] * 1 / 4 + sol[2 * i + 1] * 1 / 2 + sol[ 2 * i + 2] * 1 / 4 # Create and return a VectorHeat1D object with the restricted values ret = VectorHeat1D(len(ret_array)) ret.set_values(ret_array) return ret
def test_vector_heat_1d_get_values(): """ Test get_values() """ vector_heat_1d = VectorHeat1D(2) np.testing.assert_equal(vector_heat_1d.get_values(), np.zeros(2))