| 1 |
from dolfin import * |
| 2 |
import ufl_legacy as ufl |
| 3 |
import numpy |
| 4 |
import sys |
| 5 |
import pytest |
| 6 |
from dolfin_utils.test import set_parameters_fixture, skip_in_parallel, fixture |
| 7 |
|
| 8 |
from ufl_legacy.classes import CellOrientation, CellNormal, CellCoordinate, \ |
| 9 |
CellOrigin, Jacobian, JacobianInverse, JacobianDeterminant |
| 10 |
|
| 11 |
# This was for debugging, don't enable this permanently here in tests |
| 12 |
# parameters["reorder_dofs_serial"] = False |
| 13 |
any_representation = \ |
| 14 |
set_parameters_fixture("form_compiler.representation", |
| 15 |
["quadrature", "uflacs"]) |
| 16 |
uflacs_representation_only \ |
| 17 |
= set_parameters_fixture("form_compiler.representation", |
| 18 |
["uflacs"]) |
| 19 |
|
| 20 |
|
| 21 |
def create_mesh(vertices, cells, cellname="simplex"): |
| 22 |
"""Given list of vertex coordinate tuples and cell vertex index |
| 23 |
tuples, build and return a mesh. |
| 24 |
|
| 25 |
""" |
| 26 |
|
| 27 |
# Get dimensions |
| 28 |
gdim = len(vertices[0]) |
| 29 |
|
| 30 |
# Automatic choice of cellname for simplices |
| 31 |
if cellname == "simplex": |
| 32 |
num_cell_vertices = len(cells[0]) |
| 33 |
cellname = {1: "vertex", |
| 34 |
2: "interval", |
| 35 |
3: "triangle", |
| 36 |
4: "tetrahedron", |
| 37 |
}[num_cell_vertices] |
| 38 |
|
| 39 |
# Indirect error checking and determination of tdim via ufl |
| 40 |
ufl_cell = ufl.Cell(cellname, gdim) |
| 41 |
tdim = ufl_cell.topological_dimension() |
| 42 |
|
| 43 |
# Create mesh to return |
| 44 |
mesh = Mesh() |
| 45 |
|
| 46 |
# Open mesh in editor |
| 47 |
me = MeshEditor() |
| 48 |
me.open(mesh, cellname, tdim, gdim) |
| 49 |
|
| 50 |
# Add vertices to mesh |
| 51 |
me.init_vertices(len(vertices)) |
| 52 |
for i, v in enumerate(vertices): |
| 53 |
me.add_vertex(i, numpy.array(v, dtype='float')) |
| 54 |
|
| 55 |
# Add cells to mesh |
| 56 |
me.init_cells(len(cells)) |
| 57 |
for i, c in enumerate(cells): |
| 58 |
me.add_cell(i, numpy.array(c, dtype='uint')) |
| 59 |
|
| 60 |
me.close() |
| 61 |
|
| 62 |
return mesh |
| 63 |
|
| 64 |
|
| 65 |
def create_line_mesh(vertices): |
| 66 |
"""Given list of vertex coordinate tuples, build and return a mesh of |
| 67 |
intervals. |
| 68 |
|
| 69 |
""" |
| 70 |
|
| 71 |
# Get dimensions |
| 72 |
gdim = len(vertices[0]) |
| 73 |
tdim = 1 |
| 74 |
|
| 75 |
# Automatic choice of cellname for simplices |
| 76 |
cellname = "interval" |
| 77 |
|
| 78 |
# Indirect error checking and determination of tdim via ufl |
| 79 |
ufl_cell = ufl.Cell(cellname, gdim) |
| 80 |
assert tdim == ufl_cell.topological_dimension() |
| 81 |
|
| 82 |
# Create mesh to return |
| 83 |
mesh = Mesh() |
| 84 |
|
| 85 |
# Open mesh in editor |
| 86 |
me = MeshEditor() |
| 87 |
me.open(mesh, cellname, tdim, gdim) |
| 88 |
|
| 89 |
# Add vertices to mesh |
| 90 |
nv = len(vertices) |
| 91 |
me.init_vertices(nv) |
| 92 |
|
| 93 |
for i, v in enumerate(vertices): |
| 94 |
me.add_vertex(i, numpy.array(v, dtype='float')) |
| 95 |
|
| 96 |
# TODO: Systematically swap around vertex ordering to test cell orientation |
| 97 |
|
| 98 |
# Add cells to mesh |
| 99 |
me.init_cells(nv-1) |
| 100 |
for i in range(nv-1): |
| 101 |
c = (i, i+1) |
| 102 |
me.add_cell(i, numpy.array(c, dtype='uint')) |
| 103 |
|
| 104 |
me.close() |
| 105 |
|
| 106 |
return mesh |
| 107 |
|
| 108 |
line_resolution = 8 |
| 109 |
|
| 110 |
def line1d_impl(request): |
| 111 |
n = line_resolution |
| 112 |
us = [i/float(n-1) for i in range(n)] |
| 113 |
vertices = [(u**2,) for u in us] |
| 114 |
return create_line_mesh(vertices) |
| 115 |
|
| 116 |
def rline1d_impl(request): |
| 117 |
n = line_resolution |
| 118 |
us = [i/float(n-1) for i in range(n)] |
| 119 |
vertices = [(u**2,) for u in us] |
| 120 |
vertices = list(reversed(vertices)) # same as line1d, just reversed here |
| 121 |
return create_line_mesh(vertices) |
| 122 |
|
| 123 |
def line2d_impl(request): |
| 124 |
n = line_resolution |
| 125 |
us = [i/float(n-1) for i in range(n)] |
| 126 |
vertices = [(cos(DOLFIN_PI*u), sin(DOLFIN_PI*u)) for u in us] |
| 127 |
mesh = create_line_mesh(vertices) |
| 128 |
mesh.init_cell_orientations(Expression(("0.0", "1.0"), degree=0)) |
| 129 |
return mesh |
| 130 |
|
| 131 |
def rline2d_impl(request): |
| 132 |
n = line_resolution |
| 133 |
us = [i/float(n-1) for i in range(n)] |
| 134 |
vertices = [(cos(DOLFIN_PI*u), sin(DOLFIN_PI*u)) for u in us] |
| 135 |
vertices = list(reversed(vertices)) # same as line2d, just reversed here |
| 136 |
mesh = create_line_mesh(vertices) |
| 137 |
mesh.init_cell_orientations(Expression(("0.0", "1.0"), degree=0)) |
| 138 |
return mesh |
| 139 |
|
| 140 |
def line3d_impl(request): |
| 141 |
n = line_resolution |
| 142 |
us = [i/float(n-1) for i in range(n)] |
| 143 |
vertices = [(cos(4.0*DOLFIN_PI*u), |
| 144 |
sin(4.0*DOLFIN_PI*u), |
| 145 |
2.0*u) for u in us] |
| 146 |
mesh = create_line_mesh(vertices) |
| 147 |
return mesh |
| 148 |
|
| 149 |
def rline3d_impl(request): |
| 150 |
n = line_resolution |
| 151 |
us = [i/float(n-1) for i in range(n)] |
| 152 |
vertices = [(cos(4.0*DOLFIN_PI*u), |
| 153 |
sin(4.0*DOLFIN_PI*u), |
| 154 |
2.0*u) for u in us] |
| 155 |
vertices = list(reversed(vertices)) # same as line3d, just reversed here |
| 156 |
mesh = create_line_mesh(vertices) |
| 157 |
return mesh |
| 158 |
|
| 159 |
def square2d_impl(request): |
| 160 |
cellname = "triangle" |
| 161 |
side = sqrt(sqrt(3.0)) |
| 162 |
vertices = [ |
| 163 |
(0.0, 0.0), |
| 164 |
(side, side), |
| 165 |
(side, 0.0), |
| 166 |
(0.0, side), |
| 167 |
] |
| 168 |
cells = [ |
| 169 |
(0, 1, 2), |
| 170 |
(0, 1, 3), |
| 171 |
] |
| 172 |
mesh = create_mesh(vertices, cells) |
| 173 |
return mesh |
| 174 |
|
| 175 |
def square3d_impl(request): |
| 176 |
cellname = "triangle" |
| 177 |
vertices = [ |
| 178 |
(0.0, 0.0, 1.0), |
| 179 |
(1.0, 1.0, 1.0), |
| 180 |
(1.0, 0.0, 0.0), |
| 181 |
(0.0, 1.0, 0.0), |
| 182 |
] |
| 183 |
cells = [ |
| 184 |
(0, 1, 2), |
| 185 |
(0, 1, 3), |
| 186 |
] |
| 187 |
mesh = create_mesh(vertices, cells) |
| 188 |
mesh.init_cell_orientations(Expression(("0.0", "0.0", "1.0"), degree=0)) |
| 189 |
|
| 190 |
return mesh |
| 191 |
|
| 192 |
@fixture |
| 193 |
def line1d(request): |
| 194 |
return line1d_impl(request) |
| 195 |
|
| 196 |
@fixture |
| 197 |
def rline1d(request): |
| 198 |
return rline1d_impl(request) |
| 199 |
|
| 200 |
@fixture |
| 201 |
def line2d(request): |
| 202 |
return line2d_impl(request) |
| 203 |
|
| 204 |
@fixture |
| 205 |
def rline2d(request): |
| 206 |
return rline2d_impl(request) |
| 207 |
|
| 208 |
@fixture |
| 209 |
def line3d(request): |
| 210 |
return line3d_impl(request) |
| 211 |
|
| 212 |
@fixture |
| 213 |
def rline3d(request): |
| 214 |
return rline3d_impl(request) |
| 215 |
|
| 216 |
@fixture |
| 217 |
def square2d(request): |
| 218 |
return square2d_impl(request) |
| 219 |
|
| 220 |
@fixture |
| 221 |
def square3d(request): |
| 222 |
return square3d_impl(request) |
| 223 |
|
| 224 |
@skip_in_parallel |
| 225 |
def test_line_meshes(line1d, line2d, line3d, rline1d, rline2d, rline3d): |
| 226 |
"Check some properties of the meshes created for these tests." |
| 227 |
assert line1d.geometry().dim() == 1 |
| 228 |
assert line2d.geometry().dim() == 2 |
| 229 |
assert line3d.geometry().dim() == 3 |
| 230 |
assert line1d.topology().dim() == 1 |
| 231 |
assert line2d.topology().dim() == 1 |
| 232 |
assert line3d.topology().dim() == 1 |
| 233 |
|
| 234 |
|
| 235 |
@skip_in_parallel |
| 236 |
def test_write_line_meshes_to_files(line1d, line2d, line3d, rline1d, rline2d, |
| 237 |
rline3d, uflacs_representation_only): |
| 238 |
# Enable to write meshes to file for inspection (plot doesn't work |
| 239 |
# for 1d in 2d/3d) |
| 240 |
# CellNormal is only supported by uflacs |
| 241 |
if 0: |
| 242 |
File("line1d.xdmf") << line1d |
| 243 |
File("line2d.xdmf") << line2d |
| 244 |
File("line3d.xdmf") << line3d |
| 245 |
File("rline1d.xdmf") << rline1d |
| 246 |
File("rline2d.xdmf") << rline2d |
| 247 |
File("rline3d.xdmf") << rline3d |
| 248 |
File("line2dnormal.xdmf") << project(CellNormal(line2d), |
| 249 |
VectorFunctionSpace(line2d, |
| 250 |
"DG", 0)) |
| 251 |
File("rline2dnormal.xdmf") << project(CellNormal(rline2d), |
| 252 |
VectorFunctionSpace(rline2d, |
| 253 |
"DG", 0)) |
| 254 |
|
| 255 |
|
| 256 |
@pytest.mark.parametrize("mesh", [ |
| 257 |
line1d_impl(None), |
| 258 |
line2d_impl(None), |
| 259 |
line3d_impl(None), |
| 260 |
rline1d_impl(None), |
| 261 |
rline2d_impl(None), |
| 262 |
rline3d_impl(None), ]) |
| 263 |
@skip_in_parallel |
| 264 |
def test_manifold_line_geometry(mesh, uflacs_representation_only): |
| 265 |
assert uflacs_representation_only == "uflacs" |
| 266 |
assert parameters["form_compiler"]["representation"] == "uflacs" |
| 267 |
|
| 268 |
gdim = mesh.geometry().dim() |
| 269 |
tdim = mesh.topology().dim() |
| 270 |
|
| 271 |
# Create cell markers and integration measure |
| 272 |
mf = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 273 |
dx = Measure("dx", domain=mesh, subdomain_data=mf) |
| 274 |
|
| 275 |
# Create symbolic geometry for current mesh |
| 276 |
x = SpatialCoordinate(mesh) |
| 277 |
X = CellCoordinate(mesh) |
| 278 |
co = CellOrientation(mesh) |
| 279 |
cn = CellNormal(mesh) |
| 280 |
J = Jacobian(mesh) |
| 281 |
detJ = JacobianDeterminant(mesh) |
| 282 |
K = JacobianInverse(mesh) |
| 283 |
vol = CellVolume(mesh) |
| 284 |
h = CellDiameter(mesh) |
| 285 |
R = Circumradius(mesh) |
| 286 |
|
| 287 |
# Check that length computed via integral doesn't change with |
| 288 |
# refinement |
| 289 |
length = assemble(1.0*dx) |
| 290 |
mesh2 = refine(mesh) |
| 291 |
assert mesh2.num_cells() == 2*mesh.num_cells() |
| 292 |
dx2 = Measure("dx") |
| 293 |
length2 = assemble(1.0*dx2(mesh2)) |
| 294 |
assert round(length - length2, 7) == 0.0 |
| 295 |
|
| 296 |
# Check that number of cells can be computed correctly by scaling |
| 297 |
# integral by |detJ|^-1 |
| 298 |
num_cells = assemble(1.0/abs(detJ)*dx) |
| 299 |
assert round(num_cells - mesh.num_cells(), 7) == 0.0 |
| 300 |
|
| 301 |
# Check that norm of Jacobian column matches detJ and volume |
| 302 |
assert round(length - assemble(sqrt(J[:, 0]**2)/abs(detJ)*dx), 7) == 0.0 |
| 303 |
assert round(assemble((vol-abs(detJ))*dx), 7) == 0.0 |
| 304 |
assert round(length - assemble(vol/abs(detJ)*dx), 7) == 0.0 |
| 305 |
|
| 306 |
coords = mesh.coordinates() |
| 307 |
cells = mesh.cells() |
| 308 |
|
| 309 |
# Checks on each cell separately |
| 310 |
for k in range(mesh.num_cells()): |
| 311 |
# Mark current cell |
| 312 |
mf.set_all(0) |
| 313 |
mf[k] = 1 |
| 314 |
|
| 315 |
x0 = Constant(tuple(coords[cells[k][0], :])) |
| 316 |
|
| 317 |
# Integrate x components over a cell and compare with midpoint |
| 318 |
# computed from coords |
| 319 |
for j in range(gdim): |
| 320 |
xm = 0.5*(coords[cells[k][0], j] + coords[cells[k][1], j]) |
| 321 |
assert round(assemble(x[j]/abs(detJ)*dx(1)) - xm, 7) == 0.0 |
| 322 |
|
| 323 |
# Jacobian column is pointing away from x0 |
| 324 |
assert assemble(dot(J[:, 0], x-x0)*dx(1)) > 0.0 |
| 325 |
|
| 326 |
# Check affine coordinate relations x=x0+J*X, X=K*(x-x0), K*J=I |
| 327 |
assert round(assemble((x - (x0+J*X))**2*dx(1)), 7) == 0.0 |
| 328 |
assert round(assemble((X - K*(x-x0))**2*dx(1)), 7) == 0.0 |
| 329 |
assert round(assemble((K*J - Identity(tdim))**2*dx(1)), 7) == 0.0 |
| 330 |
|
| 331 |
# Check cell diameter and circumradius |
| 332 |
assert round(assemble(h/vol*dx(1)) - Cell(mesh, k).h(), 7) == 0.0 |
| 333 |
assert round(assemble(R/vol*dx(1)) - Cell(mesh, k).circumradius(), 7) == 0.0 |
| 334 |
|
| 335 |
# Jacobian column is orthogonal to cell normal |
| 336 |
if gdim == 2: |
| 337 |
assert round(assemble(dot(J[:, 0], cn)*dx(1)), 7) == 0.0 |
| 338 |
|
| 339 |
# Create 3d tangent and cell normal vectors |
| 340 |
tangent = as_vector((J[0, 0], J[1, 0], 0.0)) |
| 341 |
tangent = co * tangent / sqrt(tangent**2) |
| 342 |
normal = as_vector((cn[0], cn[1], 0.0)) |
| 343 |
up = cross(tangent, normal) |
| 344 |
|
| 345 |
# Check that t,n,up are orthogonal |
| 346 |
assert round(assemble(dot(tangent, normal)*dx(1)), 7) == 0.0 |
| 347 |
assert round(assemble(dot(tangent, up)*dx(1)), 7) == 0.0 |
| 348 |
assert round(assemble(dot(normal, up)*dx(1)), 7) == 0.0 |
| 349 |
assert round(assemble((cross(up, tangent) - normal)**2*dx(1)), |
| 350 |
7) == 0.0 |
| 351 |
|
| 352 |
assert round(assemble(up**2*dx(1)), 7) > 0.0 |
| 353 |
assert round(assemble((up[0]**2 + up[1]**2)*dx(1)), 7) == 0.0 |
| 354 |
assert round(assemble(up[2]*dx(1)), 7) > 0.0 |
| 355 |
|
| 356 |
|
| 357 |
@skip_in_parallel |
| 358 |
def test_manifold_area(square3d, any_representation): |
| 359 |
"""Integrate literal expressions over manifold cells, no function |
| 360 |
spaces involved.""" |
| 361 |
mesh = square3d |
| 362 |
area = sqrt(3.0) # known area of mesh |
| 363 |
|
| 364 |
# Assembling mesh area scaled by a literal |
| 365 |
assert round(assemble(0.0*dx(mesh)) - 0.0*area, 7) == 0.0 |
| 366 |
assert round(assemble(1.0*dx(mesh)) - 1.0*area, 7) == 0.0 |
| 367 |
assert round(assemble(3.0*dx(mesh)) - 3.0*area, 7) == 0.0 |
| 368 |
|
| 369 |
|
| 370 |
@skip_in_parallel |
| 371 |
def test_manifold_dg0_functions(square3d, any_representation): |
| 372 |
mesh = square3d |
| 373 |
area = sqrt(3.0) # known area of mesh |
| 374 |
|
| 375 |
mf = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 376 |
mf[0] = 0 |
| 377 |
mf[1] = 1 |
| 378 |
dx = Measure("dx", domain=mesh, subdomain_data=mf) |
| 379 |
|
| 380 |
x = SpatialCoordinate(mesh) |
| 381 |
|
| 382 |
U0 = FunctionSpace(mesh, "DG", 0) |
| 383 |
V0 = VectorFunctionSpace(mesh, "DG", 0) |
| 384 |
|
| 385 |
# Project constants to scalar and vector DG0 spaces on manifold |
| 386 |
u0 = project(1.0, U0) |
| 387 |
v0v = (1.0, 2.0, 3.0) |
| 388 |
v0 = project(as_vector(v0v), V0) |
| 389 |
assert round(sum(u0.vector().get_local()) - 2*1, 7) == 0.0 |
| 390 |
assert round(sum(v0.vector().get_local()) - 2*(1+2+3), 7) == 0.0 |
| 391 |
|
| 392 |
# Integrate piecewise constant functions over manifold cells |
| 393 |
assert round(assemble(u0*dx(0)) - 0.5*area) == 0.0 |
| 394 |
assert round(assemble(u0*dx(1)) - 0.5*area) == 0.0 |
| 395 |
assert round(assemble(u0*dx) - area) == 0.0 |
| 396 |
assert round(assemble(v0[0]*dx) - v0v[0]*area) == 0.0 |
| 397 |
assert round(assemble(v0[1]*dx) - v0v[1]*area) == 0.0 |
| 398 |
assert round(assemble(v0[2]*dx) - v0v[2]*area) == 0.0 |
| 399 |
|
| 400 |
# Project x to scalar and vector DG0 spaces on manifold |
| 401 |
u0x = project(x[0], U0) # cell averages of x[0]: 2/3, 1/3, sum = 3/3 |
| 402 |
v0x = project(x, V0) # cell averages of x[:]: (2/3, 1/3, 2/3), (1/3, 2/3, 2/3), sum = 10/3 |
| 403 |
assert round(sum(u0x.vector().get_local()) - 3.0/3.0, 7) == 0.0 |
| 404 |
assert round(sum(v0x.vector().get_local()) - 10.0/3.0, 7) == 0.0 |
| 405 |
|
| 406 |
# Evaluate in all corners and cell midpoints, value should be the |
| 407 |
# same constant everywhere |
| 408 |
points = [ |
| 409 |
(0.0, 0.0, 1.0), |
| 410 |
(1.0, 1.0, 1.0), |
| 411 |
(1.0, 0.0, 0.0), |
| 412 |
(0.0, 1.0, 0.0), |
| 413 |
(1.0/3.0, 2.0/3.0, 2.0/3.0), |
| 414 |
(2.0/3.0, 1.0/3.0, 2.0/3.0), |
| 415 |
] |
| 416 |
for point in points: |
| 417 |
assert round(sum((v0(point) - numpy.asarray(v0v))**2), 7) == 0.0 |
| 418 |
|
| 419 |
|
| 420 |
@skip_in_parallel |
| 421 |
def test_manifold_cg1_functions(square3d, any_representation): |
| 422 |
mesh = square3d |
| 423 |
area = sqrt(3.0) # known area of mesh |
| 424 |
|
| 425 |
mf = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 426 |
mf[0] = 0 |
| 427 |
mf[1] = 1 |
| 428 |
dx = Measure("dx", domain=mesh, subdomain_data=mf) |
| 429 |
|
| 430 |
# We need unit testing of some symbolic quantities to pinpoint any |
| 431 |
# sign problems etc. in the right places |
| 432 |
x = SpatialCoordinate(mesh) |
| 433 |
|
| 434 |
U1 = FunctionSpace(mesh, "CG", 1) |
| 435 |
V1 = VectorFunctionSpace(mesh, "CG", 1) |
| 436 |
|
| 437 |
# Project piecewise linears to scalar and vector CG1 spaces on |
| 438 |
# manifold |
| 439 |
u1 = project(x[0], U1) |
| 440 |
v1 = project(x, V1) |
| 441 |
# exact x in vertices is [0,0,0, 1,1,1, 1,0,0, 0,1,0], |
| 442 |
# so sum(x[0] for each vertex) is therefore sum(0 1 1 0): |
| 443 |
assert round(sum(u1.vector().get_local()) - (0+1+1+0), 7) == 0.0 |
| 444 |
# and sum(x components for each vertex) is sum(1, 3, 1, 1): |
| 445 |
assert round(sum(v1.vector().get_local()) - (1+3+1+1), 7) == 0.0 |
| 446 |
|
| 447 |
# Integrate piecewise constant functions over manifold cells, |
| 448 |
# computing midpoint coordinates |
| 449 |
midpoints = [ |
| 450 |
(1.0/3.0, 2.0/3.0, 2.0/3.0), |
| 451 |
(2.0/3.0, 1.0/3.0, 2.0/3.0), |
| 452 |
] |
| 453 |
mp = midpoints |
| 454 |
assert round(assemble(u1*dx(0)) - mp[0][0]) == 0.0 |
| 455 |
assert round(assemble(u1*dx(1)) - mp[1][0]) == 0.0 |
| 456 |
assert round(assemble(v1[0]*dx(0)) - mp[0][0]) == 0.0 |
| 457 |
assert round(assemble(v1[1]*dx(0)) - mp[0][1]) == 0.0 |
| 458 |
assert round(assemble(v1[2]*dx(0)) - mp[0][2]) == 0.0 |
| 459 |
assert round(assemble(v1[0]*dx(1)) - mp[1][0]) == 0.0 |
| 460 |
assert round(assemble(v1[1]*dx(1)) - mp[1][1]) == 0.0 |
| 461 |
assert round(assemble(v1[2]*dx(1)) - mp[1][2]) == 0.0 |
| 462 |
|
| 463 |
|
| 464 |
@skip_in_parallel |
| 465 |
def test_manifold_coordinate_projection(square3d, any_representation): |
| 466 |
mesh = square3d |
| 467 |
|
| 468 |
# Project x to a CG1 Function, i.e. setting up for v1(x) = x |
| 469 |
V1 = VectorFunctionSpace(mesh, "CG", 1) |
| 470 |
x = SpatialCoordinate(mesh) |
| 471 |
v1 = project(x, V1) |
| 472 |
|
| 473 |
# Check that v1(x) = x holds component-wise in squared l2 norm |
| 474 |
assert round(assemble((v1-x)**2*dx), 7) == 0.0 |
| 475 |
assert round(assemble((v1[0]-x[0])**2*dx), 7) == 0.0 |
| 476 |
assert round(assemble((v1[1]-x[1])**2*dx), 7) == 0.0 |
| 477 |
assert round(assemble((v1[2]-x[2])**2*dx), 7) == 0.0 |
| 478 |
assert round(assemble((v1[0]-x[0])**2*dx(0)), 7) == 0.0 |
| 479 |
assert round(assemble((v1[1]-x[1])**2*dx(0)), 7) == 0.0 |
| 480 |
assert round(assemble((v1[2]-x[2])**2*dx(0)), 7) == 0.0 |
| 481 |
assert round(assemble((v1[0]-x[0])**2*dx(1)), 7) == 0.0 |
| 482 |
assert round(assemble((v1[1]-x[1])**2*dx(1)), 7) == 0.0 |
| 483 |
assert round(assemble((v1[2]-x[2])**2*dx(1)), 7) == 0.0 |
| 484 |
|
| 485 |
|
| 486 |
@skip_in_parallel |
| 487 |
def test_manifold_point_evaluation(square3d, any_representation): |
| 488 |
mesh = square3d |
| 489 |
|
| 490 |
# Project x to a CG1 Function, i.e. setting up for v1(x) = x |
| 491 |
V1 = VectorFunctionSpace(mesh, "CG", 1) |
| 492 |
x = SpatialCoordinate(mesh) |
| 493 |
v1 = project(x, V1) |
| 494 |
|
| 495 |
# Evaluate in all corners and cell midpoints, |
| 496 |
# value should equal the evaluation coordinate |
| 497 |
# Because bounding box tree doesn't handle manifolds, |
| 498 |
# we have to specify which cell each point is in. |
| 499 |
points = [[ |
| 500 |
(0.0, 0.0, 1.0), # vertex of both cells |
| 501 |
(1.0, 1.0, 1.0), # vertex of both cells |
| 502 |
(1.0, 0.0, 0.0), # vertex of cell 0 only |
| 503 |
(2.0/3.0, 1.0/3.0, 2.0/3.0), # midpoint of cell 0 |
| 504 |
], [ |
| 505 |
(0.0, 0.0, 1.0), # vertex of both cells |
| 506 |
(1.0, 1.0, 1.0), # vertex of both cells |
| 507 |
(0.0, 1.0, 0.0), # vertex of cell 1 only |
| 508 |
(1.0/3.0, 2.0/3.0, 2.0/3.0), # midpoint of cell 1 |
| 509 |
]] |
| 510 |
values = numpy.zeros(3) |
| 511 |
bb = mesh.bounding_box_tree() |
| 512 |
for cellid in (0, 1): |
| 513 |
for point in points[cellid]: |
| 514 |
v1.eval_cell(values, numpy.asarray(point), Cell(mesh, cellid)) |
| 515 |
assert round(values[0] - point[0], 7) == 0.0 # expecting v1(x) = x |
| 516 |
assert round(values[1] - point[1], 7) == 0.0 # expecting v1(x) = x |
| 517 |
assert round(values[2] - point[2], 7) == 0.0 # expecting v1(x) = x |
| 518 |
# print cellid, [round(v,2) for v in point], |
| 519 |
# [round(v,2) for v in values] |
| 520 |
|
| 521 |
|
| 522 |
# Some symbolic quantities are only available through uflacs |
| 523 |
@skip_in_parallel |
| 524 |
def test_manifold_symbolic_geometry(square3d, uflacs_representation_only): |
| 525 |
mesh = square3d |
| 526 |
assert mesh.num_cells() == 2 |
| 527 |
gdim = mesh.geometry().dim() |
| 528 |
tdim = mesh.topology().dim() |
| 529 |
|
| 530 |
area = sqrt(3.0) # known area of mesh |
| 531 |
A = area/2.0 # area of single cell |
| 532 |
Aref = 0.5 # 0.5 is the area of the UFC reference triangle |
| 533 |
|
| 534 |
mf = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 535 |
mf[0] = 0 |
| 536 |
mf[1] = 1 |
| 537 |
dx = Measure("dx", domain=mesh, subdomain_data=mf) |
| 538 |
|
| 539 |
U0 = FunctionSpace(mesh, "DG", 0) |
| 540 |
V0 = VectorFunctionSpace(mesh, "DG", 0) |
| 541 |
|
| 542 |
# 0 means up=+1.0, 1 means down=-1.0 |
| 543 |
orientations = mesh.cell_orientations() |
| 544 |
assert orientations[0] == 1 # down |
| 545 |
assert orientations[1] == 0 # up |
| 546 |
|
| 547 |
# Check cell orientation, should be -1.0 (down) and +1.0 (up) on |
| 548 |
# the two cells respectively by construction |
| 549 |
co = CellOrientation(mesh) |
| 550 |
co0 = assemble(co/A*dx(0)) |
| 551 |
co1 = assemble(co/A*dx(1)) |
| 552 |
assert round(abs(co0) - 1.0, 7) == 0.0 # should be +1 or -1 |
| 553 |
assert round(abs(co1) - 1.0, 7) == 0.0 # should be +1 or -1 |
| 554 |
assert round(co1 + co0, 7) == 0.0 # should cancel out |
| 555 |
assert round(co0 - -1.0, 7) == 0.0 # down |
| 556 |
assert round(co1 - +1.0, 7) == 0.0 # up |
| 557 |
|
| 558 |
# Check cell normal directions component for component |
| 559 |
cn = CellNormal(mesh) |
| 560 |
assert assemble(cn[0]/A*dx(0)) > 0.0 |
| 561 |
assert assemble(cn[0]/A*dx(1)) < 0.0 |
| 562 |
assert assemble(cn[1]/A*dx(0)) < 0.0 |
| 563 |
assert assemble(cn[1]/A*dx(1)) > 0.0 |
| 564 |
assert assemble(cn[2]/A*dx(0)) > 0.0 |
| 565 |
assert assemble(cn[2]/A*dx(1)) > 0.0 |
| 566 |
# Check cell normal normalization |
| 567 |
assert round(assemble(cn**2/A*dx(0)) - 1.0, 7) == 0.0 |
| 568 |
assert round(assemble(cn**2/A*dx(1)) - 1.0, 7) == 0.0 |
| 569 |
|
| 570 |
# Check coordinates with various consistency checking |
| 571 |
x = SpatialCoordinate(mesh) |
| 572 |
X = CellCoordinate(mesh) |
| 573 |
J = Jacobian(mesh) |
| 574 |
detJ = JacobianDeterminant(mesh) # pseudo-determinant |
| 575 |
K = JacobianInverse(mesh) # pseudo-inverse |
| 576 |
vol = CellVolume(mesh) |
| 577 |
h = CellDiameter(mesh) |
| 578 |
R = Circumradius(mesh) |
| 579 |
|
| 580 |
# This is not currently implemented in uflacs: |
| 581 |
# x0 = CellOrigin(mesh) |
| 582 |
# But by happy accident, x0 is the same vertex for both our triangles: |
| 583 |
x0 = as_vector((0.0, 0.0, 1.0)) |
| 584 |
|
| 585 |
# Checks on each cell separately |
| 586 |
for k in range(mesh.num_cells()): |
| 587 |
# Mark current cell |
| 588 |
mf.set_all(0) |
| 589 |
mf[k] = 1 |
| 590 |
|
| 591 |
# Check integration area vs detJ |
| 592 |
# Validate known cell area A |
| 593 |
assert round(assemble(1.0*dx(1)) - A, 7) == 0.0 |
| 594 |
assert round(assemble(1.0/A*dx(1)) - 1.0, 7) == 0.0 |
| 595 |
assert round(assemble(A*dx(1)) - A**2, 7) == 0.0 |
| 596 |
# Compare abs(detJ) to A |
| 597 |
A2 = Aref*abs(detJ) |
| 598 |
assert round(assemble((A-A2)**2*dx(1)) - 0.0, 7) == 0.0 |
| 599 |
assert round(assemble(1.0/A2*dx(1)) - 1.0, 7) == 0.0 |
| 600 |
assert round(assemble(A2*dx(1)) - A**2, 7) == 0.0 |
| 601 |
# Validate cell orientation |
| 602 |
assert round(assemble(co*dx(1)) - A*(1 if k == 1 else -1), 7) == 0.0 |
| 603 |
# Compare co*detJ to A (detJ is pseudo-determinant with sign |
| 604 |
# restored, *co again is equivalent to abs()) |
| 605 |
A3 = Aref*co*detJ |
| 606 |
assert round(assemble((A-A3)**2*dx(1)) - 0.0, 7) == 0.0 |
| 607 |
assert round(assemble((A2-A3)**2*dx(1)) - 0.0, 7) == 0.0 |
| 608 |
assert round(assemble(1.0/A3*dx(1)) - 1.0, 7) == 0.0 |
| 609 |
assert round(assemble(A3*dx(1)) - A**2, 7) == 0.0 |
| 610 |
# Compare vol to A |
| 611 |
A4 = vol |
| 612 |
assert round(assemble((A-A4)**2*dx(1)) - 0.0, 7) == 0.0 |
| 613 |
assert round(assemble((A2-A4)**2*dx(1)) - 0.0, 7) == 0.0 |
| 614 |
assert round(assemble((A3-A4)**2*dx(1)) - 0.0, 7) == 0.0 |
| 615 |
assert round(assemble(1.0/A4*dx(1)) - 1.0, 7) == 0.0 |
| 616 |
assert round(assemble(A4*dx(1)) - A**2, 7) == 0.0 |
| 617 |
|
| 618 |
# Check cell diameter and circumradius |
| 619 |
assert round(assemble(h/vol*dx(1)) - Cell(mesh, k).h(), 7) == 0.0 |
| 620 |
assert round(assemble(R/vol*dx(1)) - Cell(mesh, k).circumradius(), 7) == 0.0 |
| 621 |
|
| 622 |
# Check integral of reference coordinate components over reference |
| 623 |
# triangle: \int_0^1 \int_0^{1-x} x dy dx = 1/6 |
| 624 |
Xmp = (1.0/6.0, 1.0/6.0) |
| 625 |
for j in range(tdim): |
| 626 |
# Scale by detJ^-1 to get reference cell integral |
| 627 |
assert round(assemble(X[j]/abs(detJ)*dx(1)) - Xmp[j], 7) == 0.0 |
| 628 |
|
| 629 |
# Check average of physical coordinate components over each cell: |
| 630 |
xmp = [(2.0/3.0, 1.0/3.0, 2.0/3.0), # midpoint of cell 0 |
| 631 |
(1.0/3.0, 2.0/3.0, 2.0/3.0), # midpoint of cell 1 |
| 632 |
] |
| 633 |
for i in range(gdim): |
| 634 |
# Scale by A^-1 to get average of x, not integral |
| 635 |
assert round(assemble(x[i]/A*dx(1)) - xmp[k][i], 7) == 0.0 |
| 636 |
|
| 637 |
# Check affine coordinate relations x=x0+J*X, X=K*(x-x0), K*J=I |
| 638 |
assert round(assemble((x - (x0+J*X))**2*dx), 7) == 0.0 |
| 639 |
assert round(assemble((X - K*(x-x0))**2*dx), 7) == 0.0 |
| 640 |
assert round(assemble((K*J - Identity(2))**2/A*dx), 7) == 0.0 |
| 641 |
|
| 642 |
|
| 643 |
@skip_in_parallel |
| 644 |
def test_manifold_piola_mapped_functions(square3d, any_representation): |
| 645 |
mesh = square3d |
| 646 |
area = sqrt(3.0) # known area of mesh |
| 647 |
A = area/2.0 |
| 648 |
|
| 649 |
mf = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 650 |
mf[0] = 0 |
| 651 |
mf[1] = 1 |
| 652 |
dx = Measure("dx", domain=mesh, subdomain_data=mf) |
| 653 |
|
| 654 |
x = SpatialCoordinate(mesh) |
| 655 |
|
| 656 |
J = Jacobian(mesh) |
| 657 |
detJ = JacobianDeterminant(mesh) # pseudo-determinant |
| 658 |
K = JacobianInverse(mesh) # pseudo-inverse |
| 659 |
|
| 660 |
Q1 = VectorFunctionSpace(mesh, "CG", 1) |
| 661 |
U1 = VectorFunctionSpace(mesh, "DG", 1) |
| 662 |
V1 = FunctionSpace(mesh, "N1div", 1) |
| 663 |
W1 = FunctionSpace(mesh, "N1curl", 1) |
| 664 |
|
| 665 |
dq = TestFunction(Q1) |
| 666 |
du = TestFunction(U1) |
| 667 |
dv = TestFunction(V1) |
| 668 |
dw = TestFunction(W1) |
| 669 |
|
| 670 |
assert U1.ufl_element().mapping() == "identity" |
| 671 |
assert V1.ufl_element().mapping() == "contravariant Piola" |
| 672 |
assert W1.ufl_element().mapping() == "covariant Piola" |
| 673 |
|
| 674 |
if any_representation != "uflacs": |
| 675 |
return |
| 676 |
|
| 677 |
# Check that projection test fails if it should fail: |
| 678 |
vec = Constant((0.0, 0.0, 0.0)) |
| 679 |
q1 = project(vec, Q1) |
| 680 |
u1 = project(vec, U1) |
| 681 |
v1 = project(vec, V1) |
| 682 |
w1 = project(vec, W1) |
| 683 |
# Projection of zero gets us zero for all spaces |
| 684 |
assert assemble(q1**2*dx) == 0.0 |
| 685 |
assert assemble(u1**2*dx) == 0.0 |
| 686 |
assert assemble(v1**2*dx) == 0.0 |
| 687 |
assert assemble(w1**2*dx) == 0.0 |
| 688 |
# Changing vec to nonzero, check that dM/df != 0 at f=0 |
| 689 |
vec = Constant((2.0, 2.0, 2.0)) |
| 690 |
assert round(assemble(derivative((q1-vec)**2*dx, q1)).norm('l2') - |
| 691 |
assemble(-4.0*sum(dq)*dx).norm('l2'), 7) == 0.0 |
| 692 |
assert round(assemble(derivative((u1-vec)**2*dx, u1)).norm('l2') - |
| 693 |
assemble(-4.0*sum(du)*dx).norm('l2'), 7) == 0.0 |
| 694 |
assert round(assemble(derivative((v1-vec)**2*dx, v1)).norm('l2') - |
| 695 |
assemble(-4.0*sum(dv)*dx).norm('l2'), 7) == 0.0 |
| 696 |
assert round(assemble(derivative((w1-vec)**2*dx, w1)).norm('l2') - |
| 697 |
assemble(-4.0*sum(dw)*dx).norm('l2'), 7) == 0.0 |
| 698 |
|
| 699 |
# Project piecewise linears to scalar and vector CG1 spaces on |
| 700 |
# manifold |
| 701 |
vec = Constant((1.0, 1.0, 1.0)) |
| 702 |
q1 = project(vec, Q1) |
| 703 |
u1 = project(vec, U1) |
| 704 |
v1 = project(vec, V1) |
| 705 |
w1 = project(vec, W1) |
| 706 |
|
| 707 |
# If vec can be represented exactly in space this should be zero: |
| 708 |
assert round(assemble((q1-vec)**2*dx), 7) == 0.0 |
| 709 |
assert round(assemble((u1-vec)**2*dx), 7) == 0.0 |
| 710 |
assert round(assemble((v1-vec)**2*dx), 7) > 0.0 # Exact representation not possible? |
| 711 |
assert round(assemble((w1-vec)**2*dx), 7) > 0.0 # Exact representation not possible? |
| 712 |
|
| 713 |
# In the l2norm projection is correct these should be zero: |
| 714 |
assert round(assemble(derivative((q1-vec)**2*dx, v1)).norm('l2'), 7) == 0.0 |
| 715 |
assert round(assemble(derivative((u1-vec)**2*dx, w1)).norm('l2'), 7) == 0.0 |
| 716 |
assert round(assemble(derivative((v1-vec)**2*dx, v1)).norm('l2'), 7) == 0.0 |
| 717 |
assert round(assemble(derivative((w1-vec)**2*dx, w1)).norm('l2'), 7) == 0.0 |
| 718 |
|
| 719 |
# Hdiv mapping of a local constant vector should be representable |
| 720 |
# in hdiv conforming space |
| 721 |
vec = (1.0/detJ)*J*as_vector((3.0, 5.0)) |
| 722 |
q1 = project(vec, Q1) |
| 723 |
u1 = project(vec, U1) |
| 724 |
v1 = project(vec, V1) |
| 725 |
w1 = project(vec, W1) |
| 726 |
assert round(assemble((q1-vec)**2*dx), 7) > 0.0 # Exact representation not possible? |
| 727 |
assert round(assemble((u1-vec)**2*dx), 7) == 0.0 |
| 728 |
assert round(assemble((v1-vec)**2*dx), 7) == 0.0 |
| 729 |
assert round(assemble((w1-vec)**2*dx), 7) > 0.0 # Exact representation not possible? |
| 730 |
|
| 731 |
# Hcurl mapping of a local constant vector should be representable |
| 732 |
# in hcurl conforming space |
| 733 |
vec = K.T*as_vector((5.0, 2.0)) |
| 734 |
q1 = project(vec, Q1) |
| 735 |
u1 = project(vec, U1) |
| 736 |
v1 = project(vec, V1) |
| 737 |
w1 = project(vec, W1) |
| 738 |
assert round(assemble((q1-vec)**2*dx), 7) > 0.0 # Exact representation not possible? |
| 739 |
assert round(assemble((u1-vec)**2*dx), 7) == 0.0 |
| 740 |
assert round(assemble((v1-vec)**2*dx), 7) > 0.0 # Exact representation not possible? |
| 741 |
assert round(assemble((w1-vec)**2*dx), 7) == 0.0 |
| 742 |
|
| 743 |
|
| 744 |
# Some symbolic quantities are only available through uflacs |
| 745 |
@skip_in_parallel |
| 746 |
def test_tetrahedron_symbolic_geometry(uflacs_representation_only): |
| 747 |
mesh = UnitCubeMesh(1, 1, 1) |
| 748 |
assert mesh.num_cells() == 6 |
| 749 |
gdim = mesh.geometry().dim() |
| 750 |
tdim = mesh.topology().dim() |
| 751 |
|
| 752 |
area = 1.0 # known volume of mesh |
| 753 |
A = area/6.0 # volume of single cell |
| 754 |
Aref = 1.0/6.0 # the volume of the UFC reference tetrahedron |
| 755 |
|
| 756 |
mf = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 757 |
dx = Measure("dx", domain=mesh, subdomain_data=mf) |
| 758 |
|
| 759 |
U0 = FunctionSpace(mesh, "DG", 0) |
| 760 |
V0 = VectorFunctionSpace(mesh, "DG", 0) |
| 761 |
U1 = FunctionSpace(mesh, "DG", 1) |
| 762 |
V1 = VectorFunctionSpace(mesh, "DG", 1) |
| 763 |
|
| 764 |
# Check coordinates with various consistency checking |
| 765 |
x = SpatialCoordinate(mesh) |
| 766 |
X = CellCoordinate(mesh) |
| 767 |
J = Jacobian(mesh) |
| 768 |
detJ = JacobianDeterminant(mesh) |
| 769 |
K = JacobianInverse(mesh) |
| 770 |
vol = CellVolume(mesh) |
| 771 |
h = CellDiameter(mesh) |
| 772 |
R = Circumradius(mesh) |
| 773 |
|
| 774 |
coordinates = mesh.coordinates() |
| 775 |
cells = mesh.cells() |
| 776 |
|
| 777 |
for k in range(mesh.num_cells()): |
| 778 |
# Mark current cell |
| 779 |
mf.set_all(0) |
| 780 |
mf[k] = 1 |
| 781 |
|
| 782 |
# Check integration area vs detJ |
| 783 |
# This is not currently implemented in uflacs: |
| 784 |
# x0 = CellOrigin(mesh) |
| 785 |
# But we can extract it from the mesh for a given cell k |
| 786 |
x0 = as_vector(coordinates[cells[k][0]][:]) |
| 787 |
# Validate known cell volume A |
| 788 |
assert round(assemble(1.0*dx(1)) - A, 7) == 0.0 |
| 789 |
assert round(assemble(1.0/A*dx(1)) - 1.0, 7) == 0.0 |
| 790 |
assert round(assemble(A*dx(1)) - A**2, 7) == 0.0 |
| 791 |
# Compare abs(detJ) to A |
| 792 |
A2 = Aref*abs(detJ) |
| 793 |
assert round(assemble((A-A2)**2*dx(1)) - 0.0, 7) == 0.0 |
| 794 |
assert round(assemble(1.0/A2*dx(1)) - 1.0, 7) == 0.0 |
| 795 |
assert round(assemble(A2*dx(1)) - A**2, 7) == 0.0 |
| 796 |
# Compare vol to A |
| 797 |
A4 = vol |
| 798 |
assert round(assemble((A-A4)**2*dx(1)) - 0.0, 7) == 0.0 |
| 799 |
assert round(assemble((A2-A4)**2*dx(1)) - 0.0, 7) == 0.0 |
| 800 |
assert round(assemble(1.0/A4*dx(1)) - 1.0, 7) == 0.0 |
| 801 |
assert round(assemble(A4*dx(1)) - A**2, 7) == 0.0 |
| 802 |
|
| 803 |
# Check integral of reference coordinate components over reference |
| 804 |
# tetrahedron: |
| 805 |
Xmp = (1.0/24.0, 1.0/24.0, 1.0/24.0) # not validated analytically |
| 806 |
for j in range(tdim): |
| 807 |
# Scale by detJ^-1 to get reference cell integral |
| 808 |
assert round(assemble(X[j]/abs(detJ)*dx(1)) - Xmp[j], 7) == 0.0 |
| 809 |
|
| 810 |
# Check average of physical coordinate components over each cell: |
| 811 |
# Compute average of vertex coordinates extracted from mesh |
| 812 |
verts = [coordinates[i][:] for i in cells[k]] |
| 813 |
vavg = sum(verts[1:], verts[0])/len(verts) |
| 814 |
for i in range(gdim): |
| 815 |
# Scale by A^-1 to get average of x, not integral |
| 816 |
assert round(assemble(x[i]/A*dx(1)) - vavg[i], 7) == 0.0 |
| 817 |
|
| 818 |
# Check affine coordinate relations x=x0+J*X, X=K*(x-x0), K*J=I |
| 819 |
x0 = as_vector(coordinates[cells[k][0]][:]) |
| 820 |
assert round(assemble((x - (x0+J*X))**2*dx(1)), 7) == 0.0 |
| 821 |
assert round(assemble((X - K*(x-x0))**2*dx(1)), 7) == 0.0 |
| 822 |
assert round(assemble((K*J - Identity(tdim))**2/A*dx(1)), 7) == 0.0 |
| 823 |
|
| 824 |
# Check cell diameter and circumradius |
| 825 |
assert round(assemble(h/vol*dx(1)) - Cell(mesh, k).h(), 7) == 0.0 |
| 826 |
assert round(assemble(R/vol*dx(1)) - Cell(mesh, k).circumradius(), 7) == 0.0 |
| 827 |
|
| 828 |
|
| 829 |
# Some symbolic quantities are only available through uflacs |
| 830 |
@skip_in_parallel |
| 831 |
def test_triangle_symbolic_geometry(uflacs_representation_only): |
| 832 |
mesh = UnitSquareMesh(1, 1) |
| 833 |
assert mesh.num_cells() == 2 |
| 834 |
gdim = mesh.geometry().dim() |
| 835 |
tdim = mesh.topology().dim() |
| 836 |
|
| 837 |
area = 1.0 # known volume of mesh |
| 838 |
A = area/2.0 # volume of single cell |
| 839 |
Aref = 1.0/2.0 # the volume of the UFC reference triangle |
| 840 |
|
| 841 |
mf = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 842 |
dx = Measure("dx", domain=mesh, subdomain_data=mf) |
| 843 |
|
| 844 |
U0 = FunctionSpace(mesh, "DG", 0) |
| 845 |
V0 = VectorFunctionSpace(mesh, "DG", 0) |
| 846 |
U1 = FunctionSpace(mesh, "DG", 1) |
| 847 |
V1 = VectorFunctionSpace(mesh, "DG", 1) |
| 848 |
|
| 849 |
# Check coordinates with various consistency checking |
| 850 |
x = SpatialCoordinate(mesh) |
| 851 |
X = CellCoordinate(mesh) |
| 852 |
J = Jacobian(mesh) |
| 853 |
detJ = JacobianDeterminant(mesh) |
| 854 |
K = JacobianInverse(mesh) |
| 855 |
vol = CellVolume(mesh) |
| 856 |
h = CellDiameter(mesh) |
| 857 |
R = Circumradius(mesh) |
| 858 |
|
| 859 |
coordinates = mesh.coordinates() |
| 860 |
cells = mesh.cells() |
| 861 |
|
| 862 |
for k in range(mesh.num_cells()): |
| 863 |
# Mark current cell |
| 864 |
mf.set_all(0) |
| 865 |
mf[k] = 1 |
| 866 |
|
| 867 |
# Check integration area vs detJ |
| 868 |
# This is not currently implemented in uflacs: |
| 869 |
# x0 = CellOrigin(mesh) |
| 870 |
# But we can extract it from the mesh for a given cell k |
| 871 |
x0 = as_vector(coordinates[cells[k][0]][:]) |
| 872 |
# Validate known cell volume A |
| 873 |
assert round(assemble(1.0*dx(1)) - A, 7) == 0.0 |
| 874 |
assert round(assemble(1.0/A*dx(1)) - 1.0, 7) == 0.0 |
| 875 |
assert round(assemble(A*dx(1)) - A**2, 7) == 0.0 |
| 876 |
# Compare abs(detJ) to A |
| 877 |
A2 = Aref*abs(detJ) |
| 878 |
assert round(assemble((A-A2)**2*dx(1)) - 0.0, 7) == 0.0 |
| 879 |
assert round(assemble(1.0/A2*dx(1)) - 1.0, 7) == 0.0 |
| 880 |
assert round(assemble(A2*dx(1)) - A**2, 7) == 0.0 |
| 881 |
# Compare vol to A |
| 882 |
A4 = vol |
| 883 |
assert round(assemble((A-A4)**2*dx(1)) - 0.0, 7) == 0.0 |
| 884 |
assert round(assemble((A2-A4)**2*dx(1)) - 0.0, 7) == 0.0 |
| 885 |
assert round(assemble(1.0/A4*dx(1)) - 1.0, 7) == 0.0 |
| 886 |
assert round(assemble(A4*dx(1)) - A**2, 7) == 0.0 |
| 887 |
|
| 888 |
# Check integral of reference coordinate components over reference |
| 889 |
# triangle: |
| 890 |
Xmp = (1.0/6.0, 1.0/6.0) |
| 891 |
for j in range(tdim): |
| 892 |
# Scale by detJ^-1 to get reference cell integral |
| 893 |
assert round(assemble(X[j]/abs(detJ)*dx(1)) - Xmp[j], 7) == 0.0 |
| 894 |
|
| 895 |
# Check average of physical coordinate components over each cell: |
| 896 |
# Compute average of vertex coordinates extracted from mesh |
| 897 |
verts = [coordinates[i][:] for i in cells[k]] |
| 898 |
vavg = sum(verts[1:], verts[0])/len(verts) |
| 899 |
for i in range(gdim): |
| 900 |
# Scale by A^-1 to get average of x, not integral |
| 901 |
assert round(assemble(x[i]/A*dx(1)) - vavg[i], 7) == 0.0 |
| 902 |
|
| 903 |
# Check affine coordinate relations x=x0+J*X, X=K*(x-x0), K*J=I |
| 904 |
x0 = as_vector(coordinates[cells[k][0]][:]) |
| 905 |
assert round(assemble((x - (x0+J*X))**2*dx(1)), 7) == 0.0 |
| 906 |
assert round(assemble((X - K*(x-x0))**2*dx(1)), 7) == 0.0 |
| 907 |
assert round(assemble((K*J - Identity(tdim))**2/A*dx(1)), 7) == 0.0 |
| 908 |
|
| 909 |
# Check cell diameter and circumradius |
| 910 |
assert round(assemble(h/vol*dx(1)) - Cell(mesh, k).h(), 7) == 0.0 |
| 911 |
assert round(assemble(R/vol*dx(1)) - Cell(mesh, k).circumradius(), 7) == 0.0 |
| 912 |
|
| 913 |
|
| 914 |
xfail_jit = pytest.mark.xfail(raises=Exception, strict=True) |
| 915 |
|
| 916 |
@pytest.mark.parametrize('mesh_factory', [ |
| 917 |
(UnitIntervalMesh, (8,)), |
| 918 |
(UnitSquareMesh, (4, 4)), |
| 919 |
(UnitDiscMesh.create, (MPI.comm_world, 4, 1, 2)), |
| 920 |
(UnitDiscMesh.create, (MPI.comm_world, 4, 1, 3)), |
| 921 |
(SphericalShellMesh.create, (MPI.comm_world, 1,)), |
| 922 |
(UnitCubeMesh, (2, 2, 2)), |
| 923 |
(UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral)), |
| 924 |
(UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron)), |
| 925 |
(line1d_impl, (None,)), |
| 926 |
(line2d_impl, (None,)), |
| 927 |
(line3d_impl, (None,)), |
| 928 |
(rline1d_impl, (None,)), |
| 929 |
(rline2d_impl, (None,)), |
| 930 |
(rline3d_impl, (None,)), |
| 931 |
(square2d_impl, (None,)), |
| 932 |
(square3d_impl, (None,)), |
| 933 |
# Tested geometric quantities are not implemented for higher-order cells |
| 934 |
pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 4, 2, 2))), marks=xfail_jit), |
| 935 |
pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 4, 2, 3))), marks=xfail_jit), |
| 936 |
pytest.param(((SphericalShellMesh.create, (MPI.comm_world, 2,))), marks=xfail_jit)]) |
| 937 |
@skip_in_parallel |
| 938 |
def test_geometric_quantities(uflacs_representation_only, mesh_factory): |
| 939 |
|
| 940 |
func, args = mesh_factory |
| 941 |
mesh = func(*args) |
| 942 |
|
| 943 |
tdim = mesh.ufl_cell().topological_dimension() |
| 944 |
|
| 945 |
cf = MeshFunction('size_t', mesh, mesh.topology().dim(), 0) |
| 946 |
dx = Measure("dx", domain=mesh, subdomain_data=cf) |
| 947 |
|
| 948 |
ff = MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0) |
| 949 |
ds = Measure("ds", domain=mesh, subdomain_data=ff) |
| 950 |
dS = Measure("dS", domain=mesh, subdomain_data=ff) |
| 951 |
|
| 952 |
h = CellDiameter(mesh) |
| 953 |
R = Circumradius(mesh) |
| 954 |
max_cell_edge = MaxCellEdgeLength(mesh) |
| 955 |
min_cell_edge = MinCellEdgeLength(mesh) |
| 956 |
max_facet_edge = MaxFacetEdgeLength(mesh) |
| 957 |
min_facet_edge = MinFacetEdgeLength(mesh) |
| 958 |
|
| 959 |
for c in cells(mesh): |
| 960 |
# Mark current cell for integration |
| 961 |
cf.set_all(0) |
| 962 |
cf[c] = 1 |
| 963 |
|
| 964 |
vol = assemble(1*dx(1)) |
| 965 |
|
| 966 |
# Check cell diameter |
| 967 |
assert numpy.isclose(assemble(h*dx(1))/vol, c.h()) |
| 968 |
|
| 969 |
# Check max/min cell edge length |
| 970 |
assert numpy.isclose(assemble(max_cell_edge*dx(1))/vol, |
| 971 |
max(e.length() for e in edges(c))) |
| 972 |
assert numpy.isclose(assemble(min_cell_edge*dx(1))/vol, |
| 973 |
min(e.length() for e in edges(c))) |
| 974 |
|
| 975 |
# Check circumradius if it makes sense |
| 976 |
if mesh.ufl_domain().is_piecewise_linear_simplex_domain(): |
| 977 |
assert numpy.isclose(assemble(R*dx(1))/vol, c.circumradius()) |
| 978 |
|
| 979 |
# Check max/min facet edge length if it makes sense |
| 980 |
if tdim >= 3: |
| 981 |
|
| 982 |
mesh.init(tdim-1, tdim) # for Facet.exterior() |
| 983 |
|
| 984 |
for f in facets(mesh): |
| 985 |
# Mark current facet for integration and pick facet measure |
| 986 |
ff.set_all(0) |
| 987 |
ff[f] = 1 |
| 988 |
df = ds(1) if f.exterior() else dS(1) |
| 989 |
|
| 990 |
vol = assemble(1*df) |
| 991 |
|
| 992 |
assert numpy.isclose(assemble(max_facet_edge*df)/vol, |
| 993 |
max(e.length() for e in edges(f))) |
| 994 |
assert numpy.isclose(assemble(min_facet_edge*df)/vol, |
| 995 |
min(e.length() for e in edges(f))) |