Helmholtz Equation: Storage issue during frequency loop

Hi,

I ran into a strange issue when I tried to store my solutions for a Helmholtz Equation inside an array like this:

for nf in range(0,len(f_axis)):
    freq = f_axis[nf]

    # Compute solution
    omega.value =f_axis[nf]*2*np.pi
    k0.value = 2*np.pi*f_axis[nf]/c0
    problem.solve()

    with XDMFFile(msh.comm, "solution" + str(f_axis[nf]) + ".xdmf", "w") as xdmf:
        xdmf.write_mesh(msh)
        xdmf.write_function(uh)

    #I want to safe the solution for every single frequency here: 
    all_values.append(uh.x.array.real)

If I check the XDMF files in paraview or print the result with print(uh.x.array.real) during the loop, everything works fine but the all_values array only contains the solution of the last frequency over and over again.

What am I missing here? Is there any other way to do this?

Thanks for any advice!

Best,

Patrick

You’re simply appending a reference to uh.x.array.real in all_values every step. If you want a record of data you should copy the values and store them at every step. Consider numpy.copy — NumPy v2.0 Manual.

1 Like