# include # include # include # include # include # include "doughnut_ode.h" int main ( ); void doughnut_euler ( int n ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: doughnut_ode_test() tests doughnut_ode(). Licensing: This code is distributed under the MIT license. Modified: 13 October 2025 Author: John Burkardt */ { double m; double n; int nt; double t0; double tstop; double y0[3]; timestamp ( ); printf ( "\n" ); printf ( "double_ode_test():\n" ); printf ( " C version\n" ); printf ( " Test double_ode(), which solves a stiff ODE using euler().\n" ); /* Get current parameter values. */ doughnut_parameters ( NULL, NULL, NULL, NULL, NULL, &m, &n, &t0, y0, &tstop ); printf ( "\n" ); printf ( " Parameter values:\n" ); printf ( " m = %g\n", m ); printf ( " n = %g\n", n ); printf ( " t0 = %g\n", t0 ); printf ( " y0 = %g, %g, %g\n", y0[0], y0[1], y0[2] ); printf ( " tstop = %g\n", tstop ); nt = 10001; doughnut_euler ( nt ); /* Terminate. */ printf ( "\n" ); printf ( "doughnut_ode_test():\n" ); printf ( " Normal end of execution.\n" ); timestamp ( ); return 0; } /******************************************************************************/ void doughnut_euler ( int nt ) /******************************************************************************/ /* Purpose: doughnut_euler() uses euler() to solve the doughnut ode. Licensing: This code is distributed under the MIT license. Modified: 13 October 2025 Author: John Burkardt Input: int NT: the number of steps to take. */ { char command_filename[] = "doughnut_ode_commands.txt"; FILE *command_unit; char data_filename[] = "doughnut_ode_data.txt"; FILE *data_unit; double dt; double dydt[3]; int i; int j; double t0; double *t; double tstop; double *y; double y0[3]; printf ( "\n" ); printf ( "doughnut_euler():\n" ); printf ( " Solve doughnut ODE using the Euler method.\n" ); doughnut_parameters ( NULL, NULL, NULL, NULL, NULL, NULL, NULL, &t0, y0, &tstop ); dt = ( tstop - t0 ) / ( nt - 1 ); t = r8vec_linspace_new ( nt, t0, tstop ); y = ( double * ) malloc ( nt * 3 * sizeof ( double ) ); for ( i = 0; i < nt; i++ ) { if ( i == 0 ) { for ( j = 0; j < 3; j++ ) { y[i*3+j] = y0[j]; } } else { doughnut_deriv ( t[i-1], y+(i-1)*3, dydt ); for ( j = 0; j < 3; j++ ) { y[i*3+j] = y[(i-1)*3+j] + dt * dydt[j]; } } } /* Plot. */ data_unit = fopen ( data_filename, "wt" ); for ( i = 0; i < nt; i++ ) { fprintf ( data_unit, " %14.6g %14.6g %14.6g %14.6g\n", t[i], y[i*3+0], y[i*3+1], y[i*3+2] ); } fclose ( data_unit ); printf ( " Created data file \"%s\".\n", data_filename ); /* Create the 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 'doughnut_ode.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 2 linecolor rgb 'blue'\n", data_filename ); fprintf ( command_unit, "quit\n" ); fclose ( command_unit ); printf ( " Created command file '%s'\n", command_filename ); /* Free memory. */ free ( t ); free ( y ); return; } /******************************************************************************/ double *r8vec_linspace_new ( int n, double a, double b ) /******************************************************************************/ /* Purpose: r8vec_linspace_new() creates a vector of linearly spaced values. Discussion: An R8VEC is a vector of R8's. 4 points evenly spaced between 0 and 12 will yield 0, 4, 8, 12. In other words, the interval is divided into N-1 even subintervals, and the endpoints of intervals are used as the points. Licensing: This code is distributed under the MIT license. Modified: 29 March 2011 Author: John Burkardt Input: int N, the number of entries in the vector. double A, B, the first and last entries. Output: double R8VEC_LINSPACE_NEW[N], a vector of linearly spaced data. */ { int i; double *x; x = ( double * ) malloc ( n * sizeof ( double ) ); if ( n == 1 ) { x[0] = ( a + b ) / 2.0; } else { for ( i = 0; i < n; i++ ) { x[i] = ( ( double ) ( n - 1 - i ) * a + ( double ) ( i ) * b ) / ( double ) ( n - 1 ); } } return x; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }