def __init__(self, X=None, T=None, time_steps=5, max_time=0.4, num_cells=25, L=1.): """Initialize the objet. Keyword Arguments: X --- The sensor locations. T --- The sensor measurment times. time_steps --- How many timesteps do you want to measure. max_time --- The maximum solution time. num_cells --- The number of cells per dimension. L --- The size of the computational domain. """ assert isinstance(num_cells, int) self._num_cells = num_cells assert isinstance(L, float) and L > 0. self._dx = L / self.num_cells self._mesh = Grid2D(dx=self.dx, dy=self.dx, nx=self.num_cells, ny=self.num_cells) self._phi = CellVariable(name='solution variable', mesh=self.mesh) self._source = CellVariable(name='source term', mesh=self.mesh, hasOld=True) self._eqX = TransientTerm() == ExplicitDiffusionTerm( coeff=1.) + self.source self._eqI = TransientTerm() == DiffusionTerm(coeff=1.) + self.source self._eq = self._eqX + self._eqI assert isinstance(max_time, float) and max_time > 0. self._max_time = max_time #self.max_time / time_steps #. if X is None: idx = range(self.num_cells**2) else: idx = [] x1, x2 = self.mesh.cellCenters for x in X: dist = (x1 - x[0])**2 + (x2 - x[1])**2 idx.append(np.argmin(dist)) self._idx = idx if T is None: T = np.linspace(0, self.max_time, time_steps)[1:] self._max_time = T[-1] self._T = T self._dt = self.T[0] / time_steps super(Diffusion, self).__init__(5, len(self.T) * len(self.idx), name='Diffusion Solver')
alpha = 0.015 temperature = 1. dx = L / nx mesh = Grid1D(dx=dx, nx=nx) phase = CellVariable(name='PhaseField', mesh=mesh, value=1.) theta = ModularVariable(name='Theta', mesh=mesh, value=1.) theta.setValue(0., where=mesh.cellCenters[0] > L / 2.) mPhiVar = phase - 0.5 + temperature * phase * (1 - phase) thetaMag = theta.old.grad.mag implicitSource = mPhiVar * (phase - (mPhiVar < 0)) implicitSource += (2 * s + epsilon**2 * thetaMag) * thetaMag phaseEq = TransientTerm(phaseTransientCoeff) == \ ExplicitDiffusionTerm(alpha**2) \ - ImplicitSourceTerm(implicitSource) \ + (mPhiVar > 0) * mPhiVar * phase if __name__ == '__main__': phaseViewer = Viewer(vars=phase) phaseViewer.plot() for step in range(steps): phaseEq.solve(phase, dt=timeStepDuration) phaseViewer.plot() input('finished')
from fipy import CellVariable, Tri2D, TransientTerm, ExplicitDiffusionTerm, DefaultSolver, Viewer from fipy.tools import numerix dx = 1. dy = 1. nx = 10 ny = 1 valueLeft = 0. valueRight = 1. timeStepDuration = 0.02 mesh = Tri2D(dx, dy, nx, ny) var = CellVariable(name="concentration", mesh=mesh, value=valueLeft) eq = TransientTerm() == ExplicitDiffusionTerm() solver = DefaultSolver(tolerance=1e-6, iterations=1000) var.constrain(valueLeft, mesh.facesLeft) var.constrain(valueRight, mesh.facesRight) answer = numerix.array([ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.58508452e-07, 6.84325019e-04, 7.05111362e-02, 7.81376523e-01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.99169535e-05, 1.49682805e-02, 3.82262622e-01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.06838361e-06, 3.67632029e-03, 1.82227062e-01, 0.00000000e+00, 0.00000000e+00,
# Provide value for diffusion coefficient D = 1.0 # Setting boundary conditions valueLeft = 1.0 valueRight = 0.0 c.constrain(valueLeft, mesh.facesLeft) c.constrain(valueRight, mesh.facesRight) # Specifying our alpha value alpha = 0 # Defining the equation with alpha which gives us the flexibility to choose the time-stepping scheme eq = (TransientTerm() == DiffusionTerm(coeff= alpha* D) + ExplicitDiffusionTerm(coeff = (1.0 - alpha)*D)) # dt = 0.00018 should be stable for forward Euler with the default simulation conditions dt = 0.00018 # We might not want to see the output from every single timestep, so we define a stride parameter time_stride = 1 timestep = 0 run_time = 1 # We need to implement the analytical solution. We can do two solutions: 1) Full solution, 2) S.S. solution pi = numerix.pi x = mesh.cellCenters[0] t = Variable(0.0)
try: print("\n done") #Intermediate Print Statement viewer = VTKViewer(vars=phi,datamin=0., datamax=1.) #Changed Statement print("\n Daemon file") #Intermediate Print Statement except (NameError,ImportError,SystemError,TypeError): viewer = VTKViewer(vars=phi,datamin=0., datamax=1.) #Changed Statement viewer.plot(filename="trial.vtk") #It will only save the final vtk file. You can change the name eqI = (TransientTerm() == DiffusionTerm(coeff=D) - ExponentialConvectionTerm(coeff=Teff)) # doctest: +GMSH eqX = (TransientTerm() == ExplicitDiffusionTerm(coeff=D) - ExponentialConvectionTerm(coeff=Teff)) # doctest: +GMSH steps = 1000 timeStepDuration = 1e-6 time = 0 for step in range(steps): if step < 0 : eqX.solve(var=phi, dt=timeStepDuration) else: eqI.solve(var=phi, dt=timeStepDuration) time = time + timeStepDuration if __name__ == "__main__":#Parameters to be changed are in the seciton below. viewer.plot(filename="trial.vtk") #It will only save the final vtk file. You can change the name