VTK file for visualisation in paraview

More
3 years 9 months ago - 3 years 9 months ago #3582 by Joe
Hello,

I would like to write out the gradient of H1 conforming function to a VTK file for visualisation in paraview.
Code:
from netgen import gui from ngsolve import * from ngsolve import atan2 from ngsolve import sin from ngsolve import cos from netgen.geom2d import SplineGeometry geo = SplineGeometry() geo.AddRectangle( (-3, -3), (3, 3), bc = 'wall') geo.AddCircle ( (0, 0), r=0.75, leftdomain=0, rightdomain=1, bc='cyl') mesh = Mesh( geo.GenerateMesh(maxh=0.2)) mesh.Curve(3); Draw(mesh) V = H1(mesh, order=2, dirichlet='wall|cyl') gfu = GridFunction(V) u = V.TrialFunction() w = V.TestFunction() f = LinearForm(V) f += 0 * w * dx R=0.75 uinf = 1 exact = CoefficientFunction(uinf*(y-(R**(2)*y)/(x**(2)+y**(2)))) gexact = CoefficientFunction( ( (uinf*((2*R**(2)*x*y)/(x**(2)+y**(2))**2)), (uinf*(1-(R**(2)*(x**(2)-y**(2))/(x**(2)+y**(2))**2))) ) ) gfu = GridFunction(V) gfu.Set(exact, definedon=mesh.Boundaries('wall|cyl')) a = BilinearForm(V, symmetric=True) a += grad(u)*grad(w)*dx #c = Preconditioner(a, "local") c = Preconditioner(a, "direct") a.Assemble() f.Assemble() # the solution field BVP=BVP(bf=a,lf=f,gf=gfu, pre=c).Do() ndof = V.ndof print(ndof) Draw (gfu) den1=sqrt(Integrate(InnerProduct(exact,exact), mesh)) print ("L2-error:", sqrt(Integrate((gfu-exact)**2, mesh))/den1) den2=sqrt(Integrate(InnerProduct(exact,exact)+InnerProduct(gexact,gexact), mesh)) print ("H1-error:", sqrt(Integrate((gfu-exact)**2+InnerProduct(grad(gfu)-gexact,grad(gfu)-gexact), mesh))/den2) vtk = VTKOutput(ma=mesh,coefs=[gfu], names=["sol"], filename="vtk_example3", subdivision=3) vtk.Do() vtk = VTKOutput(ma=mesh,coefs=[gfu,grad(gfu)],names=["sol","gradsol"],filename="vtk_example4",subdivision=3) vtk.Do()

Thank you for any help.
Last edit: 3 years 9 months ago by Joe.
More
3 years 9 months ago #3583 by schruste
Dear Joe,

Well done :). Where the problem?

Best,
Christoph
More
3 years 9 months ago #3584 by Joe
Hi Christoph,

Sorry, I should have explained my issue is with the glyphs in paraview as the output is incorrect for when I try to do a quiver plot. Is this because it can’t handle the export of grad(gridfun) to vtk where gridfun is a H^1 grid function?
More
3 years 9 months ago #3585 by christopher
Not 100% sure this is the problem here, but I think paraview wants vectors to be 3 dimensional. If you export CF((grad(gridfun)[0], grad(gridfun)[1], 0)) do you get what you want?
The following user(s) said Thank You: schruste
More
3 years 9 months ago #3586 by Joe
Sorry for the late reply.

I think this fixed my issue. Thank you very much
Time to create page: 0.123 seconds