Electric field components

Hello,

I would like to ask if there is any way to save Ex, Ey, Ez in the same file in order to have a vtu file about the total electric field. For example in the following code what could be used instead of file << u?

V = FunctionSpace(mesh, “CG”, 2)

bcs = [DirichletBC(V, Constant(0.0), boundaries, 1)]

Define variational problem

u = TrialFunction(V)
v = TestFunction(V)
a = inner(grad(u), grad(v))dx
f = Constant(0.0)
L = f
vdx + g_Xvds(5)+ g_Xvds(6) + g_Yvds(3)+ g_Yvds(4) + g_Zv*ds(2)

u = Function(V)
solve(a == L, u, bcs)

Ex = project(u.dx(0), V)
Ey = project(u.dx(1), V)
Ez = project(u.dx(2), V)

#File(‘saved_u.xml’) << u
file = File(“finalfnew.pvd”)
file << u

Hi,
You may use xdmf format. As a side note, the fields Ex, Ey and Ez shouldn’t be projected onto a CG2 space. Since u is approximated by CG2, I would project E=grad(u) onto DG1.

Vdg = VectorFunctionSpace(mesh, "DG", 1)
E = Function(Vdg, name="E")
E.assign(project(grad(u), VDg))
with XDMFFile("electricField.xdmf") as fil:
    fil.write(E)

You can then visualize individual components of E or it’s magnitude in Paraview. In order to save multiple fields, do something like

fil = XDMFFile("fields.xdmf")
fil.parameters["functions_share_mesh"] = True
fil.parameters["rewrite_function_mesh"] = False
fil.write(u)
fil.write(E)
2 Likes

Thank you very much!!