Thread: Perodic boundary conditions

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    Perodic boundary conditions

    I need to write a segment of a code where it allows the program to run in a loop basically...so the range of this code is going to be from 0 - (N-1) for example, I want:

    if the code happens to produce -1, I want it to go to N-1 thus creating a loop. Similarly
    if the code happens to produce N, I want it to go to 0, again, going back to the beginning to connect the entire sequence...

    starting at 0:

    Code:
      if(u < a / 3)
        {
          dis--;
        }
      else if(u >= 2 * a / 3)
        {
          dis++;
        }
    dis is the output.

    EDIT: I realize that else if statements must end with an else statement, but if the value turns out to be between a/3 and 2a/3, nothing happens, so should I just add:
    Code:
    else
        {
            dis = dis;
         }

    so what do I need to add at the end of that if-loop to ensure I won't get any values less than zero and greater than N-1?
    Last edited by vutek0328; 12-13-2006 at 06:08 PM.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I have no idea what you're trying to do...maybe an example ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    realize that else if statements must end with an else statement
    else and else if statements are optional.
    Code:
    if(1) {}
    
    if(1) {}
    else if(2) {}
    
    if(1) {}
    else if(2) {}
    else {}
    so what do I need to add at the end of that if-loop to ensure I won't get any values less than zero and greater than N-1?
    This?
    Code:
    if(value < 0) value = 0;
    else if(value > max) value = max;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    dis is a value that starts at 0 and will change depending on u:

    if u is < a/3, then it will decrease 1.
    if u is > 2a/3, then it will increase 1.

    if the first value u is < a/3 then the dis will go to -1 and cause the program to end, likewise with when the last u value is > 2a/3 then the dis will go to N, and also cause the program to end. How do I prevent that from occuring?


    EDIT: Ah yes dwks, thanks

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean, how do I use a while loop? http://www.cprogramming.com/tutorial/c/lesson3.html

    What do you want dis to be after the loop or whatever has ended?

    BTW, if you want nothing to happen you can use "{}" or ";":
    Code:
    if(1) {}
    else ;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    as u are decrementing on both the condition

    Code:
    if(u==-1)
        dis= N;
    elseif(u==N)
        dis=0;
    else
        dis--;
    ssharish2005

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    I just want dis to be any number between 0 and N-1. I just think of it as points on a circle. 0..1..2..3...N-1...N(back to 0)...1...2...3..etc. So there will never be N or negative numbers.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include<stdio.h>
    
    int main()
    {
        int num=0, N=5;
        int i=0;
        
        while(i++ < 20)
        {
            if(num == -1)
                    num = N;
            else if(num == N)
                    num = 0;
    
            printf("%d\t",num++);
            
            if(i%5 == 0)
              printf("\n");
        }    
        getchar();
        return 0;
    }
    
    /*my output
    0       1       2       3       4
    0       1       2       3       4
    0       1       2       3       4
    0       1       2       3       4
    */
    something like this

    ssharish2005

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Hm...thank you all for the responses, but there is one problem with my current code. When I run it, it displays nothing. This is the exact code I used previously and now that code(which was fine before) is also displaying nothing. I'm not sure what is the cause of this problem...I can post the code, but it's a bit lengthy and would require a long time to decipher it.

    When a program does not run/display anything, usually what are the reasons behind it?

    *It does not give me any error when I compile it or any segmentation faults when i run it, just blank*

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well u can place some printf and see where the control is and finds out where it breaks. Well if u don't get any display most of the time it would in a infinite loop where are no any out statements in that loop or similar. Check that by using some prints statements

    ssharish2005

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    ah, figured it out, thanks very much

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
      for(trial = 0; trial < N; trial++)
        {
          nstep = 0;
          t = 0.0;
          dis = 0;
          while(t < to)
    	{
    	  tau = tao * (1 + g * t);
    	  dt = -log(a) * tau;
    	  u = gsl_ran_flat (r, 0.0, 1.0);
    	  fprintf(stdout, "%lf\n", u);
    	  b[nstep * maxstep + dis]++;
    	  if(u <= 1 / 3)
    	    {
    	      dis--;
    	    }
    	  else if (u >= 2 / 3)
    	    {
    	      dis++;
    	    }
    	  else
    	    {
    	    }
    	  if(dis < 0) 
    	    {
    	      dis = 1;
    	    }
    	  else if(dis > 1)
    	    {
    	      dis = 0;
    	    }
    	  t = t + dt;
    	  times[nstep] = t;
    	  nstep++;
    	}
        }
    The purpose of this code is to determine whether a particle hops forward or backwards or stays the same (starting at 0) by using a random number generator. If the number u generated is less than 1/3, then the particle hops backwards by 1 unit. If the number u generated is greater than 2/3, then particle hops forward by 1 unit. If the number u generated is inbetween 1/3 and 2/3, then the particle remains still.

    the second part of the code allows the particle to hop continuously:
    If a particle hops to dis = N-1, it is to return to 0 and continue the process.
    If a particle hops to dis = -1, it is to return to N-1 and continue the process.

    The problem with my code is, despite the generated values, the particle does not behave like I intend it to, such as dis does not increase when u > 2/3...etc. Why not?

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well, what is the value which is returned by the gsl_ran_flat function. I guess it is not satisfying the if condition and reaching else part of elseif statement without taking any action on dis. Try placing any printf in the else and find the value of dis and check out the if condition.

    ssharish2005

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You seem to be comparing u against the result of an integer division (1/3 = 0). You should tell the compiler that you mean floats: (1.0/3.0)

    As to count back and forth, I came up with something like that:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    const int MAX = 10;
    const int MIN = 0;
    
    int main()
    {
        int i = 0;
    //if i == MIN -> step has to be -1
    //if i = MAX -> step has to be 1
    //else it won't matter
    //You may code proper initialization
        int step = -1; 
        while (1) {
            if (i == MAX || i == MIN)
                step *= -1;
            i += step;
            printf("%d ", i);
            Sleep(100); //give it a break
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. combining boolean conditions with parenthesis
    By mc61 in forum C Programming
    Replies: 2
    Last Post: 04-15-2008, 05:57 AM
  4. Multiple conditions for the while loop?
    By Olidivera in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2005, 03:47 AM
  5. Boundary condition
    By Juganoo in forum C Programming
    Replies: 1
    Last Post: 01-21-2003, 07:07 PM