def velocities(X, **params): v0, v1 = [], [] for x0 in X: setup = Howorka.Setup(x0=x0, **params) f, v, dv = velocity_iteration(setup, 6) assert np.linalg.norm(1e12*f[-1]) < 1e-3 #assert np.linalg.norm(dv[-1]) < 1e-3*np.linalg.norm(dv[0]) v0.append(list(v[0])) v1.append(list(v[-1])) return dict(v0=v0, v1=v1)
def F_geo_phys(overwrite=False, implicit=False, **params): "returns force field and corresponding Geometry, Physics" params1 = dict(default) params1.update(params) if implicit: F = forcefieldS1_implicit(overwrite, **params1) else: F = forcefieldS1_explicit(overwrite, **params1) mesh = F.function_space().mesh() geo, phys = Howorka.setup2D(mesh=mesh, z0=None, **params1) return F, geo, phys
def save_forcefield_implicit(**params): F, Fel, Fdrag = Howorka.F_field_implicit(**params) mesh = Howorka.geo.mesh uid = fields._unique_id() FNAME = NAME + uid nanopores.save_functions(FNAME, mesh, meta=params, F=F, Fel=Fel, Fdrag=Fdrag) fields.save_entries(NAME, params, FNAME=FNAME) fields.update()
def boundary_force(mesh=None, rMolecule=0.5, **params): geo, phys = Howorka.setup2D(mesh=mesh, z0=None, **params) mesh = geo.submesh("fluid") geo, phys = Howorka.setup2D(mesh=mesh, z0=None, **params) mesh = geo.mesh V = FunctionSpace(mesh, "CG", 1) v = TestFunction(V) u = TrialFunction(V) f = Constant(1.0) y = Function(V) bc = geo.BC(V, Constant(0.), "dnab").bcs # Initialization problem to get good initial guess for nonlinear problem: F1 = inner(grad(u), grad(v)) * dx - f * v * dx solve(lhs(F1) == rhs(F1), y, bc) # Stabilized Eikonal equation print "max cell size:", mesh.hmax() eps = Constant(mesh.hmax() / 25) F = sqrt(inner(grad(y), grad(y))) * v * dx - f * v * dx + eps * inner( grad(y), grad(v)) * dx # also works: #F = inner(grad(y), grad(y))*v*dx - f*v*dx + eps*inner(grad(y), grad(v))*dx solve(F == 0, y, bc) print "Max distance:", y.vector().max() U = LinearPotential(rMol=rMolecule) class Potential(Expression): def eval(self, value, x): value[0] = U(y(x)) U1 = Function(V) U1.interpolate(Potential()) return -phys.grad(U1), U1
def nonzero_velocities_2D(V, **params): setup = Howorka.Setup(**params) gamma = friction(setup) print "friction gamma", gamma # determine F(0), only once if not 0. in V: V.append(0.) V.sort() F = [None]*len(V) i0 = V.index(0.) F[i0] = velo2force_2D(0., setup) F0 = F[i0] print "F(0)", F0 for i, v in enumerate(V): if not i == i0: print "\n--- Velocity %d ---" %(i+1,) F[i] = velo2force_2D(v, setup) print "Velocity", v print "Force (exact)", F[i] print "Force (linear)", F0 - gamma*v return F, gamma, F0
else: return tuple(functions) def lambda_to_S1(f, mesh, dim=1): V = dolfin.FunctionSpace(mesh, "CG", 1) if dim > 1: V = dolfin.MixedFunctionSpace([V] * dim) f1 = dolfin.Function(V) value_shape = () if dim == 1 else (dim, ) class expr(dolfin.Expression): def eval(self, value, x): value[:] = f(x) def value_shape(self): return value_shape f1.interpolate(expr()) return f1 if __name__ == "__main__": from plot_forcefield import porestreamlines, plt F = forcefieldS1(implicit=False, **default) Fimp = forcefieldS1(implicit=True, **default) porestreamlines(Howorka.polygon(), 6., 8., F=F) #, Fimp=Fimp) fig = plt.figure("F") fig.axes[1].set_ylabel("force [N]") nanopores.showplots()
plt.figure() plt.triplot(tri, '-') Fi = [None] * 2 mesh = nanopores.RectangleMesh([-Rx1, -Ry], [Rx1, Ry], 60, 150) for i in (0, 1): z = Fexp[:, i] z = mirror(z, -1 if i == 0 else 1) # interpolate data interp = trid.nn_interpolator(z) interps = lambda x: interp([x[0]], [x[1]]) Fi[i] = S1(interps, mesh) #dolfin.plot(Fi[i]) V = dolfin.VectorFunctionSpace(mesh, "CG", 1) FFexp = dolfin.Function(V) dolfin.assign(FFexp, [Fi[0], Fi[1]]) dolfin.plot(Fi[1]) #dolfin.plot(Fimpl[1]) #dolfin.interactive() #dolfin.plot(FFexp) poly = Howorka.polygon() FFimpl, = nanopores.convert2D(mesh, Fimpl) porestreamlines(polygon=poly, rx=Rx1, ry=Ry, F=FFexp, Fimpl=FFimpl) #dolfin.interactive() plt.show()
def run(**phys_params): params = geo_params.copy() params.update(phys_params) geo, phys = Howorka.setup2D(mesh=mesh, **params) pb, pnps = Howorka.solve2D(geo, phys, **params) return dict(J=pnps.get_functional("Javgctr"))
import matplotlib.pyplot as plt from nanopores.models import Howorka from matplotlib2tikz import save as tikz_save nanopores.add_params(**Howorka.PARAMS) nanopores.add_params( bV=numpy.linspace(-0.1, 0.1, 11), dnaqsdamp=[0.2, 0.35, 0.5], bulkcon=300, plot="bV", nproc=4, ) print PARAMS geo_params = dict(z0=None, rMolecule=rMolecule, Rx=Rx, Ry=Ry) geo, phys = Howorka.setup2D(**geo_params) mesh = geo.mesh def run(**phys_params): params = geo_params.copy() params.update(phys_params) geo, phys = Howorka.setup2D(mesh=mesh, **params) pb, pnps = Howorka.solve2D(geo, phys, **params) return dict(J=pnps.get_functional("Javgctr")) #result, stamp = nanopores.iterate_in_parallel(run, iterkeys=[plot], **PARAMS) plots = nanopores.parallel_output(run, showplot=False, **PARAMS) # modify plot output
def save_force_field(**params): F, Fel, Fdrag = Howorka.F_field_implicit(**params) mesh = Howorka.geo.mesh nanopores.save_functions(FNAME, mesh, meta=params, F=F, Fel=Fel, Fdrag=Fdrag)
def save_force_field(**params): F, Fel, Fdrag = Howorka.F_field_implicit(**params) mesh = Howorka.geo.mesh nanopores.save_functions(FNAME, mesh, meta=params, F=F, Fel=Fel, Fdrag=Fdrag) if save: save_force_field(**PARAMS) exit() def load_force_field(): forces, mesh, meta = nanopores.load_vector_functions(FNAME) #mesh = forces["F"].function_space().mesh() return forces["F"], forces["Fel"], forces["Fdrag"], mesh F, Fel, Fdrag, mesh = load_force_field() geo, phys = Howorka.setup2D(mesh=mesh, z0=None, **PARAMS) submesh = geo.submesh("fluid") mesh = submesh geo, phys = Howorka.setup2D(mesh=mesh, z0=None, **PARAMS) V = dolfin.FunctionSpace(mesh, "CG", 1) VV = dolfin.VectorFunctionSpace(mesh, "CG", 1) F = dolfin.interpolate(F, VV) Fel = dolfin.interpolate(Fel, VV) Fdrag = dolfin.interpolate(Fdrag, VV) v2d = dolfin.vertex_to_dof_map(V) coord = mesh.coordinates() # numpy array of 2-1 arrays def function_from_values(values): u = dolfin.Function(V)
stokesLU=True, ) fields.update() # TODO calculate in parallel # TODO calculate current as well # TODO retrieve x points based on a given model and save file link # to data field X = fields.get_entry("xforce", "X") N = len(X) if fields.exists("force3D", **params): Xdone = fields.get_field("force3D", "x", **params) X = [x0 for x0 in X if x0 not in Xdone] print "Existing force file found, %d/%d points remaining." % (len(X), N) Xfailed = [] for x in X: x0 = [x[0], 0., x[1]] try: F, Fel, Fdrag = Howorka.F_explicit3D([x0], **solver_params) fields.save_fields("force3D", params, x=[x], F=F, Fel=Fel, Fdrag=Fdrag) except RuntimeError: print "RuntimeError occured, continuing without saving." Xfailed.append(x) print "failed:" print Xfailed print "%d of %d force calculations failed." % (len(Xfailed), len(X)) fields.update()
#import nanopores, dolfin import nanopores.models.Howorka as H #geo, phys = H.setup3D(h3D=8., lcpore=0.1, x0=[0.2,0,7], Rx = 8.) #pb, pnps = H.solve3D(geo, phys, Nmax3D=2e3) #nanopores.plot_sliced(geo) #dolfin.interactive() params = dict( dnaqsdamp=0.5, bV=-0.0, h3D=8., h=1., lcpore=0.1, Rx=8., Nmax3D=3e4, # 8 GB => max. 3e4, 32 GB => Nmax=1e4, stokesLU=True, ) z = [7, 4.2, 0] x = [[0, 0, z0] for z0 in z] F2, Fel2, Fdrag2 = H.F_explicit(z, **params) F, Fel, Fdrag = H.F_explicit3D(x, **params) for i in range(len(z)): print "F_z(z=%s)" % z[i] print "2D: %.3f, %.3f, %.3f" % (F2[i], Fel2[i], Fdrag2[i]) print "3D: %.3f, %.3f, %.3f" % (F[i][2], Fel[i][2], Fdrag[i][2])
F0 = F[i0] print "F(0)", F0 for i, v in enumerate(V): if not i == i0: print "\n--- Velocity %d ---" %(i+1,) F[i] = velo2force_2D(v, setup) print "Velocity", v print "Force (exact)", F[i] print "Force (linear)", F0 - gamma*v return F, gamma, F0 params = user_params(dim=3, Nmax=1.5e5, h=1., dnaqsdamp=0.25, x0=[0.2,0.,4.01], Qmol=-1., bulkcon=300.) setup = Howorka.Setup(**params) setup.prerefine() velo2force_3D([0., 0.1, 0.2], setup) do_v2f = False redo_v2f = False if do_v2f: if redo_v2f: params = user_params(dim=2, Nmax=2e4, h=.5, dnaqsdamp=0.25, x0=[0.,0.,4.5], Qmol=-1., bulkcon=300.) V = list(np.linspace(-1., 1., 3)) F, gamma, F0 = nonzero_velocities_2D(V, **params) fields.save_entries("howorka_velo2force_3", params, V=V, F=F, gamma=gamma, F0=F0) fields.update() dolfin.interactive()
F, Fel, Fdrag = Howorka.F_field_implicit(**params) mesh = Howorka.geo.mesh nanopores.save_functions(FNAME, mesh, meta=params, F=F, Fel=Fel, Fdrag=Fdrag) if save: save_force_field(**PARAMS) exit() def load_force_field(): forces, mesh, params = nanopores.load_vector_functions(FNAME) #mesh = forces["F"].function_space().mesh() return forces["F"], forces["Fel"], forces["Fdrag"], mesh, params # get forces and accompanying function spaces F, Fel, Fdrag, mesh, params = load_force_field() geo, phys = Howorka.setup2D(mesh=mesh, **params) F0, Fel0, Fdrag0 = F, Fel, Fdrag mesh0 = geo.mesh V0 = dolfin.FunctionSpace(mesh, "CG", 1) VV0 = dolfin.VectorFunctionSpace(mesh, "CG", 1) # interpolate on fluid submesh submesh = geo.submesh("fluid") mesh = submesh geo, phys = Howorka.setup2D(mesh=mesh, **params) V = dolfin.FunctionSpace(mesh, "CG", 1) VV = dolfin.VectorFunctionSpace(mesh, "CG", 1) F = dolfin.interpolate(F, VV) Fel = dolfin.interpolate(Fel, VV) Fdrag = dolfin.interpolate(Fdrag, VV)