def _create_penalty_objects(self): # using this function se we don't need to add anything extra to the stokeSLE struct velocityField = self._velocityField pressureField = self._pressureField stokesSLE = self._stokesSLE # create junk force vectors -- we provide no assembly terms for these so they are 0 vectors. self._vmfvector = sle.AssembledVector(velocityField, stokesSLE._eqNums[velocityField]) self._junkfvector = sle.AssembledVector( pressureField, stokesSLE._eqNums[pressureField]) # and matrices self._vmmatrix = sle.AssembledMatrix(stokesSLE._velocitySol, stokesSLE._velocitySol, rhs=self._vmfvector) self._mmatrix = sle.AssembledMatrix(stokesSLE._pressureSol, stokesSLE._pressureSol, rhs=self._junkfvector) # create assembly terms self._pressMassMatTerm = sle.MatrixAssemblyTerm_NA__NB__Fn( integrationSwarm=uw.swarm.GaussIntegrationSwarm( velocityField.mesh), fn=1.0, assembledObject=self._mmatrix, mesh=velocityField._mesh) # attach terms to live solver struct self._cself.vmForceVec = self._vmfvector._cself self._cself.vmStiffMat = self._vmmatrix._cself self._cself.jForceVec = self._junkfvector._cself self._cself.mStiffMat = self._mmatrix._cself
def __init__(self, velocityField, pressureField, fn_viscosity, fn_bodyforce=None, fn_one_on_lambda=None, fn_lambda=None, fn_source=None, voronoi_swarm=None, conditions=[], _removeBCs=True, _fn_viscosity2=None, _fn_director=None, fn_stresshistory=None, _fn_stresshistory=None, **kwargs): # DEPRECATION ERROR if fn_lambda != None: raise TypeError( "The parameter 'fn_lambda' has been deprecated. It has been replaced by 'fn_one_on_lambda', a simpler input parameter." ) if _fn_stresshistory: raise TypeError( "The parameter '_fn_stresshistory' has been updated to 'fn_stresshistory'." ) if not isinstance(velocityField, uw.mesh.MeshVariable): raise TypeError( "Provided 'velocityField' must be of 'MeshVariable' class.") if velocityField.nodeDofCount != velocityField.mesh.dim: raise ValueError( "Provided 'velocityField' must be a vector field of same dimensionality as its mesh." ) self._velocityField = velocityField if not isinstance(pressureField, uw.mesh.MeshVariable): raise TypeError( "Provided 'pressureField' must be of 'MeshVariable' class.") if pressureField.nodeDofCount != 1: raise ValueError( "Provided 'pressureField' must be a scalar field (ie pressureField.nodeDofCount==1)." ) self._pressureField = pressureField _fn_viscosity = uw.function.Function.convert(fn_viscosity) if not isinstance(_fn_viscosity, uw.function.Function): raise TypeError( "Provided 'fn_viscosity' must be of or convertible to 'Function' class." ) if _fn_viscosity2: _fn_viscosity2 = uw.function.Function.convert(_fn_viscosity2) if not isinstance(_fn_viscosity2, uw.function.Function): raise TypeError( "Provided 'fn_viscosity2' must be of or convertible to 'Function' class." ) if not isinstance(_removeBCs, bool): raise TypeError("Provided '_removeBCs' must be of type bool.") self._removeBCs = _removeBCs if _fn_director: _fn_director = uw.function.Function.convert(_fn_director) if not isinstance(_fn_director, uw.function.Function): raise TypeError( "Provided 'fn_director' must be of or convertible to 'Function' class." ) if fn_stresshistory: fn_stresshistory = uw.function.Function.convert(fn_stresshistory) if not isinstance(fn_stresshistory, uw.function.Function): raise TypeError( "Provided 'fn_stresshistory' must be of or convertible to 'Function' class." ) self._fn_minus_one_on_lambda = None if fn_one_on_lambda != None: self._fn_minus_one_on_lambda = uw.function.Function.convert( -1.0 * fn_one_on_lambda) if not isinstance(self._fn_minus_one_on_lambda, uw.function.Function): raise ValueError( "Provided 'fn_minus_one_on_lambda' must be of, or convertible to, the 'Function' class." ) if fn_source != None: self._fn_source = uw.function.Function.convert(fn_source) if not isinstance(self._fn_source, uw.function.Function): raise ValueError( "Provided 'fn_source' must be of, or convertible to, the 'Function' class." ) if not fn_bodyforce: if velocityField.mesh.dim == 2: fn_bodyforce = (0., 0.) else: fn_bodyforce = (0., 0., 0.) _fn_bodyforce = uw.function.Function.convert(fn_bodyforce) if voronoi_swarm and not isinstance(voronoi_swarm, uw.swarm.Swarm): raise TypeError( "Provided 'voronoi_swarm' must be of 'Swarm' class.") self._swarm = voronoi_swarm if voronoi_swarm and velocityField.mesh.elementType == 'Q2': import warnings warnings.warn( "Voronoi integration may yield unsatisfactory results for Q2 mesh." ) mesh = velocityField.mesh if not isinstance(conditions, (list, tuple)): conditionslist = [] conditionslist.append(conditions) conditions = conditionslist for cond in conditions: # set the bcs on here if not isinstance(cond, uw.conditions.SystemCondition): raise TypeError( "Provided 'conditions' must be 'SystemCondition' objects.") elif type(cond) == uw.conditions.DirichletCondition: if cond.variable == self._velocityField: libUnderworld.StgFEM.FeVariable_SetBC( self._velocityField._cself, cond._cself) elif cond.variable == self._pressureField: libUnderworld.StgFEM.FeVariable_SetBC( self._pressureField._cself, cond._cself) else: raise ValueError( "Provided condition does not appear to correspond to the system unknowns." ) self._conditions = conditions self._eqNums = dict() self._eqNums[velocityField] = sle.EqNumber(self._velocityField, self._removeBCs) self._eqNums[pressureField] = sle.EqNumber(self._pressureField, self._removeBCs) # create solutions vectors and load fevariable values onto them for best first guess self._velocitySol = sle.SolutionVector(velocityField, self._eqNums[velocityField]) self._pressureSol = sle.SolutionVector(pressureField, self._eqNums[pressureField]) libUnderworld.StgFEM.SolutionVector_LoadCurrentFeVariableValuesOntoVector( self._velocitySol._cself) libUnderworld.StgFEM.SolutionVector_LoadCurrentFeVariableValuesOntoVector( self._pressureSol._cself) # create force vectors self._fvector = sle.AssembledVector(velocityField, self._eqNums[velocityField]) self._hvector = sle.AssembledVector(pressureField, self._eqNums[pressureField]) # and matrices self._kmatrix = sle.AssembledMatrix(self._velocitySol, self._velocitySol, rhs=self._fvector) self._gmatrix = sle.AssembledMatrix(self._velocitySol, self._pressureSol, rhs=self._fvector, rhs_T=self._hvector) self._preconditioner = sle.AssembledMatrix(self._pressureSol, self._pressureSol, rhs=self._hvector) # create assembly terms which always use gauss integration gaussSwarm = uw.swarm.GaussIntegrationSwarm(self._velocityField.mesh) self._gradStiffMatTerm = sle.GradientStiffnessMatrixTerm( integrationSwarm=gaussSwarm, assembledObject=self._gmatrix) self._preCondMatTerm = sle.PreconditionerMatrixTerm( integrationSwarm=gaussSwarm, assembledObject=self._preconditioner) # for the following terms, we will use voronoi if that has been requested # by the user, else use gauss again. intswarm = gaussSwarm if self._swarm: intswarm = self._swarm._voronoi_swarm # need to ensure voronoi is populated now, as assembly terms will call # initial test functions which may require a valid voronoi swarm self._swarm._voronoi_swarm.repopulate() self._constitMatTerm = sle.ConstitutiveMatrixTerm( integrationSwarm=intswarm, assembledObject=self._kmatrix, fn_visc1=_fn_viscosity, fn_visc2=_fn_viscosity2, fn_director=_fn_director) self._forceVecTerm = sle.VectorAssemblyTerm_NA__Fn( integrationSwarm=intswarm, assembledObject=self._fvector, fn=_fn_bodyforce) if fn_source: self._cforceVecTerm = sle.VectorAssemblyTerm_NA__Fn( integrationSwarm=intswarm, assembledObject=self._hvector, fn=self.fn_source) for cond in self._conditions: if isinstance(cond, uw.conditions.NeumannCondition): #NOTE many NeumannConditions can be used but the _sufaceFluxTerm only records the last self._surfaceFluxTerm = sle.VectorSurfaceAssemblyTerm_NA__Fn__ni( assembledObject=self._fvector, surfaceGaussPoints= 3, # increase to resolve stress bc fluctuations nbc=cond) if self._fn_minus_one_on_lambda != None: # add matrix and associated assembly term for compressible stokes formulation # a mass matrix goes into the lower right block of the stokes system coeff matrix self._mmatrix = sle.AssembledMatrix(self._pressureSol, self._pressureSol, rhs=self._hvector) # -1. as per Hughes, The Finite Element Method, 1987, Table 4.3.1, [M] self._compressibleTerm = sle.MatrixAssemblyTerm_NA__NB__Fn( integrationSwarm=intswarm, assembledObject=self._mmatrix, mesh=self._velocityField.mesh, fn=self._fn_minus_one_on_lambda) if fn_stresshistory != None: self._NA_j__Fn_ijTerm = sle.VectorAssemblyTerm_NA_j__Fn_ij( integrationSwarm=intswarm, assembledObject=self._fvector, fn=fn_stresshistory) super(Stokes, self).__init__(**kwargs)
def __init__(self, velocityField, pressureField, fn_viscosity, fn_bodyforce=None, fn_lambda=None, voronoi_swarm=None, conditions=[], _removeBCs=True, _fn_viscosity2=None, _fn_director=None, _fn_stresshistory=None, **kwargs): if not isinstance(velocityField, uw.mesh.MeshVariable): raise TypeError( "Provided 'velocityField' must be of 'MeshVariable' class.") if velocityField.nodeDofCount != velocityField.mesh.dim: raise ValueError( "Provided 'velocityField' must be a vector field of same dimensionality as its mesh." ) self._velocityField = velocityField if not isinstance(pressureField, uw.mesh.MeshVariable): raise TypeError( "Provided 'pressureField' must be of 'MeshVariable' class.") if pressureField.nodeDofCount != 1: raise ValueError( "Provided 'pressureField' must be a scalar field (ie pressureField.nodeDofCount==1)." ) self._pressureField = pressureField _fn_viscosity = uw.function.Function.convert(fn_viscosity) if not isinstance(_fn_viscosity, uw.function.Function): raise TypeError( "Provided 'fn_viscosity' must be of or convertible to 'Function' class." ) if _fn_viscosity2: _fn_viscosity2 = uw.function.Function.convert(_fn_viscosity2) if not isinstance(_fn_viscosity2, uw.function.Function): raise TypeError( "Provided 'fn_viscosity2' must be of or convertible to 'Function' class." ) if not isinstance(_removeBCs, bool): raise TypeError("Provided '_removeBCs' must be of type bool.") self._removeBCs = _removeBCs if _fn_director: _fn_director = uw.function.Function.convert(_fn_director) if not isinstance(_fn_director, uw.function.Function): raise TypeError( "Provided 'fn_director' must be of or convertible to 'Function' class." ) if _fn_stresshistory: _fn_stresshistory = uw.function.Function.convert(_fn_stresshistory) if not isinstance(_fn_stresshistory, uw.function.Function): raise TypeError( "Provided '_fn_stresshistory' must be of or convertible to 'Function' class." ) if fn_lambda != None: fn_lambda = uw.function.Function.convert(fn_lambda) if not isinstance(fn_lambda, uw.function.Function): raise ValueError( "Provided 'fn_lambda' must be of, or convertible to, the 'Function' class." ) if not fn_bodyforce: if velocityField.mesh.dim == 2: fn_bodyforce = (0., 0.) else: fn_bodyforce = (0., 0., 0.) _fn_bodyforce = uw.function.Function.convert(fn_bodyforce) if voronoi_swarm and not isinstance(voronoi_swarm, uw.swarm.Swarm): raise TypeError( "Provided 'voronoi_swarm' must be of 'Swarm' class.") self._swarm = voronoi_swarm if voronoi_swarm and velocityField.mesh.elementType == 'Q2': import warnings warnings.warn( "Voronoi integration may yield unsatisfactory results for Q2 mesh." ) mesh = velocityField.mesh if not isinstance(conditions, (list, tuple)): conditionslist = [] conditionslist.append(conditions) conditions = conditionslist for cond in conditions: # set the bcs on here if not isinstance(cond, uw.conditions.SystemCondition): raise TypeError( "Provided 'conditions' must be 'SystemCondition' objects.") elif type(cond) == uw.conditions.DirichletCondition: if cond.variable == self._velocityField: libUnderworld.StgFEM.FeVariable_SetBC( self._velocityField._cself, cond._cself) if cond.variable == self._pressureField: libUnderworld.StgFEM.FeVariable_SetBC( self._pressureField._cself, cond._cself) self._conditions = conditions self._eqNums = dict() self._eqNums[velocityField] = sle.EqNumber(self._velocityField, self._removeBCs) self._eqNums[pressureField] = sle.EqNumber(self._pressureField, self._removeBCs) # create solutions vectors and load fevariable values onto them for best first guess self._velocitySol = sle.SolutionVector(velocityField, self._eqNums[velocityField]) self._pressureSol = sle.SolutionVector(pressureField, self._eqNums[pressureField]) libUnderworld.StgFEM.SolutionVector_LoadCurrentFeVariableValuesOntoVector( self._velocitySol._cself) libUnderworld.StgFEM.SolutionVector_LoadCurrentFeVariableValuesOntoVector( self._pressureSol._cself) # create force vectors self._fvector = sle.AssembledVector(velocityField, self._eqNums[velocityField]) self._hvector = sle.AssembledVector(pressureField, self._eqNums[pressureField]) # and matrices self._kmatrix = sle.AssembledMatrix(self._velocitySol, self._velocitySol, rhs=self._fvector) self._gmatrix = sle.AssembledMatrix(self._velocitySol, self._pressureSol, rhs=self._fvector, rhs_T=self._hvector) self._preconditioner = sle.AssembledMatrix(self._pressureSol, self._pressureSol, rhs=self._hvector) if fn_lambda != None: self._mmatrix = sle.AssembledMatrix(self._pressureSol, self._pressureSol, rhs=self._hvector) # create assembly terms which always use gauss integration gaussSwarm = uw.swarm.GaussIntegrationSwarm(self._velocityField.mesh) self._gradStiffMatTerm = sle.GradientStiffnessMatrixTerm( integrationSwarm=gaussSwarm, assembledObject=self._gmatrix) self._preCondMatTerm = sle.PreconditionerMatrixTerm( integrationSwarm=gaussSwarm, assembledObject=self._preconditioner) # for the following terms, we will use voronoi if that has been requested # by the user, else use gauss again. intswarm = gaussSwarm if self._swarm: intswarm = self._swarm._voronoi_swarm # need to ensure voronoi is populated now, as assembly terms will call # initial test functions which may require a valid voronoi swarm self._swarm._voronoi_swarm.repopulate() self._constitMatTerm = sle.ConstitutiveMatrixTerm( integrationSwarm=intswarm, assembledObject=self._kmatrix, fn_visc1=_fn_viscosity, fn_visc2=_fn_viscosity2, fn_director=_fn_director) self._forceVecTerm = sle.VectorAssemblyTerm_NA__Fn( integrationSwarm=intswarm, assembledObject=self._fvector, fn=_fn_bodyforce) for cond in self._conditions: if isinstance(cond, uw.conditions.NeumannCondition): #NOTE many NeumannConditions can be used but the _sufaceFluxTerm only records the last self._surfaceFluxTerm = sle.VectorSurfaceAssemblyTerm_NA__Fn__ni( assembledObject=self._fvector, surfaceGaussPoints= 3, # increase to resolve stress bc fluctuations nbc=cond) if fn_lambda != None: # some logic for constructing the lower-right [2,2] matrix in the stokes system # [M] = [Na * 1.0/fn_lambda * Nb], where in our formulation Na and Nb are the pressure shape functions. # see 4.3.21 of Hughes, Linear static and dynamic finite element analysis # If fn_lambda is negligable, ie <1.0e-8, then we set the entry to 0.0, ie, incompressible # otherwise we provide 1.0/lambda to [M] logicFn = uw.function.branching.conditional([(fn_lambda > 1.0e-8, 1.0 / fn_lambda), (True, 0.)]) self._compressibleTerm = sle.MatrixAssemblyTerm_NA__NB__Fn( integrationSwarm=intswarm, assembledObject=self._mmatrix, mesh=self._velocityField.mesh, fn=logicFn) if _fn_stresshistory != None: self._vepTerm = sle.VectorAssemblyTerm_VEP__Fn( integrationSwarm=intswarm, assembledObject=self._fvector, fn=_fn_stresshistory) super(Stokes, self).__init__(**kwargs)