Message transfered on Computational Science - Solving advection equation - periodic conditions - using roll...












0















Message transfered on Computational Science :



cf https://scicomp.stackexchange.com/questions/30635/solving-advection-equation-periodic-conditions-using-roll-python-function



I have to solve numerically the advection equation with periodic boundaries conditions : u(t,0) = u(t,L) with L the length of system to solve.



I start also with u(0,x) = uexacte(0,x) = sin(2*pi*x/L)



Here the main part of the code with loop time (we use here FTCS scheme) :



V=1
L=1
# analytical solution --------------------------
def uexacte(t,x):
return sin(2*pi*(x-V*t)/L)

# 1. Centre FTCS (Forward Time Centered Space)
cfl = 0.25
nx = 10
tend = 1

#
dx = L/(nx-1.)
dt = cfl*dx/V
nt = int(tend/dt)+1
print "CFL=%5.2f tend=%4.1f --> %i iterations en temps"%(cfl,tend,nt)

# Arrays
x = linspace(0,L,nx)
# Bounadry condition
u0 = uexacte(0,x)

# Starting solution
t=0.0 ; u=copy(u0)

# Time loop
for i in range(1,nt):
# FTCS
#u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])
# Using roll
u = u + - cfl/2*(roll(u,-1)- roll(u,1))
# Update time
t = t+dt


I don't understand the solution given by teacher who uses the python function roll in this way :



# Using roll
u = u - cfl/2*(roll(u,-1)- roll(u,1))


One says that with the using of roll, we are sure to respect the periodic boundary conditions but I don't understand why ?



Indeed, my first approach was to do :



u[0] = u[nx-1]
u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])


but this doesn't work and I don't know how to implement this periodic conditions in this way (without using roll function).



If someone could explain this matter and the trick with roll function, this would be nice to tell it.



UPDATE 1 :



I tried with classical approach (simple recurrence formula ) like this :



# Time loop
for i in range(1,nt):
# FTCS
u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])

# Try to impose periodic boundary conditions but without success
u[0] = u[0] - cfl/2*(u[0] - u[nx-1])

# Update time
t = t+dt


Indeed, the result is bad (values for each side are not the same). I could impose the theorical values at each step but in practise, we don't know always the analytical solution.



What is the trick to impose this periodic boundary condition on the numerical solution at each step of time ?










share|improve this question





























    0















    Message transfered on Computational Science :



    cf https://scicomp.stackexchange.com/questions/30635/solving-advection-equation-periodic-conditions-using-roll-python-function



    I have to solve numerically the advection equation with periodic boundaries conditions : u(t,0) = u(t,L) with L the length of system to solve.



    I start also with u(0,x) = uexacte(0,x) = sin(2*pi*x/L)



    Here the main part of the code with loop time (we use here FTCS scheme) :



    V=1
    L=1
    # analytical solution --------------------------
    def uexacte(t,x):
    return sin(2*pi*(x-V*t)/L)

    # 1. Centre FTCS (Forward Time Centered Space)
    cfl = 0.25
    nx = 10
    tend = 1

    #
    dx = L/(nx-1.)
    dt = cfl*dx/V
    nt = int(tend/dt)+1
    print "CFL=%5.2f tend=%4.1f --> %i iterations en temps"%(cfl,tend,nt)

    # Arrays
    x = linspace(0,L,nx)
    # Bounadry condition
    u0 = uexacte(0,x)

    # Starting solution
    t=0.0 ; u=copy(u0)

    # Time loop
    for i in range(1,nt):
    # FTCS
    #u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])
    # Using roll
    u = u + - cfl/2*(roll(u,-1)- roll(u,1))
    # Update time
    t = t+dt


    I don't understand the solution given by teacher who uses the python function roll in this way :



    # Using roll
    u = u - cfl/2*(roll(u,-1)- roll(u,1))


    One says that with the using of roll, we are sure to respect the periodic boundary conditions but I don't understand why ?



    Indeed, my first approach was to do :



    u[0] = u[nx-1]
    u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])


    but this doesn't work and I don't know how to implement this periodic conditions in this way (without using roll function).



    If someone could explain this matter and the trick with roll function, this would be nice to tell it.



    UPDATE 1 :



    I tried with classical approach (simple recurrence formula ) like this :



    # Time loop
    for i in range(1,nt):
    # FTCS
    u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])

    # Try to impose periodic boundary conditions but without success
    u[0] = u[0] - cfl/2*(u[0] - u[nx-1])

    # Update time
    t = t+dt


    Indeed, the result is bad (values for each side are not the same). I could impose the theorical values at each step but in practise, we don't know always the analytical solution.



    What is the trick to impose this periodic boundary condition on the numerical solution at each step of time ?










    share|improve this question



























      0












      0








      0








      Message transfered on Computational Science :



      cf https://scicomp.stackexchange.com/questions/30635/solving-advection-equation-periodic-conditions-using-roll-python-function



      I have to solve numerically the advection equation with periodic boundaries conditions : u(t,0) = u(t,L) with L the length of system to solve.



      I start also with u(0,x) = uexacte(0,x) = sin(2*pi*x/L)



      Here the main part of the code with loop time (we use here FTCS scheme) :



      V=1
      L=1
      # analytical solution --------------------------
      def uexacte(t,x):
      return sin(2*pi*(x-V*t)/L)

      # 1. Centre FTCS (Forward Time Centered Space)
      cfl = 0.25
      nx = 10
      tend = 1

      #
      dx = L/(nx-1.)
      dt = cfl*dx/V
      nt = int(tend/dt)+1
      print "CFL=%5.2f tend=%4.1f --> %i iterations en temps"%(cfl,tend,nt)

      # Arrays
      x = linspace(0,L,nx)
      # Bounadry condition
      u0 = uexacte(0,x)

      # Starting solution
      t=0.0 ; u=copy(u0)

      # Time loop
      for i in range(1,nt):
      # FTCS
      #u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])
      # Using roll
      u = u + - cfl/2*(roll(u,-1)- roll(u,1))
      # Update time
      t = t+dt


      I don't understand the solution given by teacher who uses the python function roll in this way :



      # Using roll
      u = u - cfl/2*(roll(u,-1)- roll(u,1))


      One says that with the using of roll, we are sure to respect the periodic boundary conditions but I don't understand why ?



      Indeed, my first approach was to do :



      u[0] = u[nx-1]
      u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])


      but this doesn't work and I don't know how to implement this periodic conditions in this way (without using roll function).



      If someone could explain this matter and the trick with roll function, this would be nice to tell it.



      UPDATE 1 :



      I tried with classical approach (simple recurrence formula ) like this :



      # Time loop
      for i in range(1,nt):
      # FTCS
      u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])

      # Try to impose periodic boundary conditions but without success
      u[0] = u[0] - cfl/2*(u[0] - u[nx-1])

      # Update time
      t = t+dt


      Indeed, the result is bad (values for each side are not the same). I could impose the theorical values at each step but in practise, we don't know always the analytical solution.



      What is the trick to impose this periodic boundary condition on the numerical solution at each step of time ?










      share|improve this question
















      Message transfered on Computational Science :



      cf https://scicomp.stackexchange.com/questions/30635/solving-advection-equation-periodic-conditions-using-roll-python-function



      I have to solve numerically the advection equation with periodic boundaries conditions : u(t,0) = u(t,L) with L the length of system to solve.



      I start also with u(0,x) = uexacte(0,x) = sin(2*pi*x/L)



      Here the main part of the code with loop time (we use here FTCS scheme) :



      V=1
      L=1
      # analytical solution --------------------------
      def uexacte(t,x):
      return sin(2*pi*(x-V*t)/L)

      # 1. Centre FTCS (Forward Time Centered Space)
      cfl = 0.25
      nx = 10
      tend = 1

      #
      dx = L/(nx-1.)
      dt = cfl*dx/V
      nt = int(tend/dt)+1
      print "CFL=%5.2f tend=%4.1f --> %i iterations en temps"%(cfl,tend,nt)

      # Arrays
      x = linspace(0,L,nx)
      # Bounadry condition
      u0 = uexacte(0,x)

      # Starting solution
      t=0.0 ; u=copy(u0)

      # Time loop
      for i in range(1,nt):
      # FTCS
      #u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])
      # Using roll
      u = u + - cfl/2*(roll(u,-1)- roll(u,1))
      # Update time
      t = t+dt


      I don't understand the solution given by teacher who uses the python function roll in this way :



      # Using roll
      u = u - cfl/2*(roll(u,-1)- roll(u,1))


      One says that with the using of roll, we are sure to respect the periodic boundary conditions but I don't understand why ?



      Indeed, my first approach was to do :



      u[0] = u[nx-1]
      u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])


      but this doesn't work and I don't know how to implement this periodic conditions in this way (without using roll function).



      If someone could explain this matter and the trick with roll function, this would be nice to tell it.



      UPDATE 1 :



      I tried with classical approach (simple recurrence formula ) like this :



      # Time loop
      for i in range(1,nt):
      # FTCS
      u[1:nx-1] = u[1:nx-1] - cfl/2*(u[2:nx] - u[0:nx-2])

      # Try to impose periodic boundary conditions but without success
      u[0] = u[0] - cfl/2*(u[0] - u[nx-1])

      # Update time
      t = t+dt


      Indeed, the result is bad (values for each side are not the same). I could impose the theorical values at each step but in practise, we don't know always the analytical solution.



      What is the trick to impose this periodic boundary condition on the numerical solution at each step of time ?







      python boundary periodicity






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 3 '18 at 10:45







      youpilat13

















      asked Nov 27 '18 at 16:30









      youpilat13youpilat13

      8221441




      8221441
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53504098%2fmessage-transfered-on-computational-science-solving-advection-equation-perio%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53504098%2fmessage-transfered-on-computational-science-solving-advection-equation-perio%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)