Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:13:02+00:00 2026-06-15T11:13:02+00:00

The following is from a 1D hydro code (using the Hamiltonian method with Strang

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:13:05+00:00Added an answer on June 15, 2026 at 11:13 am

    I would rewrite the code as to completely remove the if/then/else:

    integer :: num_steps, k
    logical :: one_more
    
    num_steps = tend/dt
    one_more = (mod(num_steps,2) /= 0)
    
    do k = 1,num_steps/2
       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
       ! output
       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
       ! output
    enddo
    
    if (one_more) 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
       ! output
    endif
    

    You can still have the t = t+dt statement after each step in the loop if you need the current time for the output operation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following from this: Every specific weekday, sql and php I got this: deals_bookings.everyWeekDay =
I've extracted the following from a project we are working on: http://www.bridgedev1.com/videotest.html This plays
I'm trying to write a python script that does the following from within a
I'm trying to translate the following from AT&T assembly to Intel assembly: pushl 2000
I'm trying to write a sql query to the following (from this site http://sqlzoo.net/1b.htm
I have the following from the server response: {invalid_emails:[adsasdasd],result:success,valid_emails:[jobs@apple.com]} But this errors? $.ajax({ type:
I want to execute following query: from Item i where i.categoryItems.catalogId = :catId That
following from javascript jquery and using eval i still could not get jquery to
Following from this tutorial.. http://net.tutsplus.com/articles/news/codeigniter-from-scratch-day-6-login/ I have successfully implemented and created/logged in users.. I
Following from Way/coding method to do activitylist/log I worked with the accepted answer on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.