MCPcopy Create free account
hub / github.com/FEniCS/dolfinx / test_derivative_block

Function test_derivative_block

python/test/unit/fem/test_forms.py:148–198  ·  view source on GitHub ↗

Test the function derivative_block.

()

Source from the content-addressed store, hash-verified

146
147
148def test_derivative_block():
149 """Test the function derivative_block."""
150 mesh = dolfinx.mesh.create_unit_interval(MPI.COMM_WORLD, 10)
151 V0 = functionspace(mesh, ("Lagrange", 1))
152 V1 = functionspace(mesh, ("Lagrange", 2))
153 V = MixedFunctionSpace(V0, V1)
154
155 f0, f1 = dolfinx.fem.Function(V0), dolfinx.fem.Function(V1)
156 v0, v1 = TestFunctions(V)
157 u0, u1 = TrialFunctions(V)
158
159 M = f0**2 * dx # univariate functional
160
161 F = derivative_block(M, f0)
162 assert isinstance(F, ufl_form) and len(F.arguments()) == 1
163
164 F = derivative_block(M, f0, v0)
165 assert isinstance(F, ufl_form) and len(F.arguments()) == 1
166
167 J = derivative_block(F, f0)
168 assert isinstance(J, ufl_form) and len(J.arguments()) == 2
169
170 J = derivative_block(F, f0, u0)
171 assert isinstance(J, ufl_form) and len(J.arguments()) == 2
172
173 M_block = f0**2 * f1 * dx # multivariate functional
174
175 F_block = derivative_block(M_block, [f0, f1])
176 assert all(isinstance(F_i, ufl_form) and len(F_i.arguments()) == 1 for F_i in F_block)
177
178 F_block = derivative_block(M_block, [f0, f1], [v0, v1])
179 assert all(isinstance(F_i, ufl_form) and len(F_i.arguments()) == 1 for F_i in F_block)
180
181 with pytest.raises(ValueError):
182 derivative_block(F_block, f0) # second argument not a sequence
183
184 with pytest.raises(ValueError):
185 derivative_block(F_block, [f0, f1], [u0]) # third argument has wrong length
186
187 with pytest.raises(ValueError):
188 derivative_block(F_block, [f0, f1], u0) # third argument not a sequence
189
190 J_block = derivative_block(F_block, [f0, f1])
191 assert all(
192 isinstance(J_ij, ufl_form) and len(J_ij.arguments()) == 2 for J_i in J_block for J_ij in J_i
193 )
194
195 J_block = derivative_block(F_block, [f0, f1], [u0, u1])
196 assert all(
197 isinstance(J_ij, ufl_form) and len(J_ij.arguments()) == 2 for J_i in J_block for J_ij in J_i
198 )

Callers

nothing calls this directly

Calls 3

functionspaceFunction · 0.90
derivative_blockFunction · 0.90
FunctionMethod · 0.80

Tested by

no test coverage detected