Ejemplo n.º 1
0
int main ( )

/******************************************************************************/
/*
  Purpose:

    MAIN is the main program for LORENZ_ODE.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    14 October 2013

  Author:

    John Burkardt
*/
{
  char command_filename[] = "lorenz_ode_commands.txt";
  FILE *command_unit;
  char data_filename[] = "lorenz_ode_data.txt";
  FILE *data_unit;
  double dt;
  int i;
  int j;
  int m = 3;
  int n = 200000;
  double *t;
  double t_final;
  double *x;
  double *xnew;

  timestamp ( );
  printf ( "\n" );
  printf ( "LORENZ_ODE\n" );
  printf ( "  C version\n" );
  printf ( "  Compute solutions of the Lorenz system.\n" );
  printf ( "  Write data to a file for use by gnuplot.\n" );
/*
  Data
*/
  t_final = 40.0;
  dt = t_final / ( double ) ( n );
/*
  Store the initial conditions in entry 0.
*/
  t = r8vec_linspace_new ( n + 1, 0.0, t_final );
  x = ( double * ) malloc ( m * ( n + 1 ) * sizeof ( double ) );
  x[0+0*m] = 8.0;
  x[0+1*m] = 1.0;
  x[0+2*m] = 1.0;
/*
  Compute the approximate solution at equally spaced times.
*/
  for ( j = 0; j < n; j++ )
  {
    xnew = rk4vec ( t[j], m, x+j*m, dt, lorenz_rhs );
    for ( i = 0; i < m; i++ )
    {
      x[i+(j+1)*m] = xnew[i];
    }
    free ( xnew ); 
  }
/*
  Create the plot data file.
*/
  data_unit = fopen ( data_filename, "wt" );
  for ( j = 0; j <= n; j = j + 50 )
  {
    fprintf ( data_unit, "  %14.6g  %14.6g  %14.6g  %14.6g\n", 
      t[j], x[0+j*m], x[1+j*m], x[2+j*m] );
  }
  fclose ( data_unit );
  printf ( "  Created data file \"%s\".\n", data_filename );
/*
  Create the plot command file.
*/
  command_unit = fopen ( command_filename, "wt" );
  fprintf ( command_unit, "# %s\n", command_filename );
  fprintf ( command_unit, "#\n" );
  fprintf ( command_unit, "# Usage:\n" );
  fprintf ( command_unit, "#  gnuplot < %s\n", command_filename );
  fprintf ( command_unit, "#\n" );
  fprintf ( command_unit, "set term png\n" );
  fprintf ( command_unit, "set output 'xyz_time.png'\n" );
  fprintf ( command_unit, "set xlabel '<--- T --->'\n" );
  fprintf ( command_unit, "set ylabel '<--- X(T), Y(T), Z(T) --->'\n" );
  fprintf ( command_unit, "set title 'X, Y and Z versus Time'\n" );
  fprintf ( command_unit, "set grid\n" );
  fprintf ( command_unit, "set style data lines\n" );
  fprintf ( command_unit, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue',", 
    data_filename );
  fprintf ( command_unit, "'' using 1:3 lw 3 linecolor rgb 'red'," );
  fprintf ( command_unit, "'' using 1:4 lw 3 linecolor rgb 'green'\n" );
  fprintf ( command_unit, "set output 'xyz_3d.png'\n" );
  fprintf ( command_unit, "set xlabel '<--- X(T) --->'\n" );
  fprintf ( command_unit, "set ylabel '<--- Y(T) --->'\n" );
  fprintf ( command_unit, "set zlabel '<--- Z(T) --->'\n" );
  fprintf ( command_unit, "set title '(X(T),Y(T),Z(T)) trajectory'\n" );
  fprintf ( command_unit, "set grid\n" );
  fprintf ( command_unit, "set style data lines\n" );
  fprintf ( command_unit, 
    "splot '%s' using 2:3:4 lw 1 linecolor rgb 'blue'\n", data_filename );
  fprintf ( command_unit, "quit\n" );
  fclose ( command_unit );
  printf ( "  Created command file '%s'\n", command_filename );
/*
  Terminate.
*/
  printf ( "\n" );
  printf ( "LORENZ_ODE:\n" );
  printf ( "  Normal end of execution.\n" );
  printf ( "\n" );
  timestamp ( );

  return 0;
}
Ejemplo n.º 2
0
void get_solution_vectors(double *pmin_t, double *pdvsx, double *pdvsy, double clearance, int obj, double DT, double tol)
{
// Name the file.*******************************
/*char *arr[sizeof(tol)];
memcpy(&arr,&tol,sizeof(tol));

char *strsep( char **arr, char *.);
char *file_name;
file_name = "Optimum"+ 
*/
//**********************************************
FILE *f = fopen("Optimum_%d_%f_0p%c","w");
if (f == NULL)
{
	printf("Error opening file! \n");
}
	// Initial change in velocity
	double dvsx, dvsy, min_t;
	dvsx = *pdvsx;
	dvsy = *pdvsy;
	min_t = *pmin_t;
		
	// Set initial values for Rk4 function.
	int i, M = 8;		/* Size of array needed for positions and velocities*/
	double *u0;		/* Declare initial value array pointer */
	double T0 = 0;		/* Set initial time */
	double *u1, T1;		/* Declare +1 step */
	int count = 0; 		/* Initialize count to index solutions array */
	int n = min_t/DT;	/* The size of the solution vectors */
	
	// Set Initial Conditions
	double xs, ys, xm, ym, xe, ye, vsx, vsy, vmx, vmy, vex, vey;
	double dms, des, dem, theta_m, theta_s, vs, dvs, vm, val;
	val = PI / 180.0;
	des = 340000000.0;
	theta_s = 50.0*val;
	vs = 1000.0;
	xs = des*cos(theta_s);
	ys = des*sin(theta_s);
	vsx = vs*cos(theta_s)+dvsx;
	vsy = vs*sin(theta_s)+dvsy;
	dem = 384403000.0;
	theta_m = 42.5*val;
	vm = sqrt((G*pow(me,2))/((me+mm)*dem));
	xm = dem*cos(theta_m);
	ym = dem*sin(theta_m);
	vmx = -vm*sin(theta_m);
	vmy = vm*cos(theta_m);
	xe = 0;
	ye = 0;
	vex = 0;
	vey = 0;
	dms = sqrt(pow((xs-xm),2)+pow((ys-ym),2));
	dvs = sqrt(pow(dvsx,2)+pow(dvsy,2));	


	// Allocate size and fill in initial value array.
	u0 = (double *) malloc(M * sizeof(double));
	u0[0] = vsx;
	u0[1] = vsy;
	u0[2] = vmx;
	u0[3] = vmy;
	u0[4] = xs;
	u0[5] = ys;
	u0[6] = xm;
	u0[7] = ym;
/**********************************************************	
	 Run the simulation with all input parameters.
************************************************************/
	while (dms >= rm+clearance && des >= re && des <= 2*dem)		
	{
/**********************************************************
	Call rk4 function
************************************************************/
	T1 = T0 + 1;
	u1 = rk4vec( T0 , M , u0 , DT , RHS);	
/*********************************************************
	Shift the data to prepare for another step
***********************************************************/
	T0 = T1;
	for ( i = 0; i < M; i++)
	{
		u0[i] = u1[i];
	}
/***********************************************************
	Get distances to check events.
*************************************************************/	
	vsx = u0[0];
	vsy = u0[1];
	vmx = u0[2];
	vmy = u0[3];
	xs  = u0[4];
	ys  = u0[5];
	xm  = u0[6];
	ym  = u0[7];
	dms = sqrt(pow((xs - xm),2) + pow((ys - ym),2));
	dem = sqrt(pow((xe - xm),2) + pow((ye - ym),2));
	des = sqrt(pow((xe - xs),2) + pow((ye - ys),2));
/***********************************************************
	Create solutions array using a struct.
***********************************************************/	
	if (count<n)	
/* Make sure the count stays below the predetermined array size. Replace this with total time/ time step */
	{
/*
		solution_vector_ptr[count].xs = xs;
		solution_vector_ptr[count].ys = ys;
		solution_vector_ptr[count].xm = xm;
		solution_vector_ptr[count].ym = ym;
		solution_vector_ptr[count].xe = xe;
		solution_vector_ptr[count].ye = ye;
		solution_vector_ptr[count].vsx = vsx;
		solution_vector_ptr[count].vsy = vsy;
		solution_vector_ptr[count].vmx = vmx;
		solution_vector_ptr[count].vmy = vmy;
		solution_vector_ptr[count].vex = vex;
		solution_vector_ptr[count].vey = vey;
		solution_vector_ptr[count].T0 = T0;
*******************************************************************************************************/
// Put in the values
fprintf(f, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f \n",T0,xs,ys,xm,ym,xe,ye,vsx,vsy,vmx,vmy,vex,vey);


	}
//	count++;
	free ( u1 );
	}

fclose(f);
//	free(solution_vector_ptr);
}		
Ejemplo n.º 3
0
void test02 ( )

/******************************************************************************/
/*
  Purpose:

    TEST02 tests the RK4 routine for a vector ODE.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    09 October 2013

  Author:

    John Burkardt
*/
{
  double dt = 0.2;
  int i;
  int n = 2;
  double t0;
  double t1;
  double tmax = 12.0 * 3.141592653589793;
  double *u0;
  double *u1;

  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  RK4VEC takes a Runge Kutta step for a vector ODE.\n" );

  printf ( "\n" );
  printf ( "       T       U[0]       U[1]\n" );
  printf ( "\n" );
  t0 = 0.0;

  u0 = ( double * ) malloc ( n * sizeof ( double ) );
  u0[0] = 0.0;
  u0[1] = 1.0;

  for ( ; ; )
  {
/*
  Print (T0,U0).
*/
    printf ( "  %14.6g  %14.6g  %14.6g\n", t0, u0[0], u0[1] );
/*
  Stop if we've exceeded TMAX.
*/
    if ( tmax <= t0 )
    {
      break;
    }
/*
  Otherwise, advance to time T1, and have RK4 estimate 
  the solution U1 there.
*/
    t1 = t0 + dt;
    u1 = rk4vec ( t0, n, u0, dt, test02_f );
/*
  Shift the data to prepare for another step.
*/
    t0 = t1;
    for ( i = 0; i < n; i++ )
    {
      u0[i] = u1[i];
    }
    free ( u1 );
  }
  return;
}