Commit 14f0bc3d by Jigyasa Watwani

with c

parent c249b22c
Showing with 189 additions and 0 deletions
import dolfin as df
import numpy as np
import progressbar
df.set_log_level(df.LogLevel.ERROR)
df.parameters['form_compiler']['optimize'] = True
class Growth(object):
def __init__(self, parameters):
# read in parameters
for key in parameters:
setattr(self, key, parameters[key])
# create mesh, mixed element and function space
self.mesh = df.IntervalMesh(self.resolution, 0.0, 2.0*np.pi*self.system_size_by_2PI)
scalar_element = df.FiniteElement('P', self.mesh.ufl_cell(), 1)
mixed_element = df.MixedElement([scalar_element, scalar_element, scalar_element, scalar_element]) # u, v, rho, c
# define function space with this mixed element
self.function_space = df.FunctionSpace(self.mesh, mixed_element)
# dirichlet boundaries for u, v
bc1 = df.DirichletBC(self.function_space.sub(0), df.Constant(0.0), 'on_boundary') # u,v at boundary = 0
bc2 = df.DirichletBC(self.function_space.sub(1), df.Constant(0.0), 'on_boundary')
self.bc = ([bc1,bc2])
# define the reqd functions on this function space
self.f = df.Function(self.function_space) # f at current time
self.fminus1 = df.Function(self.function_space) # f at t = -1
self.f0 = df.Function(self.function_space) # f at t =0
def advection(self, conc, vel, tconc):
return (df.inner((vel * conc).dx(0), tconc))
def active_stress(self, rho, c):
return self.lamda * (rho/(rho + self.saturation_rho)) * c
def passive_stress(self, u, v):
return (self.elasticity * u.dx(0) + self.viscosity * v.dx(0))
def stress(self, u, v, rho, c):
return (self.passive_stress(u, v) + self.active_stress(rho, c))
def reaction_rho(self, rho, trho):
return self.turnover_rho * df.inner(rho - self.average_rho, trho)
def diffusion_reaction_rho(self, rho, trho):
return (self.diffusion_rho * df.inner(rho.dx(0), trho.dx(0)) + self.reaction_rho(rho, trho))
def reaction_c(self, c, tc):
return self.turnover_c * df.inner(c - self.average_c, tc)
def diffusion_reaction_c(self, c, tc):
return (self.diffusion_c * df.inner(c.dx(0), tc.dx(0)) + self.reaction_c(c, tc))
def setup_initial_conditions(self, u0, rho0, c0):
u0 = df.interpolate(u0, self.function_space.sub(0).collapse())
rho0 = df.interpolate(rho0, self.function_space.sub(2).collapse())
c0 = df.interpolate(c0, self.function_space.sub(3).collapse())
v0_function_space = self.function_space.sub(1).collapse()
v0 = df.Function(v0_function_space)
tv0 = df.TestFunction(v0_function_space)
v0form = (self.friction * df.inner(v0, tv0)
+ df.inner(self.stress(u0, v0, rho0, c0), tv0.dx(0))
) * df.dx
bcv0 = df.DirichletBC(v0_function_space, df.Constant(0.0), 'on_boundary')
df.solve(v0form == 0, v0, bcv0)
df.assign(self.f0, [u0, v0, rho0, c0])
self.fminus1.assign(self.f0)
def setup_weak_forms(self):
uminus1, vminus1, rhominus1, cminus1 = df.split(self.fminus1)
u0, v0 , rho0, c0 = df.split(self.f0)
u, v, rho, c = df.split(self.f)
tu, tv, trho, tc = df.TestFunctions(self.function_space)
uform = (df.inner((u-u0)/self.timestep, tu)
- 9/16 * df.inner(v, tu)
- 3/8 * df.inner(v0, tu)
- 1/16 * df.inner(vminus1, tu)
)
vform = (self.friction * df.inner(v, tv)
+ df.inner(self.stress(u, v, rho, c), tv.dx(0))
)
rhoform = (df.inner((rho - rho0)/self.timestep, trho)
+ 3/2 * self.advection(rho0, v, trho)
- 1/2 * self.advection(rhominus1, v, trho)
+ 9/16 * self.diffusion_reaction_rho(rho, trho)
+ 3/8 * self.diffusion_reaction_rho(rho0, trho)
+ 1/16 * self.diffusion_reaction_rho(rhominus1, trho)
)
cform = (df.inner((c - c0)/self.timestep, tc)
+ 3/2 * self.advection(c0, v, tc)
- 1/2 * self.advection(cminus1, v, tc)
+ 9/16 * self.diffusion_reaction_c(c, tc)
+ 3/8 * self.diffusion_reaction_c(c0, tc)
+ 1/16 * self.diffusion_reaction_c(cminus1, tc)
)
self.form = (uform + vform + rhoform + cform) * df.dx
def solve(self, u0, rho0, c0):
times = np.arange(0.0, self.maxtime, self.timestep)
x = self.mesh.coordinates()
u_array = np.zeros((len(times), len(x)))
v_array = np.zeros_like(u_array)
rho_array = np.zeros_like(u_array)
c_array = np.zeros_like(u_array)
self.setup_initial_conditions(u0, rho0, c0)
self.setup_weak_forms()
u, v, rho, c = self.f0.split(deepcopy=True)
u_array[0] = u.compute_vertex_values(self.mesh)
v_array[0] = v.compute_vertex_values(self.mesh)
rho_array[0] = rho.compute_vertex_values(self.mesh)
c_array[0] = c.compute_vertex_values(self.mesh)
for i in progressbar.progressbar(range(0, len(times))):
df.solve(self.form == 0,self.f, self.bc)
u, v, rho, c = self.f.split(deepcopy=True)
u_array[i] = u.compute_vertex_values(self.mesh)
v_array[i] = v.compute_vertex_values(self.mesh)
rho_array[i] = rho.compute_vertex_values(self.mesh)
c_array[i] = c.compute_vertex_values(self.mesh)
self.f0.assign(self.f)
self.fminus1.assign(self.f0)
return (u_array, v_array, rho_array, c_array)
if __name__ == '__main__':
import json
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
with open('growth_parameters.json') as jsonFile:
params = json.load(jsonFile)
g = Growth(params)
u0 = df.Expression('sin(x[0])', degree=1)
rho0 = df.Expression('rho0 + 0.1 * (cos(x[0])+cos(x[0]/2))', rho0 =g.average_rho, degree=1)
c0 = df.Expression('c0 + 0.001 * cos(x[0]/2)', c0 = g.average_c, degree=1)
(u, v, rho, c) = g.solve(u0, rho0, c0)
x = g.mesh.coordinates()
times = np.arange(0, g.maxtime, g.timestep)
fig, (axu, axv, axrho, axc) = plt.subplots(4,1, sharex=True, figsize=(8,6))
axu.set_xlabel(r'$x$')
axu.set_ylabel(r'$u(x,t)$')
axv.set_ylabel(r'$v(x,t)$')
axrho.set_ylabel(r'$\rho(x,t)$')
axc.set_ylabel(r'$c(x,t)$')
axu.set_ylim(np.min(u), np.max(u))
axv.set_ylim(np.min(v), np.max(v))
axrho.set_ylim(np.min(rho), np.max(rho))
axc.set_ylim(np.min(c), np.max(c))
uplot, = axu.plot(x, u[0])
vplot, = axv.plot(x, v[0])
rhoplot, = axrho.plot(x, rho[0])
cplot, = axc.plot(x, c[0])
def update(value):
ti = np.abs(times-value).argmin()
uplot.set_ydata(u[ti])
vplot.set_ydata(v[ti])
rhoplot.set_ydata(rho[ti])
cplot.set_ydata(c[ti])
plt.draw()
sax = plt.axes([0.1, 0.92, 0.7, 0.02])
slider = Slider(sax, r'$t/\tau$', min(times), max(times),
valinit=min(times), valfmt='%3.1f',
fc='#999999')
slider.drawon = False
slider.on_changed(update)
plt.show()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment