def test_solving(self): from sfepy.base.base import IndexedStruct from sfepy.fem \ import FieldVariable, Material, ProblemDefinition, \ Function, Equation, Equations, Integral from sfepy.fem.conditions import Conditions, EssentialBC from sfepy.terms import Term from sfepy.solvers.ls import ScipyDirect from sfepy.solvers.nls import Newton u = FieldVariable('u', 'unknown', self.field, self.dim) v = FieldVariable('v', 'test', self.field, self.dim, primary_var_name='u') m = Material('m', lam=1.0, mu=1.0) f = Material('f', val=[[0.02], [0.01]]) bc_fun = Function('fix_u_fun', fix_u_fun, extra_args={'extra_arg' : 'hello'}) fix_u = EssentialBC('fix_u', self.gamma1, {'u.all' : bc_fun}) shift_u = EssentialBC('shift_u', self.gamma2, {'u.0' : 0.1}) integral = Integral('i', order=3) t1 = Term.new('dw_lin_elastic_iso(m.lam, m.mu, v, u)', integral, self.omega, m=m, v=v, u=u) t2 = Term.new('dw_volume_lvf(f.val, v)', integral, self.omega, f=f, v=v) eq = Equation('balance', t1 + t2) eqs = Equations([eq]) ls = ScipyDirect({}) nls_status = IndexedStruct() nls = Newton({}, lin_solver=ls, status=nls_status) pb = ProblemDefinition('elasticity', equations=eqs, nls=nls, ls=ls) ## pb.save_regions_as_groups('regions') pb.time_update(ebcs=Conditions([fix_u, shift_u])) state = pb.solve() name = op.join(self.options.out_dir, 'test_high_level_solving.vtk') pb.save_state(name, state) ok = nls_status.condition == 0 if not ok: self.report('solver did not converge!') _ok = state.has_ebc() if not _ok: self.report('EBCs violated!') ok = ok and _ok return ok
def make_h1_projection_data(target, eval_data): """ Project scalar data given by a material-like `eval_data()` function to a scalar `target` field variable using the :math:`H^1` dot product. """ order = target.field.approx_order * 2 integral = Integral('i', order=order) un = target.name v = FieldVariable('v', 'test', target.field, 1, primary_var_name=un) lhs1 = Term.new('dw_volume_dot(v, %s)' % un, integral, target.field.region, v=v, **{un: target}) lhs2 = Term.new('dw_laplace(v, %s)' % un, integral, target.field.region, v=v, **{un: target}) def _eval_data(ts, coors, mode, **kwargs): if mode == 'qp': val = eval_data(ts, coors, mode, 'val', **kwargs) gval = eval_data(ts, coors, mode, 'grad', **kwargs) return {'val': val, 'gval': gval} m = Material('m', function=_eval_data) rhs1 = Term.new('dw_volume_lvf(m.val, v)', integral, target.field.region, m=m, v=v) rhs2 = Term.new('dw_diffusion_r(m.gval, v)', integral, target.field.region, m=m, v=v) eq = Equation('projection', lhs1 + lhs2 - rhs1 - rhs2) eqs = Equations([eq]) ls = ScipyDirect({}) nls_status = IndexedStruct() nls = Newton({}, lin_solver=ls, status=nls_status) pb = ProblemDefinition('aux', equations=eqs, nls=nls, ls=ls) pb.time_update() # This sets the target variable with the projection solution. pb.solve() if nls_status.condition != 0: output('H1 projection: solver did not converge!')
def main(): from sfepy import data_dir parser = OptionParser(usage=usage, version="%prog") parser.add_option("-s", "--show", action="store_true", dest="show", default=False, help=help["show"]) options, args = parser.parse_args() mesh = Mesh.from_file(data_dir + "/meshes/2d/rectangle_tri.mesh") domain = Domain("domain", mesh) min_x, max_x = domain.get_mesh_bounding_box()[:, 0] eps = 1e-8 * (max_x - min_x) omega = domain.create_region("Omega", "all") gamma1 = domain.create_region("Gamma1", "nodes in x < %.10f" % (min_x + eps)) gamma2 = domain.create_region("Gamma2", "nodes in x > %.10f" % (max_x - eps)) field = Field("fu", nm.float64, "vector", omega, space="H1", poly_space_base="lagrange", approx_order=2) u = FieldVariable("u", "unknown", field, mesh.dim) v = FieldVariable("v", "test", field, mesh.dim, primary_var_name="u") m = Material("m", lam=1.0, mu=1.0) f = Material("f", val=[[0.02], [0.01]]) integral = Integral("i", order=3) t1 = Term.new("dw_lin_elastic_iso(m.lam, m.mu, v, u)", integral, omega, m=m, v=v, u=u) t2 = Term.new("dw_volume_lvf(f.val, v)", integral, omega, f=f, v=v) eq = Equation("balance", t1 + t2) eqs = Equations([eq]) fix_u = EssentialBC("fix_u", gamma1, {"u.all": 0.0}) bc_fun = Function("shift_u_fun", shift_u_fun, extra_args={"shift": 0.01}) shift_u = EssentialBC("shift_u", gamma2, {"u.0": bc_fun}) ls = ScipyDirect({}) nls_status = IndexedStruct() nls = Newton({}, lin_solver=ls, status=nls_status) pb = ProblemDefinition("elasticity", equations=eqs, nls=nls, ls=ls) pb.save_regions_as_groups("regions") pb.time_update(ebcs=Conditions([fix_u, shift_u])) vec = pb.solve() print nls_status pb.save_state("linear_elasticity.vtk", vec) if options.show: view = Viewer("linear_elasticity.vtk") view(vector_mode="warp_norm", rel_scaling=2, is_scalar_bar=True, is_wireframe=True)
def make_h1_projection_data(target, eval_data): """ Project scalar data given by a material-like `eval_data()` function to a scalar `target` field variable using the :math:`H^1` dot product. """ order = target.field.approx_order * 2 integral = Integral('i', order=order) un = target.name v = FieldVariable('v', 'test', target.field, 1, primary_var_name=un) lhs1 = Term.new('dw_volume_dot(v, %s)' % un, integral, target.field.region, v=v, **{un : target}) lhs2 = Term.new('dw_laplace(v, %s)' % un, integral, target.field.region, v=v, **{un : target}) def _eval_data(ts, coors, mode, **kwargs): if mode == 'qp': val = eval_data(ts, coors, mode, 'val', **kwargs) gval = eval_data(ts, coors, mode, 'grad', **kwargs) return {'val' : val, 'gval' : gval} m = Material('m', function=_eval_data) rhs1 = Term.new('dw_volume_lvf(m.val, v)', integral, target.field.region, m=m, v=v) rhs2 = Term.new('dw_diffusion_r(m.gval, v)', integral, target.field.region, m=m, v=v) eq = Equation('projection', lhs1 + lhs2 - rhs1 - rhs2) eqs = Equations([eq]) ls = ScipyDirect({}) nls_status = IndexedStruct() nls = Newton({}, lin_solver=ls, status=nls_status) pb = ProblemDefinition('aux', equations=eqs, nls=nls, ls=ls) pb.time_update() # This sets the target variable with the projection solution. pb.solve() if nls_status.condition != 0: output('H1 projection: solver did not converge!')
def make_l2_projection(target, source): """ Project `source` field variable to `target` field variable using :math:`L^2` dot product. """ order = target.field.get_true_order()**2 integral = Integral('i', order=order) un = target.name v = FieldVariable('v', 'test', target.field, 1, primary_var_name=un) lhs = Term.new('dw_mass_scalar(v, %s)' % un, integral, target.field.region, v=v, **{un : target}) def eval_variable(ts, coors, mode, **kwargs): if mode == 'qp': val = source.evaluate_at(coors) val.shape = val.shape + (1,) out = {'val' : val} return out m = Material('m', function=eval_variable) rhs = Term.new('dw_volume_lvf(m.val, v)', integral, target.field.region, m=m, v=v) eq = Equation('projection', lhs - rhs) eqs = Equations([eq]) ls = ScipyDirect({}) nls_status = IndexedStruct() nls = Newton({}, lin_solver=ls, status=nls_status) pb = ProblemDefinition('aux', equations=eqs, nls=nls, ls=ls) pb.time_update() # This sets the target variable with the projection solution. pb.solve() if nls_status.condition != 0: output('L2 projection: solver did not converge!')
def make_l2_projection_data(target, eval_data): """ Project scalar data given by a material-like `eval_data()` function to a scalar `target` field variable using the :math:`L^2` dot product. """ order = target.field.approx_order * 2 integral = Integral("i", order=order) un = target.name v = FieldVariable("v", "test", target.field, primary_var_name=un) lhs = Term.new("dw_volume_dot(v, %s)" % un, integral, target.field.region, v=v, **{un: target}) def _eval_data(ts, coors, mode, **kwargs): if mode == "qp": val = eval_data(ts, coors, mode, **kwargs) return {"val": val} m = Material("m", function=_eval_data) rhs = Term.new("dw_volume_lvf(m.val, v)", integral, target.field.region, m=m, v=v) eq = Equation("projection", lhs - rhs) eqs = Equations([eq]) ls = ScipyDirect({}) nls_status = IndexedStruct() nls = Newton({}, lin_solver=ls, status=nls_status) pb = ProblemDefinition("aux", equations=eqs, nls=nls, ls=ls) pb.time_update() # This sets the target variable with the projection solution. pb.solve() if nls_status.condition != 0: output("L2 projection: solver did not converge!")
term = Term.new('dw_laplace(s, t)', integral, omega, s=s, t=t) eq = Equation('temperature', term) eqs = Equations([eq]) t_left = EssentialBC('t_left', left, {'t.0' : 10.0}) t_right = EssentialBC('t_right', right, {'t.0' : 30.0}) ls = ScipyDirect({}) nls = Newton({}, lin_solver=ls) pb = ProblemDefinition('temperature', equations=eqs, nls=nls, ls=ls) pb.time_update(ebcs=Conditions([t_left, t_right])) temperature = pb.solve() out = temperature.create_output_dict() field_u = Field.from_args('displacement', np.float64, 'vector', omega, 1) u = FieldVariable('u', 'unknown', field_u, mesh.dim) v = FieldVariable('v', 'test', field_u, mesh.dim, primary_var_name='u') lam = 10.0 # Lame parameters. mu = 5.0 te = 0.5 # Thermal expansion coefficient. T0 = 20.0 # Background temperature. eye_sym = np.array([[1], [1], [0]],
def main(): from sfepy import data_dir parser = OptionParser(usage=usage, version='%prog') parser.add_option('-s', '--show', action="store_true", dest='show', default=False, help=help['show']) options, args = parser.parse_args() mesh = Mesh.from_file(data_dir + '/meshes/2d/rectangle_tri.mesh') domain = Domain('domain', mesh) min_x, max_x = domain.get_mesh_bounding_box()[:, 0] eps = 1e-8 * (max_x - min_x) omega = domain.create_region('Omega', 'all') gamma1 = domain.create_region('Gamma1', 'nodes in x < %.10f' % (min_x + eps)) gamma2 = domain.create_region('Gamma2', 'nodes in x > %.10f' % (max_x - eps)) field = H1NodalVolumeField('fu', nm.float64, 'vector', omega, approx_order=2) u = FieldVariable('u', 'unknown', field, mesh.dim) v = FieldVariable('v', 'test', field, mesh.dim, primary_var_name='u') m = Material('m', lam=1.0, mu=1.0) f = Material('f', val=[[0.02], [0.01]]) integral = Integral('i', order=3) t1 = Term.new('dw_lin_elastic_iso(m.lam, m.mu, v, u)', integral, omega, m=m, v=v, u=u) t2 = Term.new('dw_volume_lvf(f.val, v)', integral, omega, f=f, v=v) eq = Equation('balance', t1 + t2) eqs = Equations([eq]) fix_u = EssentialBC('fix_u', gamma1, {'u.all': 0.0}) bc_fun = Function('shift_u_fun', shift_u_fun, extra_args={'shift': 0.01}) shift_u = EssentialBC('shift_u', gamma2, {'u.0': bc_fun}) ls = ScipyDirect({}) nls_status = IndexedStruct() nls = Newton({}, lin_solver=ls, status=nls_status) pb = ProblemDefinition('elasticity', equations=eqs, nls=nls, ls=ls) pb.save_regions_as_groups('regions') pb.time_update(ebcs=Conditions([fix_u, shift_u])) vec = pb.solve() print nls_status pb.save_state('linear_elasticity.vtk', vec) if options.show: view = Viewer('linear_elasticity.vtk') view(vector_mode='warp_norm', rel_scaling=2, is_scalar_bar=True, is_wireframe=True)
def main(): from sfepy import data_dir parser = OptionParser(usage=usage, version='%prog') parser.add_option('-s', '--show', action="store_true", dest='show', default=False, help=help['show']) options, args = parser.parse_args() mesh = Mesh.from_file(data_dir + '/meshes/2d/rectangle_tri.mesh') domain = Domain('domain', mesh) min_x, max_x = domain.get_mesh_bounding_box()[:,0] eps = 1e-8 * (max_x - min_x) omega = domain.create_region('Omega', 'all') gamma1 = domain.create_region('Gamma1', 'nodes in x < %.10f' % (min_x + eps)) gamma2 = domain.create_region('Gamma2', 'nodes in x > %.10f' % (max_x - eps)) field = Field('fu', nm.float64, 'vector', omega, space='H1', poly_space_base='lagrange', approx_order=2) u = FieldVariable('u', 'unknown', field, mesh.dim) v = FieldVariable('v', 'test', field, mesh.dim, primary_var_name='u') m = Material('m', lam=1.0, mu=1.0) f = Material('f', val=[[0.02], [0.01]]) integral = Integral('i', order=3) t1 = Term.new('dw_lin_elastic_iso(m.lam, m.mu, v, u)', integral, omega, m=m, v=v, u=u) t2 = Term.new('dw_volume_lvf(f.val, v)', integral, omega, f=f, v=v) eq = Equation('balance', t1 + t2) eqs = Equations([eq]) fix_u = EssentialBC('fix_u', gamma1, {'u.all' : 0.0}) bc_fun = Function('shift_u_fun', shift_u_fun, extra_args={'shift' : 0.01}) shift_u = EssentialBC('shift_u', gamma2, {'u.0' : bc_fun}) ls = ScipyDirect({}) nls_status = IndexedStruct() nls = Newton({}, lin_solver=ls, status=nls_status) pb = ProblemDefinition('elasticity', equations=eqs, nls=nls, ls=ls) pb.save_regions_as_groups('regions') pb.time_update(ebcs=Conditions([fix_u, shift_u])) vec = pb.solve() print nls_status pb.save_state('linear_elasticity.vtk', vec) if options.show: view = Viewer('linear_elasticity.vtk') view(vector_mode='warp_norm', rel_scaling=2, is_scalar_bar=True, is_wireframe=True)