²é¿´: 749  |  »Ø¸´: 2

zyj8119

ľ³æ (ÖøÃûдÊÖ)

[ÇóÖú] Ò»¸ö³ÌÐò±àÒ룬³öÏÖÁËÒ»¸öÆæ¹ÖµÄÎÊÌâ¡£

CODE:
// Planet2.cc
//  see also gnu20, gnu21
// usage:
//  Planet2 1300 0.03 25 | gnuplot -persist
// This program requires the directory 'planet' to exist, and
// expects it to contain a file gnu0.

#include
#include
#include
using namespace std;

#define D  2  // number of dimensions
struct particle {
  double x[D] ; // (x,y) coordinates
  double p[D] ; // momentum
  double F[D] ; // force on particle
  double im   ; // inverse mass of particle
  double GMm  ; // gravitational parameter of particle
  double v[D] ; // velocity
  double T    ; // kinetic energy
  double J    ; // angular momentum
  double V    ; // potential energy
  double r    ; // absolute distance from origin
  int  law    ; // which force law
} ; // Note the definition of a structure ends with a semicolon

struct control {
  int verbose    ; // program verbosity
  int printing   ; // period with which to print
  int commanding ; // period with which to issue commands
  //  ofstream *fout ; // where we write data to
  int time_in_ms ; // time between frames (commands)
  clock_t latest_time;
  ofstream fout  ;
  int euler      ; // whether to do euler method after leapfrog
};

// the "&" in "&a" indicates 'this is a pass by reference'.
void Force( particle &a )
  // sets the force vector and the potential energy
{
  double R = 0.0 ;
  for ( int i = 0 ; i < D ; i++ ) {
    R += a.x[i] * a.x[i] ;
  }
  double r = sqrt(R) ;
  a.r = r ;
  if ( a.law == -2 ) { // inverse square law
    a.V = - a.GMm / r ;
    for ( int i = 0 ; i < D ; i++ ) {
      a.F[i] = (- a.GMm * a.x[i] / (r*R) ) ;  // inverse sq
    }
  } else { // Hooke Law
    a.V = 0.5 * a.GMm * R ;
    for ( int i = 0 ; i < D ; i++ ) {
      a.F[i] = (- a.GMm * a.x[i])  ;  // linear spring law
    }
  }
}

// Implement     x += v dt
void PositionStep ( particle &a , double dt )
{
  for ( int i = 0 ; i < D ; i++ )
    a.x[i] += dt * a.p[i] * a.im ;
}

// Implement     p += f dt
void MomentumStep ( particle &a , double dt )
{
  for ( int i = 0 ; i < D ; i++ )
    a.p[i] += dt * a.F[i] ;
}

void v2p( particle &a )
  // propagate the changed velocity into the momentum vector
{
  for ( int i = 0 ; i < D ; i++ )
    a.p[i] =  a.v[i] / a.im ;
}

void p2v ( particle &a ) {
  for ( int i = 0 ; i < D ; i++ )
    a.v[i]  = a.p[i] * a.im ;
}

void pv2T ( particle &a ) {
  // compute the kinetic energy and angular momentum
  a.T=0.0;
  for ( int i = 0 ; i < D ; i++ ) {
    a.T    += 0.5*a.v[i] * a.p[i] ;
  }
  a.J  =  a.x[0] * a.p[1] - a.x[1] * a.p[0] ;
}

// Print out the state (x,v,Energies,J) in columns
void showState ( particle &a , ostream &fout )
{
  for ( int i = 0 ; i < D ; i++ ) {
    fout << "\t"<   }
  for ( int i = 0 ; i < D ; i++ ) {
    fout << "\t"<   }
  fout << "\t" << a.T     // KE
       << "\t" << a.V     // PE
       << "\t" << a.T+a.V // Total E
       << "\t" << a.J     // Angular momentum
       << endl;
  fout << "#" ;
  for ( int i = 0 ; i < D ; i++ ) {
    fout << "\tx["<   }
  for ( int i = 0 ; i < D ; i++ ) {
    fout << "\tv["<   }
  fout << "\tT\t\tV\t\tT+V\t\tJ" ;
  fout << endl;
}

void showStateByArrow ( particle &a , ostream &fout )
{
  fout << "set arrow 101 from " << a.x[0] << ","
       << a.x[1]  << " to "
       << a.x[0]+a.v[0] << ","
       << a.x[1]+a.v[1] << " lt 5 " << endl ;
}

void PossibleOutput( particle &a , control &c , int i , double t ) {
  if( ( c.printing && !(i%c.printing) )
      || ( c.commanding  && !(i%c.commanding) ) ) {
    p2v(a) ;  pv2T(a) ;  Force( a ) ;
  }
  if( ( c.printing && !(i%c.printing) ) ) {
    c.fout << t ; showState( a ,  c.fout ) ;
  }
  if ( c.commanding  && !(i%c.commanding) ) {
    // SEND COMMANDS TO gnuplot live via 'cout' pipe
    c.fout.flush() ; // make sure file is out
    system("tail -39 /tmp/1 > /tmp/2");
    showStateByArrow( a , cout ) ; // sends 'set arrow' command to gnuplot
    if(i==0) {
      cout << "load 'planet/gnu0'" << endl ; // to gnuplot
      cout.flush() ;
      c.latest_time = clock() ;
    } else {
      c.latest_time += CLOCKS_PER_SEC * c.time_in_ms / 1000 ;
      clock_t endwait = c.latest_time ;
      clock_t time_to_go = endwait-clock() ;
      while( clock() < endwait ) {} ;       // wait till the appointed time
      cout << "load 'planet/gnu0'" << endl ; // to gnuplot
      //      cout << "replot" << endl ;
      while( clock() < endwait+time_to_go/3 ) {} ; // add a little further
      // pause to give gnuplot a chance to do its thing before we edit
      // files again
    }
  }
}

// The Leapfrog method simulates the equations of motion
//  dx/dt = p/m
//  dp/dt = f
// by alternately updating the position (using the latest
// momentum), then the momentum (using the force at the
// updated position).
void LeapfrogDynamics( particle &a,
                       double dt  , // step size
                       double &t  , // current time
                       int N      , // number of steps
                       control &c   // parameters controlling output, etc
                       )
{
  for ( int i=0 ; i < N ; i ++ ) {
    PossibleOutput( a , c , i , t ) ;
    // each iteration we move the position a half-step
    PositionStep( a , dt*0.5 ) ;
    Force( a ) ; // (computes the force at this position)
    // then we move the momentum full-step
    MomentumStep( a , dt )     ;
    // then we move the position a half-step
    PositionStep( a , dt*0.5 ) ;
    t += dt ;
  }
}

// The Euler method updates the position and momentum
// simultaneously.  For the force laws considered here,
// we can get the effect of simultaneous update by
// finding the force, then
// updating the position, THEN the momentum.
void EulerDynamics( particle &a,
                    double dt  , // step size
                    double &t  , // current time
                    int N      , // number of steps
                    control &c   // parameters controlling output, etc
                    )
{
  for ( int i=0 ; i < N ; i ++ ) {
    PossibleOutput( a , c , i , t ) ;
    Force( a ) ; // (computes the force at this position)
    PositionStep( a , dt ) ;
    MomentumStep( a , dt ) ;
    t += dt ;
  }
}

int main(int argc, char* argv[])
{
  particle   a ;
  control    c ;
  // set defaults
  int N = 250 ;  
  double dt = 0.1 ;
  double t = 0.0 ;
  c.verbose = 1 ;
  c.printing   =  4 ;
  c.commanding =  1 ; // number of iterations between plots
  c.time_in_ms = 100; // real time between plots (in ms)
  c.euler = 0 ;       // whether to do euler after leapfrog

  // read in any command-line arguments
  if(argc>1)   {
    sscanf( argv[1], "%d", &N ) ; // put the first command-line argument in N
  }
  if(argc>2) {
    sscanf( argv[2], "%lf", &dt ) ; // put the 2nd argument in dt
  }
  if(argc>3) {
    sscanf( argv[3], "%d", &(c.time_in_ms) ) ;
  }

  // try to write output file
  char fileName[]="/tmp/1";
  c.fout.open(fileName);
  if(c.fout.good()==false){
    cerr << "can't write to file " << fileName << endl;
    exit(0);
  }

  // set initial conditions for a particle
  a.law  = -2 ; // -2: inverse square
  a.im   = 1.0 ;
  a.v[0] = 0.4;
  a.v[1] = 0.0;
  a.x[0] = 1.5;
  a.x[1] = 2;
  a.GMm = 1.0;
  v2p(a) ; // get the initial momentum from the velocity

  LeapfrogDynamics( a , dt , t , N , c ) ;
  if ( c.euler ) {
    cerr << "# SWITCHING TO EULER\n" ;
    c.fout << "\n\n\n" ;
    EulerDynamics( a , dt , t , N , c ) ;
  }
  
  return 0;
}

--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.c
c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal error C1189: #error :  "eh.h is only for C++!"
Error executing cl.exe.

1.obj - 1 error(s), 0 warning(s)
»Ø¸´´ËÂ¥

» ²ÂÄãϲ»¶

» ±¾Ö÷ÌâÏà¹Ø¼ÛÖµÌùÍÆ¼ö£¬¶ÔÄúͬÑùÓаïÖú:

ºÃºÃѧϰ£¬ÌìÌìÏòÉÏ¡£
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

sudo

ľ³æ (ÕýʽдÊÖ)

¡¾´ð°¸¡¿Ó¦Öú»ØÌû

¡ï
xzhdty(½ð±Ò+1): ÖÐÇï¿ìÀÖ£¬»¶Ó­³£À´ 2011-09-10 23:30:51
¸ÄÃû³É1.cpp¹À¼Æ¾ÍºÃÁË

¾ßÌ岡Òò¿ÉÒÔÔĶÁeh.hÀïÃæµÄ#errorºê
2Â¥2011-09-10 23:00:25
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

yalefield

½ð³æ (ÎÄ̳¾«Ó¢)

ÀϺºÒ»Ã¶

¡¾´ð°¸¡¿Ó¦Öú»ØÌû

¡ï
΢³¾¡¢ÃÎÏë(½ð±Ò+1): ÀϺºµÄµãÆÀ×ÜÊÇÄÇôϬÀûÓÖµ½Î»~ 2011-09-12 19:50:40
ÔÚLinux¡¢UNIX»·¾³Ï£¨»òWindowsϵÄCygwin£©£¬ÓÃ.cc±íʾC++Ô´³ÌÐò¡£
ÔÚWindowsÏ£¬VC++ÓÃ.cpp±íʾC++Ô´³ÌÐò¡£
ÄúµÄ´úÂ룬ÎļþÃûÊÇ1.c£¬×¢ÊÍÈ´ÊÇ.cc£¬ÓÖÓÃVC++À´±àÒ룬´¿´âÊǺȿ§·ÈÈöËâÄ©£¬Íâ¼Ó¶¹°ê½´¡£
3Â¥2011-09-12 02:29:13
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌø×ª ÎÒÒª¶©ÔÄÂ¥Ö÷ zyj8119 µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[»ù½ðÉêÇë] ÌåÖÆÄÚ³¤±²ËµÌåÖÆÄÚ¾ø´ó²¿·ÖÒ»±²×ÓÔڵײ㣬ÈçͬÄãÃÇÒ»Ñù´ó²¿·ÖÆÕͨ½ÌʦæÇÒÊÕÈëµÍ +10 ˲ϢÓîÖæ 2026-02-20 13/650 2026-02-23 11:23 by holypower
[˶²©¼ÒÔ°] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +3 8rmuugja8q 2026-02-22 7/350 2026-02-23 09:44 by w4l55oybr1
[¿¼ÑÐ] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +3 khieu8v8m0 2026-02-22 8/400 2026-02-23 09:35 by w4l55oybr1
[ÂÛÎÄͶ¸å] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +4 khieu8v8m0 2026-02-22 8/400 2026-02-23 09:29 by w4l55oybr1
[¿¼ÑÐ] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +5 usprnugpzw 2026-02-21 11/550 2026-02-23 09:24 by w4l55oybr1
[½Ìʦ֮¼Ò] ΪʲôÖйú´óѧ¹¤¿Æ½ÌÊÚÃÇË®ÁËÄÇô¶àËùνµÄ¶¥»á¶¥¿¯£¬µ«»¹ÊÇ×ö²»³öÓîÊ÷»úÆ÷ÈË£¿ +5 »¶ÀÖËÌÒ¶Ýè 2026-02-21 8/400 2026-02-23 09:19 by »¶ÀÖËÌÒ¶Ýè
[ÂÛÎÄͶ¸å] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +3 w89i99eaeh 2026-02-22 5/250 2026-02-23 08:04 by w4l55oybr1
[²©ºóÖ®¼Ò] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +4 khieu8v8m0 2026-02-22 6/300 2026-02-23 07:59 by w4l55oybr1
[²©ºóÖ®¼Ò] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +6 3dfhjxgsh7 2026-02-22 9/450 2026-02-23 07:49 by w4l55oybr1
[¿¼²©] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +4 khieu8v8m0 2026-02-22 4/200 2026-02-23 06:46 by jsjzfl
[¹«Åɳö¹ú] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +3 khieu8v8m0 2026-02-22 5/250 2026-02-23 06:29 by w4l55oybr1
[¿¼²©] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +5 3dfhjxgsh7 2026-02-22 6/300 2026-02-23 02:04 by 5jlh3qtdvx
[»ù½ðÉêÇë] ÃæÉÏ¿ÉÒÔ³¬¹ý30Ò³°É£¿ +4 °¢À­¹±aragon 2026-02-22 4/200 2026-02-22 21:22 by ɽÎ÷Ðü¿ÕË¿ÕÐüÎ
[ÂÛÎÄͶ¸å] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +4 usprnugpzw 2026-02-21 6/300 2026-02-22 19:48 by w89i99eaeh
[¿¼ÑÐ] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +3 3dfhjxgsh7 2026-02-22 4/200 2026-02-22 16:52 by khieu8v8m0
[ÕÒ¹¤×÷] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +3 usprnugpzw 2026-02-22 3/150 2026-02-22 16:37 by khieu8v8m0
[¹«Åɳö¹ú] ÊÛSCIÒ»ÇøÎÄÕ£¬ÎÒ:8 O5 51O 54,¿ÆÄ¿ÆëÈ«,¿É+¼± +3 usprnugpzw 2026-02-21 4/200 2026-02-22 16:27 by khieu8v8m0
[»ù½ðÉêÇë] ¡°ÈËÎÄÉç¿Æ¶øÂÛ£¬Ðí¶àѧÊõÑо¿»¹Ã»ÓдﵽÃñ¹úʱÆÚµÄˮƽ¡± +4 ËÕ¶«ÆÂ¶þÊÀ 2026-02-18 5/250 2026-02-22 16:07 by liangep1573
[»ù½ðÉêÇë] ʲôÊÇÈËÒ»Éú×îÖØÒªµÄ£¿ +4 ˲ϢÓîÖæ 2026-02-21 4/200 2026-02-22 11:44 by huagongfeihu
[»ù½ðÉêÇë] ½ñÄê´ºÍíÓм¸¸ö½ÚÄ¿ºÜ²»´í£¬µãÔÞ£¡ +11 ˲ϢÓîÖæ 2026-02-16 12/600 2026-02-21 21:14 by lq493392203
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û