In Julia I am integrating two fields in a struct: x position and x velocity. In function d(u, du) I am trying to only return the du vector without altering any values in u. u is only used to calculate the du value. Instead it is changing the values of u instead of du. I have a vector of one struct for du and one vector for u. On each time step, I update my du.x with u.xvelocity, and I update my u.xvelocity for my acceleration. For some reason it seems to break when I calculate the k2 for my runge kutta. The MWE is appended below and should run and get the same error that I am getting. Also, if my runge kutta looks incorrect, also let me know. Best.
module MyOde
mutable struct Particle
x::Float64
xvelocity::Float64
end # End struct
function d(u, du)
for i in 1:length(u)
testme = u[i].x
display(testme)
display(u[i].xvelocity)
du[i].x = 1.
println("now see the issue:")
display(u[i].x)
testme != u[i].x ? error("\n What the heck is going on here? ") : nothing
du[i].xvelocity = 1.
end # End function
return du
end # End function
function f(u::Vector{Particle}, d, timeend, dt)
du = Vector{Particle}(undef, length(u))
k2 = Vector{Particle}(undef, length(u))
k3 = Vector{Particle}(undef, length(u))
k4 = Vector{Particle}(undef, length(u))
for i ∈ 1:length(u)
du[i] = Particle(0.0, 0.0)
k2[i] = Particle(0.0, 0.0)
k3[i] = Particle(0.0, 0.0)
k4[i] = Particle(0.0, 0.0)
end # End list push
for i in 0.0:dt:timeend
# Calculate the k values which will be going into the 4th order Runge-Kutta method.
k1 = d(u, du)
for i ∈ 1:length(u)
k2[i].x = u[i].x + k1[i].x *dt/2
k2[i].xvelocity = u[i].xvelocity + k1[i].xvelocity *dt/2
end # End k2 loop
k2 = d(k2, du)
for i ∈ 1:length(u)
k3[i].x = u[i].x + k2[i].x *dt/2
k3[i].xvelocity = u[i].xvelocity + k2[i].xvelocity *dt/2
end # End k3 loop
k3 = d(k3, du)
for i ∈ 1:length(u)
k4[i].x = u[i].x + k3[i].x *dt
k4[i].xvelocity = u[i].xvelocity + k3[i].xvelocity *dt
end # End k4 loop
k4 = d(k4, du)
for i ∈ 1:length(u)
u[i].x += 1/6 * dt * (k1[i].x + 2k2[i].x + 2k3[i].x + k4[i].x)
u[i].xvelocity += 1/6 * dt * (k1[i].xvelocity + 2k2[i].xvelocity + 2k3[i].xvelocity + k4[i].xvelocity)
end # End loop
end # End loop
end # End function
u = [Particle(0.0, 0.0); Particle(6.00, 0.0)]
timeend = .01
dt = 0.01
@time f(u, d, timeend, dt)
end # End module