Skip to content

Commit

Permalink
add logging test
Browse files Browse the repository at this point in the history
  • Loading branch information
gardner48 committed Aug 23, 2024
1 parent 49149fd commit eef87d7
Show file tree
Hide file tree
Showing 6 changed files with 594 additions and 0 deletions.
109 changes: 109 additions & 0 deletions test/unit_tests/logging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# ------------------------------------------------------------------------------
# Programmer(s): David J. Gardner @ LLNL
# ------------------------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2002-2024, Lawrence Livermore National Security
# and Southern Methodist University.
# All rights reserved.
#
# See the top-level LICENSE and NOTICE files for details.
#
# SPDX-License-Identifier: BSD-3-Clause
# SUNDIALS Copyright End
# ------------------------------------------------------------------------------

# List of test tuples of the form "name\;args"
set(unit_tests)

# if(BUILD_ARKODE)
# list(APPEND unit_tests "test_logging_arkode.cpp\;")
# endif()

if(BUILD_CVODE)
list(APPEND unit_tests "test_logging_cvode.cpp\;")
endif()

# if(BUILD_CVODES)
# list(APPEND unit_tests "test_logging_cvodes.cpp\;")
# endif()

# if(BUILD_IDA)
# list(APPEND unit_tests "test_logging_ida.cpp\;")
# endif()

# if(BUILD_IDAS)
# list(APPEND unit_tests "test_logging_idas.cpp\;")
# endif()

# if(BUILD_KINSOL)
# list(APPEND unit_tests "test_logging_kinsol.cpp\;")
# endif()

# Add the build and install targets for each test
foreach(test_tuple ${unit_tests})

# parse the test tuple
list(GET test_tuple 0 test_src)
list(GET test_tuple 1 test_args)

# extract the file name without extension
get_filename_component(test_target ${test_src} NAME_WE)

string(REGEX MATCH "arkode|cvode|cvodes|ida|idas|kinsol" _pkg ${test_target})

# check if this test has already been added, only need to add test source
# files once for testing with different inputs
if(NOT TARGET ${test_target})

# test source files
add_executable(${test_target} ${test_src})

set_target_properties(${test_target} PROPERTIES FOLDER "unit_tests")

# include location of public and private header files
target_include_directories(
${test_target} PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/test/unit_tests)

# libraries to link against
target_link_libraries(${test_target} sundials_${_pkg} sundials_nvecserial
${EXE_EXTRA_LINK_LIBS})

endif()

# check if test args are provided and set the test name
if("${test_args}" STREQUAL "")
set(test_name ${test_target})
else()
string(REPLACE " " "_" test_name "${test_target}_${test_args}")
string(REPLACE " " ";" test_args "${test_args}")
endif()

if(${SUNDIALS_LOGGING_LEVEL} GREATER 2)
set(_answer_file ${test_name}_lvl${SUNDIALS_LOGGING_LEVEL}.out)
else()
set(_answer_file ${test_name}.out)
endif()

# For now, only diff results with double precision
# TODO(DJG): Update Jenkins diff handling for testing single/extended
if(SUNDIALS_PRECISION MATCHES "DOUBLE")
set(diff_output "")
else()
set(diff_output "NODIFF")
endif()

# add test to regression tests
sundials_add_test(
${test_name} ${test_target}
TEST_ARGS ${test_args}
ANSWER_DIR ${CMAKE_CURRENT_SOURCE_DIR}
ANSWER_FILE ${_answer_file}
EXAMPLE_TYPE "develop"
LABELS "UnitTest" "Logging"
${diff_output})

endforeach()

message(STATUS "Added logging units tests")
146 changes: 146 additions & 0 deletions test/unit_tests/logging/test_logging_cvode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* -----------------------------------------------------------------------------
* Programmer(s): David J. Gardner @ LLNL
* -----------------------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2002-2024, Lawrence Livermore National Security
* and Southern Methodist University.
* All rights reserved.
*
* See the top-level LICENSE and NOTICE files for details.
*
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
* -----------------------------------------------------------------------------
* Test logging output
* ---------------------------------------------------------------------------*/

#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits>

// Include desired integrators, vectors, linear solvers, and nonlinear solvers
#include "cvode/cvode.h"
#include "nvector/nvector_serial.h"
#include "sundials/sundials_context.hpp"
#include "sundials/sundials_logger.h"
#include "sunlinsol/sunlinsol_dense.h"
#include "sunmatrix/sunmatrix_dense.h"

#include "problems/kpr.hpp"
#include "utilities/check_return.hpp"

using namespace std;

int main(int argc, char* argv[])
{
// SUNDIALS context object for this simulation
sundials::Context sunctx;

// Ensure logging output goes to stdout
SUNLogger logger;
int flag = SUNContext_GetLogger(sunctx, &logger);
if (check_flag(flag, "SUNContext_GetLogger")) { return 1; }

SUNLogger_SetErrorFilename(logger, "stdout");
SUNLogger_SetWarningFilename(logger, "stdout");
SUNLogger_SetInfoFilename(logger, "stdout");
SUNLogger_SetDebugFilename(logger, "stdout");

// Create initial condition
N_Vector y = N_VNew_Serial(2, sunctx);
if (check_ptr(y, "N_VNew_Serial")) { return 1; }

sunrealtype utrue, vtrue;
flag = kpr_true_sol(ZERO, &utrue, &vtrue);
if (check_flag(flag, "true_sol")) { return 1; }

sunrealtype* ydata = N_VGetArrayPointer(y);
ydata[0] = utrue;
ydata[1] = vtrue;

// Create CVODE memory structure
void* cvode_mem = CVodeCreate(CV_BDF, sunctx);
if (check_ptr(cvode_mem, "CVodeCreate")) { return 1; }

flag = CVodeInit(cvode_mem, kpr_rhs, ZERO, y);
if (check_flag(flag, "CVodeInit")) { return 1; }

// Relative and absolute tolerances
const sunrealtype rtol = SUN_RCONST(1.0e-6);
const sunrealtype atol = SUN_RCONST(1.0e-10);

flag = CVodeSStolerances(cvode_mem, rtol, atol);
if (check_flag(flag, "CVodeSStolerances")) { return 1; }

SUNMatrix A = SUNDenseMatrix(2, 2, sunctx);
if (check_ptr(A, "SUNDenseMatrix")) { return 1; }

SUNLinearSolver LS = SUNLinSol_Dense(y, A, sunctx);
if (check_ptr(LS, "SUNLinSol_Dense")) { return 1; }

flag = CVodeSetLinearSolver(cvode_mem, LS, A);
if (check_flag(flag, "CVodeSetLinearSolver")) { return 1; }

flag = CVodeSetJacFn(cvode_mem, kpr_rhs_jac);
if (check_flag(flag, "CVodeSetJacFn")) { return 1; }

sunrealtype udata[4] = {-TWO, HALF, HALF, -ONE};
flag = CVodeSetUserData(cvode_mem, udata);
if (check_flag(flag, "CVodeSetUserData")) { return 1; }

// Initial time and fist output time
const sunrealtype dtout = ONE; // output interval
const int nout = 3; // number of outputs
sunrealtype tret = ZERO;
sunrealtype tout = tret + dtout;

// Output initial contion
cout << scientific;
cout << setprecision(numeric_limits<sunrealtype>::digits10);
cout << " t ";
cout << " u ";
cout << " v ";
cout << " u err ";
cout << " v err " << endl;
for (int i = 0; i < 9; i++) { cout << "--------------"; }
cout << endl;

cout << setw(22) << tret << setw(25) << ydata[0] << setw(25) << ydata[1]
<< setw(25) << abs(ydata[0] - utrue) << setw(25) << abs(ydata[1] - vtrue)
<< endl;

// Advance in time
for (int i = 0; i < nout; i++)
{
flag = CVode(cvode_mem, tout, y, &tret, CV_ONE_STEP);
if (check_flag(flag, "CVode")) { return 1; }

flag = kpr_true_sol(tret, &utrue, &vtrue);
if (check_flag(flag, "true_sol")) { return 1; }

cout << setw(22) << tret << setw(25) << ydata[0] << setw(25) << ydata[1]
<< setw(25) << abs(ydata[0] - utrue) << setw(25)
<< abs(ydata[1] - vtrue) << endl;

// update output time
tout += dtout;
}
for (int i = 0; i < 9; i++) { cout << "--------------"; }
cout << endl;

// Print some final statistics
flag = CVodePrintAllStats(cvode_mem, stdout, SUN_OUTPUTFORMAT_TABLE);
if (check_flag(flag, "CVodePrintAllStats")) { return 1; }

// Clean up and return with successful completion
N_VDestroy(y);
SUNMatDestroy(A);
SUNLinSolFree(LS);
CVodeFree(&cvode_mem);

return 0;
}

/*---- end of file ----*/
34 changes: 34 additions & 0 deletions test/unit_tests/logging/test_logging_cvode.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
t u v u err v err
------------------------------------------------------------------------------------------------------------------------------
0.000000000000000e+00 1.224744871391589e+00 1.732050807568877e+00 0.000000000000000e+00 0.000000000000000e+00
1.029860256095084e-04 1.224744869195325e+00 1.732049582942459e+00 1.113781733508290e-09 6.122818176912403e-07
2.059720512190168e-04 1.224744864802806e+00 1.732047133692213e+00 2.258852216385776e-09 1.224499561969239e-06
4.870434726350260e-04 1.224744841922706e+00 1.732034367987314e+00 5.258602220337139e-09 2.744232363793842e-06
------------------------------------------------------------------------------------------------------------------------------
Current time = 0.000487043472635026
Steps = 3
Error test fails = 1
NLS step fails = 0
Initial step size = 0.0001029860256095084
Last step size = 0.0002810714214160092
Current step size = 0.0002810714214160092
Last method order = 2
Current method order = 2
Stab. lim. order reductions = 0
RHS fn evals = 10
NLS iters = 7
NLS fails = 0
NLS iters per step = 2.333333333333333
LS setups = 3
Jac fn evals = 1
LS RHS fn evals = 0
Prec setup evals = 0
Prec solves = 0
LS iters = 0
LS fails = 0
Jac-times setups = 0
Jac-times evals = 0
LS iters per NLS iter = 0
Jac evals per NLS iter = 0.1428571428571428
Prec evals per NLS iter = 0
Root fn evals = 0
89 changes: 89 additions & 0 deletions test/unit_tests/logging/test_logging_cvode_lvl3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
t u v u err v err
------------------------------------------------------------------------------------------------------------------------------
0.000000000000000e+00 1.224744871391589e+00 1.732050807568877e+00 0.000000000000000e+00 0.000000000000000e+00
[INFO][rank 0][cvStep][begin-step-attempt] step = 1, t_n = 0, h = 0.0001029860256095084, q = 1
[INFO][rank 0][cvNls][begin-nonlinear-solve] tol = 0.2
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-solver] solver = Newton
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 1, total-iters = 1, update-norm = 0.4999243864411202
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = continue
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 2, total-iters = 2, update-norm = 2.207987840210415e-11
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = success
[INFO][rank 0][cvNls][end-nonlinear-solve] status = success, iters = 2
[INFO][rank 0][cvStep][end-step-attempt] status = success, dsm = 0.2499621932116032
1.029860256095084e-04 1.224744869195325e+00 1.732049582942459e+00 1.113781733508290e-09 6.122818176912403e-07
[INFO][rank 0][cvStep][begin-step-attempt] step = 2, t_n = 0.0001029860256095084, h = 0.0001029860256095084, q = 1
[INFO][rank 0][cvNls][begin-nonlinear-solve] tol = 0.2
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-solver] solver = Newton
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 1, total-iters = 1, update-norm = 0.499923682675401
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = success
[INFO][rank 0][cvNls][end-nonlinear-solve] status = success, iters = 1
[INFO][rank 0][cvStep][end-step-attempt] status = success, dsm = 0.2499618413377005
2.059720512190168e-04 1.224744864802806e+00 1.732047133692213e+00 2.258852216385776e-09 1.224499561969239e-06
[INFO][rank 0][cvStep][begin-step-attempt] step = 3, t_n = 0.0002059720512190168, h = 0.001029860256095084, q = 2
[INFO][rank 0][cvNls][begin-nonlinear-solve] tol = 0.4064516129032258
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-solver] solver = Newton
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 1, total-iters = 1, update-norm = 33.32111802218621
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = continue
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 2, total-iters = 2, update-norm = 0.001726792321093216
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = success
[INFO][rank 0][cvNls][end-nonlinear-solve] status = success, iters = 2
[INFO][rank 0][cvStep][end-step-attempt] status = failed error test, dsm = 8.198477691401965, eflag = 5
[INFO][rank 0][cvStep][begin-step-attempt] step = 3, t_n = 0.0002059720512190168, h = 0.0002810714214160092, q = 2
[INFO][rank 0][cvNls][begin-nonlinear-solve] tol = 0.4217683352485581
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-solver] solver = Newton
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 1, total-iters = 1, update-norm = 2.482483008202045
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = continue
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 2, total-iters = 2, update-norm = 1.190779226370788e-05
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = success
[INFO][rank 0][cvNls][end-nonlinear-solve] status = success, iters = 2
[INFO][rank 0][cvStep][end-step-attempt] status = success, dsm = 0.5885920559914323
4.870434726350260e-04 1.224744841922706e+00 1.732034367987314e+00 5.258602220337139e-09 2.744232363793842e-06
------------------------------------------------------------------------------------------------------------------------------
Current time = 0.000487043472635026
Steps = 3
Error test fails = 1
NLS step fails = 0
Initial step size = 0.0001029860256095084
Last step size = 0.0002810714214160092
Current step size = 0.0002810714214160092
Last method order = 2
Current method order = 2
Stab. lim. order reductions = 0
RHS fn evals = 10
NLS iters = 7
NLS fails = 0
NLS iters per step = 2.333333333333333
LS setups = 3
Jac fn evals = 1
LS RHS fn evals = 0
Prec setup evals = 0
Prec solves = 0
LS iters = 0
LS fails = 0
Jac-times setups = 0
Jac-times evals = 0
LS iters per NLS iter = 0
Jac evals per NLS iter = 0.1428571428571428
Prec evals per NLS iter = 0
Root fn evals = 0
Loading

0 comments on commit eef87d7

Please sign in to comment.