def buildPhaseEquation(phase, theta): mPhiVar = phase - 0.5 + temperature * phase * (1 - phase) thetaMag = theta.getOld().getGrad().getMag() implicitSource = mPhiVar * (phase - (mPhiVar < 0)) implicitSource += (2 * s + epsilon**2 * thetaMag) * thetaMag return TransientTerm(phaseTransientCoeff) == \ ExplicitDiffusionTerm(alpha**2) \ - ImplicitSourceTerm(implicitSource) \ + (mPhiVar > 0) * mPhiVar * phase
def define_ode(self, current_time): x, y = self.mesh.faceCenters # Internal source specificatio - currently no functional internal_source_value = self.parameter.internal_source_value internal_source_region = self.parameter.internal_source_region internal_source_mask = ( (x > internal_source_region.xmin) & (x < internal_source_region.xmax) & (y > internal_source_region.ymin) & (y < internal_source_region.ymax) ) # Get convection data convection = self.define_convection_variable(current_time) eq = TransientTerm() == - ConvectionTerm(coeff=convection) \ + DiffusionTerm(coeff=self.parameter.Diffusivity)\ - ImplicitSourceTerm(coeff=self.parameter.Decay)\ # + ImplicitSourceTerm(coeff=internal_source_value*internal_source_mask) # Internal source not working return eq
phase = CellVariable(name='PhaseField', mesh=mesh, value=1.) from fipy.variables.modularVariable import ModularVariable theta = ModularVariable(name='Theta', mesh=mesh, value=1.) theta.setValue(0., where=mesh.getCellCenters()[..., 0] > L / 2.) from fipy.terms.implicitSourceTerm import ImplicitSourceTerm mPhiVar = phase - 0.5 + temperature * phase * (1 - phase) thetaMag = theta.getOld().getGrad().getMag() implicitSource = mPhiVar * (phase - (mPhiVar < 0)) implicitSource += (2 * s + epsilon**2 * thetaMag) * thetaMag from fipy.terms.transientTerm import TransientTerm from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm phaseEq = TransientTerm(phaseTransientCoeff) == \ ExplicitDiffusionTerm(alpha**2) \ - ImplicitSourceTerm(implicitSource) \ + (mPhiVar > 0) * mPhiVar * phase if __name__ == '__main__': import fipy.viewers phaseViewer = fipy.viewers.make(vars=phase) phaseViewer.plot() for step in range(steps): phaseEq.solve(phase, dt=timeStepDuration) phaseViewer.plot() raw_input('finished')
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()) KMspCoeff = params['lambdaK'] / (1 + PN / params['kappaK']) KMEq = TransientTerm() - KMscCoeff + ImplicitSourceTerm(KMspCoeff) TMscCoeff = params['chiT'] * (1 - TCVar - TMVar.getCellVolumeAverage()) TMspCoeff = params['lambdaT'] * (KMVar + params['zetaT']) TMEq = TransientTerm() - TMscCoeff + ImplicitSourceTerm(TMspCoeff) TCscCoeff = params['lambdaT'] * (TMVar * KMVar).getCellVolumeAverage() TCspCoeff = params['lambdaTstar'] TCEq = TransientTerm() - TCscCoeff + ImplicitSourceTerm(TCspCoeff) PIP2PITP = PN / (PN / params['kappam'] + PN.getCellVolumeAverage() / params['kappac'] + 1) + params['zetaPITP'] P3spCoeff = params['lambda3'] * (TMVar + params['zeta3T']) P3scCoeff = params['chi3'] * KMVar * (PIP2PITP / (1 + KMVar / params['kappa3']) +
def buildMetalIonDiffusionEquation(ionVar=None, distanceVar=None, depositionRate=1, transientCoeff=1, diffusionCoeff=1, metalIonMolarVolume=1): r""" The `MetalIonDiffusionEquation` solves the diffusion of the metal species with a source term at the electrolyte interface. The governing equation is given by, .. math:: \frac{\partial c}{\partial t} = \nabla \cdot D \nabla c where, .. math:: D = \begin{cases} D_c & \text{when $\phi > 0$} \\ 0 & \text{when $\phi \le 0$} \end{cases} The velocity of the interface generally has a linear dependence on ion concentration. The following boundary condition applies at the zero level set, .. math:: D \hat{n} \cdot \nabla c = \frac{v(c)}{\Omega} \qquad \text{at $phi = 0$} where .. math:: v(c) = c V_0 The test case below is for a 1D steady state problem. The solution is given by: .. math:: c(x) = \frac{c^{\infty}}{\Omega D / V_0 + L}\left(x - L\right) + c^{\infty} This is the test case, >>> from fipy.meshes import Grid1D >>> nx = 11 >>> dx = 1. >>> from fipy.tools import serialComm >>> mesh = Grid1D(nx = nx, dx = dx, communicator=serialComm) >>> x, = mesh.cellCenters >>> from fipy.variables.cellVariable import CellVariable >>> ionVar = CellVariable(mesh = mesh, value = 1.) >>> from fipy.variables.distanceVariable \ ... import DistanceVariable >>> disVar = DistanceVariable(mesh = mesh, ... value = (x - 0.5) - 0.99, ... hasOld = 1) >>> v = 1. >>> diffusion = 1. >>> omega = 1. >>> cinf = 1. >>> eqn = buildMetalIonDiffusionEquation(ionVar = ionVar, ... distanceVar = disVar, ... depositionRate = v * ionVar, ... diffusionCoeff = diffusion, ... metalIonMolarVolume = omega) >>> ionVar.constrain(cinf, mesh.facesRight) >>> from builtins import range >>> for i in range(10): ... eqn.solve(ionVar, dt = 1000) >>> L = (nx - 1) * dx - dx / 2 >>> gradient = cinf / (omega * diffusion / v + L) >>> answer = gradient * (x - L - dx * 3 / 2) + cinf >>> answer[x < dx] = 1 >>> print(ionVar.allclose(answer)) 1 Testing the interface source term >>> from fipy.meshes import Grid2D >>> from fipy import numerix, serialComm >>> mesh = Grid2D(dx = 1., dy = 1., nx = 2, ny = 2, communicator=serialComm) >>> from fipy.variables.distanceVariable import DistanceVariable >>> distance = DistanceVariable(mesh = mesh, value = (-.5, .5, .5, 1.5)) >>> ionVar = CellVariable(mesh = mesh, value = (1, 1, 1, 1)) >>> depositionRate = CellVariable(mesh=mesh, value=(1, 1, 1, 1)) >>> source = depositionRate * distance.cellInterfaceAreas / mesh.cellVolumes / ionVar >>> sqrt = numerix.sqrt(2) >>> ans = CellVariable(mesh=mesh, value=(0, 1 / sqrt, 1 / sqrt, 0)) >>> print(numerix.allclose(source, ans)) True >>> distance[:] = (-1.5, -0.5, -0.5, 0.5) >>> print(numerix.allclose(source, (0, 0, 0, sqrt))) True :Parameters: - `ionVar`: The metal ion concentration variable. - `distanceVar`: A `DistanceVariable` object. - `depositionRate`: A float or a `CellVariable` representing the interface deposition rate. - `transientCoeff`: The transient coefficient. - `diffusionCoeff`: The diffusion coefficient - `metalIonMolarVolume`: Molar volume of the metal ions. """ diffusionCoeff = _LevelSetDiffusionVariable(distanceVar, diffusionCoeff) eq = TransientTerm(transientCoeff) - DiffusionTermNoCorrection( diffusionCoeff) mesh = distanceVar.mesh coeff = depositionRate * distanceVar.cellInterfaceAreas / ( mesh.cellVolumes * metalIonMolarVolume) / ionVar return eq + ImplicitSourceTerm(coeff)
def buildSurfactantBulkDiffusionEquation(bulkVar=None, distanceVar=None, surfactantVar=None, otherSurfactantVar=None, diffusionCoeff=None, transientCoeff=1., rateConstant=None): r""" The `buildSurfactantBulkDiffusionEquation` function returns a bulk diffusion of a species with a source term for the jump from the bulk to an interface. The governing equation is given by, .. math:: \frac{\partial c}{\partial t} = \nabla \cdot D \nabla c where, .. math:: D = \begin{cases} D_c & \text{when $\phi > 0$} \\ 0 & \text{when $\phi \le 0$} \end{cases} The jump condition at the interface is defined by Langmuir adsorption. Langmuir adsorption essentially states that the ability for a species to jump from an electrolyte to an interface is proportional to the concentration in the electrolyte, available site density and a jump coefficient. The boundary condition at the interface is given by .. math:: D \hat{n} \cdot \nabla c = -k c (1 - \theta) \qquad \text{at $\phi = 0$}. Parameters ---------- bulkVar : ~fipy.variables.cellVariable.CellVariable The bulk surfactant concentration variable. distanceVar : ~fipy.variables.distanceVariable.DistanceVariable surfactantVar : ~fipy.variables.surfactantVariable.SurfactantVariable otherSurfactantVar : ~fipy.variables.surfactantVariable.SurfactantVariable Any other surfactants that may remove this one. diffusionCoeff : float or ~fipy.variables.faceVariable.FaceVariable transientCoeff : float In general 1 is used. rateConstant : float The adsorption coefficient. """ spCoeff = rateConstant * distanceVar.cellInterfaceAreas / bulkVar.mesh.cellVolumes spSourceTerm = ImplicitSourceTerm(spCoeff) bulkSpCoeff = spCoeff * bulkVar coeff = bulkSpCoeff * surfactantVar.interfaceVar diffusionCoeff = _LevelSetDiffusionVariable(distanceVar, diffusionCoeff) eq = TransientTerm(transientCoeff) - DiffusionTermNoCorrection( diffusionCoeff) if otherSurfactantVar is not None: otherCoeff = bulkSpCoeff * otherSurfactantVar.interfaceVar else: otherCoeff = 0 return eq - coeff + spSourceTerm - otherCoeff
phaseY = phase.getFaceGrad().dot((0, 1)) phaseX = phase.getFaceGrad().dot((1, 0)) psi = theta + numerix.arctan2(phaseY, phaseX) Phi = numerix.tan(N * psi / 2) PhiSq = Phi**2 beta = (1. - PhiSq) / (1. + PhiSq) betaPsi = -N * 2 * Phi / (1 + PhiSq) A = alpha**2 * c * (1. + c * beta) * betaPsi D = alpha**2 * (1. + c * beta)**2 dxi = phase.getFaceGrad()._take((1, 0), axis=1) * (-1, 1) anisotropySource = (A * dxi).getDivergence() from fipy.terms.transientTerm import TransientTerm from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm from fipy.terms.implicitSourceTerm import ImplicitSourceTerm phaseEq = TransientTerm(tau) == ExplicitDiffusionTerm(D) + \ ImplicitSourceTerm(mVar * ((mVar < 0) - phase)) + \ ((mVar > 0.) * mVar * phase + anisotropySource) from fipy.terms.implicitDiffusionTerm import ImplicitDiffusionTerm temperatureEq = TransientTerm() == \ ImplicitDiffusionTerm(tempDiffusionCoeff) + \ (phase - phase.getOld()) / timeStepDuration bench.stop('terms') phase.updateOld() temperature.updateOld() phaseEq.solve(phase, dt=timeStepDuration) temperatureEq.solve(temperature, dt=timeStepDuration) steps = 10
def __init__(self, surfactantVar=None, distanceVar=None, bulkVar=None, rateConstant=None, otherVar=None, otherBulkVar=None, otherRateConstant=None, consumptionCoeff=None): """ Create a `AdsorbingSurfactantEquation` object. :Parameters: - `surfactantVar`: The `SurfactantVariable` to be solved for. - `distanceVar`: The `DistanceVariable` that marks the interface. - `bulkVar`: The value of the `surfactantVar` in the bulk. - `rateConstant`: The adsorption rate of the `surfactantVar`. - `otherVar`: Another `SurfactantVariable` with more surface affinity. - `otherBulkVar`: The value of the `otherVar` in the bulk. - `otherRateConstant`: The adsorption rate of the `otherVar`. - `consumptionCoeff`: The rate that the `surfactantVar` is consumed during deposition. """ self.eq = TransientTerm(coeff=1) - ExplicitUpwindConvectionTerm( SurfactantConvectionVariable(distanceVar)) self.dt = Variable(0.) mesh = distanceVar.mesh adsorptionCoeff = self.dt * bulkVar * rateConstant spCoeff = adsorptionCoeff * distanceVar._cellInterfaceFlag scCoeff = adsorptionCoeff * distanceVar.cellInterfaceAreas / mesh.cellVolumes self.eq += ImplicitSourceTerm(spCoeff) - scCoeff if otherVar is not None: otherSpCoeff = self.dt * otherBulkVar * otherRateConstant * distanceVar._cellInterfaceFlag otherScCoeff = -otherVar.interfaceVar * scCoeff self.eq += ImplicitSourceTerm(otherSpCoeff) - otherScCoeff vars = (surfactantVar, otherVar) else: vars = (surfactantVar, ) total = 0 for var in vars: total += var.interfaceVar maxVar = (total > 1) * distanceVar._cellInterfaceFlag val = distanceVar.cellInterfaceAreas / mesh.cellVolumes for var in vars[1:]: val -= distanceVar._cellInterfaceFlag * var spMaxCoeff = 1e20 * maxVar scMaxCoeff = spMaxCoeff * val * (val > 0) self.eq += ImplicitSourceTerm(spMaxCoeff) - scMaxCoeff - 1e-40 if consumptionCoeff is not None: self.eq += ImplicitSourceTerm(consumptionCoeff)