mesh = Grid1D(dx = dx, nx = nx) from fipy.tools import numerix from fipy.variables.cellVariable import CellVariable var = CellVariable(mesh = mesh) from fipy.solvers.linearLUSolver import LinearLUSolver from fipy.boundaryConditions.nthOrderBoundaryCondition import NthOrderBoundaryCondition from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm from fipy.terms.transientTerm import TransientTerm eq = ImplicitDiffusionTerm((1.0, 1.0)) BCs = (NthOrderBoundaryCondition(mesh.getFacesLeft(), 0., 0), NthOrderBoundaryCondition(mesh.getFacesRight(), Lx, 0), NthOrderBoundaryCondition(mesh.getFacesLeft(), 0., 2), NthOrderBoundaryCondition(mesh.getFacesRight(), 0., 2)) solver = LinearLUSolver(iterations=10) if __name__ == '__main__': eq.solve(var, boundaryConditions = BCs, solver = solver) from fipy.viewers import make viewer = make(var) viewer.plot() raw_input("finished")
dx = Lx / nx mesh = Grid1D(dx=dx, nx=nx) from fipy.tools import numerix from fipy.variables.cellVariable import CellVariable var = CellVariable(mesh=mesh) from fipy.solvers.linearLUSolver import LinearLUSolver from fipy.boundaryConditions.nthOrderBoundaryCondition import NthOrderBoundaryCondition from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm from fipy.terms.transientTerm import TransientTerm eq = ImplicitDiffusionTerm((1.0, 1.0)) BCs = (NthOrderBoundaryCondition(mesh.getFacesLeft(), 0., 0), NthOrderBoundaryCondition(mesh.getFacesRight(), Lx, 0), NthOrderBoundaryCondition(mesh.getFacesLeft(), 0., 2), NthOrderBoundaryCondition(mesh.getFacesRight(), 0., 2)) solver = LinearLUSolver(iterations=10) if __name__ == '__main__': eq.solve(var, boundaryConditions=BCs, solver=solver) from fipy.viewers import make viewer = make(var) viewer.plot() raw_input("finished")
KCscCoeff = params['alphaKstar'] * params['lambdaK'] * ( KMVar / (1 + PN / params['kappaK'])).getCellVolumeAverage() KCspCoeff = params['lambdaKstar'] / (params['kappaKstar'] + KCVar) KCEq = TransientTerm() - KCscCoeff + ImplicitSourceTerm(KCspCoeff) eqs = ((KMVar, KMEq), (TMVar, TMEq), (TCVar, TCEq), (P3Var, P3Eq), (P2Var, P2Eq), (KCVar, KCEq)) if __name__ == '__main__': from fipy.viewers import make PNView = PN / PN.getCellVolumeAverage() PNView.setName('PN') PNViewer = make(PNView, limits={'datamax': 2., 'datamin': 0.}, title='') KMView = KMVar / KMVar.getCellVolumeAverage() KMView.setName('KM') KMViewer = make(KMView, limits={'datamax': 2., 'datamin': 0.}, title='') TMView = TMVar / TMVar.getCellVolumeAverage() TMView.setName('TM') TMViewer = make(TMView, limits={'datamax': 2., 'datamin': 0.}, title='') for i in range(100): for var, eqn in eqs: var.updateOld() for var, eqn in eqs: eqn.solve(var, dt=1.)
#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()
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
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)) from fipy import viewers viewer = viewers.make(vars=(psi,u,v,U)) eq.solve(var=psi,boundaryConditions=BC) viewer.plot() def hit_continue(Prompt='Hit any key to continue'): raw_input(Prompt) hit_continue()
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()
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() hit_continue() #getOld seems to be not working
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
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
## Define boundary condition values leftValue = 1.0 rightValue = 0 ## Creation of boundary conditions from fipy.boundaryConditions.fixedValue import FixedValue BCs = (FixedValue(faces = mesh_example.getFacesRight(), value=rightValue), FixedValue(faces=mesh_example.getFacesLeft(),value=leftValue)) D = 1.0 ## diffusivity (m^2/s) ## Transient diffusion equation is defined next from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm from fipy.terms.transientTerm import TransientTerm eqX = TransientTerm() == ExplicitDiffusionTerm(coeff = D) timeStep = 0.09*deltax**2/(2*D) ## size of time step steps = 9000 ## number of time-steps ## create a GUI from fipy import viewers viewer = viewers.make(vars = phi, limits={'datamin':0.0, 'datamax':1.0}) ## iterate until you get tired for step in range(steps): eqX.solve(var = phi, boundaryConditions = BCs, dt = timeStep) ## solving the equation viewer.plot() ## update the GUI
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() hit_continue() #getOld seems to be not working
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
P2spCoeff = params['lambda2'] * (TMVar + params['zeta2T']) P2Eq = TransientTerm() - ImplicitDiffusionTerm(params['diffusionCoeff']) - P2scCoeff + ImplicitSourceTerm(P2spCoeff) KCscCoeff = params['alphaKstar'] * params['lambdaK'] * (KMVar / (1 + PN / params['kappaK'])).getCellVolumeAverage() KCspCoeff = params['lambdaKstar'] / (params['kappaKstar'] + KCVar) KCEq = TransientTerm() - KCscCoeff + ImplicitSourceTerm(KCspCoeff) eqs = ((KMVar, KMEq), (TMVar, TMEq), (TCVar, TCEq), (P3Var, P3Eq), (P2Var, P2Eq), (KCVar, KCEq)) if __name__ == '__main__': from fipy.viewers import make PNView = PN / PN.getCellVolumeAverage() PNView.setName('PN') PNViewer = make(PNView, limits = {'datamax' : 2., 'datamin' : 0.}, title = '') KMView = KMVar / KMVar.getCellVolumeAverage() KMView.setName('KM') KMViewer = make(KMView, limits = {'datamax' : 2., 'datamin' : 0.}, title = '') TMView = TMVar / TMVar.getCellVolumeAverage() TMView.setName('TM') TMViewer = make(TMView, limits = {'datamax' : 2., 'datamin' : 0.}, title = '') for i in range(100): for var, eqn in eqs: var.updateOld() for var, eqn in eqs: eqn.solve(var, dt = 1.)