|
| 1 | +/* ----------------------------------------------------------------------------- |
| 2 | + * Programmer(s): David J. Gardner @ LLNL |
| 3 | + * ----------------------------------------------------------------------------- |
| 4 | + * SUNDIALS Copyright Start |
| 5 | + * Copyright (c) 2002-2024, Lawrence Livermore National Security |
| 6 | + * and Southern Methodist University. |
| 7 | + * All rights reserved. |
| 8 | + * |
| 9 | + * See the top-level LICENSE and NOTICE files for details. |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: BSD-3-Clause |
| 12 | + * SUNDIALS Copyright End |
| 13 | + * ----------------------------------------------------------------------------- |
| 14 | + * Test logging output |
| 15 | + * ---------------------------------------------------------------------------*/ |
| 16 | + |
| 17 | +#include <cmath> |
| 18 | +#include <cstdio> |
| 19 | +#include <iomanip> |
| 20 | +#include <iostream> |
| 21 | +#include <limits> |
| 22 | + |
| 23 | +// Include desired integrators, vectors, linear solvers, and nonlinear solvers |
| 24 | +#include "cvode/cvode.h" |
| 25 | +#include "nvector/nvector_serial.h" |
| 26 | +#include "sundials/sundials_context.hpp" |
| 27 | +#include "sundials/sundials_logger.h" |
| 28 | +#include "sunlinsol/sunlinsol_dense.h" |
| 29 | +#include "sunmatrix/sunmatrix_dense.h" |
| 30 | + |
| 31 | +#include "problems/kpr.hpp" |
| 32 | +#include "utilities/check_return.hpp" |
| 33 | + |
| 34 | +using namespace std; |
| 35 | + |
| 36 | +int main(int argc, char* argv[]) |
| 37 | +{ |
| 38 | + // SUNDIALS context object for this simulation |
| 39 | + sundials::Context sunctx; |
| 40 | + |
| 41 | + // Ensure logging output goes to stdout |
| 42 | + SUNLogger logger; |
| 43 | + int flag = SUNContext_GetLogger(sunctx, &logger); |
| 44 | + if (check_flag(flag, "SUNContext_GetLogger")) { return 1; } |
| 45 | + |
| 46 | + SUNLogger_SetErrorFilename(logger, "stdout"); |
| 47 | + SUNLogger_SetWarningFilename(logger, "stdout"); |
| 48 | + SUNLogger_SetInfoFilename(logger, "stdout"); |
| 49 | + SUNLogger_SetDebugFilename(logger, "stdout"); |
| 50 | + |
| 51 | + // Create initial condition |
| 52 | + N_Vector y = N_VNew_Serial(2, sunctx); |
| 53 | + if (check_ptr(y, "N_VNew_Serial")) { return 1; } |
| 54 | + |
| 55 | + sunrealtype utrue, vtrue; |
| 56 | + flag = kpr_true_sol(ZERO, &utrue, &vtrue); |
| 57 | + if (check_flag(flag, "true_sol")) { return 1; } |
| 58 | + |
| 59 | + sunrealtype* ydata = N_VGetArrayPointer(y); |
| 60 | + ydata[0] = utrue; |
| 61 | + ydata[1] = vtrue; |
| 62 | + |
| 63 | + // Create CVODE memory structure |
| 64 | + void* cvode_mem = CVodeCreate(CV_BDF, sunctx); |
| 65 | + if (check_ptr(cvode_mem, "CVodeCreate")) { return 1; } |
| 66 | + |
| 67 | + flag = CVodeInit(cvode_mem, kpr_rhs, ZERO, y); |
| 68 | + if (check_flag(flag, "CVodeInit")) { return 1; } |
| 69 | + |
| 70 | + // Relative and absolute tolerances |
| 71 | + const sunrealtype rtol = SUN_RCONST(1.0e-6); |
| 72 | + const sunrealtype atol = SUN_RCONST(1.0e-10); |
| 73 | + |
| 74 | + flag = CVodeSStolerances(cvode_mem, rtol, atol); |
| 75 | + if (check_flag(flag, "CVodeSStolerances")) { return 1; } |
| 76 | + |
| 77 | + SUNMatrix A = SUNDenseMatrix(2, 2, sunctx); |
| 78 | + if (check_ptr(A, "SUNDenseMatrix")) { return 1; } |
| 79 | + |
| 80 | + SUNLinearSolver LS = SUNLinSol_Dense(y, A, sunctx); |
| 81 | + if (check_ptr(LS, "SUNLinSol_Dense")) { return 1; } |
| 82 | + |
| 83 | + flag = CVodeSetLinearSolver(cvode_mem, LS, A); |
| 84 | + if (check_flag(flag, "CVodeSetLinearSolver")) { return 1; } |
| 85 | + |
| 86 | + flag = CVodeSetJacFn(cvode_mem, kpr_rhs_jac); |
| 87 | + if (check_flag(flag, "CVodeSetJacFn")) { return 1; } |
| 88 | + |
| 89 | + sunrealtype udata[4] = {-TWO, HALF, HALF, -ONE}; |
| 90 | + flag = CVodeSetUserData(cvode_mem, udata); |
| 91 | + if (check_flag(flag, "CVodeSetUserData")) { return 1; } |
| 92 | + |
| 93 | + // Initial time and fist output time |
| 94 | + const sunrealtype dtout = ONE; // output interval |
| 95 | + const int nout = 3; // number of outputs |
| 96 | + sunrealtype tret = ZERO; |
| 97 | + sunrealtype tout = tret + dtout; |
| 98 | + |
| 99 | + // Output initial contion |
| 100 | + cout << scientific; |
| 101 | + cout << setprecision(numeric_limits<sunrealtype>::digits10); |
| 102 | + cout << " t "; |
| 103 | + cout << " u "; |
| 104 | + cout << " v "; |
| 105 | + cout << " u err "; |
| 106 | + cout << " v err " << endl; |
| 107 | + for (int i = 0; i < 9; i++) { cout << "--------------"; } |
| 108 | + cout << endl; |
| 109 | + |
| 110 | + cout << setw(22) << tret << setw(25) << ydata[0] << setw(25) << ydata[1] |
| 111 | + << setw(25) << abs(ydata[0] - utrue) << setw(25) << abs(ydata[1] - vtrue) |
| 112 | + << endl; |
| 113 | + |
| 114 | + // Advance in time |
| 115 | + for (int i = 0; i < nout; i++) |
| 116 | + { |
| 117 | + flag = CVode(cvode_mem, tout, y, &tret, CV_ONE_STEP); |
| 118 | + if (check_flag(flag, "CVode")) { return 1; } |
| 119 | + |
| 120 | + flag = kpr_true_sol(tret, &utrue, &vtrue); |
| 121 | + if (check_flag(flag, "true_sol")) { return 1; } |
| 122 | + |
| 123 | + cout << setw(22) << tret << setw(25) << ydata[0] << setw(25) << ydata[1] |
| 124 | + << setw(25) << abs(ydata[0] - utrue) << setw(25) |
| 125 | + << abs(ydata[1] - vtrue) << endl; |
| 126 | + |
| 127 | + // update output time |
| 128 | + tout += dtout; |
| 129 | + } |
| 130 | + for (int i = 0; i < 9; i++) { cout << "--------------"; } |
| 131 | + cout << endl; |
| 132 | + |
| 133 | + // Print some final statistics |
| 134 | + flag = CVodePrintAllStats(cvode_mem, stdout, SUN_OUTPUTFORMAT_TABLE); |
| 135 | + if (check_flag(flag, "CVodePrintAllStats")) { return 1; } |
| 136 | + |
| 137 | + // Clean up and return with successful completion |
| 138 | + N_VDestroy(y); |
| 139 | + SUNMatDestroy(A); |
| 140 | + SUNLinSolFree(LS); |
| 141 | + CVodeFree(&cvode_mem); |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +/*---- end of file ----*/ |
0 commit comments