How a dolfin.function.function.Function could be converted into a numpy array?

How a dolfin.function.function.Function could be converted into a numpy array?

u=Function(V)
arr =u.vector().get_local()
1 Like

Thanks Dokken, I have tried your solution and I have checked that the type of the object is indeed numpy.array, but when I save my numpy array I only see 0.000000000000000000e+00. It weird because if I export my solution to VTK/VTU formart I can see my results perfectly. This is the code I am using

u_0 = Constant(0)
u_n = interpolate(u_0, V)

T = 1 #seg

numero_de_muestras = 5
num_steps = frecuencia * numero_de_muestras * T

dt = T / num_steps 

u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0)
F = u*v*dx + dt*epsilon*dot(grad(u), grad(v))*dx - (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)
u = Function(V)

# VTK
vtk_file = File("resultados.pvd", "compressed")

# FILE TO RECORD SOLUTION u AS NUMPY
f=open('/content/drive/My Drive/Colab Notebooks/TENS/u.txt','ab')

t = 0
for n in range(num_steps):

    t += dt
    u_electrodo_pos.j = t
    u_electrodo_neg.j = t

    # Compute solution
    solve(a == L, u, bcs)

    # PLOT:

    # Convert dolfin.function.Function into a numpy array
    arr=u.vector().get_local()
    np.savetxt(f, arr)

    # VTK: Save solution to vtk format
    vtk_file << u
    
    # Update previous solution
    u_n.assign(u)

    arr=u.vector().get_local()

Hi Dokken, you solution works correctly it was an issue with the numpy.savetxt() method.