# include # include # include # include using namespace std; # include "kdv_exact.hpp" int main ( ); void kdv_exact_rational_test ( ); void kdv_exact_sech_test ( ); double *r8vec_linspace_new ( int n, double a, double b ); void timestamp ( ); //****************************************************************************80 int main ( ) /******************************************************************************/ // // Purpose: // // kdv_exact_test() tests kdv_exact(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 02 May 2024 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "kdv_exact_test():\n"; cout << " C++ version\n"; cout << " Test kdv_exact().\n"; kdv_exact_rational_test ( ); kdv_exact_sech_test ( ); // // Terminate. // cout << "\n"; cout << "kdv_exact_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void kdv_exact_rational_test ( ) //****************************************************************************80 // // Purpose: // // kdv_exact_rational_test() tests kdv_exact_rational(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 02 May 2024 // // Author: // // John Burkardt // { int nt = 6; int nx = 6; int i; int j; double r; double *t; double t0; double tstop; double u; double ut; double ux; double uxx; double uxxx; double *x; cout << "\n"; cout << "kdv_exact_rational_test():\n"; cout << " Test kdv_exact_rational().\n"; kdv_parameters ( NULL, NULL, NULL, NULL, NULL, NULL, &t0, &tstop ); cout << "\n"; cout << " Parameters:\n"; cout << " t0 = " << t0 << "\n"; cout << " tstop = " << tstop << "\n"; cout << "\n"; cout << " Evaluate solution and residual at selected points (X,T)\n"; x = r8vec_linspace_new ( nx, 0.0, 1.0 ); t = r8vec_linspace_new ( nt, t0, tstop ); cout << "\n"; cout << " T X U(X,T) Resid(X,T)\n"; cout << "\n"; for ( i = 0; i < nt; i++ ) { for ( j = 0; j < nx; j++ ) { kdv_exact_rational ( x[j], t[i], &u, &ut, &ux, &uxx, &uxxx ); r = kdv_residual ( u, ut, ux, uxxx ); if ( t[i] == 0.0 && x[j] == 0.0 ) { cout << setw(14) << t[i] << setw(14) << x[j] << " Undefined Undefined\n"; } else { cout << setw(14) << t[i] << setw(14) << x[j] << setw(14) << u << setw(14) << r << "\n"; } } cout << "\n"; } delete [] t; delete [] x; return; } //****************************************************************************80 void kdv_exact_sech_test ( ) //****************************************************************************80 // // Purpose: // // kdv_exact_sech_test() tests kdv_exact_sech(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 02 May 2024 // // Author: // // John Burkardt // { int nt = 6; int nx = 6; double a; int i; int j; double r; double *t; double t0; double tstop; double u; double ut; double ux; double uxx; double uxxx; double v; double *x; cout << "\n"; cout << "kdv_exact_sech_test():\n"; cout << " Test kdv_exact_sech().\n"; kdv_parameters ( NULL, NULL, NULL, NULL, &a, &v, &t0, &tstop ); cout << "\n"; cout << " Parameters:\n"; cout << " a = " << a << "\n"; cout << " v = " << v << "\n"; cout << " t0 = " << t0 << "\n"; cout << " tstop = " << tstop << "\n"; cout << "\n"; cout << " Evaluate solution and residual at selected points (X,T)\n"; x = r8vec_linspace_new ( nx, 0.0, 1.0 ); t = r8vec_linspace_new ( nt, t0, tstop ); cout << "\n"; cout << " T X U(X,T) Resid(X,T)\n"; cout << "\n"; for ( i = 0; i < nt; i++ ) { for ( j = 0; j < nx; j++ ) { kdv_exact_sech ( x[j], t[i], &u, &ut, &ux, &uxx, &uxxx ); r = kdv_residual ( u, ut, ux, uxxx ); if ( t[i] == 0.0 && x[j] == 0.0 ) { cout << setw(14) << t[i] << setw(14) << x[j] << " Undefined Undefined\n"; } else { cout << setw(14) << t[i] << setw(14) << x[j] << setw(14) << u << setw(14) << r << "\n"; } } cout << "\n"; } delete [] t; delete [] x; return; } //****************************************************************************80 double *r8vec_linspace_new ( int n, double a_first, double a_last ) //****************************************************************************80 // // 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 // // Parameters: // // Input, int N, the number of entries in the vector. // // Input, double A_FIRST, A_LAST, the first and last entries. // // Output, double R8VEC_LINSPACE_NEW[N], a vector of linearly spaced data. // { double *a; int i; a = new double[n]; if ( n == 1 ) { a[0] = ( a_first + a_last ) / 2.0; } else { for ( i = 0; i < n; i++ ) { a[i] = ( ( double ) ( n - 1 - i ) * a_first + ( double ) ( i ) * a_last ) / ( double ) ( n - 1 ); } } return a; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // 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: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }