def test_get_controlpoint(): srf = lr.LRSplineSurface(3, 3, 3, 3) # all controlpoints srf[i] should equal the greville absiccae for i, bf in enumerate(srf.basis): np.testing.assert_allclose( srf[i], [np.mean(bf[0][1:3]), np.mean(bf[1][1:3])])
def srf2(): p = np.array([3, 3]) # order=3 n = p + 7 # 8 elements srf = lr.LRSplineSurface(n[0], n[1], p[0], p[1]) srf.basis[34].refine() srf.basis[29].refine() srf.basis[100].refine() with open('ex.eps', 'wb') as f: srf.write_postscript(f) return srf
def from_dataset(group): type_ = group.attrs['type'].decode() if type_ == 'FileBacked': return FileBacked.read(group) if type_ == 'PickledObject': return dill.loads(group[()]) if has_lrspline and type_ == 'LRSplineSurface': return lr.LRSplineSurface(group[()]) if type_ == 'Array': return group[:] if type_ == 'String': return group[()].decode() if type_ in {'CSRMatrix', 'CSCMatrix'}: cls = sp.csr_matrix if type_ == 'CSRMatrix' else sp.csc_matrix return cls((group['data'][:], group['indices'][:], group['indptr'][:]), shape=group.attrs['shape']) if type_ == 'COOMatrix': return sp.coo_matrix( (group['data'][:], (group['row'][:], group['col'][:])), shape=group.attrs['shape']) raise NotImplementedError(f'Unknown type: {type_}')
def ifem_solve(root, mu, i): with open('case.xinp') as f: template = Template(f.read()) with open(root / 'case.xinp', 'w') as f: f.write(template.render(geometry='square.g2', **mu)) patch = Surface() patch.set_dimension(3) with G2(str(root / 'square.g2')) as g2: g2.write(patch) try: g2.fstream.close() except: pass result = run([ '/home/eivind/repos/IFEM/Apps/Poisson/build/bin/Poisson', 'case.xinp', '-adap', '-hdf5' ], cwd=root, stdout=PIPE, stderr=PIPE) result.check_returncode() with h5py.File(root / 'case.hdf5', 'r') as h5: final = str(len(h5) - 1) group = h5[final]['Poisson-1'] patchbytes = group['basis']['1'][:].tobytes() geompatch = lr.LRSplineSurface(patchbytes) coeffs = group['fields']['u']['1'][:] solpatch = geompatch.clone() solpatch.controlpoints = coeffs.reshape(len(solpatch), -1) with open(f'poisson-mesh-single-{i}.ps', 'wb') as f: geompatch.write_postscript(f) return geompatch, solpatch
3]) # quadratic functions (this is order, polynomial degree+1) n = p + 3 # number of basis functions, start with 4x4 elements n_refinements = 6 # number of edge refinements ### Target approximation function def f(u, v): eps = 1e-4 d1 = np.power(u - .2, 2) + np.power(v - .2, 2) d2 = np.power(u - .9, 2) + np.power(v - .5, 2) d3 = np.power(u - .1, 2) + np.power(v - .8, 2) return 1 / (d1 + eps) + 1 / (d2 + eps) + 1 / (d3 + eps) ### GENERATE THE MESH and initiate variables surf = lr.LRSplineSurface(n[0], n[1], p[0], p[1]) bezier1 = BSplineBasis(order=p[0], knots=[-1] * p[0] + [1] * p[0]) bezier2 = BSplineBasis(order=p[1], knots=[-1] * p[1] + [1] * p[1]) u, wu = np.polynomial.legendre.leggauss(p[0] + 3) v, wv = np.polynomial.legendre.leggauss(p[1] + 3) Iwu = np.diag(wu) Iwv = np.diag(wv) for i_ref in range(n_refinements): print('Assembling mass matrix') n = len(surf.basis) M = np.zeros((n, n)) # mass matrix b = np.zeros((n, 1)) # right-hand-side for el in surf.elements:
def test_bezier_extraction(): # single linear element: 4x4 identity matrix srf = raw.LRSurface(2, 2, 2, 2) np.testing.assert_allclose(srf.getBezierExtraction(0), np.identity(4)) # single quadratic element: 9x9 identity matrix srf = raw.LRSurface(3, 3, 3, 3) np.testing.assert_allclose(srf.getBezierExtraction(0), np.identity(9)) # two quadratic elements (Note that LR basisfunctions are "randomly" ordered # which means that the rows of this matrix can be permuted at a later point) srf = lr.LRSplineSurface(4, 3, 3, 3) example_C = [ [ 1, 0, 0, 0, 0, 0, 0, 0, 0, ], # the leftmost columns [ 0, 0, 0, 1, 0, 0, 0, 0, 0, ], # should have exactly one 1 [ 0, 0, 0, 0, 0, 0, 1, 0, 0, ], [ 0, 0, 0.5, 0, 0, 0, 0, 0, 0, ], # the rightmost columns [ 0, 0, 0, 0, 0, 0.5, 0, 0, 0, ], # should have one 0.5's [0, 0, 0, 0, 0, 0, 0, 0, 0.5], [ 0, 1, 0.5, 0, 0, 0, 0, 0, 0, ], # the center column [ 0, 0, 0, 0, 1, 0.5, 0, 0, 0, ], # should contain [1,.5] [0, 0, 0, 0, 0, 0, 0, 1, 0.5] ] C = srf.bezier_extraction(0) for i, bf in enumerate(srf.elements[0].support()): if bf[0][1] == 0.5: # rightmost function of left element assert np.sum(C[i]) == 0.5 assert np.max(C[i]) == 0.5 assert np.count_nonzero(C[i]) == 1 elif bf[0][2] == 0.5: # center column assert np.sum(C[i]) == 1.5 assert np.max(C[i]) == 1.0 assert np.count_nonzero(C[i]) == 2 else: # leftmost column assert np.sum(C[i]) == 1 assert np.max(C[i]) == 1 assert np.count_nonzero(C[i]) == 1
def test_derivative(): srf = lr.LRSplineSurface(2, 2, 2, 2) # Single point, single derivative np.testing.assert_allclose(srf.derivative(0.123, 0.323, d=(0, 0)), [0.123, 0.323]) np.testing.assert_allclose(srf.derivative(0.123, 0.323, d=(1, 0)), [1.0, 0.0]) np.testing.assert_allclose(srf.derivative(0.123, 0.323, d=(0, 1)), [0.0, 1.0]) np.testing.assert_allclose(srf.derivative(0.123, 0.323, d=(1, 1)), [0.0, 0.0]) np.testing.assert_allclose(srf.basis[0].derivative(0.123, 0.323, d=(0, 0)), 0.593729) np.testing.assert_allclose(srf.basis[1].derivative(0.123, 0.323, d=(1, 0)), 0.677) np.testing.assert_allclose(srf.basis[2].derivative(0.123, 0.323, d=(0, 1)), 0.877) np.testing.assert_allclose(srf.basis[3].derivative(0.123, 0.323, d=(1, 1)), 1.0) # Multiple points, single derivative pt = (np.array([0.123, 0.821]), np.array([0.323, 0.571])) np.testing.assert_allclose(srf.derivative(*pt, d=(0, 0)), [[0.123, 0.323], [0.821, 0.571]]) np.testing.assert_allclose(srf.derivative(*pt, d=(1, 0)), [[1.0, 0.0], [1.0, 0.0]]) np.testing.assert_allclose(srf.derivative(*pt, d=(0, 1)), [[0.0, 1.0], [0.0, 1.0]]) np.testing.assert_allclose(srf.derivative(*pt, d=(1, 1)), [[0.0, 0.0], [0.0, 0.0]]) np.testing.assert_allclose(srf.basis[0].derivative(*pt, d=(0, 0)), [0.593729, 0.076791]) np.testing.assert_allclose(srf.basis[1].derivative(*pt, d=(1, 0)), [0.677, 0.429]) np.testing.assert_allclose(srf.basis[2].derivative(*pt, d=(0, 1)), [0.877, 0.179]) np.testing.assert_allclose(srf.basis[3].derivative(*pt, d=(1, 1)), [1.0, 1.0]) # Single point, multiple derivatives np.testing.assert_allclose( srf.derivative(0.123, 0.323, d=[(0, 0), (1, 0), (0, 1), (1, 1)]), [[0.123, 0.323], [1.0, 0.0], [0.0, 1.0], [0.0, 0.0]]) np.testing.assert_allclose( srf.basis[3].derivative(0.123, 0.323, d=[(0, 0), (1, 0), (0, 1), (1, 1)]), [0.039729, 0.323, 0.123, 1.0]) # Multiple points, multiple derivatives np.testing.assert_allclose( srf.derivative(*pt, d=[(0, 0), (1, 0), (0, 1), (1, 1)]), [ [[0.123, 0.323], [0.821, 0.571]], [[1.0, 0.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], ]) np.testing.assert_allclose( srf.basis[3].derivative(*pt, d=[(0, 0), (1, 0), (0, 1), (1, 1)]), [ [0.039729, 0.468791], [0.323, 0.571], [0.123, 0.821], [1.0, 1.0], ])
def srf(): with open(path / 'mesh01.lr', 'rb') as f: return lr.LRSplineSurface(f)
def srf3(): p = [4, 4] n = [7, 7] ans = lr.LRSplineSurface(n[0], n[1], p[0], p[1]) ans.basis[10].refine() return ans
def get_case(num: int): with open(f'poisson-mesh-{num}.lr', 'rb') as f: mesh = lr.LRSplineSurface(f) case = cases.lrpoisson(mesh) return case