from fipy.meshes.grid2D import Grid2D from fipy.solvers.linearPCGSolver import LinearPCGSolver from fipy.variables.cellVariable import CellVariable from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm nx = 2 valueLeft = 0. fluxRight = 1. timeStepDuration = 1. L = 1. dx = L / nx mesh = Grid2D(dx=dx, nx=nx) var = CellVariable(name="solution variable", mesh=mesh, value=valueLeft) x = mesh.getFaceCenters()[:, 0] middleFaces = numerix.logical_or(x < L / 4., x >= 3. * L / 4.) diffCoeff = numerix.where(middleFaces, 1., 0.1) boundaryConditions = (FixedValue(mesh.getFacesLeft(), valueLeft), FixedFlux(mesh.getFacesRight(), fluxRight)) if __name__ == '__main__': ImplicitDiffusionTerm(coeff=diffCoeff).solve( var, boundaryConditions=boundaryConditions) import fipy.viewers
from parameters import parameters from fipy.meshes.grid2D import Grid2D from fipy.variables.cellVariable import CellVariable from fipy.terms.transientTerm import TransientTerm from fipy.terms.implicitSourceTerm import ImplicitSourceTerm from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm params = parameters['case 2'] nx = 50 ny = 50 dx = 1. L = nx * dx mesh = Grid2D(nx=nx, ny=ny, dx=dx, dy=1.) shift = 1. KMVar = CellVariable(mesh=mesh, value=params['KM'] * shift, hasOld=1) KCVar = CellVariable(mesh=mesh, value=params['KC'] * shift, hasOld=1) TMVar = CellVariable(mesh=mesh, value=params['TM'] * shift, hasOld=1) TCVar = CellVariable(mesh=mesh, value=params['TC'] * shift, hasOld=1) P3Var = CellVariable(mesh=mesh, value=params['P3'] * shift, hasOld=1) P2Var = CellVariable(mesh=mesh, value=params['P2'] * shift, hasOld=1) RVar = CellVariable(mesh=mesh, value=params['R'], hasOld=1) PN = P3Var + P2Var KMscCoeff = params['chiK'] * (RVar + 1) * (1 - KCVar - KMVar.getCellVolumeAverage())
$ python setup.py efficiency_test """ __docformat__ = 'restructuredtext' if __name__ == "__main__": from fipy.tools.parser import parse from benchmarker import Benchmarker bench = Benchmarker() numberOfElements = parse('--numberOfElements', action='store', type='int', default=100) bench.start() from fipy.tools import numerix nx = int(numerix.sqrt(numberOfElements)) ny = nx dx = 1. dy = 1. from fipy.meshes.grid2D import Grid2D mesh = Grid2D(nx=nx, ny=nx, dx=dx, dy=dy) bench.stop('mesh') print bench.report(numberOfElements=numberOfElements)
L = 1. dx = 0.1 velocity = 1. cfl = 0.1 distanceToTravel = L / 5. boxSize = .2 nx = int(L / dx) ny = int(L / dx) steps = int(distanceToTravel / dx / cfl) timeStepDuration = cfl * dx / velocity mesh = Grid2D(dx=dx, dy=dx, nx=nx, ny=ny) x0 = (L - boxSize) / 2 x1 = (L + boxSize) / 2 distanceVariable = DistanceVariable(mesh=mesh, value=1, hasOld=1) x, y = mesh.getCellCenters()[..., 0], mesh.getCellCenters()[..., 1] distanceVariable.setValue(-1, where=((x0 < x) & (x < x1)) & ((x0 < y) & (y < x1))) surfactantVariable = SurfactantVariable(distanceVar=distanceVariable, value=1.) surfactantEquation = SurfactantEquation(distanceVar=distanceVariable) advectionEquation = buildHigherOrderAdvectionEquation(advectionCoeff=velocity)
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
#!/usr/bin/env python L = 1.0 N = 40 dL = L / N viscosity = 1. pressureRelaxation = 0.2 velocityRelaxation = 0.5 sweeps=10 from fipy.meshes.grid2D import Grid2D mesh = Grid2D(nx=N, ny=N, dx=dL, dy=dL) from fipy.variables.cellVariable import CellVariable psi = CellVariable(mesh=mesh, name='stream function') Xhat=(1,0) Yhat=(0,1) 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)
import fipy.tools.numerix as numerix if numberOfElements != -1: pos = trenchSpacing * cellsBelowTrench / 4 / numberOfElements sqr = trenchSpacing * (trenchDepth + boundaryLayerDepth) \ / (2 * numberOfElements) cellSize = pos + numerix.sqrt(pos**2 + sqr) else: cellSize = 0.1e-7 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) bench.stop('mesh') bench.start() narrowBandWidth = numberOfCellsInNarrowBand * cellSize from fipy.models.levelSet.distanceFunction.distanceVariable import \ DistanceVariable distanceVar = DistanceVariable(name='distance variable', mesh=mesh, value=-1, narrowBandWidth=narrowBandWidth, hasOld=1)
nx = int(numerix.sqrt(numberOfElements)) ny = int(numerix.sqrt(numberOfElements)) steps = numberOfSteps dx = 2. dy = 2. L = dx * nx asq = 1.0 epsilon = 1 diffusionCoeff = 1 from fipy.meshes.grid2D import Grid2D mesh = Grid2D(dx, dy, nx, ny) from fipy.variables.cellVariable import CellVariable from fipy.tools.numerix import random var = CellVariable(name="phase field", mesh=mesh, value=random.random(nx * ny)) faceVar = var.getArithmeticFaceValue() doubleWellDerivative = asq * (1 - 6 * faceVar * (1 - faceVar)) from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm from fipy.terms.transientTerm import TransientTerm diffTerm2 = ImplicitDiffusionTerm(coeff=(diffusionCoeff * doubleWellDerivative, )) diffTerm4 = ImplicitDiffusionTerm(coeff=(diffusionCoeff, -epsilon**2)) eqch = TransientTerm() - diffTerm2 - diffTerm4
action='store', type='int', default=400) bench.start() import fipy.tools.numerix as numerix steps = 10 nx = int(numerix.sqrt(numberOfElements)) ny = nx Lx = 2.5 * nx / 100. dx = Lx / nx from fipy.meshes.grid2D import Grid2D mesh = Grid2D(dx, dx, nx, nx) bench.stop('mesh') timeStepDuration = 0.02 phaseTransientCoeff = 0.1 thetaSmallValue = 1e-6 beta = 1e5 mu = 1e3 thetaTransientCoeff = 0.01 gamma = 1e3 epsilon = 0.008 s = 0.01 alpha = 0.015 temperature = 10.
import fipy import numpy as np import matplotlib.pyplot as plt # fipy example from https://math.nist.gov/mcsd/Seminars/2005/2005-03-01-wheeler-presentation.pdf nx = 10 dx = 1 steps = 100 # create a mesh: L = nx * dx from fipy.meshes.grid2D import Grid2D mesh = Grid2D(nx=nx, dx=dx) # create a field variable, set the initial conditions: from fipy.variables.cellVariable import CellVariable var = CellVariable(mesh=mesh, value=0) def centerCells(cell): return abs(cell.getCenter()[0] - L / 2.0) < L / 10 var.setValue(value=1.0, cells=mesh.getCells(filter=centerCells)) # create the equation: from fipy.terms.transientTerm import TransientTerm from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm
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", mesh=mesh2, value=valueBottomTop) boundaryConditions2 = (FixedValue(mesh2.getFacesLeft(), valueLeftRight), FixedValue(mesh2.getFacesRight(), valueLeftRight), FixedValue(mesh2.getFacesTop(), valueBottomTop), FixedValue(mesh2.getFacesBottom(), valueBottomTop)) eqn = ImplicitDiffusionTerm() if __name__ == '__main__': eqn.solve(var2, boundaryConditions=boundaryConditions2) from fipy.viewers import make