def test(model, spaceName, dimD, dimR, storage): # print("########################################") # print("#### ",spaceName,storage,dimD,dimR,flush=True) spaceD = create.space(spaceName, grid, dimRange=dimD, order=1, storage=storage) spaceR = create.space(spaceName, grid, dimRange=dimR, order=1, storage=storage) scheme = create.operator("galerkin", model, spaceD, spaceR) uD = create.function("discrete", spaceD, name=storage) uD.clear() start = time.time() for i in range(testLoop): A = linearOperator(scheme) # , parameters={"petsc.blockedmode":False}) end = time.time() # print( "setup+assembly:",(end-start)/testLoop, flush=True ) start = time.time() for i in range(testLoop): jacobian(scheme, uD, A) end = time.time() # print( "assembly only: ",(end-start)/testLoop, flush=True ) # sys.stdout.flush() try: import petsc4py from petsc4py import PETSc mat = A.as_petsc # print(mat.getInfo(), flush=True) except: pass
# <codecell> uflSpace0 = Space((P.dimWorld, P.dimWorld), 1) v0 = TestFunction(uflSpace0) Rvol = P.Phi * (u[1] - solution_old[1]) / tau - div(dBulk_s) - bulk_s estimator = hT**2 * Rvol**2 * v0[0] * dx + he * inner( jump(dBulk_s), n('+'))**2 * avg(v0[0]) * dS + heBnd * (s_N + inner( dBulk_s, n))**2 * v0[0] * P.inflow * ds + penalty_s[0]**2 / he * jump( u[1])**2 * avg(v0[0]) * dS + penalty_s[1]**2 / heBnd * ( s_D - u[1])**2 * v0[0] * P.dirichlet * ds estimator = replace(estimator, {intermediate: u}) estimatorModel = create.model("integrands", grid, estimator == 0) # estimatorModel.timeStep = dt # estimatorModel.penalty = penalty estimator = create.operator("galerkin", estimatorModel, spc, fvspc) # <markdowncell> # # Marker for grid adaptivity (h) # <codecell> hTol = 1e-16 # changed later def markh(element): center = element.geometry.referenceElement.center eta = estimate.localFunction(element).evaluate(center)[0] if eta > hTol and element.level < maxLevel: return Marker.refine elif eta < 0.01 * hTol: return Marker.coarsen
from dune.grid import structuredGrid from dune.istl import blockVector import ufl g = structuredGrid([0, 0], [1, 1], [2, 3]) s = create.space("lagrange", g, dimRange=2, storage="istl") f1 = s.interpolate(expr=[2, 1], name="tmp") dofs = blockVector(int(s.size / s.localBlockSize), s.localBlockSize) # f2 = s.function("tmp", expr=[2,1], dofVector=dofs) f2 = s.function("tmp", dofVector=dofs) f2.interpolate([2, 1]) assert all([(d1 - d2).two_norm == 0 for d1, d2 in zip(dofs, f1.as_istl)]) assert all([(d1 - d2).two_norm == 0 for d1, d2 in zip(dofs, f2.as_istl)]) operator = create.operator( "galerkin", ufl.dot(ufl.TrialFunction(s), ufl.TestFunction(s)) * ufl.dx) f1 = s.function("tmp", [2, 1], blockVector(s.size // s.localBlockSize, s.localBlockSize)) f2 = s.function("tmp", [2, 1], blockVector(s.size // s.localBlockSize, s.localBlockSize)) operator(f1, f2) s = create.space("lagrange", g, dimRange=2, storage="numpy") f1 = s.interpolate([2, 1], name="tmp") dofs = numpy.ndarray(s.size) f2 = s.function("tmp", [2, 1], dofs) assert not (dofs - f1.as_numpy).any() assert not (dofs - f2.as_numpy).any() f1.interpolate([3, 2]) dofs[:] = f1.as_numpy
exact_p = as_vector([(-2 * x[0] + 2) * mu]) f = as_vector([ 0, ] * grid.dimension) f += nu * exact_u mainModel = (nu * dot(u, v) + mu * inner(grad(u) + grad(u).T, grad(v)) - dot(f, v)) * dx gradModel = -inner(p[0] * Identity(grid.dimension), grad(v)) * dx divModel = -div(u) * q[0] * dx massModel = inner(p, q) * dx preconModel = inner(grad(p), grad(q)) * dx # can also use 'operator' everywhere mainOp = create.scheme("galerkin", (mainModel == 0, DirichletBC(spcU, exact_u, 1)), spcU) gradOp = create.operator("galerkin", gradModel, spcP, spcU) divOp = create.operator("galerkin", divModel, spcU, spcP) massOp = create.scheme("galerkin", massModel == 0, spcP) preconOp = create.scheme("galerkin", preconModel == 0, spcP) mainOp.model.mu = 0.1 mainOp.model.nu = 0.01 velocity = spcU.interpolate([ 0, ] * spcU.dimRange, name="velocity") pressure = spcP.interpolate([0], name="pressure") rhsVelo = velocity.copy() rhsPress = pressure.copy() sol_u = velocity.as_numpy
destD = space.interpolate(as_vector([ 0, ] * dimR), name='destD') destE = space.interpolate(as_vector([ 0, ] * dimR), name='destE') u = TrialFunction(space) v = TestFunction(space) x = SpatialCoordinate(space.cell()) ubar = space.interpolate(as_vector([ dot(x, x), ] * dimR), name="ubar") a = (inner(0.5 * dot(u, u), v[0]) + inner(u[0] * grad(u), grad(v))) * dx op = create.operator("galerkin", a, space) A = linearOperator(op) op.jacobian(ubar, A) A(arg, destA) da = apply_derivatives(derivative(action(a, ubar), ubar, u)) dop = create.operator("galerkin", da, space) dop(arg, destB) err = integrate(grid, (destA - destB)**2, 5) # print("error=",err) assert (err < 1e-15) A = linearOperator(dop) dop.jacobian(arg, A) A(arg, destC) err = integrate(grid, (destA - destC)**2, 5)