def _getDerivative(self, order): newOrder = self.order - order if newOrder not in self.derivative: if newOrder > 1: self.derivative[newOrder] = NthOrderBoundaryCondition(self.faces, self.value, newOrder) elif newOrder == 1: self.derivative[newOrder] = FixedFlux(self.faces, self.value) elif newOrder == 0: self.derivative[newOrder] = FixedValue(self.faces, self.value) else: self.derivative[newOrder] = None return self.derivative[newOrder]
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
valueLeft = 0. valueRight = 1. import examples.diffusion.steadyState.mesh20x20 import os.path mesh = GmshImporter2D( os.path.join(examples.diffusion.steadyState.mesh20x20.__path__[0], 'modifiedMesh.msh')) ## "%s/%s" % (sys.__dict__['path'][0], "examples/diffusion/steadyState/mesh20x20/modifiedMesh.msh")) var = CellVariable(name="solution variable", mesh=mesh, value=valueLeft) exteriorFaces = mesh.getExteriorFaces() xFace = exteriorFaces.getCenters()[..., 0] boundaryConditions = (FixedValue( exteriorFaces.where(xFace**2 < 0.000000000000001), valueLeft), FixedValue( exteriorFaces.where( (xFace - 20)**2 < 0.000000000000001), valueRight)) if __name__ == '__main__': ImplicitDiffusionTerm().solve(var, boundaryConditions=boundaryConditions) viewer = fipy.viewers.make(vars=var) viewer.plot() varArray = numerix.array(var) x = mesh.getCellCenters()[:, 0] analyticalArray = valueLeft + (valueRight - valueLeft) * x / 20 errorArray = varArray - analyticalArray errorVar = CellVariable(name="absolute error", mesh=mesh,
#Laplacian=0 nx = 50 dx = 1. from fipy.meshes.grid1D import Grid1D mesh = Grid1D(nx=nx, dx=dx) from fipy.variables.cellVariable import CellVariable phi = CellVariable(name="solution variable", mesh=mesh, value=0) z = CellVariable(name="dummy", mesh=mesh, value=0) valueLeft = 1 valueRight = 0 from fipy.boundaryConditions.fixedValue import FixedValue BCs = (FixedValue(faces=mesh.getFacesRight(), value=valueRight), FixedValue(faces=mesh.getFacesLeft(), value=valueLeft)) from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm eqX = ExplicitDiffusionTerm(coeff=1) from fipy import viewers viewer = viewers.make(vars=(phi), limits={'datamin': 0., 'datamax': 1.}) viewer.plot() eqX.solve(var=phi, boundaryConditions=BCs) viewer.plot()
velocity = -1. timeStepDuration = cfl * dx / abs(velocity) steps = 1000 mesh = Grid1D(dx = dx, nx = nx) startingArray = numerix.zeros(nx, 'd') startingArray[50:90] = 1. var = CellVariable( name = "advection variable", mesh = mesh, value = startingArray) boundaryConditions = ( FixedValue(mesh.getFacesLeft(), valueLeft), FixedValue(mesh.getFacesRight(), valueRight) ) from fipy.terms.transientTerm import TransientTerm from fipy.terms.explicitUpwindConvectionTerm import ExplicitUpwindConvectionTerm eq = TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (velocity,)) if __name__ == '__main__': viewer = fipy.viewers.make(vars=(var,)) for step in range(steps): eq.solve(var, dt = timeStepDuration, boundaryConditions = boundaryConditions,
u=psi.getGrad().dot(Yhat) v=-psi.getGrad().dot(Xhat) U=psi.getFaceGrad() from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm eq=ImplicitDiffusionTerm(0.00001) \ + u * u.getGrad().dot(Xhat) \ + v * u.getGrad().dot(Yhat) \ - 2 * u.getGrad().dot(Yhat).getGrad().dot(Yhat) from fipy.boundaryConditions.nthOrderBoundaryCondition import NthOrderBoundaryCondition from fipy.boundaryConditions.fixedValue import FixedValue BC = (NthOrderBoundaryCondition(faces=mesh.getFacesBottom(), value=0,order=1), FixedValue(faces=mesh.getFacesBottom(), value=0), NthOrderBoundaryCondition(faces=mesh.getFacesLeft(), value=0,order=1), NthOrderBoundaryCondition(faces=mesh.getFacesTop(), value=1,order=1), NthOrderBoundaryCondition(faces=mesh.getFacesTop(), value=0,order=2)) from fipy.boundaryConditions.fixedFlux import FixedFlux BC = (FixedFlux(faces=mesh.getFacesBottom(), value=0), FixedValue(faces=mesh.getFacesBottom(), value=0), FixedValue(faces=mesh.getFacesTop(), value=1), FixedFlux(faces=mesh.getFacesTop(), value=1), FixedFlux(faces=mesh.getFacesLeft(), value=0))
##viewer1 = Grid3DPyxViewer(var, zvalue = 1.0) ##viewer3 = Grid3DPyxViewer(var, zvalue = 3.0) ##viewer5 = Grid3DPyxViewer(var, zvalue = 5.0) ##viewer7 = Grid3DPyxViewer(var, zvalue = 7.0) ##viewer9 = Grid3DPyxViewer(var, zvalue = 9.0) ## from fipy import viewers ## viewer = viewers.make(vars = var) ## viewer.plot() from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm ImplicitDiffusionTerm().solve(var, boundaryConditions=( FixedValue(mesh.getFacesLeft(), valueSides), FixedValue(mesh.getFacesRight(), valueSides), FixedValue(mesh.getFacesTop(), valueSides), FixedValue(mesh.getFacesBottom(), valueSides), FixedValue(mesh.getFacesFront(), valueFront), FixedValue(mesh.getFacesBack(), valueBack), )) ## viewer.plot() if __name__ == '__main__': ##viewer1.plot(resolution = 0.2, xlabel = "X values (Z value = 1)", minval = valueFront, maxval = valueBack) ##raw_input("press enter to continue") ##viewer3.plot(resolution = 0.2, xlabel = "X values (Z value = 3)", minval = valueFront, maxval = valueBack) ##raw_input("press enter to continue")
from fipy.models.levelSet.surfactant.surfactantBulkDiffusionEquation \ import buildSurfactantBulkDiffusionEquation bulkCatalystEquation = buildSurfactantBulkDiffusionEquation( bulkVar=bulkCatalystVar, distanceVar=distanceVar, surfactantVar=catalystVar, diffusionCoeff=catalystDiffusion, rateConstant=rateConstant0 * siteDensity) bench.stop('terms') bench.start() from fipy.boundaryConditions.fixedValue import FixedValue metalEquationBCs = (FixedValue(mesh.getFacesTop(), bulkMetalConcentration), ) catalystBCs = (FixedValue(mesh.getFacesTop(), catalystConcentration), ) bench.stop('BCs') levelSetUpdateFrequency = int(0.8 * narrowBandWidth \ / (cellSize * cflNumber * 2)) step = 0 if step % levelSetUpdateFrequency == 0: distanceVar.calcDistanceFunction() extensionVelocityVariable.setValue(depositionRateVariable())
diffTerm = ImplicitDiffusionTerm(coeff=viscosity) from fipy.terms.exponentialConvectionTerm import ExponentialConvectionTerm convTerm = ExponentialConvectionTerm(coeff=velocity, diffusionTerm=diffTerm) eq1 = diffTerm - u * u.getGrad().dot(Xhat) - v * u.getGrad().dot(Yhat) eq2 = velocity.getDivergence() #eq2= u.getGrad() + v.getGrad() eq2 = u.getFaceGrad().dot(Xhat) * Xhat + v.getFaceGrad().dot(Yhat) * Yhat eq2 = u.getGrad().dot(Xhat) + v.getGrad().dot(Yhat) eq2 = ImplicitDiffusionTerm(1) + u.getGrad().dot(Xhat) + v.getGrad().dot(Yhat) from fipy.boundaryConditions.fixedValue import FixedValue BCu = (FixedValue(faces=mesh.getFacesBottom(), value=0), FixedValue(faces=mesh.getFacesLeft(), value=1)) BCv = (FixedValue(faces=mesh.getFacesBottom(), value=0), FixedValue(faces=mesh.getFacesLeft(), value=0)) from fipy import viewers viewer = viewers.make(vars=(u, v), limits={'datamin': 0., 'datamax': 1.}) def hit_continue(Prompt='Hit any key to continue'): raw_input(Prompt) steps = 10 for step in range(steps):
if ((leftSide(face) or inMiddle(face) or rightSide(face)) or not (face.getID() in bigMesh.getExteriorFaces())): return 0 else: return 1 var = CellVariable(name="concentration", mesh=bigMesh, value=valueLeft) eqn = TransientTerm() == ExplicitDiffusionTerm() exteriorFaces = bigMesh.getExteriorFaces() xFace = exteriorFaces.getCenters()[..., 0] boundaryConditions = ( FixedValue(exteriorFaces.where(xFace**2 < 0.000000000000001), valueLeft), FixedValue(exteriorFaces.where((xFace - (dx * nx))**2 < 0.000000000000001), (valueLeft + valueRight) * 0.5), FixedValue( exteriorFaces.where((xFace - (2 * dx * nx))**2 < 0.000000000000001), valueRight)) answer = numerix.array([ 0.00000000e+00, 8.78906250e-23, 1.54057617e-19, 1.19644866e-16, 5.39556276e-14, 1.55308505e-11, 2.94461712e-09, 3.63798469e-07, 2.74326174e-05, 1.01935828e-03, 9.76562500e-24, 1.92578125e-20, 1.70937109e-17, 8.99433979e-15, 3.10726059e-12, 7.36603377e-10, 1.21397338e-07, 1.37456643e-05, 1.02532568e-03, 4.57589878e-02, 2.63278194e-07, 5.70863224e-12, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.51165440e-13, 1.23805218e-07, 1.51873310e-03, 5.87457842e-01, 3.78270971e-06, 2.41898556e-10,
nx = 10 ny = 5 nz = 3 dx = 1. dy = 1. dz = 1. valueBottomTop = 0. valueLeftRight = 1. mesh = Grid3D(dx=dx, dy=dy, dz=dz, nx=nx, ny=ny, nz=nz) var = CellVariable(name="solution variable", mesh=mesh, value=valueBottomTop) boundaryConditions = (FixedValue(mesh.getFacesLeft(), valueLeftRight), FixedValue(mesh.getFacesRight(), valueLeftRight), FixedValue(mesh.getFacesTop(), valueBottomTop), FixedValue(mesh.getFacesBottom(), valueBottomTop)) #do the 2D problem for comparison nx = 10 ny = 5 dx = 1. dy = 1. mesh2 = Grid2D(dx=dx, dy=dy, nx=nx, ny=ny) var2 = CellVariable(name="solution variable 2D",
from fipy.meshes.grid2D import Grid2D mesh = Grid2D(dx=dx, dy=dy, nx=nx, ny=ny) from fipy.variables.cellVariable import CellVariable phi = CellVariable(name = "solution variable",mesh = mesh,value = 0) D = 1. from fipy.terms.transientTerm import TransientTerm from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm eq = ImplicitDiffusionTerm(coeff=D) valueLeft = 1 valueRight = 0 from fipy.boundaryConditions.fixedValue import FixedValue BCs = (FixedValue(faces=mesh.getFacesLeft(),value=0), FixedValue(faces=mesh.getFacesRight(),value=1), FixedValue(faces=mesh.getFacesTop(),value=0), FixedValue(faces=mesh.getFacesBottom(),value=0)) from fipy import viewers viewer = viewers.make(vars=phi, limits={'datamin': 0., 'datamax': 1.}) viewer.plot() timeStepDuration = 10 * 0.9 * dx**2 / (2 * D) steps = 10 eq.solve(var=phi,boundaryConditions=BCs)
distanceVar = DistanceVariable(mesh=mesh, value=value, hasOld=1) ## Build the bulk diffusion equation bulkVar = CellVariable(mesh=mesh, value=cinf) surfactantVar = SurfactantVariable(distanceVar=distanceVar) bulkEqn = buildSurfactantBulkDiffusionEquation(bulkVar, distanceVar=distanceVar, surfactantVar=surfactantVar, diffusionCoeff=diffusion, rateConstant=rateConstant * siteDensity) bcs = (FixedValue(mesh.getFacesRight(), cinf), ) ## Build the surfactant equation surfEqn = AdsorbingSurfactantEquation(surfactantVar=surfactantVar, distanceVar=distanceVar, bulkVar=bulkVar, rateConstant=rateConstant) ## Build the analytical solutions, x = mesh.getCellCenters()[1:, 0] - dx def concentrationFunc(theta): tmp = (1 + rateConstant * siteDensity * (1 - theta) * L / diffusion)
def _getDerivative(self, order): if order == 1: return FixedValue(self.faces, self.value) else: return BoundaryCondition._getDerivative(self, order)
def runSimpleTrenchSystem(faradaysConstant=9.6e4, gasConstant=8.314, transferCoefficient=0.5, rateConstant0=1.76, rateConstant3=-245e-6, catalystDiffusion=1e-9, siteDensity=9.8e-6, molarVolume=7.1e-6, charge=2, metalDiffusion=5.6e-10, temperature=298., overpotential=-0.3, metalConcentration=250., catalystConcentration=5e-3, catalystCoverage=0., currentDensity0=0.26, currentDensity1=45., cellSize=0.1e-7, trenchDepth=0.5e-6, aspectRatio=2., trenchSpacing=0.6e-6, boundaryLayerDepth=0.3e-6, numberOfSteps=5, displayViewers=True): cflNumber = 0.2 numberOfCellsInNarrowBand = 10 cellsBelowTrench = 10 yCells = cellsBelowTrench \ + int((trenchDepth + boundaryLayerDepth) / cellSize) xCells = int(trenchSpacing / 2 / cellSize) from fipy.meshes.grid2D import Grid2D mesh = Grid2D(dx=cellSize, dy=cellSize, nx=xCells, ny=yCells) narrowBandWidth = numberOfCellsInNarrowBand * cellSize from fipy.models.levelSet.distanceFunction.distanceVariable import \ DistanceVariable distanceVar = DistanceVariable(name='distance variable', mesh=mesh, value=-1, narrowBandWidth=narrowBandWidth, hasOld=1) bottomHeight = cellsBelowTrench * cellSize trenchHeight = bottomHeight + trenchDepth trenchWidth = trenchDepth / aspectRatio sideWidth = (trenchSpacing - trenchWidth) / 2 x, y = mesh.getCellCenters()[..., 0], mesh.getCellCenters()[..., 1] distanceVar.setValue(1, where=(y > trenchHeight) | ((y > bottomHeight) & (x < xCells * cellSize - sideWidth))) 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 bulkCatalystVar = CellVariable(name='bulk catalyst variable', mesh=mesh, value=catalystConcentration) metalVar = CellVariable(name='metal variable', mesh=mesh, value=metalConcentration) expoConstant = -transferCoefficient * faradaysConstant \ / (gasConstant * temperature) tmp = currentDensity1 * catalystVar.getInterfaceVar() exchangeCurrentDensity = currentDensity0 + tmp import fipy.tools.numerix as numerix expo = numerix.exp(expoConstant * overpotential) currentDensity = expo * exchangeCurrentDensity * metalVar \ / metalConcentration depositionRateVariable = currentDensity * molarVolume \ / (charge * faradaysConstant) extensionVelocityVariable = CellVariable(name='extension velocity', mesh=mesh, value=depositionRateVariable) from fipy.models.levelSet.surfactant.adsorbingSurfactantEquation \ import AdsorbingSurfactantEquation surfactantEquation = AdsorbingSurfactantEquation( surfactantVar=catalystVar, distanceVar=distanceVar, bulkVar=bulkCatalystVar, rateConstant=rateConstant0 + rateConstant3 * overpotential**3) 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.getFacesTop(), metalConcentration) from fipy.models.levelSet.surfactant.surfactantBulkDiffusionEquation \ import buildSurfactantBulkDiffusionEquation bulkCatalystEquation = buildSurfactantBulkDiffusionEquation( bulkVar=bulkCatalystVar, distanceVar=distanceVar, surfactantVar=catalystVar, diffusionCoeff=catalystDiffusion, rateConstant=rateConstant0 * siteDensity) catalystBCs = FixedValue(mesh.getFacesTop(), catalystConcentration) if displayViewers: try: from fipy.viewers.mayaviViewer.mayaviSurfactantViewer import MayaviSurfactantViewer viewers = (MayaviSurfactantViewer(distanceVar, catalystVar.getInterfaceVar(), zoomFactor=1e6, limits={ 'datamax': 0.5, 'datamin': 0.0 }, smooth=1, title='catalyst coverage'), ) except: from fipy.viewers import make viewers = (make(distanceVar, limits={ 'datamin': -1e-9, 'datamax': 1e-9 }), make(catalystVar.getInterfaceVar())) else: viewers = () levelSetUpdateFrequency = int(0.8 * narrowBandWidth \ / (cellSize * cflNumber * 2)) for step in range(numberOfSteps): if step % 5 == 0: for viewer in viewers: viewer.plot() if step % levelSetUpdateFrequency == 0: distanceVar.calcDistanceFunction() extensionVelocityVariable.setValue(depositionRateVariable()) distanceVar.updateOld() catalystVar.updateOld() metalVar.updateOld() bulkCatalystVar.updateOld() distanceVar.extendVariable(extensionVelocityVariable) dt = cflNumber * cellSize / numerix.max(extensionVelocityVariable) advectionEquation.solve(distanceVar, dt=dt) surfactantEquation.solve(catalystVar, dt=dt) metalEquation.solve(metalVar, dt=dt, boundaryConditions=metalEquationBCs) bulkCatalystEquation.solve(bulkCatalystVar, dt=dt, boundaryConditions=catalystBCs) try: import os import examples.levelSet.electroChem filepath = os.path.join(examples.levelSet.electroChem.__path__[0], 'test.gz') from fipy.tools import dump from fipy.tools import numerix print catalystVar.allclose(numerix.array(dump.read(filepath)), rtol=1e-4) except: return 0
for i in range(1, 501): meshList = meshList + [ SkewedGrid2D(dx=1.0, dy=1.0, nx=20, ny=20, rand=(0.001 * i)) ] for mesh in meshList: var = CellVariable(name="solution variable", mesh=mesh, value=valueLeft) from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm ImplicitDiffusionTerm().solve( var, boundaryConditions=(FixedValue(mesh.getFacesLeft(), valueLeft), FixedValue(mesh.getFacesRight(), valueRight))) varArray = numerix.array(var) x = mesh.getCellCenters()[:, 0] analyticalArray = valueLeft + (valueRight - valueLeft) * x / 20 errorArray = varArray - analyticalArray nonOrthoArray = mesh._getNonOrthogonality() RMSError = (numerix.add.reduce(errorArray * errorArray) / len(errorArray))**0.5 RMSNonOrtho = (numerix.add.reduce(nonOrthoArray * nonOrthoArray) / len(nonOrthoArray))**0.5 RMSNonOrthoList += [RMSNonOrtho] RMSErrorList += [RMSError]
from fipy.terms.transientTerm import TransientTerm a = 1 timeStepDuration = 0.01 steps = 1000 eq = ImplicitDiffusionTerm(coeff=( a, a)) + (u.getOld().getOld() - 2 * u.getOld() + u) / timeStepDuration**2 from fipy.boundaryConditions.fixedValue import FixedValue from fipy.boundaryConditions.fixedFlux import FixedFlux from fipy.boundaryConditions.nthOrderBoundaryCondition import NthOrderBoundaryCondition BCs = (NthOrderBoundaryCondition(faces=mesh.getFacesRight(), value=0, order=2), NthOrderBoundaryCondition(faces=mesh.getFacesRight(), value=1, order=3), FixedValue(faces=mesh.getFacesLeft(), value=0), FixedFlux(faces=mesh.getFacesLeft(), value=0)) from fipy import viewers viewer = viewers.make(vars=(u)) def hit_continue(Prompt='Hit any key to continue'): raw_input(Prompt) for step in range(steps): u.updateOld() eq.solve(var=u, boundaryConditions=BCs, dt=timeStepDuration) eq = ImplicitDiffusionTerm(coeff=(a, a)) + ( u.getOld().getOld() - 2 * u.getOld() + u) / timeStepDuration**2 viewer.plot()