Thread: How can I create a numerical simulacion for my physics problem?

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    2

    How can I create a numerical simulacion for my physics problem?

    I have an 3 body problem in physics and I want to create a simulation for it. I have never done anything like that (beginner to C programming) so any advice would be appreciated. I used Code Blocks so far, is it possible to do my simulation in it? Or I have to use an other program for plotting or I just simply have to use some kind of header file for this?

    Thank you for your help in advance!

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    225
    2D or 3D? What do you need your output as?

    Here's the pseudocode for your program.

    Code:
    main function() 
        read in initial state (positions, masses, velocities) from a file
        while(sim_time < end_time)
        do
            calculate_forces(); 
            timestep = assess_timestep(); // when forces are great use a smaller delta-t
            update_state(timestep);  // update positions and velocities
            log_current_state();  // Append to the output, for later analysis.
            update_display(); // If you wanted to interact with the user, or just print the sim time on the screen
        end loop

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    2
    Thanks for the pseudocode!
    I need a 2D program. I would love to have an output like this:
    Programming a simulation of the Earth orbiting the Sun
    Can you recommend any programs/header files that I cansomething like this?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,629
    > I have never done anything like that (beginner to C programming)
    But you're an experienced programmer in some other language - right?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    225
    You might find this interesting.

    It uses "ANSI Escape Sequences" to plot things on the console - or at least it does under Linux. I haven't tested it in a Windows Console.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <math.h>
    
    
    static const char ansi_clear[] = "\033[3J\033[H\033[2J";
    static const char ansi_goto[]  = "\033[%i;%iH";
    
    
    void clear(void) {
      printf(ansi_clear);
    }
    
    
    void plot(int x, int y, char c) {
      printf(ansi_goto,y,x);
      putchar(c);
      putchar('\b');
      fflush(stdout);
    }
    
    
    int main(int argc, char *argv[]) {
       int i;
       clear();
       for(i=0; i < 1000; i++) {
          // Map to screen coordinates
          int pos_x = sin(i*M_PI/200)*30+40+0.5;
          int pos_y = cos(i*M_PI/200)*10+16+0.5;
          plot(pos_x, pos_y,'O');
          usleep(50000);
          plot(pos_x, pos_y,'.');
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numerical Recipe Problem
    By Nooby 1 Kenooby in forum C Programming
    Replies: 5
    Last Post: 10-27-2012, 11:04 AM
  2. problem with numerical calculations
    By phyushin in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2006, 07:12 PM
  3. problem with numerical calculations
    By Calef13 in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2006, 08:21 AM
  4. Create arrays with constructor problem
    By dee.dw in forum C Programming
    Replies: 7
    Last Post: 10-26-2005, 11:36 AM
  5. Physics Engine Problem
    By Muphin in forum Game Programming
    Replies: 4
    Last Post: 07-26-2005, 09:04 PM

Tags for this Thread