def test_update_intensity(self, chan): # check that setting surface intensity updates the intensity profile chan.setup() I = chan.intensities.numericValue assert (I == 0).all() chan.update_intensities(100) assert numerix.allclose((100 * chan.attenuation_profile), chan.intensities.numericValue)
def assertArrayWithinTolerance(self, first, second, atol=1e-10, rtol=1e-10, msg=None): """Fail if the two objects are unequal by more than tol. """ if not numerix.allclose(first, second, rtol=rtol, atol=atol): raise self.failureException(msg or '\n%s\nis not\n%s' % (first, second))
def test_seed_normal(self, unit, loc, scale, coeff, error): create = dict(value=3., unit=unit, hasOld=True) name = 'MyVar' seed = dict(profile='normal', params=dict(loc=loc, scale=scale, coeff=coeff)) v = ModelVariable(name=name, create=create, seed=seed) domain = SedimentDBLDomain() v.domain = domain if error: with pytest.raises(error): v.setup() return else: v.setup() from scipy.stats import norm from fipy.tools import numerix C = 1.0 / numerix.sqrt(2 * numerix.pi) # loc and scale should be in units of the domain mesh if hasattr(loc, 'unit'): loc_ = loc.inUnitsOf(domain.depths.unit).value else: loc_ = loc if hasattr(scale, 'unit'): scale_ = scale.inUnitsOf(domain.depths.unit).value else: scale_ = scale if unit: coeff = PF(coeff, unit) normrv = norm(loc=loc_, scale=C**2 * scale_) val = coeff * normrv.pdf(domain.depths) * C * scale_ # from pprint import pprint # pprint(zip(v.var, val)) print(type(val), type(v.var)) if unit: # array comparison between variable & physicalfield is problematic val = val.numericValue assert numerix.allclose(v.var.numericValue, val)
def allclose(self, other, rtol=1.e-5, atol=1.e-8): """ >>> var = Variable((1, 1)) >>> print var.allclose((1, 1)) 1 >>> print var.allclose((1,)) 1 The following test is to check that the system does not run out of memory. >>> from fipy.tools import numerix >>> var = Variable(numerix.ones(10000)) >>> print var.allclose(numerix.zeros(10000)) False """ operatorClass = Variable._OperatorVariableClass(self, baseClass=Variable) return self._BinaryOperatorVariable(lambda a,b: numerix.allclose(a, b, atol=atol, rtol=rtol), other, operatorClass=operatorClass, opShape=(), canInline=False)
def _connectFaces(self, faces0, faces1): """ Merge faces on the same mesh. This is used to create periodic meshes. The first list of faces, `faces1`, will be the faces that are used to add to the matrix diagonals. The faces in `faces2` will not be used. They aren't deleted but their adjacent cells are made to point at `faces1`. The list `faces2` are not altered, they still remain as members of exterior faces. >>> from fipy.meshes.nonUniformGrid2D import NonUniformGrid2D >>> mesh = NonUniformGrid2D(nx = 2, ny = 2, dx = 1., dy = 1.) >>> print((mesh.cellFaceIDs == [[0, 1, 2, 3], ... [7, 8, 10, 11], ... [2, 3, 4, 5], ... [6, 7, 9, 10]]).flatten().all()) # doctest: +PROCESSOR_0 True >>> mesh._connectFaces(numerix.nonzero(mesh.facesLeft), numerix.nonzero(mesh.facesRight)) >>> print((mesh.cellFaceIDs == [[0, 1, 2, 3], ... [7, 6, 10, 9], ... [2, 3, 4, 5], ... [6, 7, 9, 10]]).flatten().all()) # doctest: +PROCESSOR_0 True """ ## check for errors ## check that faces are members of exterior faces from fipy.variables.faceVariable import FaceVariable faces = FaceVariable(mesh=self, value=False) faces[faces0] = True faces[faces1] = True assert (faces | self.exteriorFaces == self.exteriorFaces).all() ## following assert checks number of faces are equal, normals are opposite and areas are the same assert numerix.allclose(numerix.take(self._areaProjections, faces0, axis=1), numerix.take(-self._areaProjections, faces1, axis=1)) ## extract the adjacent cells for both sets of faces faceCellIDs0 = self.faceCellIDs[0] faceCellIDs1 = self.faceCellIDs[1] ## set the new adjacent cells for `faces0` MA.put(faceCellIDs1, faces0, MA.take(faceCellIDs0, faces0)) MA.put(faceCellIDs0, faces0, MA.take(faceCellIDs0, faces1)) self.faceCellIDs[0] = faceCellIDs0 self.faceCellIDs[1] = faceCellIDs1 ## extract the face to cell distances for both sets of faces faceToCellDistances0 = self._faceToCellDistances[0] faceToCellDistances1 = self._faceToCellDistances[1] ## set the new faceToCellDistances for `faces0` MA.put(faceToCellDistances1, faces0, MA.take(faceToCellDistances0, faces0)) MA.put(faceToCellDistances0, faces0, MA.take(faceToCellDistances0, faces1)) self._faceToCellDistances[0] = faceToCellDistances0 self._faceToCellDistances[1] = faceToCellDistances1 ## calculate new cell distances and add them to faces0 numerix.put(self._cellDistances, faces0, MA.take(faceToCellDistances0 + faceToCellDistances1, faces0)) ## change the direction of the face normals for faces0 for dim in range(self.dim): faceNormals = self.faceNormals[dim].copy() numerix.put(faceNormals, faces0, MA.take(faceNormals, faces1)) self.faceNormals[dim] = faceNormals ## Cells that are adjacent to faces1 are changed to point at faces0 ## get the cells adjacent to faces1 faceCellIDs = MA.take(self.faceCellIDs[0], faces1) ## get all the adjacent faces for those particular cells cellFaceIDs = numerix.take(self.cellFaceIDs, faceCellIDs, axis=1) for i in range(cellFaceIDs.shape[0]): ## if the faces is a member of faces1 then change the face to point at ## faces0 cellFaceIDs[i] = MA.where(cellFaceIDs[i] == faces1, faces0, cellFaceIDs[i]) ## add those faces back to the main self.cellFaceIDs numerix.put(self.cellFaceIDs[i], faceCellIDs, cellFaceIDs[i]) ## calculate new topology self._setTopology() ## calculate new geometry self._handleFaceConnection() self.scale = self.scale['length']
def runLeveler(kLeveler=0.018, bulkLevelerConcentration=0.02, cellSize=0.1e-7, rateConstant=0.00026, initialAcceleratorCoverage=0.0, levelerDiffusionCoefficient=5e-10, numberOfSteps=400, displayRate=10, displayViewers=True): kLevelerConsumption = 0.0005 aspectRatio = 1.5 faradaysConstant = 9.6485e4 gasConstant = 8.314 acceleratorDiffusionCoefficient = 4e-10 siteDensity = 6.35e-6 atomicVolume = 7.1e-6 charge = 2 metalDiffusionCoefficient = 4e-10 temperature = 298. overpotential = -0.25 bulkMetalConcentration = 250. bulkAcceleratorConcentration = 50.0e-3 initialLevelerCoverage = 0. cflNumber = 0.2 numberOfCellsInNarrowBand = 20 cellsBelowTrench = 10 trenchDepth = 0.4e-6 trenchSpacing = 0.6e-6 boundaryLayerDepth = 98.7e-6 i0Suppressor = 0.3 i0Accelerator = 22.5 alphaSuppressor = 0.5 alphaAccelerator = 0.4 alphaAdsorption = 0.62 m = 4 b = 2.65 A = 0.3 Ba = -40 Bb = 60 Vd = 0.098 Bd = 0.0008 etaPrime = faradaysConstant * overpotential / gasConstant / temperature from fipy import TrenchMesh from fipy.tools import numerix mesh = TrenchMesh(cellSize=cellSize, trenchSpacing=trenchSpacing, trenchDepth=trenchDepth, boundaryLayerDepth=boundaryLayerDepth, aspectRatio=aspectRatio, angle=numerix.pi * 4. / 180., bowWidth=0., overBumpRadius=0., overBumpWidth=0.) narrowBandWidth = numberOfCellsInNarrowBand * cellSize from fipy.models.levelSet.distanceFunction.distanceVariable import DistanceVariable distanceVar = DistanceVariable(name='distance variable', mesh=mesh, value=-1, narrowBandWidth=narrowBandWidth) distanceVar.setValue(1, where=mesh.getElectrolyteMask()) distanceVar.calcDistanceFunction(narrowBandWidth=1e10) from fipy.models.levelSet.surfactant.surfactantVariable import SurfactantVariable levelerVar = SurfactantVariable(name="leveler variable", value=initialLevelerCoverage, distanceVar=distanceVar) acceleratorVar = SurfactantVariable(name="accelerator variable", value=initialAcceleratorCoverage, distanceVar=distanceVar) from fipy.variables.cellVariable import CellVariable bulkAcceleratorVar = CellVariable(name='bulk accelerator variable', mesh=mesh, value=bulkAcceleratorConcentration) bulkLevelerVar = CellVariable(name='bulk leveler variable', mesh=mesh, value=bulkLevelerConcentration) metalVar = CellVariable(name='metal variable', mesh=mesh, value=bulkMetalConcentration) def depositionCoeff(alpha, i0): expo = numerix.exp(-alpha * etaPrime) return 2 * i0 * (expo - expo * numerix.exp(etaPrime)) coeffSuppressor = depositionCoeff(alphaSuppressor, i0Suppressor) coeffAccelerator = depositionCoeff(alphaAccelerator, i0Accelerator) exchangeCurrentDensity = acceleratorVar.getInterfaceVar() * ( coeffAccelerator - coeffSuppressor) + coeffSuppressor currentDensity = metalVar / bulkMetalConcentration * exchangeCurrentDensity depositionRateVariable = currentDensity * atomicVolume / charge / faradaysConstant extensionVelocityVariable = CellVariable(name='extension velocity', mesh=mesh, value=depositionRateVariable) from fipy.models.levelSet.surfactant.adsorbingSurfactantEquation \ import AdsorbingSurfactantEquation kAccelerator = rateConstant * numerix.exp(-alphaAdsorption * etaPrime) kAcceleratorConsumption = Bd + A / (numerix.exp(Ba * (overpotential + Vd)) + numerix.exp(Bb * (overpotential + Vd))) q = m * overpotential + b levelerSurfactantEquation = AdsorbingSurfactantEquation( levelerVar, distanceVar=distanceVar, bulkVar=bulkLevelerVar, rateConstant=kLeveler, consumptionCoeff=kLevelerConsumption * depositionRateVariable) accVar1 = acceleratorVar.getInterfaceVar() accVar2 = (accVar1 > 0) * accVar1 accConsumptionCoeff = kAcceleratorConsumption * (accVar2**(q - 1)) acceleratorSurfactantEquation = AdsorbingSurfactantEquation( acceleratorVar, distanceVar=distanceVar, bulkVar=bulkAcceleratorVar, rateConstant=kAccelerator, otherVar=levelerVar, otherBulkVar=bulkLevelerVar, otherRateConstant=kLeveler, consumptionCoeff=accConsumptionCoeff) from fipy.models.levelSet.advection.higherOrderAdvectionEquation \ import buildHigherOrderAdvectionEquation advectionEquation = buildHigherOrderAdvectionEquation( advectionCoeff=extensionVelocityVariable) from fipy.boundaryConditions.fixedValue import FixedValue from fipy.models.levelSet.electroChem.metalIonDiffusionEquation \ import buildMetalIonDiffusionEquation metalEquation = buildMetalIonDiffusionEquation( ionVar=metalVar, distanceVar=distanceVar, depositionRate=depositionRateVariable, diffusionCoeff=metalDiffusionCoefficient, metalIonMolarVolume=atomicVolume) metalEquationBCs = FixedValue(mesh.getTopFaces(), bulkMetalConcentration) from fipy.models.levelSet.surfactant.surfactantBulkDiffusionEquation \ import buildSurfactantBulkDiffusionEquation bulkAcceleratorEquation = buildSurfactantBulkDiffusionEquation( bulkVar=bulkAcceleratorVar, distanceVar=distanceVar, surfactantVar=acceleratorVar, otherSurfactantVar=levelerVar, diffusionCoeff=acceleratorDiffusionCoefficient, rateConstant=kAccelerator * siteDensity) bulkAcceleratorEquationBCs = (FixedValue(mesh.getTopFaces(), bulkAcceleratorConcentration), ) bulkLevelerEquation = buildSurfactantBulkDiffusionEquation( bulkVar=bulkLevelerVar, distanceVar=distanceVar, surfactantVar=levelerVar, diffusionCoeff=levelerDiffusionCoefficient, rateConstant=kLeveler * siteDensity) bulkLevelerEquationBCs = (FixedValue(mesh.getTopFaces(), bulkLevelerConcentration), ) eqnTuple = ((advectionEquation, distanceVar, ()), (levelerSurfactantEquation, levelerVar, ()), (acceleratorSurfactantEquation, acceleratorVar, ()), (metalEquation, metalVar, metalEquationBCs), (bulkAcceleratorEquation, bulkAcceleratorVar, bulkAcceleratorEquationBCs), (bulkLevelerEquation, bulkLevelerVar, bulkLevelerEquationBCs)) levelSetUpdateFrequency = int(0.7 * narrowBandWidth / cellSize / cflNumber / 2) totalTime = 0.0 if displayViewers: from fipy.viewers.mayaviViewer.mayaviSurfactantViewer import MayaviSurfactantViewer viewers = (MayaviSurfactantViewer(distanceVar, acceleratorVar.getInterfaceVar(), zoomFactor=1e6, limits={ 'datamax': 0.5, 'datamin': 0.0 }, smooth=1, title='accelerator coverage'), MayaviSurfactantViewer(distanceVar, levelerVar.getInterfaceVar(), zoomFactor=1e6, limits={ 'datamax': 0.5, 'datamin': 0.0 }, smooth=1, title='leveler coverage')) for step in range(numberOfSteps): if displayViewers: if step % displayRate == 0: for viewer in viewers: viewer.plot() if step % levelSetUpdateFrequency == 0: distanceVar.calcDistanceFunction(deleteIslands=True) extensionVelocityVariable.setValue(depositionRateVariable) extOnInt = numerix.where( distanceVar > 0, numerix.where(distanceVar < 2 * cellSize, extensionVelocityVariable, 0), 0) dt = cflNumber * cellSize / numerix.max(extOnInt) id = numerix.max(numerix.nonzero(distanceVar._getInterfaceFlag())) distanceVar.extendVariable(extensionVelocityVariable, deleteIslands=True) extensionVelocityVariable[mesh.getFineMesh().getNumberOfCells():] = 0. for eqn, var, BCs in eqnTuple: var.updateOld() for eqn, var, BCs in eqnTuple: eqn.solve(var, boundaryConditions=BCs, dt=dt) totalTime += dt try: testFile = 'testLeveler.gz' import os import examples.levelSet.electroChem from fipy.tools import dump data = dump.read( os.path.join(examples.levelSet.electroChem.__path__[0], testFile)) N = mesh.getFineMesh().getNumberOfCells() print numerix.allclose(data[:N], levelerVar[:N], rtol=1e-3) except: return 0
def runGold(faradaysConstant=9.6e4, consumptionRateConstant=2.6e+6, molarVolume=10.21e-6, charge=1.0, metalDiffusion=1.7e-9, metalConcentration=20.0, catalystCoverage=0.15, currentDensity0=3e-2 * 16, currentDensity1=6.5e-1 * 16, cellSize=0.1e-7, trenchDepth=0.2e-6, aspectRatio=1.47, trenchSpacing=0.5e-6, boundaryLayerDepth=90.0e-6, numberOfSteps=10, taperAngle=6.0, displayViewers=True): cflNumber = 0.2 numberOfCellsInNarrowBand = 20 cellsBelowTrench = 10 from fipy.tools import numerix from fipy import TrenchMesh mesh = TrenchMesh(cellSize=cellSize, trenchSpacing=trenchSpacing, trenchDepth=trenchDepth, boundaryLayerDepth=boundaryLayerDepth, aspectRatio=aspectRatio, angle=numerix.pi * taperAngle / 180., bowWidth=0., overBumpRadius=0., overBumpWidth=0.) narrowBandWidth = numberOfCellsInNarrowBand * cellSize from fipy.models.levelSet.distanceFunction.distanceVariable import DistanceVariable distanceVar = DistanceVariable(name='distance variable', mesh=mesh, value=-1, narrowBandWidth=narrowBandWidth) distanceVar.setValue(1, where=mesh.getElectrolyteMask()) distanceVar.calcDistanceFunction(narrowBandWidth=1e10) from fipy.models.levelSet.surfactant.surfactantVariable import SurfactantVariable catalystVar = SurfactantVariable(name="catalyst variable", value=catalystCoverage, distanceVar=distanceVar) from fipy.variables.cellVariable import CellVariable metalVar = CellVariable(name='metal variable', mesh=mesh, value=metalConcentration) exchangeCurrentDensity = currentDensity0 + currentDensity1 * catalystVar.getInterfaceVar( ) currentDensity = metalVar / metalConcentration * exchangeCurrentDensity depositionRateVariable = currentDensity * molarVolume / charge / faradaysConstant extensionVelocityVariable = CellVariable(name='extension velocity', mesh=mesh, value=depositionRateVariable) from fipy.models.levelSet.surfactant.adsorbingSurfactantEquation \ import AdsorbingSurfactantEquation catalystSurfactantEquation = AdsorbingSurfactantEquation( catalystVar, distanceVar=distanceVar, bulkVar=0, rateConstant=0, consumptionCoeff=consumptionRateConstant * extensionVelocityVariable) from fipy.models.levelSet.advection.higherOrderAdvectionEquation \ import buildHigherOrderAdvectionEquation advectionEquation = buildHigherOrderAdvectionEquation( advectionCoeff=extensionVelocityVariable) from fipy.boundaryConditions.fixedValue import FixedValue from fipy.models.levelSet.electroChem.metalIonDiffusionEquation \ import buildMetalIonDiffusionEquation metalEquation = buildMetalIonDiffusionEquation( ionVar=metalVar, distanceVar=distanceVar, depositionRate=depositionRateVariable, diffusionCoeff=metalDiffusion, metalIonMolarVolume=molarVolume) metalEquationBCs = FixedValue(mesh.getTopFaces(), metalConcentration) if displayViewers: try: from fipy.viewers.mayaviViewer.mayaviSurfactantViewer import MayaviSurfactantViewer viewers = (MayaviSurfactantViewer(distanceVar, catalystVar.getInterfaceVar(), zoomFactor=1e6, limits={ 'datamax': 1.0, 'datamin': 0.0 }, smooth=1, title='catalyst coverage'), ) except: class PlotVariable(CellVariable): def __init__(self, var=None, name=''): CellVariable.__init__(self, mesh=mesh.getFineMesh(), name=name) self.var = self._requires(var) def _calcValue(self): return numerix.array( self.var[:self.mesh.getNumberOfCells()]) from fipy.viewers import make viewers = (make(PlotVariable(var=distanceVar), limits={ 'datamax': 1e-9, 'datamin': -1e-9 }), make(PlotVariable(var=catalystVar.getInterfaceVar()))) else: viewers = () levelSetUpdateFrequency = int(0.7 * narrowBandWidth / cellSize / cflNumber / 2) step = 0 while step < numberOfSteps: if step % 10 == 0: for viewer in viewers: viewer.plot() if step % levelSetUpdateFrequency == 0: distanceVar.calcDistanceFunction(deleteIslands=True) extensionVelocityVariable.setValue( numerix.array(depositionRateVariable)) argmax = numerix.argmax(extensionVelocityVariable) dt = cflNumber * cellSize / extensionVelocityVariable[argmax] distanceVar.extendVariable(extensionVelocityVariable, deleteIslands=True) distanceVar.updateOld() catalystVar.updateOld() metalVar.updateOld() advectionEquation.solve(distanceVar, dt=dt) catalystSurfactantEquation.solve(catalystVar, dt=dt) metalEquation.solve(metalVar, boundaryConditions=metalEquationBCs, dt=dt) step += 1 try: from fipy.tools import dump import os import examples.levelSet.electroChem data = dump.read( os.path.join(examples.levelSet.electroChem.__path__[0], 'goldData.gz')) n = mesh.getFineMesh().getNumberOfCells() print numerix.allclose(catalystVar[:n], data[:n], atol=1.0) except: return 0
def allclose(self, a, b, rtol=1.e-5, atol=1.e-8): return numerix.allclose(a, b, rtol=rtol, atol=atol)
def runGold(faradaysConstant=9.6e4, consumptionRateConstant=2.6e+6, molarVolume=10.21e-6, charge=1.0, metalDiffusion=1.7e-9, metalConcentration=20.0, catalystCoverage=0.15, currentDensity0=3e-2 * 16, currentDensity1=6.5e-1 * 16, cellSize=0.1e-7, trenchDepth=0.2e-6, aspectRatio=1.47, trenchSpacing=0.5e-6, boundaryLayerDepth=90.0e-6, numberOfSteps=10, taperAngle=6.0, displayViewers=True): cflNumber = 0.2 numberOfCellsInNarrowBand = 20 cellsBelowTrench = 10 from fipy.tools import numerix from fipy import TrenchMesh mesh = TrenchMesh(cellSize = cellSize, trenchSpacing = trenchSpacing, trenchDepth = trenchDepth, boundaryLayerDepth = boundaryLayerDepth, aspectRatio = aspectRatio, angle = numerix.pi * taperAngle / 180., bowWidth = 0., overBumpRadius = 0., overBumpWidth = 0.) narrowBandWidth = numberOfCellsInNarrowBand * cellSize from fipy.models.levelSet.distanceFunction.distanceVariable import DistanceVariable distanceVar = DistanceVariable( name = 'distance variable', mesh = mesh, value = -1, narrowBandWidth = narrowBandWidth) distanceVar.setValue(1, where=mesh.getElectrolyteMask()) distanceVar.calcDistanceFunction(narrowBandWidth = 1e10) from fipy.models.levelSet.surfactant.surfactantVariable import SurfactantVariable catalystVar = SurfactantVariable( name = "catalyst variable", value = catalystCoverage, distanceVar = distanceVar) from fipy.variables.cellVariable import CellVariable metalVar = CellVariable( name = 'metal variable', mesh = mesh, value = metalConcentration) exchangeCurrentDensity = currentDensity0 + currentDensity1 * catalystVar.getInterfaceVar() currentDensity = metalVar / metalConcentration * exchangeCurrentDensity depositionRateVariable = currentDensity * molarVolume / charge / faradaysConstant extensionVelocityVariable = CellVariable( name = 'extension velocity', mesh = mesh, value = depositionRateVariable) from fipy.models.levelSet.surfactant.adsorbingSurfactantEquation \ import AdsorbingSurfactantEquation catalystSurfactantEquation = AdsorbingSurfactantEquation( catalystVar, distanceVar = distanceVar, bulkVar = 0, rateConstant = 0, consumptionCoeff = consumptionRateConstant * extensionVelocityVariable) from fipy.models.levelSet.advection.higherOrderAdvectionEquation \ import buildHigherOrderAdvectionEquation advectionEquation = buildHigherOrderAdvectionEquation( advectionCoeff = extensionVelocityVariable) from fipy.boundaryConditions.fixedValue import FixedValue from fipy.models.levelSet.electroChem.metalIonDiffusionEquation \ import buildMetalIonDiffusionEquation metalEquation = buildMetalIonDiffusionEquation( ionVar = metalVar, distanceVar = distanceVar, depositionRate = depositionRateVariable, diffusionCoeff = metalDiffusion, metalIonMolarVolume = molarVolume) metalEquationBCs = FixedValue(mesh.getTopFaces(), metalConcentration) if displayViewers: try: from fipy.viewers.mayaviViewer.mayaviSurfactantViewer import MayaviSurfactantViewer viewers = ( MayaviSurfactantViewer(distanceVar, catalystVar.getInterfaceVar(), zoomFactor = 1e6, limits = { 'datamax' : 1.0, 'datamin' : 0.0 }, smooth = 1, title = 'catalyst coverage'),) except: class PlotVariable(CellVariable): def __init__(self, var = None, name = ''): CellVariable.__init__(self, mesh = mesh.getFineMesh(), name = name) self.var = self._requires(var) def _calcValue(self): return numerix.array(self.var[:self.mesh.getNumberOfCells()]) from fipy.viewers import make viewers = ( make(PlotVariable(var = distanceVar), limits = {'datamax' : 1e-9, 'datamin' : -1e-9}), make(PlotVariable(var = catalystVar.getInterfaceVar()))) else: viewers = () levelSetUpdateFrequency = int(0.7 * narrowBandWidth / cellSize / cflNumber / 2) step = 0 while step < numberOfSteps: if step % 10 == 0: for viewer in viewers: viewer.plot() if step % levelSetUpdateFrequency == 0: distanceVar.calcDistanceFunction(deleteIslands = True) extensionVelocityVariable.setValue(numerix.array(depositionRateVariable)) argmax = numerix.argmax(extensionVelocityVariable) dt = cflNumber * cellSize / extensionVelocityVariable[argmax] distanceVar.extendVariable(extensionVelocityVariable, deleteIslands = True) distanceVar.updateOld() catalystVar.updateOld() metalVar.updateOld() advectionEquation.solve(distanceVar, dt = dt) catalystSurfactantEquation.solve(catalystVar, dt = dt) metalEquation.solve(metalVar, boundaryConditions = metalEquationBCs, dt = dt) step += 1 try: from fipy.tools import dump import os import examples.levelSet.electroChem data = dump.read(os.path.join(examples.levelSet.electroChem.__path__[0], 'goldData.gz')) n = mesh.getFineMesh().getNumberOfCells() print numerix.allclose(catalystVar[:n], data[:n], atol=1.0) except: return 0
def runLeveler(kLeveler=0.018, bulkLevelerConcentration=0.02, cellSize=0.1e-7, rateConstant=0.00026, initialAcceleratorCoverage=0.0, levelerDiffusionCoefficient=5e-10, numberOfSteps=400, displayRate=10, displayViewers=True): kLevelerConsumption = 0.0005 aspectRatio = 1.5 faradaysConstant = 9.6485e4 gasConstant = 8.314 acceleratorDiffusionCoefficient = 4e-10 siteDensity = 6.35e-6 atomicVolume = 7.1e-6 charge = 2 metalDiffusionCoefficient = 4e-10 temperature = 298. overpotential = -0.25 bulkMetalConcentration = 250. bulkAcceleratorConcentration = 50.0e-3 initialLevelerCoverage = 0. cflNumber = 0.2 numberOfCellsInNarrowBand = 20 cellsBelowTrench = 10 trenchDepth = 0.4e-6 trenchSpacing = 0.6e-6 boundaryLayerDepth = 98.7e-6 i0Suppressor = 0.3 i0Accelerator = 22.5 alphaSuppressor = 0.5 alphaAccelerator = 0.4 alphaAdsorption = 0.62 m = 4 b = 2.65 A = 0.3 Ba = -40 Bb = 60 Vd = 0.098 Bd = 0.0008 etaPrime = faradaysConstant * overpotential / gasConstant / temperature from fipy import TrenchMesh from fipy.tools import numerix mesh = TrenchMesh(cellSize = cellSize, trenchSpacing = trenchSpacing, trenchDepth = trenchDepth, boundaryLayerDepth = boundaryLayerDepth, aspectRatio = aspectRatio, angle = numerix.pi * 4. / 180., bowWidth = 0., overBumpRadius = 0., overBumpWidth = 0.) narrowBandWidth = numberOfCellsInNarrowBand * cellSize from fipy.models.levelSet.distanceFunction.distanceVariable import DistanceVariable distanceVar = DistanceVariable( name = 'distance variable', mesh = mesh, value = -1, narrowBandWidth = narrowBandWidth) distanceVar.setValue(1, where=mesh.getElectrolyteMask()) distanceVar.calcDistanceFunction(narrowBandWidth = 1e10) from fipy.models.levelSet.surfactant.surfactantVariable import SurfactantVariable levelerVar = SurfactantVariable( name = "leveler variable", value = initialLevelerCoverage, distanceVar = distanceVar) acceleratorVar = SurfactantVariable( name = "accelerator variable", value = initialAcceleratorCoverage, distanceVar = distanceVar) from fipy.variables.cellVariable import CellVariable bulkAcceleratorVar = CellVariable(name = 'bulk accelerator variable', mesh = mesh, value = bulkAcceleratorConcentration) bulkLevelerVar = CellVariable( name = 'bulk leveler variable', mesh = mesh, value = bulkLevelerConcentration) metalVar = CellVariable( name = 'metal variable', mesh = mesh, value = bulkMetalConcentration) def depositionCoeff(alpha, i0): expo = numerix.exp(-alpha * etaPrime) return 2 * i0 * (expo - expo * numerix.exp(etaPrime)) coeffSuppressor = depositionCoeff(alphaSuppressor, i0Suppressor) coeffAccelerator = depositionCoeff(alphaAccelerator, i0Accelerator) exchangeCurrentDensity = acceleratorVar.getInterfaceVar() * (coeffAccelerator - coeffSuppressor) + coeffSuppressor currentDensity = metalVar / bulkMetalConcentration * exchangeCurrentDensity depositionRateVariable = currentDensity * atomicVolume / charge / faradaysConstant extensionVelocityVariable = CellVariable( name = 'extension velocity', mesh = mesh, value = depositionRateVariable) from fipy.models.levelSet.surfactant.adsorbingSurfactantEquation \ import AdsorbingSurfactantEquation kAccelerator = rateConstant * numerix.exp(-alphaAdsorption * etaPrime) kAcceleratorConsumption = Bd + A / (numerix.exp(Ba * (overpotential + Vd)) + numerix.exp(Bb * (overpotential + Vd))) q = m * overpotential + b levelerSurfactantEquation = AdsorbingSurfactantEquation( levelerVar, distanceVar = distanceVar, bulkVar = bulkLevelerVar, rateConstant = kLeveler, consumptionCoeff = kLevelerConsumption * depositionRateVariable) accVar1 = acceleratorVar.getInterfaceVar() accVar2 = (accVar1 > 0) * accVar1 accConsumptionCoeff = kAcceleratorConsumption * (accVar2**(q - 1)) acceleratorSurfactantEquation = AdsorbingSurfactantEquation( acceleratorVar, distanceVar = distanceVar, bulkVar = bulkAcceleratorVar, rateConstant = kAccelerator, otherVar = levelerVar, otherBulkVar = bulkLevelerVar, otherRateConstant = kLeveler, consumptionCoeff = accConsumptionCoeff) from fipy.models.levelSet.advection.higherOrderAdvectionEquation \ import buildHigherOrderAdvectionEquation advectionEquation = buildHigherOrderAdvectionEquation( advectionCoeff = extensionVelocityVariable) from fipy.boundaryConditions.fixedValue import FixedValue from fipy.models.levelSet.electroChem.metalIonDiffusionEquation \ import buildMetalIonDiffusionEquation metalEquation = buildMetalIonDiffusionEquation( ionVar = metalVar, distanceVar = distanceVar, depositionRate = depositionRateVariable, diffusionCoeff = metalDiffusionCoefficient, metalIonMolarVolume = atomicVolume) metalEquationBCs = FixedValue(mesh.getTopFaces(), bulkMetalConcentration) from fipy.models.levelSet.surfactant.surfactantBulkDiffusionEquation \ import buildSurfactantBulkDiffusionEquation bulkAcceleratorEquation = buildSurfactantBulkDiffusionEquation( bulkVar = bulkAcceleratorVar, distanceVar = distanceVar, surfactantVar = acceleratorVar, otherSurfactantVar = levelerVar, diffusionCoeff = acceleratorDiffusionCoefficient, rateConstant = kAccelerator * siteDensity) bulkAcceleratorEquationBCs = (FixedValue( mesh.getTopFaces(), bulkAcceleratorConcentration),) bulkLevelerEquation = buildSurfactantBulkDiffusionEquation( bulkVar = bulkLevelerVar, distanceVar = distanceVar, surfactantVar = levelerVar, diffusionCoeff = levelerDiffusionCoefficient, rateConstant = kLeveler * siteDensity) bulkLevelerEquationBCs = (FixedValue( mesh.getTopFaces(), bulkLevelerConcentration),) eqnTuple = ( (advectionEquation, distanceVar, ()), (levelerSurfactantEquation, levelerVar, ()), (acceleratorSurfactantEquation, acceleratorVar, ()), (metalEquation, metalVar, metalEquationBCs), (bulkAcceleratorEquation, bulkAcceleratorVar, bulkAcceleratorEquationBCs), (bulkLevelerEquation, bulkLevelerVar, bulkLevelerEquationBCs)) levelSetUpdateFrequency = int(0.7 * narrowBandWidth / cellSize / cflNumber / 2) totalTime = 0.0 if displayViewers: from fipy.viewers.mayaviViewer.mayaviSurfactantViewer import MayaviSurfactantViewer viewers = ( MayaviSurfactantViewer(distanceVar, acceleratorVar.getInterfaceVar(), zoomFactor = 1e6, limits = { 'datamax' : 0.5, 'datamin' : 0.0 }, smooth = 1, title = 'accelerator coverage'), MayaviSurfactantViewer(distanceVar, levelerVar.getInterfaceVar(), zoomFactor = 1e6, limits = { 'datamax' : 0.5, 'datamin' : 0.0 }, smooth = 1, title = 'leveler coverage')) for step in range(numberOfSteps): if displayViewers: if step % displayRate == 0: for viewer in viewers: viewer.plot() if step % levelSetUpdateFrequency == 0: distanceVar.calcDistanceFunction(deleteIslands = True) extensionVelocityVariable.setValue(depositionRateVariable) extOnInt = numerix.where(distanceVar > 0, numerix.where(distanceVar < 2 * cellSize, extensionVelocityVariable, 0), 0) dt = cflNumber * cellSize / numerix.max(extOnInt) id = numerix.max(numerix.nonzero(distanceVar._getInterfaceFlag())) distanceVar.extendVariable(extensionVelocityVariable, deleteIslands = True) extensionVelocityVariable[mesh.getFineMesh().getNumberOfCells():] = 0. for eqn, var, BCs in eqnTuple: var.updateOld() for eqn, var, BCs in eqnTuple: eqn.solve(var, boundaryConditions = BCs, dt = dt) totalTime += dt try: testFile = 'testLeveler.gz' import os import examples.levelSet.electroChem from fipy.tools import dump data = dump.read(os.path.join(examples.levelSet.electroChem.__path__[0], testFile)) N = mesh.getFineMesh().getNumberOfCells() print numerix.allclose(data[:N], levelerVar[:N], rtol = 1e-3) except: return 0
def assertArrayWithinTolerance(self, first, second, atol = 1e-10, rtol = 1e-10, msg=None): """Fail if the two objects are unequal by more than tol. """ if not numerix.allclose(first, second, rtol = rtol, atol = atol): raise self.failureException, (msg or '\n%s\nis not\n%s' % (first, second))
def relative_error(a, b): if np.allclose(b.numericValue, 0): return PhysicalField(0.0, '') else: return PhysicalField((a - b) / a, '')
def allclose(self, a, b, rtol=1.e-5, atol=1.e-8): return self.mpi4py_comm.allreduce(numerix.allclose(a, b, rtol=rtol, atol=atol), op=MPI.LAND)
def allclose(self, a, b, rtol=1.e-5, atol=1.e-8): return self.mpi4py_comm.allreduce(numerix.allclose(a, b, rtol=rtol, atol=atol), op=self.MPI.LAND)
def allcloseParallel(a, b): return MPI.COMM_WORLD.allreduce(numerix.allclose(a, b, rtol=rtol, atol=atol), op=MPI.LAND)