def test_projection_iga_fem(self): from sfepy.discrete import FieldVariable from sfepy.discrete.fem import FEDomain, Field from sfepy.discrete.iga.domain import IGDomain from sfepy.mesh.mesh_generators import gen_block_mesh from sfepy.discrete.iga.domain_generators import gen_patch_block_domain from sfepy.discrete.projections import (make_l2_projection, make_l2_projection_data) shape = [10, 12, 12] dims = [5, 6, 6] centre = [0, 0, 0] degrees = [2, 2, 2] nurbs, bmesh, regions = gen_patch_block_domain(dims, shape, centre, degrees, cp_mode='greville', name='iga') ig_domain = IGDomain('iga', nurbs, bmesh, regions=regions) ig_omega = ig_domain.create_region('Omega', 'all') ig_field = Field.from_args('iga', nm.float64, 1, ig_omega, approx_order='iga', poly_space_base='iga') ig_u = FieldVariable('ig_u', 'parameter', ig_field, primary_var_name='(set-to-None)') mesh = gen_block_mesh(dims, shape, centre, name='fem') fe_domain = FEDomain('fem', mesh) fe_omega = fe_domain.create_region('Omega', 'all') fe_field = Field.from_args('fem', nm.float64, 1, fe_omega, approx_order=2) fe_u = FieldVariable('fe_u', 'parameter', fe_field, primary_var_name='(set-to-None)') def _eval_data(ts, coors, mode, **kwargs): return nm.prod(coors**2, axis=1)[:, None, None] make_l2_projection_data(ig_u, _eval_data) make_l2_projection(fe_u, ig_u) # This calls ig_u.evaluate_at(). coors = 0.5 * nm.random.rand(20, 3) * dims ig_vals = ig_u.evaluate_at(coors) fe_vals = fe_u.evaluate_at(coors) ok = nm.allclose(ig_vals, fe_vals, rtol=0.0, atol=1e-12) if not ok: self.report('iga-fem projection failed!') self.report('coors:') self.report(coors) self.report('iga fem diff:') self.report(nm.c_[ig_vals, fe_vals, nm.abs(ig_vals - fe_vals)]) return ok
def test_projection_iga_fem(self): from sfepy.discrete import FieldVariable from sfepy.discrete.fem import FEDomain, Field from sfepy.discrete.iga.domain import IGDomain from sfepy.mesh.mesh_generators import gen_block_mesh from sfepy.discrete.iga.domain_generators import gen_patch_block_domain from sfepy.discrete.projections import (make_l2_projection, make_l2_projection_data) shape = [10, 12, 12] dims = [5, 6, 6] centre = [0, 0, 0] degrees = [2, 2, 2] nurbs, bmesh, regions = gen_patch_block_domain(dims, shape, centre, degrees, cp_mode='greville', name='iga') ig_domain = IGDomain('iga', nurbs, bmesh, regions=regions) ig_omega = ig_domain.create_region('Omega', 'all') ig_field = Field.from_args('iga', nm.float64, 1, ig_omega, approx_order='iga', poly_space_base='iga') ig_u = FieldVariable('ig_u', 'parameter', ig_field, primary_var_name='(set-to-None)') mesh = gen_block_mesh(dims, shape, centre, name='fem') fe_domain = FEDomain('fem', mesh) fe_omega = fe_domain.create_region('Omega', 'all') fe_field = Field.from_args('fem', nm.float64, 1, fe_omega, approx_order=2) fe_u = FieldVariable('fe_u', 'parameter', fe_field, primary_var_name='(set-to-None)') def _eval_data(ts, coors, mode, **kwargs): return nm.prod(coors**2, axis=1)[:, None, None] make_l2_projection_data(ig_u, _eval_data) make_l2_projection(fe_u, ig_u) # This calls ig_u.evaluate_at(). coors = 0.5 * nm.random.rand(20, 3) * dims ig_vals = ig_u.evaluate_at(coors) fe_vals = fe_u.evaluate_at(coors) ok = nm.allclose(ig_vals, fe_vals, rtol=0.0, atol=1e-12) if not ok: self.report('iga-fem projection failed!') self.report('coors:') self.report(coors) self.report('iga fem diff:') self.report(nm.c_[ig_vals, fe_vals, nm.abs(ig_vals - fe_vals)]) return ok
def main(): parser = OptionParser(usage=usage, version='%prog') parser.add_option('-o', '', metavar='filename', action='store', dest='filename', default=None, help=helps['filename']) parser.add_option('-d', '--dims', metavar='dims', action='store', dest='dims', default='[1.0, 1.0, 1.0]', help=helps['dims']) parser.add_option('-c', '--centre', metavar='centre', action='store', dest='centre', default='[0.0, 0.0, 0.0]', help=helps['centre']) parser.add_option('-s', '--shape', metavar='shape', action='store', dest='shape', default='[5, 5, 5]', help=helps['shape']) parser.add_option('', '--degrees', metavar='degrees', action='store', dest='degrees', default='[2, 2, 2]', help=helps['degrees']) parser.add_option('', '--continuity', metavar='continuity', action='store', dest='continuity', default=None, help=helps['continuity']) parser.add_option('', '--cp-mode', metavar="'greville' or 'uniform'", action='store', dest='cp_mode', choices=['greville', 'uniform'], default='greville', help=helps['cp_mode']) parser.add_option('-2', '--2d', action='store_true', dest='is_2d', default=False, help=helps['2d']) parser.add_option('-p', '--plot', action='store_true', dest='plot', default=False, help=helps['plot']) parser.add_option('-l', '--label', action='store_true', dest='label', default=False, help=helps['label']) (options, args) = parser.parse_args() dim = 2 if options.is_2d else 3 filename = options.filename if filename is None: filename = 'block%dd.iga' % dim dims = nm.array(eval(options.dims), dtype=nm.float64)[:dim] centre = nm.array(eval(options.centre), dtype=nm.float64)[:dim] shape = nm.array(eval(options.shape), dtype=nm.int32)[:dim] degrees = nm.array(eval(options.degrees), dtype=nm.int32)[:dim] if options.continuity is None: continuity = degrees - 1 else: continuity = nm.array(eval(options.continuity), dtype=nm.int32)[:dim] n_dofs = degrees + 1 + (shape - 2) * (degrees - continuity) output('dimensions:', dims) output('centre: ', centre) output('shape: ', shape) output('degrees: ', degrees) output('continuity:', continuity) output('cp_mode: ', options.cp_mode) output('-> :', filename) output('number of DOFs per axis:', n_dofs) nurbs, bmesh, regions = gen_patch_block_domain(dims, shape, centre, degrees, continuity=continuity, cp_mode=options.cp_mode, name='block', verbose=True) io.write_iga_data(filename, nurbs.knots, nurbs.degrees, nurbs.cps, nurbs.weights, nurbs.cs, nurbs.conn, bmesh.cps, bmesh.weights, bmesh.conn, regions) if options.plot: pn.plt.rcParams['lines.linewidth'] = 2 block = nurbs.nurbs ax = pn.plot_parametric_mesh(None, block.knots) ax.set_title('parametric mesh') ax.axis('equal') points = block.points[..., :dim] ax = pn.plot_control_mesh(None, points, label=options.label) ax = pn.plot_iso_lines(ax, block) ax.set_title('control mesh and iso lines (blue)' ' in Greville abscissae coordinates') ax.axis('equal') points = bmesh.cps ax = pn.plot_bezier_mesh(None, points, bmesh.conn, block.degree, label=options.label) ax = pn.plot_iso_lines(ax, block) ax.set_title('Bezier mesh and iso lines (blue)' ' in Greville abscissae coordinates') ax.axis('equal') pn.plt.rcParams['lines.linewidth'] = 3 line = block.extract(0, 0.5) if dim == 3: line = line.extract(0, 0.5) ax = pn.plot_nurbs_basis_1d(None, line, n_points=1000, legend=options.label) ax.set_xlabel('last parametric coordinate') ax.set_title('1D NURBS basis') ax = pn.plot_nurbs_basis_1d(None, line, n_points=1000, x_axis=dim - 1, legend=options.label) ax.set_xlabel('last physical coordinate') ax.set_title('1D NURBS basis') pn.plt.show()
def main(): parser = OptionParser(usage=usage, version='%prog') parser.add_option('-o', '', metavar='filename', action='store', dest='filename', default=None, help=helps['filename']) parser.add_option('-d', '--dims', metavar='dims', action='store', dest='dims', default='[1.0, 1.0, 1.0]', help=helps['dims']) parser.add_option('-c', '--centre', metavar='centre', action='store', dest='centre', default='[0.0, 0.0, 0.0]', help=helps['centre']) parser.add_option('-s', '--shape', metavar='shape', action='store', dest='shape', default='[5, 5, 5]', help=helps['shape']) parser.add_option('', '--degrees', metavar='degrees', action='store', dest='degrees', default='[2, 2, 2]', help=helps['degrees']) parser.add_option('', '--continuity', metavar='continuity', action='store', dest='continuity', default=None, help=helps['continuity']) parser.add_option('', '--cp-mode', metavar="'greville' or 'uniform'", action='store', dest='cp_mode', choices=['greville', 'uniform'], default='greville', help=helps['cp_mode']) parser.add_option('-2', '--2d', action='store_true', dest='is_2d', default=False, help=helps['2d']) parser.add_option('-p', '--plot', action='store_true', dest='plot', default=False, help=helps['plot']) parser.add_option('-l', '--label', action='store_true', dest='label', default=False, help=helps['label']) (options, args) = parser.parse_args() dim = 2 if options.is_2d else 3 filename = options.filename if filename is None: filename = 'block%dd.iga' % dim dims = nm.array(eval(options.dims), dtype=nm.float64)[:dim] centre = nm.array(eval(options.centre), dtype=nm.float64)[:dim] shape = nm.array(eval(options.shape), dtype=nm.int32)[:dim] degrees = nm.array(eval(options.degrees), dtype=nm.int32)[:dim] if options.continuity is None: continuity = degrees - 1 else: continuity = nm.array(eval(options.continuity), dtype=nm.int32)[:dim] n_dofs = degrees + 1 + (shape - 2) * (degrees - continuity) output('dimensions:', dims) output('centre: ', centre) output('shape: ', shape) output('degrees: ', degrees) output('continuity:', continuity) output('cp_mode: ', options.cp_mode) output('-> :', filename) output('number of DOFs per axis:', n_dofs) nurbs, bmesh, regions = gen_patch_block_domain(dims, shape, centre, degrees, continuity=continuity, cp_mode=options.cp_mode, name='block', verbose=True) io.write_iga_data(filename, nurbs.knots, nurbs.degrees, nurbs.cps, nurbs.weights, nurbs.cs, nurbs.conn, bmesh.cps, bmesh.weights, bmesh.conn, regions) if options.plot: pn.plt.rcParams['lines.linewidth'] = 2 block = nurbs.nurbs ax = pn.plot_parametric_mesh(None, block.knots) ax.set_title('parametric mesh') ax.axis('equal') points = block.points[..., :dim] ax = pn.plot_control_mesh(None, points, label=options.label) ax = pn.plot_iso_lines(ax, block) ax.set_title('control mesh and iso lines (blue)' ' in Greville abscissae coordinates') ax.axis('equal') points = bmesh.cps ax = pn.plot_bezier_mesh(None, points, bmesh.conn, block.degree, label=options.label) ax = pn.plot_iso_lines(ax, block) ax.set_title('Bezier mesh and iso lines (blue)' ' in Greville abscissae coordinates') ax.axis('equal') pn.plt.rcParams['lines.linewidth'] = 3 line = block.extract(0, 0.5) if dim == 3: line = line.extract(0, 0.5) ax = pn.plot_nurbs_basis_1d(None, line, n_points=1000, legend=options.label) ax.set_xlabel('last parametric coordinate') ax.set_title('1D NURBS basis') ax = pn.plot_nurbs_basis_1d(None, line, n_points=1000, x_axis=dim-1, legend=options.label) ax.set_xlabel('last physical coordinate') ax.set_title('1D NURBS basis') pn.plt.show()
def test_hdf5_meshio(self): import tempfile import numpy as nm import scipy.sparse as sps from sfepy.discrete.fem.meshio import HDF5MeshIO from sfepy.base.base import Struct from sfepy.base.ioutils import Cached, Uncached, SoftLink, \ DataSoftLink from sfepy.discrete.iga.domain import IGDomain from sfepy.discrete.iga.domain_generators import gen_patch_block_domain from sfepy.solvers.ts import TimeStepper from sfepy.discrete.fem import Mesh conf_dir = op.dirname(__file__) mesh0 = Mesh.from_file(data_dir + '/meshes/various_formats/small3d.mesh', prefix_dir=conf_dir) shape = [4, 4, 4] dims = [5, 5, 5] centre = [0, 0, 0] degrees = [2, 2, 2] nurbs, bmesh, regions = gen_patch_block_domain(dims, shape, centre, degrees, cp_mode='greville', name='iga') ig_domain = IGDomain('iga', nurbs, bmesh, regions=regions) int_ar = nm.arange(4) data = { 'list': range(4), 'mesh1': mesh0, 'mesh2': mesh0, 'mesh3': Uncached(mesh0), 'mesh4': SoftLink('/step0/__cdata/data/data/mesh2'), 'mesh5': DataSoftLink('Mesh','/step0/__cdata/data/data/mesh1/data'), 'mesh6': DataSoftLink('Mesh','/step0/__cdata/data/data/mesh2/data', mesh0), 'mesh7': DataSoftLink('Mesh','/step0/__cdata/data/data/mesh1/data', True), 'iga' : ig_domain, 'cached1': Cached(1), 'cached2': Cached(int_ar), 'cached3': Cached(int_ar), 'types': ( True, False, None ), 'tuple': ('first string', 'druhý UTF8 řetězec'), 'struct': Struct( double=nm.arange(4, dtype=float), int=nm.array([2,3,4,7]), sparse=sps.csr_matrix(nm.array([1,0,0,5]). reshape((2,2))) ) } with tempfile.NamedTemporaryFile(suffix='.h5') as fil: io = HDF5MeshIO(fil.name) ts = TimeStepper(0,1.,0.1, 10) io.write(fil.name, mesh0, { 'cdata' : Struct( mode='custom', data=data, unpack_markers=False ) }, ts=ts) ts.advance() mesh = io.read() data['problem_mesh'] = DataSoftLink('Mesh', '/mesh', mesh) io.write(fil.name, mesh0, { 'cdata' : Struct( mode='custom', data=data, unpack_markers=True ) }, ts=ts) cache = {'/mesh': mesh } fout = io.read_data(0, cache=cache) fout2 = io.read_data(1, cache=cache ) out = fout['cdata'] out2 = fout2['cdata'] assert_(out['mesh7'] is out2['mesh7'], 'These two meshes should be in fact the same object') assert_(out['mesh6'] is out2['mesh6'], 'These two meshes should be in fact the same object') assert_(out['mesh5'] is not out2['mesh5'], 'These two meshes shouldn''t be in fact the same object') assert_(out['mesh1'] is out['mesh2'], 'These two meshes should be in fact the same object') assert_(out['mesh1'] is out['mesh2'], 'These two meshes should be in fact the same object') assert_(out['mesh4'] is out['mesh2'], 'These two meshes should be in fact the same object') assert_(out['mesh5'] is not out['mesh2'], 'These two meshes shouldn''t be in fact the same object') assert_(out['mesh6'] is out['mesh2'], 'These two meshes should be in fact the same object') assert_(out['mesh7'] is not out['mesh2'], 'These two meshes shouldn''t be in fact the same object') assert_(out['mesh3'] is not out['mesh2'], 'These two meshes should be different objects') assert_(out['cached2'] is out['cached3'], 'These two array should be the same object') assert_(out2['problem_mesh'] is mesh, 'These two meshes should be the same objects') assert_(self._compare_meshes(out['mesh1'], mesh0), 'Failed to restore mesh') assert_(self._compare_meshes(out['mesh3'], mesh0), 'Failed to restore mesh') assert_((out['struct'].sparse == data['struct'].sparse).todense() .all(), 'Sparse matrix restore failed') ts.advance() io.write(fil.name, mesh0, { 'cdata' : Struct( mode='custom', data=[ DataSoftLink('Mesh', '/step0/__cdata/data/data/mesh1/data', mesh0), mesh0 ] ) }, ts=ts) out3 = io.read_data(2)['cdata'] assert_(out3[0] is out3[1]) #this property is not restored del data['iga'].nurbs.nurbs #not supporting comparison del data['iga']._bnf del out2['iga']._bnf #restoration of this property fails del data['iga'].vertex_set_bcs del out2['iga'].vertex_set_bcs #these soflink has no information how to unpack, so it must be #done manually data['mesh4'] = mesh0 data['mesh5'] = mesh0 data['mesh7'] = mesh0 for key, val in six.iteritems(out2): self.report('comparing:', key) self.assert_equal(val, data[key]) return True
def test_hdf5_meshio(self): try: from igakit import igalib except ImportError: self.report('hdf5_meshio not-tested (missing igalib module)!') return True import tempfile import numpy as nm import scipy.sparse as sps from sfepy.discrete.fem.meshio import HDF5MeshIO from sfepy.base.base import Struct from sfepy.base.ioutils import Cached, Uncached, SoftLink, \ DataSoftLink from sfepy.discrete.iga.domain import IGDomain from sfepy.discrete.iga.domain_generators import gen_patch_block_domain from sfepy.solvers.ts import TimeStepper from sfepy.discrete.fem import Mesh conf_dir = op.dirname(__file__) mesh0 = Mesh.from_file(data_dir + '/meshes/various_formats/small3d.mesh', prefix_dir=conf_dir) shape = [4, 4, 4] dims = [5, 5, 5] centre = [0, 0, 0] degrees = [2, 2, 2] nurbs, bmesh, regions = gen_patch_block_domain(dims, shape, centre, degrees, cp_mode='greville', name='iga') ig_domain = IGDomain('iga', nurbs, bmesh, regions=regions) int_ar = nm.arange(4) data = { 'list': range(4), 'mesh1': mesh0, 'mesh2': mesh0, 'mesh3': Uncached(mesh0), 'mesh4': SoftLink('/step0/__cdata/data/data/mesh2'), 'mesh5': DataSoftLink('Mesh', '/step0/__cdata/data/data/mesh1/data'), 'mesh6': DataSoftLink('Mesh', '/step0/__cdata/data/data/mesh2/data', mesh0), 'mesh7': DataSoftLink('Mesh', '/step0/__cdata/data/data/mesh1/data', True), 'iga': ig_domain, 'cached1': Cached(1), 'cached2': Cached(int_ar), 'cached3': Cached(int_ar), 'types': (True, False, None), 'tuple': ('first string', 'druhý UTF8 řetězec'), 'struct': Struct(double=nm.arange(4, dtype=float), int=nm.array([2, 3, 4, 7]), sparse=sps.csr_matrix( nm.array([1, 0, 0, 5]).reshape((2, 2)))) } with tempfile.NamedTemporaryFile(suffix='.h5', delete=False) as fil: io = HDF5MeshIO(fil.name) ts = TimeStepper(0, 1., 0.1, 10) io.write(fil.name, mesh0, { 'cdata': Struct(mode='custom', data=data, unpack_markers=False) }, ts=ts) ts.advance() mesh = io.read() data['problem_mesh'] = DataSoftLink('Mesh', '/mesh', mesh) io.write(fil.name, mesh0, { 'cdata': Struct(mode='custom', data=data, unpack_markers=True) }, ts=ts) cache = {'/mesh': mesh} fout = io.read_data(0, cache=cache) fout2 = io.read_data(1, cache=cache) out = fout['cdata'] out2 = fout2['cdata'] assert_(out['mesh7'] is out2['mesh7'], 'These two meshes should be in fact the same object') assert_(out['mesh6'] is out2['mesh6'], 'These two meshes should be in fact the same object') assert_(out['mesh5'] is not out2['mesh5'], 'These two meshes shouldn' 't be in fact the same object') assert_(out['mesh1'] is out['mesh2'], 'These two meshes should be in fact the same object') assert_(out['mesh1'] is out['mesh2'], 'These two meshes should be in fact the same object') assert_(out['mesh4'] is out['mesh2'], 'These two meshes should be in fact the same object') assert_(out['mesh5'] is not out['mesh2'], 'These two meshes shouldn' 't be in fact the same object') assert_(out['mesh6'] is out['mesh2'], 'These two meshes should be in fact the same object') assert_(out['mesh7'] is not out['mesh2'], 'These two meshes shouldn' 't be in fact the same object') assert_(out['mesh3'] is not out['mesh2'], 'These two meshes should be different objects') assert_(out['cached2'] is out['cached3'], 'These two array should be the same object') assert_(out2['problem_mesh'] is mesh, 'These two meshes should be the same objects') assert_(self._compare_meshes(out['mesh1'], mesh0), 'Failed to restore mesh') assert_(self._compare_meshes(out['mesh3'], mesh0), 'Failed to restore mesh') assert_(( out['struct'].sparse == data['struct'].sparse).todense().all(), 'Sparse matrix restore failed') ts.advance() io.write(fil.name, mesh0, { 'cdata': Struct(mode='custom', data=[ DataSoftLink( 'Mesh', '/step0/__cdata/data/data/mesh1/data', mesh0), mesh0 ]) }, ts=ts) out3 = io.read_data(2)['cdata'] assert_(out3[0] is out3[1]) os.remove(fil.name) #this property is not restored del data['iga'].nurbs.nurbs #not supporting comparison del data['iga']._bnf del out2['iga']._bnf #restoration of this property fails del data['iga'].vertex_set_bcs del out2['iga'].vertex_set_bcs #these soflink has no information how to unpack, so it must be #done manually data['mesh4'] = mesh0 data['mesh5'] = mesh0 data['mesh7'] = mesh0 for key, val in six.iteritems(out2): self.report('comparing:', key) self.assert_equal(val, data[key]) return True