I tried to solved 2D Poisson equation. -\Delta u = sin(\pi x) sin(\pi y) on \Gamma =[-1,1] \times [-1,1]
and u=0 on \partial \Gamma . I want to read the solution, but don’t know how to read vtu file? Can anyone give me some suggestions?
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" >
<UnstructuredGrid>
<Piece NumberOfPoints="121" NumberOfCells="200">
<Points>
<DataArray type="Float64" NumberOfComponents="3" format="ascii">-1 -1 0 -0.$
</Points>
<Cells>
<DataArray type="UInt32" Name="connectivity" format="ascii">0 1 12 0 11 12 $
<DataArray type="UInt32" Name="offsets" format="ascii">3 6 9 12 15 18 21 24 $
<DataArray type="UInt8" Name="types" format="ascii">5 5 5 5 5 5 5 5 5 5 5 5 $
</Cells>
<PointData Scalars="f_8">
<DataArray type="Float64" Name="f_8" format="ascii">0.0000000000000000e+00 $
</PointData>
</Piece>
</UnstructuredGrid>
</VTKFile>
bmf
March 28, 2021, 9:11am
2
Hi,
I am not quite certain what do you mean by reading vtu file. If you want to visualize the solution, I would suggest to import your vtu file into Paraview , visit or to use vedo . However, if you want to re-read your solution from file for further calculations, you should initially save them in XDMF format instead and load them back in as explained here .
1 Like
Thanks for your answer. I visualized the solution by using Paraview. I want to print the value of solution u. Can you please tell me how do I get ?
dokken
March 28, 2021, 8:47pm
4
Thanks for your help. I get the following output. Could you please suggest me what the first, second , third and fourth column represent?
Solving linear variational problem.
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
2.02357554e-31 0.00000000e+00 0.00000000e+00 3.12913879e-31
3.18614325e-31 0.00000000e+00 0.00000000e+00 7.83995969e-31
5.15427819e-31 2.77597368e-31 0.00000000e+00 0.00000000e+00
6.03202626e-31 9.73856828e-31 4.63448419e-31 1.70876769e-31
0.00000000e+00 0.00000000e+00 1.14467826e-31 7.75270595e-31
8.39797650e-31 3.14673521e-31 -9.31088756e-32 0.00000000e+00
0.00000000e+00 -2.79529819e-31 1.82859009e-31 6.58015827e-31
4.79509704e-31 -6.47965954e-32 -3.55577719e-31 0.00000000e+00
0.00000000e+00 -3.32384365e-31 -3.35349501e-31 1.51430189e-31
3.77606213e-31 -9.27037501e-32 -4.87941646e-31 -3.92505207e-31
0.00000000e+00 0.00000000e+00 -3.89293782e-31 -4.98807266e-31
-2.90388226e-31 6.67559400e-32 -1.52584006e-31 -7.51407832e-31
-7.12112325e-31 -2.29060806e-31 0.00000000e+00 0.00000000e+00
-1.71754340e-31 -5.46672908e-31 -4.38326311e-31 -1.76842326e-31
-7.36485222e-32 -6.80082649e-31 -9.51305729e-31 -4.18876430e-31
-1.74060280e-31 0.00000000e+00 0.00000000e+00 -3.89293782e-31
-4.98807266e-31 -2.90388226e-31 6.67559400e-32 -1.52584006e-31
-7.51407832e-31 -7.12112325e-31 -2.29060806e-31 0.00000000e+00
0.00000000e+00 -3.32384365e-31 -3.35349501e-31 1.51430189e-31
3.77606213e-31 -9.27037501e-32 -4.87941646e-31 -3.92505207e-31
0.00000000e+00 0.00000000e+00 -2.79529819e-31 1.82859009e-31
6.58015827e-31 4.79509704e-31 -6.47965954e-32 -3.55577719e-31
0.00000000e+00 0.00000000e+00 1.14467826e-31 7.75270595e-31
8.39797650e-31 3.14673521e-31 -9.31088756e-32 0.00000000e+00
0.00000000e+00 6.03202626e-31 9.73856828e-31 4.63448419e-31
1.70876769e-31 0.00000000e+00 0.00000000e+00 7.83995969e-31
5.15427819e-31 2.77597368e-31 0.00000000e+00 0.00000000e+00
3.12913879e-31 3.18614325e-31 0.00000000e+00 0.00000000e+00
2.02357554e-31 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00]
I believe third column represent the solution u.
dokken
March 29, 2021, 6:00am
6
You need to supply the script you used to obtain this print for anyone to help interpret the result
I wrote the following scripts.
“”"
-Laplace(u) = f in the [-1,1]*[-1,1]
u = u_D on the boundary
u_D = 0
f = sin(pi*x)*sin(pi*y)
"""
from __future__ import print_function
from fenics import *
from dolfin import *
import matplotlib.pyplot as plt
import numpy as np
# Create mesh and define function space
nx = ny = 10
mesh = RectangleMesh(Point(-1, -1), Point(1, 1), nx, ny)
V = FunctionSpace(mesh, 'P', 1)
# Define boundary condition
u_D = Constant(0.0)
def boundary(x, on_boundary):
return on_boundary
bc = DirichletBC(V, u_D, boundary)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f= Expression('sin(pi*x[0])*sin(pi*x[1])', degree=2)
a = dot(grad(u), grad(v))*dx
L = f*v*dx
# Compute solution
u = Function(V)
solve(a == L, u, bc)
#print('Solution=',u)
print(u.vector().get_local())
# Plot solution and mesh
plot(u)
plot(mesh, title="Solution to Poisson Equation")
# Save solution to file in VTK format
vtkfile = File('poisson/solution.pvd')
vtkfile << u
# Compute error in L2 norm
error_L2 = errornorm(u_D, u, 'L2')
# Compute maximum error at vertices
vertex_values_u_D = u_D.compute_vertex_values(mesh)
vertex_values_u = u.compute_vertex_values(mesh)
import numpy as np
error_max = np.max(np.abs(vertex_values_u_D - vertex_values_u))
# Print errors
print('error_L2 =', error_L2)
print('error_max =', error_max)
#print(print(error_max.vector().get_local()))
# Hold plot
#interactive()
# Plot solution
#import matplotlib.pyplot as plt
plt.show()
The output I got:
Solving linear variational problem.
[ 0. 0. 0. 0. -0.01660297 0.
0. -0.02715756 -0.02715756 0. 0. -0.02739565
-0.04402962. -0.02739565 0. 0. -0.01714926. -0.04403778
-0.04403778 -0.01714926 0. 0. -0.00030823 -0.02713136
-0.04362375 -0.02713136. -0.00030823 0. 0. 0.01670001
0.0002454 -0.0263458 -0.0263458 0.0002454 0.01670001 0.
0. 0.02737363 0.02763792 0.00117635 -0.01520246 0.00117635
0.02763792 0.02737363 0. 0. 0.02761172 0.04458078
0.02842348 0.00198926 0.00198926 0.02842348 0.04458078 0.02761172
0. 0. 0.01724631 0.04458893 0.04499481 0.01864682
0.00230726 0.01864682 0.04499481 0.04458893 0.01724631 0.
0. 0.02761172 0.04458078 0.02842348 0.00198926 0.00198926
0.02842348 0.04458078 0.02761172 0. 0. 0.02737363
0.02763792 0.00117635 -0.01520246 0.00117635 0.02763792 0.02737363
0. 0. 0.01670001 0.0002454 -0.0263458 -0.0263458
0.0002454 0.01670001 0. 0. -0.00030823 -0.02713136
-0.04362375 -0.02713136. -0.00030823 0. 0. -0.01714926
-0.04403778 -0.04403778 -0.01714926 0. 0. -0.02739565
-0.04402962 -0.02739565 0. 0. -0.02715756 -0.02715756
0. 0. -0.01660297 0. 0. 0.
0. ]
*** Warning: Degree of exact solution may be inadequate for accurate result in errornorm.
error_L2 = 0.04599078271466321
error_max = 0.04499481177889299
Could you please tell me what’s the column represent?
dokken
March 29, 2021, 4:20pm
8
As explained in the post I referred to, the i-th value in the vector you are printing corresponds to the function value at the coordinate from the i-th row of V.tabulate_dof_coordinates()
.