def test_assemble_derivatives(): """This test checks the original_coefficient_positions, which may change under differentiation (some coefficients and constants are eliminated)""" mesh = dolfinx.generation.UnitSquareMesh(dolfinx.MPI.comm_world, 12, 12) Q = dolfinx.FunctionSpace(mesh, ("Lagrange", 1)) u = dolfinx.Function(Q) v = ufl.TestFunction(Q) du = ufl.TrialFunction(Q) b = dolfinx.Function(Q) c1 = function.Constant(mesh, [[1.0, 0.0], [3.0, 4.0]]) c2 = function.Constant(mesh, 2.0) with b.vector.localForm() as b_local: b_local.set(2.0) # derivative eliminates 'u' and 'c1' L = ufl.inner(c1, c1) * v * dx + c2 * b * inner(u, v) * dx a = derivative(L, u, du) A1 = dolfinx.fem.assemble_matrix(a) A1.assemble() a = c2 * b * inner(du, v) * dx A2 = dolfinx.fem.assemble_matrix(a) A2.assemble() assert (A1 - A2).norm() == pytest.approx(0.0, rel=1e-12, abs=1e-12)
def test_basic_assembly_constant(): """Tests assembly with Constant The following test should be sensitive to order of flattening the matrix-valued constant. """ mesh = dolfinx.generation.UnitSquareMesh(dolfinx.MPI.comm_world, 5, 5) V = function.FunctionSpace(mesh, ("Lagrange", 1)) u, v = ufl.TrialFunction(V), ufl.TestFunction(V) c = function.Constant(mesh, [[1.0, 2.0], [5.0, 3.0]]) a = inner(c[1, 0] * u, v) * dx + inner(c[1, 0] * u, v) * ds L = inner(c[1, 0], v) * dx + inner(c[1, 0], v) * ds # Initial assembly A1 = dolfinx.fem.assemble_matrix(a) A1.assemble() b1 = dolfinx.fem.assemble_vector(L) b1.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE) c.value = [[1.0, 2.0], [3.0, 4.0]] A2 = dolfinx.fem.assemble_matrix(a) A2.assemble() b2 = dolfinx.fem.assemble_vector(L) b2.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE) assert (A1 * 3.0 - A2 * 5.0).norm() == pytest.approx(0.0) assert (b1 * 3.0 - b2 * 5.0).norm() == pytest.approx(0.0)