The following is from a 1D hydro code (using the Hamiltonian method with Strang splitting for evolving the variables p & q) that I whipped up this weekend for some introductory research work
do
if(num==1) then
p2 = p(i) - (dt/2.)*q(i)/abs(q(i)) ! half step in P
q(i) = q(i) + dt*p2 ! full step in Q
p(i) = p2 - (dt/2.)*q(i)/abs(q(i)) ! half step in P
num=2
elseif(num==2) then
q2 = q(i) + (dt/2.)*p(i) ! half step in Q
p(i) = p(i) - dt*q2/abs(q2) ! full step in P
q(i) = q(i) + (dt/2.)*p(i) ! half step in Q
num=1
endif
t = t+dt
if(t >= tend) exit
enddo
Is there any more efficient way to alternate between the two algorithms (which is necessary to reduce spurious data) than what I have here? If it matters, p and q have roughly 100,000 cells each (the code is parallelized).
EDIT: I added the do-loop portion of the code, rather than just the if-elseif portion. There is also a write-to-file portion after the endif, but I do not believe that is necessary to the potential optimization.
I would rewrite the code as to completely remove the
if/then/else:You can still have the
t = t+dtstatement after each step in the loop if you need the current time for the output operation.