def test_project_tensors(self): from sfepy.discrete import FieldVariable from sfepy.discrete.projections import project_by_component ok = True u = FieldVariable('u', 'parameter', self.field, primary_var_name='(set-to-None)') u.set_constant(1.0) component = FieldVariable('component', 'parameter', self.field, primary_var_name='(set-to-None)') nls_options = {'eps_a': 1e-16, 'i_max': 1} u_qp = u.evaluate() u2 = FieldVariable('u2', 'parameter', self.field, primary_var_name='(set-to-None)') project_by_component(u2, u_qp, component, self.field.approx_order, nls_options=nls_options) _ok = self.compare_vectors(u(), u2()) ok = ok and _ok gu_qp = u.evaluate(mode='grad') gfield = Field.from_args('gu', nm.float64, 2, self.field.region, approx_order=self.field.approx_order) gu = FieldVariable('gu', 'parameter', gfield, primary_var_name='(set-to-None)') project_by_component(gu, gu_qp, component, gfield.approx_order, nls_options=nls_options) _ok = self.compare_vectors(gu(), nm.zeros_like(gu())) ok = ok and _ok return ok
def test_variables(self): from sfepy.discrete import FieldVariable, Integral ok = True u = FieldVariable('u', 'parameter', self.field, primary_var_name='(set-to-None)') u.set_constant(1.0) vec = u() # Nodal values. _ok = nm.allclose(vec, 1.0) self.report('set constant:', _ok) ok = _ok and ok def fun(coors): val = nm.empty_like(coors) val[:, 0] = 2 * coors[:, 1] - coors[:, 0] val[:, 1] = coors[:, 0] + 3 * coors[:, 1] return val u.set_from_function(fun) coors = u.field.get_coor() eu = u.evaluate_at(coors) _ok = nm.allclose(eu, fun(coors), rtol=0.0, atol=1e-13) self.report('set from function:', _ok) ok = _ok and ok integral = Integral('i', order=2) gu_qp = u.evaluate(mode='grad', integral=integral) # du_i/dx_j, i = column, j = row. gu = nm.array([[-1., 1.], [2., 3.]]) _ok = nm.allclose(gu_qp, gu[None, None, ...], rtol=0.0, atol=1e-13) self.report('set from function - gradient:', _ok) ok = _ok and ok u_qp = gu_qp[..., :, :1] u.set_from_qp(u_qp, integral) vu = u() _ok = (nm.allclose(vu[::2], -1, rtol=0.0, atol=1e-13) and nm.allclose(vu[1::2], 2, rtol=0.0, atol=1e-13)) self.report('set from qp:', _ok) ok = _ok and ok return ok
def test_variables(self): from sfepy.discrete import FieldVariable, Integral ok = True u = FieldVariable('u', 'parameter', self.field, primary_var_name='(set-to-None)') u.set_constant(1.0) vec = u() # Nodal values. _ok = nm.allclose(vec, 1.0) self.report('set constant:', _ok) ok = _ok and ok def fun(coors): val = nm.empty_like(coors) val[:, 0] = 2 * coors[:, 1] - coors[:, 0] val[:, 1] = coors[:, 0] + 3 * coors[:, 1] return val u.set_from_function(fun) coors = u.field.get_coor() eu = u.evaluate_at(coors) _ok = nm.allclose(eu, fun(coors), rtol=0.0, atol=1e-13) self.report('set from function:', _ok) ok = _ok and ok integral = Integral('i', order=2) gu_qp = u.evaluate(mode='grad', integral=integral) # du_i/dx_j, i = column, j = row. gu = nm.array([[-1., 1.], [ 2., 3.]]) _ok = nm.allclose(gu_qp, gu[None, None, ...], rtol=0.0, atol=1e-13) self.report('set from function - gradient:', _ok) ok = _ok and ok u_qp = gu_qp[..., :, :1] u.set_from_qp(u_qp, integral) vu = u() _ok = (nm.allclose(vu[::2], -1, rtol=0.0, atol=1e-13) and nm.allclose(vu[1::2], 2, rtol=0.0, atol=1e-13)) self.report('set from qp:', _ok) ok = _ok and ok return ok
def test_variables(self): from sfepy.discrete import FieldVariable u = FieldVariable('u', 'parameter', self.field, primary_var_name='(set-to-None)') u.set_constant(1.0) vec = u() # Nodal values. ok = nm.allclose(vec, 1.0) ## print u() ## print u.get_vector() # Coefficient vector w.r.t. the field space basis. ## print u(gamma1) ## print u.get_vector(gamma2) return ok
def test_project_tensors(self): from sfepy.discrete import FieldVariable from sfepy.discrete.projections import project_by_component ok = True u = FieldVariable('u', 'parameter', self.field, primary_var_name='(set-to-None)') u.set_constant(1.0) component = FieldVariable('component', 'parameter', self.field, primary_var_name='(set-to-None)') nls_options = {'eps_a' : 1e-16, 'i_max' : 1} u_qp = u.evaluate() u2 = FieldVariable('u2', 'parameter', self.field, primary_var_name='(set-to-None)') project_by_component(u2, u_qp, component, self.field.approx_order, nls_options=nls_options) _ok = self.compare_vectors(u(), u2()) ok = ok and _ok gu_qp = u.evaluate(mode='grad') gfield = Field.from_args('gu', nm.float64, 2, self.field.region, approx_order=self.field.approx_order) gu = FieldVariable('gu', 'parameter', gfield, primary_var_name='(set-to-None)') project_by_component(gu, gu_qp, component, gfield.approx_order, nls_options=nls_options) _ok = self.compare_vectors(gu(), nm.zeros_like(gu())) ok = ok and _ok return ok
def make_term_args(arg_shapes, arg_kinds, arg_types, ats_mode, domain, material_value=None, poly_space_base=None): from sfepy.base.base import basestr from sfepy.discrete import FieldVariable, Material, Variables, Materials from sfepy.discrete.fem import Field from sfepy.solvers.ts import TimeStepper from sfepy.mechanics.tensors import dim2sym omega = domain.regions['Omega'] dim = domain.shape.dim sym = dim2sym(dim) def _parse_scalar_shape(sh): if isinstance(sh, basestr): if sh == 'D': return dim elif sh == 'D2': return dim**2 elif sh == 'S': return sym elif sh == 'N': # General number ;) return 1 else: return int(sh) else: return sh def _parse_tuple_shape(sh): if isinstance(sh, basestr): return [_parse_scalar_shape(ii.strip()) for ii in sh.split(',')] else: return (int(sh),) args = {} str_args = [] materials = [] variables = [] for ii, arg_kind in enumerate(arg_kinds): if arg_kind != 'ts': if ats_mode is not None: extended_ats = arg_types[ii] + ('/%s' % ats_mode) else: extended_ats = arg_types[ii] try: sh = arg_shapes[arg_types[ii]] except KeyError: sh = arg_shapes[extended_ats] if arg_kind.endswith('variable'): shape = _parse_scalar_shape(sh[0] if isinstance(sh, tuple) else sh) field = Field.from_args('f%d' % ii, nm.float64, shape, omega, approx_order=1, poly_space_base=poly_space_base) if arg_kind == 'virtual_variable': if sh[1] is not None: istate = arg_types.index(sh[1]) else: # Only virtual variable in arguments. istate = -1 # -> Make fake variable. var = FieldVariable('u-1', 'unknown', field) var.set_constant(0.0) variables.append(var) var = FieldVariable('v', 'test', field, primary_var_name='u%d' % istate) elif arg_kind == 'state_variable': var = FieldVariable('u%d' % ii, 'unknown', field) var.set_constant(0.0) elif arg_kind == 'parameter_variable': var = FieldVariable('p%d' % ii, 'parameter', field, primary_var_name='(set-to-None)') var.set_constant(0.0) variables.append(var) str_args.append(var.name) args[var.name] = var elif arg_kind.endswith('material'): if sh is None: # Switched-off opt_material. continue prefix = '' if isinstance(sh, basestr): aux = sh.split(':') if len(aux) == 2: prefix, sh = aux if material_value is None: material_value = 1.0 shape = _parse_tuple_shape(sh) if (len(shape) > 1) or (shape[0] > 1): if ((len(shape) == 2) and (shape[0] == shape[1]) and (material_value != 0.0)): # Identity matrix. val = nm.eye(shape[0], dtype=nm.float64) else: # Array. val = nm.empty(shape, dtype=nm.float64) val.fill(material_value) values = {'%sc%d' % (prefix, ii) : val} elif (len(shape) == 1) and (shape[0] == 1): # Single scalar as a special value. values = {'.c%d' % ii : material_value} else: raise ValueError('wrong material shape! (%s)' % shape) mat = Material('m%d' % ii, values=values) materials.append(mat) str_args.append(mat.name + '.' + 'c%d' % ii) args[mat.name] = mat elif arg_kind == 'ts': ts = TimeStepper(0.0, 1.0, 1.0, 5) str_args.append('ts') args['ts'] = ts else: str_args.append('user%d' % ii) args[str_args[-1]] = None materials = Materials(materials) variables = Variables(variables) return args, str_args, materials, variables
def make_term_args(arg_shapes, arg_kinds, arg_types, ats_mode, domain, material_value=None, poly_space_base=None): from sfepy.base.base import basestr from sfepy.discrete import FieldVariable, Material, Variables, Materials from sfepy.discrete.fem import Field from sfepy.solvers.ts import TimeStepper from sfepy.mechanics.tensors import dim2sym omega = domain.regions['Omega'] dim = domain.shape.dim sym = dim2sym(dim) def _parse_scalar_shape(sh): if isinstance(sh, basestr): if sh == 'D': return dim elif sh == 'S': return sym elif sh == 'N': # General number ;) return 1 else: return int(sh) else: return sh def _parse_tuple_shape(sh): if isinstance(sh, basestr): return [_parse_scalar_shape(ii.strip()) for ii in sh.split(',')] else: return (int(sh), ) args = {} str_args = [] materials = [] variables = [] for ii, arg_kind in enumerate(arg_kinds): if arg_kind != 'ts': if ats_mode is not None: extended_ats = arg_types[ii] + ('/%s' % ats_mode) else: extended_ats = arg_types[ii] try: sh = arg_shapes[arg_types[ii]] except KeyError: sh = arg_shapes[extended_ats] if arg_kind.endswith('variable'): shape = _parse_scalar_shape(sh[0] if isinstance(sh, tuple) else sh) field = Field.from_args('f%d' % ii, nm.float64, shape, omega, approx_order=1, poly_space_base=poly_space_base) if arg_kind == 'virtual_variable': if sh[1] is not None: istate = arg_types.index(sh[1]) else: # Only virtual variable in arguments. istate = -1 # -> Make fake variable. var = FieldVariable('u-1', 'unknown', field) var.set_constant(0.0) variables.append(var) var = FieldVariable('v', 'test', field, primary_var_name='u%d' % istate) elif arg_kind == 'state_variable': var = FieldVariable('u%d' % ii, 'unknown', field) var.set_constant(0.0) elif arg_kind == 'parameter_variable': var = FieldVariable('p%d' % ii, 'parameter', field, primary_var_name='(set-to-None)') var.set_constant(0.0) variables.append(var) str_args.append(var.name) args[var.name] = var elif arg_kind.endswith('material'): if sh is None: # Switched-off opt_material. continue prefix = '' if isinstance(sh, basestr): aux = sh.split(':') if len(aux) == 2: prefix, sh = aux if material_value is None: material_value = 1.0 shape = _parse_tuple_shape(sh) if (len(shape) > 1) or (shape[0] > 1): if ((len(shape) == 2) and (shape[0] == shape[1]) and (material_value != 0.0)): # Identity matrix. val = nm.eye(shape[0], dtype=nm.float64) else: # Array. val = nm.empty(shape, dtype=nm.float64) val.fill(material_value) values = {'%sc%d' % (prefix, ii): val} elif (len(shape) == 1) and (shape[0] == 1): # Single scalar as a special value. values = {'.c%d' % ii: material_value} else: raise ValueError('wrong material shape! (%s)' % shape) mat = Material('m%d' % ii, values=values) materials.append(mat) str_args.append(mat.name + '.' + 'c%d' % ii) args[mat.name] = mat elif arg_kind == 'ts': ts = TimeStepper(0.0, 1.0, 1.0, 5) str_args.append('ts') args['ts'] = ts else: str_args.append('user%d' % ii) args[str_args[-1]] = None materials = Materials(materials) variables = Variables(variables) return args, str_args, materials, variables