# include # include # include # include # include "doughnut_exact.h" int main ( ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: doughnut_exact_test() tests doughnut_exact(). Licensing: This code is distributed under the MIT license. Modified: 11 October 2025 Author: John Burkardt */ { char command_filename[] = "doughnut_exact_commands.txt"; FILE *command_unit; char data_filename[] = "doughnut_exact_data.txt"; FILE *data_unit; int i; double m; double n; int nt; double *t; double t0; double tstop; double *y; double y0[3]; timestamp ( ); printf ( "\n" ); printf ( "doughnut_exact_test():\n" ); printf ( " C version\n" ); printf ( " Test doughnut_exact(), which evaluates exact solutions\n" ); printf ( " of the doughnut ODE.\n" ); 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 = 1001; t = r8vec_linspace_new ( nt, t0, tstop ); y = doughnut_exact ( nt, t ); /* 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[0+i*3], y[1+i*3], y[2+i*3] ); } 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_exact.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 ); /* Terminate. */ printf ( "\n" ); printf ( "doughnut_exact_test():\n" ); printf ( " Normal end of execution.\n" ); timestamp ( ); return 0; } /******************************************************************************/ 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: 31 May 2001 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 24 September 2003 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 }