±±¾©Ê¯ÓÍ»¯¹¤Ñ§Ôº2026ÄêÑо¿ÉúÕÐÉú½ÓÊÕµ÷¼Á¹«¸æ
²é¿´: 760  |  »Ø¸´: 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 µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] 366Çóµ÷¼Á +6 sbdnd 2026-04-03 6/300 2026-04-03 11:18 by wangjy2002
[¿¼ÑÐ] Ò»Ö¾Ô¸±±¾©¿Æ¼¼´óѧ²ÄÁϹ¤³Ì085601£¬Çóµ÷¼Á +12 cdyw 2026-04-02 12/600 2026-04-03 10:18 by À¶ÔÆË¼Óê
[¿¼ÑÐ] 279Çóµ÷¼Á +6 qazplm0852 2026-04-02 6/300 2026-04-03 10:03 by À¶ÔÆË¼Óê
[¿¼ÑÐ] 338Çóµ÷¼Á£¬Ò»Ö¾Ô¸ÄÜÔ´¶¯Á¦£¬ÍâÓïÊÇÈÕÓï203 +5 zzz£¬£¬r 2026-04-02 5/250 2026-04-03 09:45 by À¶ÔÆË¼Óê
[¿¼ÑÐ] Ò»Ö¾Ô¸±±¾©¹¤Òµ´óѧ£¬324·ÖÇóµ÷¼Á +7 Áã°Ë# 2026-03-28 7/350 2026-04-02 21:09 by 1104338198
[¿¼ÑÐ] 346Çóµ÷¼Á +5 Ö£³ÏÀÖ 2026-04-02 5/250 2026-04-02 16:38 by SZW_UJN
[¿¼ÑÐ] 270Çóµ÷¼Á +8 С½Üpp 2026-03-31 10/500 2026-04-02 12:57 by yulian1987
[¿¼ÑÐ] 08¿ªÍ·¿´¹ýÀ´£¡£¡£¡ +4 wwwwffffff 2026-03-31 6/300 2026-04-02 11:42 by ¾ùÖµ»Ø¹é
[¿¼ÑÐ] Ò»Ö¾Ô¸°²»Õ´óѧ¼ÆËã»ú¿ÆÑ§Óë¼¼Êõѧ˶£¬331·ÖÇóµ÷¼Á +5 ½¯²ýÅôqtj 2026-04-01 5/250 2026-04-02 08:10 by fxue1114
[¿¼ÑÐ] 339Çóµ÷¼Á£¬Ïëµ÷»Ø½­ËÕ +7 ¿¾ÂóÑ¿ 2026-03-27 10/500 2026-04-01 21:35 by 495374996
[¿¼ÑÐ] 085600 Ò»Ö¾Ô¸9 ×Ü·Ö351 Çóµ÷¼ÁѧУ +7 czhcz 2026-03-31 9/450 2026-04-01 19:24 by Î޼ʵIJÝÔ­
[˶²©¼ÒÔ°] ¿¼Ñе÷¼Á +5 ÂæÍÕÄÐÈË 2026-04-01 5/250 2026-04-01 14:28 by syjjj0321
[¿¼ÑÐ] 283Çóµ÷¼Á +9 A child 2026-03-28 9/450 2026-04-01 14:20 by Jaylen.
[¿¼ÑÐ] 267Çóµ÷¼Á +13 uiybh 2026-03-31 13/650 2026-04-01 10:25 by ̽123
[¿¼ÑÐ] Çóµ÷¼Á£¬Ò»Ö¾Ô¸±±ÁÖʳƷÓëÓªÑø095500£¬301·Ö£¬ÒѹýÁù¼¶£¬ÓпÆÑо­Àú +4 ¿ìÀÖ´¢Ðî¹Þ 2026-03-31 4/200 2026-04-01 09:26 by JourneyLucky
[¿¼ÑÐ] ±¾2Ò»Ö¾Ô¸C9-333·Ö£¬²ÄÁÏ¿ÆÑ§Ó빤³Ì£¬Çóµ÷¼Á +9 ÉýÉý²»½µ 2026-03-31 9/450 2026-03-31 18:01 by Î޼ʵIJÝÔ­
[¿¼ÑÐ] ¿¼Ñе÷¼ÁÇóÖú +7 13287130938 2026-03-31 7/350 2026-03-31 16:39 by 690616278
[¿¼ÑÐ] µ÷¼Á¿¼ÑÐ +3 Íõ½ÜÒ» 2026-03-29 3/150 2026-03-29 08:09 by fmesaito
[¿¼ÑÐ] ±¾¿ÆÐÂÄÜÔ´¿ÆÑ§Ó빤³Ì£¬Ò»Ö¾Ô¸»ªÀíÄܶ¯285Çóµ÷¼Á +3 AZMK 2026-03-27 5/250 2026-03-28 16:19 by xxxsssccc
[¿¼ÑÐ] 081200-314 +3 LILIQQ 2026-03-27 4/200 2026-03-28 09:41 by ±£»¤µØÇòÄãÎÒ×öÆ
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û