def get_gen_block_mesh_hook(dims, shape, centre, mat_id=0, name='block', coors=None, verbose=True): """ Parameters ---------- dims : array of 2 or 3 floats Dimensions of the block. shape : array of 2 or 3 ints Shape (counts of nodes in x, y, z) of the block mesh. centre : array of 2 or 3 floats Centre of the block. mat_id : int, optional The material id of all elements. name : string Mesh name. verbose : bool If True, show progress of the mesh generation. Returns ------- mio : UserMeshIO instance """ def mesh_hook(mesh, mode): """ Generate the 1D mesh. """ if mode == 'read': mesh = gen_block_mesh(dims, shape, centre, mat_id=mat_id, name=name, coors=coors, verbose=verbose) return mesh elif mode == 'write': pass mio = UserMeshIO(mesh_hook) return mio
def get_gen_1D_mesh_hook(XS, XE, n_nod): """ Parameters ---------- XS : float leftmost coordinate XE : float rightmost coordinate n_nod : int number of nodes, number of cells is then n_nod - 1 Returns ------- mio : UserMeshIO instance """ def mesh_hook(mesh, mode): """ Generate the 1D mesh. """ if mode == 'read': coors = nm.linspace(XS, XE, n_nod).reshape((n_nod, 1)) conn = nm.arange(n_nod, dtype=nm.int32) \ .repeat(2)[1:-1].reshape((-1, 2)) mat_ids = nm.zeros(n_nod - 1, dtype=nm.int32) descs = ['1_2'] mesh = Mesh.from_data('uniform_1D{}'.format(n_nod), coors, None, [conn], [mat_ids], descs) return mesh elif mode == 'write': pass mio = UserMeshIO(mesh_hook) return mio
def mesh_hook(mesh, mode): """ Generate the block mesh. """ if mode == 'read': mesh = gen_block_mesh(dims, shape, [0, 0], name='user_block', verbose=False) return mesh elif mode == 'write': pass filename_mesh = UserMeshIO(mesh_hook) regions = { 'Omega': 'all', 'Gamma': ('vertices of surface', 'facet'), } fields = { 'temperature': ('real', 1, 'Omega', 1), } variables = { 't': ('unknown field', 'temperature', 0), 's': ('test field', 'temperature', 't'), }
def define(K=8.333, mu_nh=3.846, mu_mr=1.923, kappa=1.923, lam=5.769, mu=3.846): """Define the problem to solve.""" from sfepy.discrete.fem.meshio import UserMeshIO from sfepy.mesh.mesh_generators import gen_block_mesh from sfepy.mechanics.matcoefs import stiffness_from_lame def mesh_hook(mesh, mode): """ Generate the block mesh. """ if mode == 'read': mesh = gen_block_mesh([2, 2, 3], [2, 2, 4], [0, 0, 1.5], name='el3', verbose=False) return mesh elif mode == 'write': pass filename_mesh = UserMeshIO(mesh_hook) options = { 'nls': 'newton', 'ls': 'ls', 'ts': 'ts', 'save_times': 'all', } functions = { 'linear_pressure': (linear_pressure, ), 'empty': (lambda ts, coor, mode, region, ig: None, ), } fields = { 'displacement': ('real', 3, 'Omega', 1), } # Coefficients are chosen so that the tangent stiffness is the same for all # material for zero strains. materials = { 'solid': ( { 'K': K, # bulk modulus 'mu_nh': mu_nh, # shear modulus of neoHookean term 'mu_mr': mu_mr, # shear modulus of Mooney-Rivlin term 'kappa': kappa, # second modulus of Mooney-Rivlin term # elasticity for LE term 'D': stiffness_from_lame(dim=3, lam=lam, mu=mu), }, ), 'load': 'empty', } variables = { 'u': ('unknown field', 'displacement', 0), 'v': ('test field', 'displacement', 'u'), } regions = { 'Omega': 'all', 'Bottom': ('vertices in (z < 0.1)', 'facet'), 'Top': ('vertices in (z > 2.9)', 'facet'), } ebcs = { 'fixb': ('Bottom', { 'u.all': 0.0 }), 'fixt': ('Top', { 'u.[0,1]': 0.0 }), } integrals = { 'i': 1, 'isurf': 2, } equations = { 'linear': """dw_lin_elastic.i.Omega(solid.D, v, u) = dw_surface_ltr.isurf.Top(load.val, v)""", 'neo-Hookean': """dw_tl_he_neohook.i.Omega(solid.mu_nh, v, u) + dw_tl_bulk_penalty.i.Omega(solid.K, v, u) = dw_surface_ltr.isurf.Top(load.val, v)""", 'Mooney-Rivlin': """dw_tl_he_neohook.i.Omega(solid.mu_mr, v, u) + dw_tl_he_mooney_rivlin.i.Omega(solid.kappa, v, u) + dw_tl_bulk_penalty.i.Omega(solid.K, v, u) = dw_surface_ltr.isurf.Top(load.val, v)""", } solvers = { 'ls': ('ls.scipy_direct', {}), 'newton': ('nls.newton', { 'i_max': 5, 'eps_a': 1e-10, 'eps_r': 1.0, }), 'ts': ( 'ts.simple', { 't0': 0, 't1': 1, 'time_interval': None, 'n_step': 26, # has precedence over time_interval! 'verbose': 1, }), } return locals()
def define(order=5, basis='lagrange', min_level=0, max_level=5, eps=1e-3): filename_mesh = UserMeshIO(mesh_hook) # Get the mesh bounding box. io = MeshIO.any_from_filename(base_mesh) bbox, dim = io.read_bounding_box(ret_dim=True) options = { 'nls': 'newton', 'ls': 'ls', 'post_process_hook': 'post_process', 'linearization': { 'kind': 'adaptive', 'min_level': min_level, # Min. refinement level applied everywhere. 'max_level': max_level, # Max. refinement level. 'eps': eps, # Relative error tolerance. }, } materials = { 'coef': ({ 'val': 1.0 }, ), } regions = { 'Omega': 'all', } regions.update(define_box_regions(dim, bbox[0], bbox[1], 1e-5)) fields = { 'temperature': ('real', 1, 'Omega', order, 'H1', basis), } variables = { 't': ('unknown field', 'temperature', 0), 's': ('test field', 'temperature', 't'), } amplitude = 1.0 def ebc_sin(ts, coor, **kwargs): x0 = 0.5 * (coor[:, 1].min() + coor[:, 1].max()) val = amplitude * nm.sin((coor[:, 1] - x0) * 2. * nm.pi) return val ebcs = { 't1': ('Left', { 't.0': 'ebc_sin' }), 't2': ('Right', { 't.0': -0.5 }), 't3': ('Top', { 't.0': 1.0 }), } functions = { 'ebc_sin': (ebc_sin, ), } equations = {'Temperature': """dw_laplace.10.Omega(coef.val, s, t) = 0"""} solvers = { 'ls': ('ls.scipy_direct', {}), 'newton': ('nls.newton', { 'i_max': 1, 'eps_a': 1e-10, }), } return locals()
def define(dims=(3, 1, 0.5), shape=(11, 15, 15), u_order=1, refine=0, ls='ls_d', u_inlet=None, mode='lcbc', term_mode='original', backend='numpy', optimize='optimal', verbosity=0): """ Parameters ---------- dims : tuple The block domain dimensions. shape : tuple The mesh resolution: increase to improve accuracy. u_order : int The velocity field approximation order. refine : int The refinement level. ls : 'ls_d' or 'ls_i' The pre-configured linear solver name. u_inlet : float, optional The x-component of the inlet velocity. mode : 'lcbc' or 'penalty' The alternative formulations. term_mode : 'original' or 'einsum' The switch to use either the original or new experimental einsum-based terms. backend : str The einsum mode backend. optimize : str The einsum mode optimization (backend dependent). verbosity : 0, 1, 2, 3 The verbosity level of einsum-based terms. """ output('dims: {}, shape: {}, u_order: {}, refine: {}, u_inlet: {}'.format( dims, shape, u_order, refine, u_inlet)) output('linear solver: {}'.format(ls)) output('mode: {}, term_mode: {}'.format(mode, term_mode)) if term_mode == 'einsum': output('backend: {}, optimize: {}, verbosity: {}'.format( backend, optimize, verbosity)) assert_(mode in {'lcbc', 'penalty'}) assert_(term_mode in {'original', 'einsum'}) if u_order > 1: assert_(mode == 'penalty', msg='set mode=penalty to use u_order > 1!') dims = nm.array(dims, dtype=nm.float64) shape = nm.array(shape, dtype=nm.int32) def mesh_hook(mesh, mode): """ Generate the block mesh. """ if mode == 'read': mesh = gen_block_mesh(dims, shape, [0, 0, 0], name='user_block', verbose=False) return mesh elif mode == 'write': pass filename_mesh = UserMeshIO(mesh_hook) regions = define_box_regions(3, 0.5 * dims) regions.update({ 'Omega': 'all', 'Edges_v': ("""(r.Near *v r.Bottom) +v (r.Bottom *v r.Far) +v (r.Far *v r.Top) +v (r.Top *v r.Near)""", 'edge'), 'Gamma1_f': ('copy r.Top', 'face'), 'Gamma2_f': ('r.Near +v r.Bottom +v r.Far', 'face'), 'Gamma_f': ('r.Gamma1_f +v r.Gamma2_f', 'face'), 'Gamma_v': ('r.Gamma_f -v r.Edges_v', 'face'), 'Inlet_f': ('r.Left -v r.Gamma_f', 'face'), }) fields = { 'velocity': ('real', 3, 'Omega', u_order), 'pressure': ('real', 1, 'Omega', 1), } def get_u_d(ts, coors, region=None): """ Given stator velocity. """ out = nm.zeros_like(coors) out[:] = [1.0, 1.0, 0.0] return out functions = { 'get_u_d': (get_u_d, ), } variables = { 'u': ('unknown field', 'velocity', 0), 'v': ('test field', 'velocity', 'u'), 'u_d': ('parameter field', 'velocity', { 'setter': 'get_u_d' }), 'p': ('unknown field', 'pressure', 1), 'q': ('test field', 'pressure', 'p'), } materials = { 'm': ({ 'nu': 1e-3, 'beta': 1e-2, 'mu': 1e-10, }, ), } ebcs = {} if u_inlet is not None: ebcs['inlet'] = ('Inlet_f', {'u.0': u_inlet, 'u.[1, 2]': 0.0}) if mode == 'lcbc': lcbcs = { 'walls': ('Gamma_v', { 'u.all': None }, None, 'no_penetration', 'normals_Gamma.vtk'), 'edges': ('Edges_v', [(-0.5, 1.5)], { 'u.all': None }, None, 'edge_direction', 'edges_Edges.vtk'), } if term_mode == 'original': equations = { 'balance': """dw_div_grad.5.Omega(m.nu, v, u) - dw_stokes.5.Omega(v, p) + dw_dot.5.Gamma1_f(m.beta, v, u) + dw_dot.5.Gamma2_f(m.beta, v, u) = + dw_dot.5.Gamma1_f(m.beta, v, u_d)""", 'incompressibility': """dw_laplace.5.Omega(m.mu, q, p) + dw_stokes.5.Omega(u, q) = 0""", } else: equations = { 'balance': """de_div_grad.5.Omega(m.nu, v, u) - de_stokes.5.Omega(v, p) + de_dot.5.Gamma1_f(m.beta, v, u) + de_dot.5.Gamma2_f(m.beta, v, u) = + de_dot.5.Gamma1_f(m.beta, v, u_d)""", 'incompressibility': """de_laplace.5.Omega(m.mu, q, p) + de_stokes.5.Omega(u, q) = 0""", } else: materials['m'][0]['np_eps'] = 1e3 if term_mode == 'original': equations = { 'balance': """dw_div_grad.5.Omega(m.nu, v, u) - dw_stokes.5.Omega(v, p) + dw_dot.5.Gamma1_f(m.beta, v, u) + dw_dot.5.Gamma2_f(m.beta, v, u) + dw_non_penetration_p.5.Gamma1_f(m.np_eps, v, u) + dw_non_penetration_p.5.Gamma2_f(m.np_eps, v, u) = + dw_dot.5.Gamma1_f(m.beta, v, u_d)""", 'incompressibility': """dw_laplace.5.Omega(m.mu, q, p) + dw_stokes.5.Omega(u, q) = 0""", } else: equations = { 'balance': """de_div_grad.5.Omega(m.nu, v, u) - de_stokes.5.Omega(v, p) + de_dot.5.Gamma1_f(m.beta, v, u) + de_dot.5.Gamma2_f(m.beta, v, u) + de_non_penetration_p.5.Gamma1_f(m.np_eps, v, u) + de_non_penetration_p.5.Gamma2_f(m.np_eps, v, u) = + de_dot.5.Gamma1_f(m.beta, v, u_d)""", 'incompressibility': """de_laplace.5.Omega(m.mu, q, p) + de_stokes.5.Omega(u, q) = 0""", } solvers = { 'ls_d': ('ls.auto_direct', {}), 'ls_i': ( 'ls.petsc', { 'method': 'bcgsl', # ksp_type 'precond': 'bjacobi', # pc_type 'sub_precond': 'ilu', # sub_pc_type 'eps_a': 0.0, # abstol 'eps_r': 1e-12, # rtol 'eps_d': 1e10, # Divergence tolerance. 'i_max': 200, # maxits }), 'newton': ('nls.newton', { 'i_max': 1, 'eps_a': 1e-10, }), } options = { 'nls': 'newton', 'ls': ls, 'eterm': { 'verbosity': verbosity, 'backend_args': { 'backend': backend, 'optimize': optimize, 'layout': None, }, }, 'refinement_level': refine, } return locals()
if mode == 'read': nodes = [[0, 0], [1, 0], [1, 1], [0, 1]] nod_ids = [0, 0, 1, 1] conns = [[[0, 1, 2], [0, 2, 3]]] mat_ids = [[0, 1]] descs = ['2_3'] mesh._set_io_data(nodes, nod_ids, conns, mat_ids, descs) elif mode == 'write': pass from sfepy.discrete.fem.meshio import UserMeshIO filename_meshes.extend([mesh_hook, UserMeshIO(mesh_hook)]) same = [(0, 1), (2, 3)] import os.path as op from sfepy.base.base import assert_ from sfepy.base.testing import TestCommon class Test(TestCommon): """Write test names explicitely to impose a given order of evaluation.""" tests = [ 'test_read_meshes', 'test_compare_same_meshes', 'test_read_dimension', 'test_write_read_meshes' ]
def define(is_opt=False): filename_mesh = UserMeshIO(mesh_hook) mnodes = (107, 113) # nodes for elongation eval. regions = { 'Omega': 'all', 'Bottom': ('vertices in (z < 0.001)', 'facet'), 'Top': ('vertices in (z > 0.099)', 'facet'), } functions = { 'get_mat': (lambda ts, coors, mode=None, problem=None, **kwargs: get_mat( coors, mode, problem), ), } S = 1.083500e-05 # specimen cross-section F = 5.0e3 # force materials = { 'solid': 'get_mat', 'load': ({ 'val': F / S }, ), } fields = { 'displacement': ('real', 'vector', 'Omega', 1), } variables = { 'u': ('unknown field', 'displacement', 0), 'v': ('test field', 'displacement', 'u'), } ebcs = { 'FixedBottom': ('Bottom', { 'u.all': 0.0 }), 'FixedTop': ('Top', { 'u.0': 0.0, 'u.1': 0.0 }), } equations = { 'balance_of_forces': """dw_lin_elastic.5.Omega(solid.D, v, u) = dw_surface_ltr.5.Top(load.val, v)""", } solvers = { 'ls': ('ls.scipy_direct', {}), 'newton': ('nls.newton', { 'eps_a': 1e-6, 'eps_r': 1.e-6, 'check': 0, 'problem': 'nonlinear' }), } options = { 'parametric_hook': 'optimization_hook', 'output_dir': 'output', } return locals()