def set_D_from_data(phys, data): if data is not None: func, mesh = fields.get_functions(**data) D = func["D"] #dolfin.plot(D[0], title="Dx", interactive=True) D = dolfin.as_matrix(np.diag([D[i] for i in range(phys.dim)])) phys.update(Dp=D, Dm=D)
def force_diff(**params): # for random walk (with pointsize force field, no current) setup = Setup(create_geo=False, **params) # DEBUG #print "active params", setup.active_params #print "inactive params", setup.inactive_params F = force_pointsize(**params) if setup.phys.posDTarget: D = diffusivity_simple(**params) name = "diffusivity_div_simple" if not fields.exists(name, **params): V = D.function_space() divD = dolfin.project( dolfin.as_vector([dolfin.grad(D[0])[0], dolfin.grad(D[1])[1]]), V) fields.save_functions(name, setup.active_params, divD=divD) fields.update() divD, = fields.get_functions(name, "divD", **params) else: D0 = setup.phys.DTargetBulk D0a = np.array([D0, D0, D0]) divDa = np.array([0., 0., 0.]) D = lambda x: D0a divD = lambda x: divDa return F, D, divD
def diffusivity_simple(**params): from nanopores.models.diffusion_interpolation import diffusivity_field name = "diffusivity_simple" if not fields.exists(name, **params): setup = Setup(**params) dic = diffusivity_field(setup, r=params["rMolecule"], boundary="poresolidb") fields.save_functions(name, setup.active_params, **dic) fields.update() D, = fields.get_functions(name, "D", **params) return D
def force_pointsize(**params): name = "force_pointsize" if not fields.exists(name, **params): setup = Setup(**params) #print setup.geo #setup.geo.plot_boundaries(interactive=True) _, pnps = solve(setup, True) v, cp, cm, u, p = pnps.solutions() F, Fel, Fdrag = setup.phys.ForceField(v, u, "fluid") fields.save_functions(name, setup.active_params, F=F, Fel=Fel, Fdrag=Fdrag) fields.update() F, = fields.get_functions(name, "F", **params) return F
def plot_streamlines(self, both=False, Hbot=None, Htop=None, R=None, **params): R = self.params.R if R is None else R Htop = self.params.Htop if Htop is None else Htop Hbot = self.params.Hbot if Hbot is None else Hbot #ax = plt.axes(xlim=(-R, R), ylim=(-Hbot, Htop)) dolfin.parameters["allow_extrapolation"] = True if both: Fel, Fdrag = fields.get_functions("force_pointsize", "Fel", "Fdrag", **self.sim_params) streamlines( patches=[self.polygon_patches(), self.polygon_patches()], R=R, Htop=Htop, Hbot=Hbot, Nx=100, Ny=100, Fel=Fel, Fdrag=Fdrag, **params) else: streamlines(patches=[self.polygon_patches()], R=R, Htop=Htop, Hbot=Hbot, Nx=100, Ny=100, F=self.F, **params) dolfin.parameters["allow_extrapolation"] = False # for p in patches: # p.set_zorder(100) # plt.gca().add_patch(p) plt.xlim(-R, R) plt.ylim(-Hbot, Htop)
# (c) 2016 Gregor Mitscha-Baude from dolfin import * from nanopores.tools import fields # set save/load directory fields.set_dir("/tmp/nanopores/") mesh = UnitSquareMesh(10, 10) V = FunctionSpace(mesh, "CG", 1) u = Function(V) u.interpolate(Expression("sin(x[0]*x[1]*4*pi)")) if not fields.exists("test_save"): fields.save_functions("test_save", {}, utest=u) fields.update() functions, mesh = fields.get_functions("test_save") u1 = functions["utest"] plot(u1, interactive=True)
import nanopores.models.pughpore as pughm from nanopores.tools import fields import nanopores import folders nanopores.add_params(h=1.) functions, mesh = fields.get_functions("pugh_distance", h=h) y = functions["y"] pughm.Plotter().plot(y, title="distance", interactive=True)
params = dict( h=0.5, Nmax=3e4, bV=0.0, ) name = "pot-ahem" if not fields.exists(name, **params): setup = model.Setup(**params) pb, pnps = model.solve(setup) v = pnps.solutions()[0] fields.save_functions(name, params, v=v) fields.update() fun, mesh = fields.get_functions(name, **params) f = fun["v"] def F(x, z): if x >= 0: return f([x, z]) else: return f([-x, z]) R, Htop, Hbot = 7, 2, 12 fig = plt.figure(figsize=(2.6, 2.15)) ax = plt.axes(xlim=(-R, R), ylim=(-Hbot, Htop)) tr, vertex_values = mesh2triang(mesh)
def get_pugh_diffusivity_alt(**params): cache_pugh_diffusivity_alt(**params) functions, mesh = fields.get_functions("Dpugh_alt", **params) return functions, mesh
v, cp, cm, u, p = pnps.solutions() F, Fel, Fdrag = setup.phys.ForceField(v, u, "fluid") fields.save_functions(name, params, F=F, Fel=Fel, Fdrag=Fdrag) fields.update() name_D = "wei_D_2D" if not fields.exists(name_D, **params): setup = nanopore.Setup(**params) dic = diffusivity_field(setup, r=params["rMolecule"], boundary="poresolidb") fields.save_functions(name_D, params, **dic) fields.update() F, Fel, Fdrag = fields.get_functions(name, "F", "Fel", "Fdrag", **params) D, dist = fields.get_functions(name_D, "D", "dist", **params) if __name__ == "__main__": Fplot = nano.user_param(Fplot=False) Dplot = nano.user_param(Dplot=False) if Fplot: plt = nanopore.Plotter(dim=2) plt.plot_vector(F, title="F", interactive=True) #plt.plot_vector(Fel, title="Fel") #plt.plot_vector(Fdrag, title="Fdrag", interactive=True) pore = WeiPore() params = nano.Params(pore.default, **params) sam, au, sin, _ = pore.polygons(params) poly = MultiPolygon(Polygon(sam), Polygon(au), Polygon(sin)).nodes
def set_D_with_protein(setup): meshp = setup.geo.mesh # mesh WITH protein x0 = np.array(setup.geop.x0) dim = setup.phys.dim x0 = x0 if dim == 3 else x0[::2] r0 = setup.geop.rMolecule rion = 0.11 # load diffusivity on mesh without protein functions, mesh = fields.get_functions(**setup.solverp.diffusivity_data) #dist = functions["dist"] D0 = functions["D"] # evaluate dist, D on meshp nodes #Vp = dolfin.FunctionSpace(meshp, "CG", 1) VVp = dolfin.VectorFunctionSpace(meshp, "CG", 1) #distp_ = dolfin.interpolate(dist, Vp) D0p_ = dolfin.interpolate(D0, VVp) #distp = distp_.vector()[dolfin.vertex_to_dof_map(Vp)] D0p = D0p_.vector()[dolfin.vertex_to_dof_map(VVp)] D0p = np.column_stack([D0p[i::dim] for i in range(dim)]) x = meshp.coordinates() # first create (N,3,3) array from D0 (N,3) N = len(D0p) Da = np.zeros((N, dim, dim)) i3 = np.array(range(dim)) Da[:, i3, i3] = D0p # modify (N,3,3) array to include protein interaction R = x - x0 r = np.sqrt(np.sum(R**2, 1)) overlap = r < rion + r0 #near = ~overlap & (r - r0 < distp) h = np.maximum(r - r0, rion) eps = 1e-2 D00 = setup.phys.D Dt = np.zeros_like(r) Dn = np.zeros_like(r) Dt[overlap] = eps Dn[overlap] = eps Dt[~overlap] = Dt_plane(h[~overlap], rion) Dn[~overlap] = Dn_plane(h[~overlap], rion, N=20) # D(R) = Dn(h) RR^T + Dt(h) (I - RR^T) where R is normalized R0 = R / (r[:, None] + 1e-100) RR = (R0[:, :, None] * R0[:, None, :]) I = np.zeros((N, dim, dim)) I[:, i3, i3] = 1. Dpp = D00 * (Dn[:, None, None] * RR + Dt[:, None, None] * (I - RR)) near = Dpp[:, dim - 1, dim - 1] < Da[:, dim - 1, dim - 1] Da[near] = Dpp[near] # assign final result to dolfin P1 TensorFunction VVV = dolfin.TensorFunctionSpace(meshp, "CG", 1, shape=(dim, dim)) D = dolfin.Function(VVV) v2d = dolfin.vertex_to_dof_map(VVV) Dv = D.vector() for k, (i, j) in enumerate(product(i3, i3)): Dv[np.ascontiguousarray(v2d[k::dim**2])] = np.ascontiguousarray(Da[:, i, j]) setup.phys.update(Dp=D, Dm=D) return D
def get_diffusivity(geoname="alphahem", mode="coupled", **params): name = cache_diffusivity(geoname, mode, **params) functions, mesh = fields.get_functions(name=name, **params) D = functions["D"] return D
def get_diffusivity_simple(geoname="alphahem", **params): cache_diffusivity_simple(geoname, **params) name = "D%s" % (geoname, ) functions, mesh = fields.get_functions(name=name, **params) D = functions["D"] return D
#def new_get_geo(**params): # return get_geo(poly, **params) # #p = Pore(poly, **default) #p.build(h=.5) # #p.polygons["alphahem"].plot("ok") #p.polygons["membrane"].plot() #p.polygons["bulkfluid_top"].plot() #p.polygons["bulkfluid_bottom"].plot() #plt.show() #setup = Setup(get_geo=new_get_geo, geop=default, h=.5) #setup = Setup(h=.5) #setup.geo.plot_boundaries() functions, mesh = fields.get_functions(name="Dalphahem-coupled", **params) dist = functions["dist"] #dolfin.plot(dist, interactive=True) # construct D fit from Noskov2004 and plot tabulated D values A = 0.64309 B = 0.00044 C = 0.06894 D = 0.35647 E = 0.19409 def Dfit(z, rion=0.11): rpore = dist([0., z]) beta = rion / rpore
def get_diffusivity(**params): cache_diffusivity(**params) functions, mesh = fields.get_functions("Diffusivity", **params) return functions, mesh
def set_D_from_data(phys, data): if data is not None: func, mesh = fields.get_functions(**data) D = func["D"] D = dolfin.as_matrix(np.diag([D[i] for i in range(phys.dim)])) phys.update(Dp=D, Dm=D)