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_ref_coors_iga(self): from sfepy.discrete.iga.domain import IGDomain domain = IGDomain.from_file( op.join(sfepy.data_dir, 'meshes/iga/block2d.iga')) omega = domain.create_region('Omega', 'all') field = Field.from_args('iga', nm.float64, 'scalar', omega, approx_order='iga', poly_space_base='iga') mcoors = field.nurbs.cps conn = field.get_econn('volume', field.region) bbox = domain.eval_mesh.get_bounding_box() ray = nm.linspace(bbox[0, 0], bbox[1, 0], 11) coors = nm.c_[ray, ray] ref_coors, cells, status = gi.get_ref_coors(field, coors, strategy='general', close_limit=0.0, verbose=False) self.report(ref_coors) self.report(cells) self.report(status) ok = nm.all(status == 0) ctx = field.create_basis_context() for ic, cell in enumerate(cells): ctx.iel = cell bf = ctx.evaluate(ref_coors[ic:ic + 1]) cell_coors = mcoors[conn[cell]] coor = nm.dot(bf, cell_coors).ravel() _ok = nm.allclose(coor, coors[ic], atol=1e-14, rtol=0.0) if not _ok: self.report('point %d:' % ic) self.report(' - wrong reference coordinates %s!' % ref_coors[ic]) self.report(' - given point: %s' % coors[ic]) self.report(' - found point: %s' % coor) ok = ok and _ok return ok
def test_ref_coors_iga(self): from sfepy.discrete.iga.domain import IGDomain domain = IGDomain.from_file(op.join(sfepy.data_dir, 'meshes/iga/block2d.iga')) omega = domain.create_region('Omega', 'all') field = Field.from_args('iga', nm.float64, 'scalar', omega, approx_order='iga', poly_space_base='iga') mcoors = field.nurbs.cps conn = field.get_econn('volume', field.region) bbox = domain.eval_mesh.get_bounding_box() ray = nm.linspace(bbox[0, 0], bbox[1, 0], 11) coors = nm.c_[ray, ray] ref_coors, cells, status = gi.get_ref_coors(field, coors, strategy='general', close_limit=0.0, verbose=False) self.report(ref_coors) self.report(cells) self.report(status) ok = nm.all(status == 0) ctx = field.create_basis_context() for ic, cell in enumerate(cells): ctx.iel = cell bf = ctx.evaluate(ref_coors[ic:ic+1]) cell_coors = mcoors[conn[cell]] coor = nm.dot(bf, cell_coors).ravel() _ok = nm.allclose(coor, coors[ic], atol=1e-14, rtol=0.0) if not _ok: self.report('point %d:' % ic) self.report(' - wrong reference coordinates %s!' % ref_coors[ic]) self.report(' - given point: %s' % coors[ic]) self.report(' - found point: %s' % coor) ok = ok and _ok return ok
def from_conf(conf, init_fields=True, init_equations=True, init_solvers=True): if conf.options.get('absolute_mesh_path', False): conf_dir = None else: conf_dir = op.dirname(conf.funmod.__file__) functions = Functions.from_conf(conf.functions) if conf.get('filename_mesh') is not None: from sfepy.discrete.fem.domain import FEDomain mesh = Mesh.from_file(conf.filename_mesh, prefix_dir=conf_dir) domain = FEDomain(mesh.name, mesh) if conf.options.get('ulf', False): domain.mesh.coors_act = domain.mesh.coors.copy() elif conf.get('filename_domain') is not None: from sfepy.discrete.iga.domain import IGDomain domain = IGDomain.from_file(conf.filename_domain) else: raise ValueError('missing filename_mesh or filename_domain!') obj = Problem('problem_from_conf', conf=conf, functions=functions, domain=domain, auto_conf=False, auto_solvers=False) obj.set_regions(conf.regions, obj.functions) obj.clear_equations() if init_fields: obj.set_fields(conf.fields) if init_equations: obj.set_equations(conf.equations, user={'ts' : obj.ts}) if init_solvers: obj.set_solvers(conf.solvers, conf.options) return obj
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
def main(): parser = ArgumentParser(description=__doc__.rstrip(), formatter_class=RawDescriptionHelpFormatter) parser.add_argument('-o', '--output-dir', default='.', help=helps['output_dir']) parser.add_argument('--R1', metavar='R1', action='store', dest='R1', default='0.5', help=helps['R1']) parser.add_argument('--R2', metavar='R2', action='store', dest='R2', default='1.0', help=helps['R2']) parser.add_argument('--C1', metavar='C1', action='store', dest='C1', default='0.0,0.0', help=helps['C1']) parser.add_argument('--C2', metavar='C2', action='store', dest='C2', default='0.0,0.0', help=helps['C2']) parser.add_argument('--order', metavar='int', type=int, action='store', dest='order', default=2, help=helps['order']) parser.add_argument('-v', '--viewpatch', action='store_true', dest='viewpatch', default=False, help=helps['viewpatch']) options = parser.parse_args() # Creation of the NURBS-patch with igakit R1 = eval(options.R1) R2 = eval(options.R2) C1 = list(eval(options.C1)) C2 = list(eval(options.C2)) order = options.order viewpatch = options.viewpatch create_patch(R1, R2, C1, C2, order=order, viewpatch=viewpatch) # Setting a Domain instance filename_domain = data_dir + '/meshes/iga/concentric_circles.iga' domain = IGDomain.from_file(filename_domain) # Sub-domains omega = domain.create_region('Omega', 'all') Gamma_out = domain.create_region('Gamma_out', 'vertices of set xi01', kind='facet') Gamma_in = domain.create_region('Gamma_in', 'vertices of set xi00', kind='facet') # Field (featuring order elevation) order_increase = order - domain.nurbs.degrees[0] order_increase *= int(order_increase > 0) field = Field.from_args('fu', nm.float64, 'scalar', omega, approx_order='iga', space='H1', poly_space_base='iga') # Variables u = FieldVariable('u', 'unknown', field) # unknown function v = FieldVariable('v', 'test', field, primary_var_name='u') # test function # Integral integral = Integral('i', order=2 * field.approx_order) # Term t = Term.new('dw_laplace( v, u )', integral, omega, v=v, u=u) # Equation eq = Equation('laplace', t) eqs = Equations([eq]) # Boundary Conditions u_in = EssentialBC('u_in', Gamma_in, {'u.all': 7.0}) u_out = EssentialBC('u_out', Gamma_out, {'u.all': 3.0}) # solvers ls = ScipyDirect({}) nls_status = IndexedStruct() nls = Newton({}, lin_solver=ls, status=nls_status) # problem instance pb = Problem('potential', equations=eqs, active_only=True) # Set boundary conditions pb.set_bcs(ebcs=Conditions([u_in, u_out])) # solving pb.set_solver(nls) status = IndexedStruct() state = pb.solve(status=status, save_results=True, verbose=True) # Saving the results to a classic VTK file filename = os.path.join(options.output_dir, 'concentric_circles.vtk') ensure_path(filename) pb.save_state(filename, state)