|
| 1 | +############################################################################## |
| 2 | +# |
| 3 | +# Copyright (c) 2003-2026 by the esys.escript Group |
| 4 | +# https://github.com/LutzGross/esys-escript.github.io |
| 5 | +# |
| 6 | +# Primary Business: Queensland, Australia |
| 7 | +# Licensed under the Apache License, version 2.0 |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# See CREDITS file for contributors and development history |
| 11 | +# |
| 12 | +############################################################################## |
| 13 | +""" |
| 14 | +Validation of the oxley lnodes mesh-access interface (design milestone A2). |
| 15 | +
|
| 16 | +For a uniform, conforming Block domain the mesh is a regular grid, so the |
| 17 | +lnodes-based getMeshInfo() output can be checked analytically: |
| 18 | +
|
| 19 | + N_i = numBlocks_i * 2**refine_level (elements per axis) |
| 20 | + numElements = prod(N_i) |
| 21 | + numNodes = prod(N_i + 1) (shared nodes deduplicated) |
| 22 | + nodeCoords = the regular grid points |
| 23 | + connectivity = a consistent coordinate<->index bijection |
| 24 | +
|
| 25 | +The node-count / dedup checks are what distinguish correct lnodes numbering from |
| 26 | +the old floating-point coordinate hashing. |
| 27 | +""" |
| 28 | +import itertools |
| 29 | +import sys |
| 30 | +import numpy as np |
| 31 | +from esys.oxley import Block |
| 32 | + |
| 33 | + |
| 34 | +def _check(ok_list, name, cond): |
| 35 | + print((" PASS " if cond else " FAIL ") + name) |
| 36 | + ok_list.append(bool(cond)) |
| 37 | + |
| 38 | + |
| 39 | +def _coord_set(a): |
| 40 | + return set(tuple(r) for r in np.round(a, 9)) |
| 41 | + |
| 42 | + |
| 43 | +def validate(numBlocks, length, origin, refine_level): |
| 44 | + dim = len(numBlocks) |
| 45 | + N = [numBlocks[i] * (2 ** refine_level) for i in range(dim)] |
| 46 | + h = [length[i] / N[i] for i in range(dim)] |
| 47 | + nc = 4 if dim == 2 else 8 |
| 48 | + exp_nelem = int(np.prod(N)) |
| 49 | + exp_nnode = int(np.prod([n + 1 for n in N])) |
| 50 | + print("Block numBlocks=%s length=%s origin=%s refine_level=%d -> %s grid, " |
| 51 | + "%d elements, %d nodes" % (numBlocks, length, origin, refine_level, |
| 52 | + "x".join(map(str, N)), exp_nelem, exp_nnode)) |
| 53 | + |
| 54 | + dom = Block(numBlocks=numBlocks, length=length, origin=origin, |
| 55 | + refine_level=refine_level) |
| 56 | + info = dom.getMeshInfo() |
| 57 | + coords = info["nodeCoords"] |
| 58 | + conn = info["elementNodes"] |
| 59 | + |
| 60 | + ok = [] |
| 61 | + _check(ok, "numDim", info["numDim"] == dim) |
| 62 | + _check(ok, "nodesPerElement == %d" % nc, info["nodesPerElement"] == nc) |
| 63 | + _check(ok, "numElements == prod(N) = %d" % exp_nelem, |
| 64 | + info["numElements"] == exp_nelem) |
| 65 | + _check(ok, "numNodes == prod(N+1) = %d (dedup)" % exp_nnode, |
| 66 | + info["numNodes"] == exp_nnode) |
| 67 | + _check(ok, "nodeCoords shape (%d,%d)" % (exp_nnode, dim), |
| 68 | + coords.shape == (exp_nnode, dim)) |
| 69 | + _check(ok, "elementNodes shape (%d,%d)" % (exp_nelem, nc), |
| 70 | + conn.shape == (exp_nelem, nc)) |
| 71 | + _check(ok, "serial global ids are identity", |
| 72 | + np.array_equal(info["nodeGlobalId"], np.arange(exp_nnode))) |
| 73 | + _check(ok, "connectivity indices in [0,numNodes)", |
| 74 | + conn.size and conn.min() >= 0 and conn.max() < exp_nnode) |
| 75 | + _check(ok, "every node referenced by an element", |
| 76 | + len(np.unique(conn)) == exp_nnode) |
| 77 | + |
| 78 | + for d in range(dim): |
| 79 | + lo, hi = float(coords[:, d].min()), float(coords[:, d].max()) |
| 80 | + _check(ok, "axis %d span == [%g,%g]" % (d, origin[d], origin[d] + length[d]), |
| 81 | + np.isclose(lo, origin[d]) and np.isclose(hi, origin[d] + length[d])) |
| 82 | + |
| 83 | + axes = [origin[d] + h[d] * np.arange(N[d] + 1) for d in range(dim)] |
| 84 | + grid = np.array(list(itertools.product(*axes))) |
| 85 | + _check(ok, "node coordinates == regular grid", _coord_set(coords) == _coord_set(grid)) |
| 86 | + _check(ok, "no duplicate node coordinates", |
| 87 | + np.unique(np.round(coords, 9), axis=0).shape[0] == exp_nnode) |
| 88 | + |
| 89 | + cells = coords[conn] # (nelem, nc, dim) |
| 90 | + sizes = cells.max(axis=1) - cells.min(axis=1) |
| 91 | + _check(ok, "every element is an axis-aligned cell of size h", |
| 92 | + np.allclose(sizes, h)) |
| 93 | + |
| 94 | + _check(ok, "element tags default to 0", |
| 95 | + np.array_equal(info["elementTags"], np.zeros(exp_nelem, dtype=info["elementTags"].dtype))) |
| 96 | + |
| 97 | + print(" => %s\n" % ("OK" if all(ok) else "FAILED")) |
| 98 | + return all(ok) |
| 99 | + |
| 100 | + |
| 101 | +CASES = [ |
| 102 | + ((2, 2), (1., 1.), (0., 0.), 0), |
| 103 | + ((2, 2), (1., 1.), (0., 0.), 2), |
| 104 | + ((3, 4), (6., 8.), (-1., -2.), 1), |
| 105 | + ((1, 1), (2., 3.), (5., 5.), 3), |
| 106 | + ((2, 2, 2), (1., 1., 1.), (0., 0., 0.), 1), |
| 107 | + ((3, 2, 2), (3., 2., 2.), (-1., 0., -5.), 1), |
| 108 | + ((1, 1, 1), (1., 1., 1.), (0., 0., 0.), 2), |
| 109 | +] |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + allok = all(validate(*c) for c in CASES) |
| 113 | + print("ALL PASSED" if allok else "SOME CASES FAILED") |
| 114 | + sys.exit(0 if allok else 1) |
0 commit comments