def evaluate_constraint(self, mesh, element, eqn, masterpos, smallsystem): # TODO: Test for the right equation. # Compute the stress -- start with the strain. strain = symmmatrix.SymmMatrix3 ep = field.getField('Plastic') efi = element.funcnode_iterator() while not efi.end(): coef = efi.shapefunction(masterpos) ep_output = ep.output(mesh, efi.funcnode()) strain -= ep_output.valuePtr()*coef efi += 1 disp = field.getField('Displacement') efi = element.funcnode_iterator() while not efi.end(): ddx = efi.dshapefunction(0,masterpos) ddy = efi.dshapefunction(1,masterpos) uxx = disp(efi,0).value()*ddx uxy = disp(efi,0).value()*ddy uyx = disp(efi,1).value()*ddx uyy = disp(efi,1).value()*ddy strain.set(0,0,strain.get(0,0)+uxx) strain.set(0,1,strain.get(0,1)+(0.5*uxy+uyx)) strain.set(1,1,strain.get(1,1)+uyy) try: ops = field.getField('Displacement_z') except: pass else: efi = element.funcnode_iterator() while not efi.end(): uxz = ops(efi,0).value() uyz = ops(efi,1).value() uyz = ops(efi,2).value() strain.set(0,2,strain.get(0,2)+uxz) strain.set(1,2,strain.get(1,2)+uyz) strain.set(2,2,strain.get(2,2)+uzz) # At this point, "strain" contains the elastic strain, # geometric minus plastic. modulus = self.elasticity.cijkl(mesh, element, masterpos) ij = fieldindex.SymTensorIterator() while (not ij.end()): stressij = 0.0 kl = ep_val.getIterator() while (not kl.end()): idx = kl.integer cijkl = modulus[ij.integer(),kl.integer()] if idx<3: stressij += cijkl*strain[kl] else: stressij += 2.0*cijkl*strain[kl] kl.next() # Accumulate the stress somewhere. ij.next()
def heatEqnEngine(self, soln_no, z_soln_no, test_solver): soln_func = self.heat_solns[soln_no]["Solution"] z_soln_func = self.heat_solns[z_soln_no]["Solution"] OOF.Subproblem.Set_Solver( subproblem='microstructure:skeleton:mesh:default', solver_mode=AdvancedSolverMode( time_stepper=StaticDriver(), nonlinear_solver=test_solver, symmetric_solver=ConjugateGradient( preconditioner=ILUPreconditioner(), tolerance=1e-13, max_iterations=1000), asymmetric_solver=BiConjugateGradient( preconditioner=ILUPreconditioner(), tolerance=1e-13, max_iterations=1000))) OOF.Mesh.Set_Field_Initializer( mesh='microstructure:skeleton:mesh', field=Temperature, initializer=ConstScalarFieldInit(value=0.0)) OOF.Mesh.Set_Field_Initializer( mesh='microstructure:skeleton:mesh', field=Temperature_z, initializer=ConstScalarFieldInit(value=0.0)) OOF.Mesh.Apply_Field_Initializers( mesh='microstructure:skeleton:mesh') OOF.Mesh.Solve( mesh='microstructure:skeleton:mesh', endtime=self.time) from ooflib.engine import mesh from ooflib.SWIG.engine import field mesh_obj = mesh.meshes["microstructure:skeleton:mesh"].getObject() # first compute the numerical error in the computed temperature field field_ptr = field.getField( "Temperature" ) L2_error = computeScalarErrorL2( soln_func, mesh_obj, field_ptr, self.numX, self.numY, time=self.time ) print "Temperature L2 error: ", L2_error self.assert_( L2_error < 1.e-2 ) # now compute the numerical error in the computed temperature_z field field_ptr = field.getField( "Temperature_z" ) L2_error = computeScalarErrorL2( z_soln_func, mesh_obj, field_ptr, self.numX, self.numY, time=self.time ) print "Temperture_z L2 error: ", L2_error self.assert_( L2_error < 1.e-2 )
def elasticityEqnEngine(self, soln_no, z_soln_no, test_solver): soln_func = self.elasticity_solns[soln_no]["Solution"] z_soln_func = self.elasticity_z_solns[z_soln_no]["Solution"] OOF.Subproblem.Set_Solver( subproblem='microstructure:skeleton:mesh:default', solver_mode=AdvancedSolverMode( time_stepper=StaticDriver(), nonlinear_solver=test_solver, symmetric_solver=ConjugateGradient( preconditioner=ILUPreconditioner(), tolerance=1e-13, max_iterations=1000), asymmetric_solver=BiConjugateGradient( preconditioner=ILUPreconditioner(), tolerance=1e-13, max_iterations=1000))) OOF.Mesh.Set_Field_Initializer( mesh='microstructure:skeleton:mesh', field=Displacement, initializer=ConstTwoVectorFieldInit(cx=0.0,cy=0.0)) OOF.Mesh.Apply_Field_Initializers( mesh='microstructure:skeleton:mesh') OOF.Mesh.Set_Field_Initializer( mesh='microstructure:skeleton:mesh', field=Displacement_z, initializer=ConstThreeVectorFieldInit(cx=0.0,cy=0.0,cz=0.0)) OOF.Mesh.Solve( mesh='microstructure:skeleton:mesh', endtime=self.time) from ooflib.engine import mesh from ooflib.SWIG.engine import field mesh_obj = mesh.meshes["microstructure:skeleton:mesh"].getObject() field_ptr = field.getField( "Displacement" ) L2_error = computeVector2DErrorL2( soln_func, mesh_obj, field_ptr, self.numX, self.numY, time=self.time ) print "Displacement L2 error: ", L2_error self.assert_( L2_error < 6.e-2 ) field_ptr = field.getField( "Displacement_z" ) L2_error = computeVector3DErrorL2( z_soln_func, mesh_obj, field_ptr, self.numX, self.numY, time=self.time ) print "Displacement_z L2 error: ", L2_error self.assert_( L2_error < 1.1e-1 )
def flux_offset(self, mesh, element, flux, masterpos, time, smallsystem): # Retrieve the plastic strain field and evaluate it at this # masterposition. ep_val = symmmatrix.SymmMatrix3() ep = field.getField('Plastic') efi = element.funcnode_iterator() while not efi.end(): coef = efi.shapefunction(masterpos) ep_output = ep.output(mesh, efi.funcnode()) ep_val += ep_output.valuePtr()*coef efi += 1 # Now ep_val contains the plastic strain at this gausspoint. # We can do the arithmetic. Note that this assumes the same # gausspoint set from one iteration to the next, which will in # general be a bad assumption. modulus = self.elasticity.cijkl(mesh, element, masterpos) ij = fieldindex.SymTensorIterator() while (not ij.end()): offset = 0.0 kl = ep_val.getIterator() while (not kl.end()): idx = kl.integer() cijkl = modulus[ij.integer(),kl.integer()] if idx<3: offset += cijkl*ep_val[kl] else: offset += 2.0*cijkl*ep_val[kl] kl.next() smallsystem.add_offset_element(ij, offset) ij.next()
def initialize_fields(self, mesh): if config.dimension() == 2: initializer = fieldinit.ConstTwoVectorFieldInit(cx=0.0,cy=0.0) elif config.dimension() == 3: initializer = fieldinit.ConstTwoVectorFieldInit(cx=0.0,cy=0.0,cz=0.0) meshmenu.initField(self, self.meshname, field.getField('Displacement'), initializer) meshmenu.applyFieldInits(self, self.meshname)
def define_fields(self, meshctxt): femesh = meshctxt.femesh() subp = meshctxt.get_default_subproblem().getObject() displacement = field.getField('Displacement') subp.define_field(displacement) subp.activate_field(displacement) if config.dimension() == 2: meshctxt.set_in_plane_field(displacement, True)
def elasticityEqnEngine(self,test_no,soln_no,test_solver,test_stepper): soln_func = self.elasticity_solns[soln_no]["Solution"] init_func = self.elasticity_solns[soln_no]["InitialValue"] init_deriv_func = self.elasticity_solns[soln_no]["InitialTimeDeriv"] OOF.Subproblem.Set_Solver( subproblem='microstructure:skeleton:mesh:default', solver_mode=AdvancedSolverMode( time_stepper=UniformDriver( stepsize=self.timestep, stepper = test_stepper ), nonlinear_solver = test_solver, symmetric_solver=ConjugateGradient( preconditioner=ILUPreconditioner(), tolerance=1e-13, max_iterations=1000), asymmetric_solver=BiConjugateGradient( preconditioner=ILUPreconditioner(), tolerance=1e-13, max_iterations=1000))) OOF.Mesh.Set_Field_Initializer( mesh='microstructure:skeleton:mesh', field=Displacement, initializer=FuncTwoVectorFieldInit( fx = init_func[0], fy = init_func[1] )) OOF.Mesh.Set_Field_Initializer( mesh='microstructure:skeleton:mesh', field=Displacement_t, initializer=FuncTwoVectorFieldInit( fx = init_deriv_func[0], fy = init_deriv_func[1] )) OOF.Mesh.Apply_Field_Initializers_at_Time( mesh='microstructure:skeleton:mesh', time=0.0) OOF.Mesh.Solve( mesh='microstructure:skeleton:mesh', endtime=self.time) from ooflib.engine import mesh from ooflib.SWIG.engine import field mesh_obj = mesh.meshes["microstructure:skeleton:mesh"].getObject() field_ptr = field.getField( "Displacement" ) L2_error = computeVector2DErrorL2( soln_func, mesh_obj, field_ptr, self.numX, self.numY, time=self.time) print "L2 error: ", L2_error self.assert_( L2_error < 1.e-1 )
def set_boundary_conditions(self, mesh): ## here, mesh is a Mesh (Who) object displacement = field.getField('Displacement') if config.dimension() == 2: ## left boundary self.leftBoundaryCondition = \ bdycondition.DirichletBC(displacement, 'x', equation.getEquation('Force_Balance'), 'x', profile.ConstantProfile(0), 'left' ) self.leftBoundaryCondition.add_to_mesh('left', mesh.path()) ## right boundary self.rightBoundaryCondition = \ bdycondition.DirichletBC(displacement, 'x', equation.getEquation('Force_Balance'), 'x', profile.ConstantProfile(0), 'right' ) self.rightBoundaryCondition.add_to_mesh('right', mesh.path()) ## top boundary self.topBoundaryCondition = \ bdycondition.DirichletBC(displacement, 'y', equation.getEquation('Force_Balance'), 'y', profile.ConstantProfile(0), 'top' ) self.topBoundaryCondition.add_to_mesh('top', mesh.path()) ## bottom boundary self.bottomBoundaryCondition = \ bdycondition.DirichletBC(displacement, 'y', equation.getEquation('Force_Balance'), 'y', profile.ConstantProfile(0), 'bottom' ) self.bottomBoundaryCondition.add_to_mesh('bottom', mesh.path())
def update_node_positions(self, skeleton, mesh): skeleton.timestamp.increment() ## mesh is a Mesh Who object femesh = mesh.getObject() displacement = field.getField('Displacement') for node in skeleton.nodes: #Interface branch ## realnode = femesh.getNode(node.meshindex) #TODO: Do we have to update all the realmesh nodes #that correspond to node? skelel = node.neighborElements()[0] realel = femesh.getElement(skelel.meshindex) realnode = realel.getCornerNode(skelel.getNodeIndexIntoList(node)) dx = displacement.value(femesh, realnode, 0) dy = displacement.value(femesh, realnode, 1) if config.dimension() == 2: skeleton.moveNodeBy(node, primitives.Point(dx, dy)) elif config.dimension() == 3: dz = displacement.value(femesh, realnode, 2) skeleton.moveNodeBy(node, primitives.Point(dx, dy, dz))
def Small(self): OOF.Microstructure.New( name='microstructure', width=1.0, height=1.0, width_in_pixels=10, height_in_pixels=10) OOF.Material.New( name='material', material_type='bulk') # Reset the default parameter values for isotropic elasticity. # This shouldn't be necessary if earlier tests clean up after # themselves properly. OOF.Property.Parametrize.Mechanical.Elasticity.Isotropic( cijkl=IsotropicRank4TensorCij(c11=1.0,c12=0.5)) OOF.Material.Add_property( name='material', property='Mechanical:Elasticity:Isotropic') OOF.Material.Assign( material='material', microstructure='microstructure', pixels=all) OOF.Skeleton.New( name='skeleton', microstructure='microstructure', x_elements=1, y_elements=1, skeleton_geometry=QuadSkeleton( left_right_periodicity=False,top_bottom_periodicity=False)) OOF.Mesh.New( name='mesh', skeleton='microstructure:skeleton', element_types=['D2_2', 'T3_3', 'Q4_4']) OOF.Subproblem.Field.Define( subproblem='microstructure:skeleton:mesh:default', field=Displacement) OOF.Subproblem.Field.Activate( subproblem='microstructure:skeleton:mesh:default', field=Displacement) OOF.Subproblem.Equation.Activate( subproblem='microstructure:skeleton:mesh:default', equation=Force_Balance) OOF.Subproblem.Equation.Activate( subproblem='microstructure:skeleton:mesh:default', equation=Plane_Stress) OOF.Mesh.Boundary_Conditions.New( name='bc', mesh='microstructure:skeleton:mesh', condition=DirichletBC( field=Displacement,field_component='x', equation=Force_Balance,eqn_component='x', profile=ConstantProfile(value=0.0), boundary='left')) OOF.Mesh.Boundary_Conditions.New( name='bc<2>', mesh='microstructure:skeleton:mesh', condition=DirichletBC( field=Displacement,field_component='x', equation=Force_Balance,eqn_component='x', profile=ConstantProfile(value=0.1), boundary='right')) OOF.Mesh.Boundary_Conditions.New( name='bc<3>', mesh='microstructure:skeleton:mesh', condition=DirichletBC( field=Displacement,field_component='y', equation=Force_Balance,eqn_component='y', profile=ConstantProfile(value=0), boundary='left')) OOF.Mesh.Boundary_Conditions.New( name='bc<4>', mesh='microstructure:skeleton:mesh', condition=DirichletBC( field=Displacement,field_component='y', equation=Force_Balance,eqn_component='y', profile=ConstantProfile(value=0), boundary='right')) OOF.Mesh.Set_Field_Initializer( mesh='microstructure:skeleton:mesh', field=Displacement, initializer=ConstTwoVectorFieldInit(cx=0.0,cy=0.0)) OOF.Mesh.Set_Field_Initializer( mesh='microstructure:skeleton:mesh', field=Displacement_z, initializer=ConstThreeVectorFieldInit(cx=0.0,cy=0.0,cz=0.0)) OOF.Mesh.Apply_Field_Initializers_at_Time( mesh='microstructure:skeleton:mesh', time=0.0) OOF.Subproblem.Set_Solver( subproblem='microstructure:skeleton:mesh:default', solver_mode=AdvancedSolverMode( time_stepper=StaticDriver(), nonlinear_solver=Newton( relative_tolerance=1e-08, absolute_tolerance=1.e-13, maximum_iterations=200), symmetric_solver=ConjugateGradient( preconditioner=ILUPreconditioner(), tolerance=1e-13, max_iterations=1000), asymmetric_solver=BiConjugateGradient( preconditioner=ILUPreconditioner(), tolerance=1e-13,max_iterations=1000))) OOF.Mesh.Solve( mesh='microstructure:skeleton:mesh', endtime=0.0) # Check that the average out-of-plane stress is 0.0. OOF.Mesh.Analyze.Average( mesh='microstructure:skeleton:mesh', time=latest, data=getOutput('Flux:Value',flux=Stress), domain=EntireMesh(), sampling=ElementSampleSet(order=automatic), destination=OutputStream(filename='test.dat', mode='w')) self.assert_(file_utils.compare_last( 'test.dat', (0.0, 0.075, 0.025, 0.0, 0.0, 0.0, 0.0))) file_utils.remove('test.dat') # Check that the out-of-plane displacement derivative at the # nodes is correct. u_zz should be -0.05 and the other # components should be 0.0. from ooflib.engine import mesh from ooflib.SWIG.engine import field msh = mesh.meshes["microstructure:skeleton:mesh"] msh_obj = msh.getObject() dispz = field.getField("Displacement_z") for node in msh_obj.funcnode_iterator(): vals = [dispz.value(msh_obj, node, i) for i in range(3)] self.assert_(vals[0] == 0.0 and vals[1] == 0 and math.fabs(vals[2]+0.05) < 1.e-13)
def updateSomething(self, container): debug.mainthreadTest() node = container.object femesh = container.context.femesh() self.index.set_text(`node.index()`) self.type.set_text(node.classname()) if config.dimension() == 2: self.pos.set_text("(%s, %s)" % (node.position().x, node.position().y)) elif config.dimension() == 3: self.pos.set_text("(%s, %s, %s)" % (node.position().x, node.position().y, node.position().z)) fieldnames = node.fieldNames() # Find out which fields are defined at the node nfieldrows = 0 listedfields = [] for fieldname in fieldnames: fld = field.getField(fieldname) zfld = fld.out_of_plane() tfld = fld.time_derivative() nfieldrows += fld.ndof() listedfields.append(fld) if node.hasField(zfld): listedfields.append(zfld) nfieldrows += zfld.ndof() if node.hasField(tfld): listedfields.append(tfld) nfieldrows += tfld.ndof() # Rebuild the table of field values, but only if the fields # have changed. if self.fieldslisted != listedfields: for entry in self.fieldvalEntries.values(): entry.destroy() for widget in self.fieldvalWidgets: widget.destroy() self.fieldvalEntries.clear() self.fieldvalWidgets.clear() self.table.resize(rows=self.baserows + nfieldrows, columns=3) fldrow = self.baserows # starting row for a field for fld in listedfields: sep = gtk.HSeparator() self.fieldvalWidgets.add(sep) self.table.attach(sep, 0,3, fldrow,fldrow+1,xoptions=gtk.FILL) fldrow += 1 label = gtk.Label(fld.name()) label.set_alignment(1.0, 0.5) self.fieldvalWidgets.add(label) self.table.attach(label, 0,1, fldrow,fldrow+fld.ndof(), xoptions=0) fcomp = fld.iterator(planarity.ALL_INDICES) while not fcomp.end(): row = fldrow + fcomp.integer() label = gtk.Label(" " + fcomp.shortrepr()+"=") self.fieldvalWidgets.add(label) self.table.attach(label, 1,2, row,row+1, xoptions=gtk.FILL) e = gtk.Entry() e.set_size_request(10*guitop.top().charsize, -1) e.set_editable(False) self.fieldvalEntries[(fld, fcomp.integer())] = e self.table.attach(e, 2,3, row,row+1, xoptions=gtk.EXPAND|gtk.FILL) fcomp.next() fldrow += fld.ndof() self.table.show_all() self.fieldslisted = listedfields # Fill in the field values in the table. for fld in self.fieldslisted: fcomp = fld.iterator(planarity.ALL_INDICES) while not fcomp.end(): e = self.fieldvalEntries[(fld, fcomp.integer())] e.set_text("%-13.6g"%fld.value(femesh, node, fcomp.integer())) fcomp.next()
from ooflib.common.IO import xmlmenudump from ooflib.engine import problem from ooflib.engine.IO import output from ooflib.engine.IO import outputClones from types import * import string ###################################### # Displacement # Although Displacement is a Field, it's convenient to treat it as a # Point so that it can be added to a position. Displacement = field.getField("Displacement") if config.dimension() == 2: iter = Displacement.iterator(planarity.IN_PLANE) elif config.dimension() == 3: iter = Displacement.iterator(planarity.ALL_INDICES) disp0 = iter.cloneIndex() iter.next() disp1 = iter.cloneIndex() displacementFieldOutput = outputClones.FieldOutput.clone( name="displacement field", params=dict(field=Displacement)) # displacementOutput produces the Displacement Field as a set of Points. def _disp2point(mesh, elements, coords, field):