Skip to content

Commit eef87d7

Browse files
committed
add logging test
1 parent 49149fd commit eef87d7

File tree

6 files changed

+594
-0
lines changed

6 files changed

+594
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
15+
# List of test tuples of the form "name\;args"
16+
set(unit_tests)
17+
18+
# if(BUILD_ARKODE)
19+
# list(APPEND unit_tests "test_logging_arkode.cpp\;")
20+
# endif()
21+
22+
if(BUILD_CVODE)
23+
list(APPEND unit_tests "test_logging_cvode.cpp\;")
24+
endif()
25+
26+
# if(BUILD_CVODES)
27+
# list(APPEND unit_tests "test_logging_cvodes.cpp\;")
28+
# endif()
29+
30+
# if(BUILD_IDA)
31+
# list(APPEND unit_tests "test_logging_ida.cpp\;")
32+
# endif()
33+
34+
# if(BUILD_IDAS)
35+
# list(APPEND unit_tests "test_logging_idas.cpp\;")
36+
# endif()
37+
38+
# if(BUILD_KINSOL)
39+
# list(APPEND unit_tests "test_logging_kinsol.cpp\;")
40+
# endif()
41+
42+
# Add the build and install targets for each test
43+
foreach(test_tuple ${unit_tests})
44+
45+
# parse the test tuple
46+
list(GET test_tuple 0 test_src)
47+
list(GET test_tuple 1 test_args)
48+
49+
# extract the file name without extension
50+
get_filename_component(test_target ${test_src} NAME_WE)
51+
52+
string(REGEX MATCH "arkode|cvode|cvodes|ida|idas|kinsol" _pkg ${test_target})
53+
54+
# check if this test has already been added, only need to add test source
55+
# files once for testing with different inputs
56+
if(NOT TARGET ${test_target})
57+
58+
# test source files
59+
add_executable(${test_target} ${test_src})
60+
61+
set_target_properties(${test_target} PROPERTIES FOLDER "unit_tests")
62+
63+
# include location of public and private header files
64+
target_include_directories(
65+
${test_target} PRIVATE
66+
${CMAKE_SOURCE_DIR}/include
67+
${CMAKE_SOURCE_DIR}/test/unit_tests)
68+
69+
# libraries to link against
70+
target_link_libraries(${test_target} sundials_${_pkg} sundials_nvecserial
71+
${EXE_EXTRA_LINK_LIBS})
72+
73+
endif()
74+
75+
# check if test args are provided and set the test name
76+
if("${test_args}" STREQUAL "")
77+
set(test_name ${test_target})
78+
else()
79+
string(REPLACE " " "_" test_name "${test_target}_${test_args}")
80+
string(REPLACE " " ";" test_args "${test_args}")
81+
endif()
82+
83+
if(${SUNDIALS_LOGGING_LEVEL} GREATER 2)
84+
set(_answer_file ${test_name}_lvl${SUNDIALS_LOGGING_LEVEL}.out)
85+
else()
86+
set(_answer_file ${test_name}.out)
87+
endif()
88+
89+
# For now, only diff results with double precision
90+
# TODO(DJG): Update Jenkins diff handling for testing single/extended
91+
if(SUNDIALS_PRECISION MATCHES "DOUBLE")
92+
set(diff_output "")
93+
else()
94+
set(diff_output "NODIFF")
95+
endif()
96+
97+
# add test to regression tests
98+
sundials_add_test(
99+
${test_name} ${test_target}
100+
TEST_ARGS ${test_args}
101+
ANSWER_DIR ${CMAKE_CURRENT_SOURCE_DIR}
102+
ANSWER_FILE ${_answer_file}
103+
EXAMPLE_TYPE "develop"
104+
LABELS "UnitTest" "Logging"
105+
${diff_output})
106+
107+
endforeach()
108+
109+
message(STATUS "Added logging units tests")
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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 ----*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
t u v u err v err
2+
------------------------------------------------------------------------------------------------------------------------------
3+
0.000000000000000e+00 1.224744871391589e+00 1.732050807568877e+00 0.000000000000000e+00 0.000000000000000e+00
4+
1.029860256095084e-04 1.224744869195325e+00 1.732049582942459e+00 1.113781733508290e-09 6.122818176912403e-07
5+
2.059720512190168e-04 1.224744864802806e+00 1.732047133692213e+00 2.258852216385776e-09 1.224499561969239e-06
6+
4.870434726350260e-04 1.224744841922706e+00 1.732034367987314e+00 5.258602220337139e-09 2.744232363793842e-06
7+
------------------------------------------------------------------------------------------------------------------------------
8+
Current time = 0.000487043472635026
9+
Steps = 3
10+
Error test fails = 1
11+
NLS step fails = 0
12+
Initial step size = 0.0001029860256095084
13+
Last step size = 0.0002810714214160092
14+
Current step size = 0.0002810714214160092
15+
Last method order = 2
16+
Current method order = 2
17+
Stab. lim. order reductions = 0
18+
RHS fn evals = 10
19+
NLS iters = 7
20+
NLS fails = 0
21+
NLS iters per step = 2.333333333333333
22+
LS setups = 3
23+
Jac fn evals = 1
24+
LS RHS fn evals = 0
25+
Prec setup evals = 0
26+
Prec solves = 0
27+
LS iters = 0
28+
LS fails = 0
29+
Jac-times setups = 0
30+
Jac-times evals = 0
31+
LS iters per NLS iter = 0
32+
Jac evals per NLS iter = 0.1428571428571428
33+
Prec evals per NLS iter = 0
34+
Root fn evals = 0
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
t u v u err v err
2+
------------------------------------------------------------------------------------------------------------------------------
3+
0.000000000000000e+00 1.224744871391589e+00 1.732050807568877e+00 0.000000000000000e+00 0.000000000000000e+00
4+
[INFO][rank 0][cvStep][begin-step-attempt] step = 1, t_n = 0, h = 0.0001029860256095084, q = 1
5+
[INFO][rank 0][cvNls][begin-nonlinear-solve] tol = 0.2
6+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-solver] solver = Newton
7+
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
8+
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
9+
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
10+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 1, total-iters = 1, update-norm = 0.4999243864411202
11+
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = continue
12+
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
13+
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
14+
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
15+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 2, total-iters = 2, update-norm = 2.207987840210415e-11
16+
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = success
17+
[INFO][rank 0][cvNls][end-nonlinear-solve] status = success, iters = 2
18+
[INFO][rank 0][cvStep][end-step-attempt] status = success, dsm = 0.2499621932116032
19+
1.029860256095084e-04 1.224744869195325e+00 1.732049582942459e+00 1.113781733508290e-09 6.122818176912403e-07
20+
[INFO][rank 0][cvStep][begin-step-attempt] step = 2, t_n = 0.0001029860256095084, h = 0.0001029860256095084, q = 1
21+
[INFO][rank 0][cvNls][begin-nonlinear-solve] tol = 0.2
22+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-solver] solver = Newton
23+
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
24+
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
25+
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
26+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 1, total-iters = 1, update-norm = 0.499923682675401
27+
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = success
28+
[INFO][rank 0][cvNls][end-nonlinear-solve] status = success, iters = 1
29+
[INFO][rank 0][cvStep][end-step-attempt] status = success, dsm = 0.2499618413377005
30+
2.059720512190168e-04 1.224744864802806e+00 1.732047133692213e+00 2.258852216385776e-09 1.224499561969239e-06
31+
[INFO][rank 0][cvStep][begin-step-attempt] step = 3, t_n = 0.0002059720512190168, h = 0.001029860256095084, q = 2
32+
[INFO][rank 0][cvNls][begin-nonlinear-solve] tol = 0.4064516129032258
33+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-solver] solver = Newton
34+
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
35+
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
36+
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
37+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 1, total-iters = 1, update-norm = 33.32111802218621
38+
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = continue
39+
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
40+
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
41+
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
42+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 2, total-iters = 2, update-norm = 0.001726792321093216
43+
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = success
44+
[INFO][rank 0][cvNls][end-nonlinear-solve] status = success, iters = 2
45+
[INFO][rank 0][cvStep][end-step-attempt] status = failed error test, dsm = 8.198477691401965, eflag = 5
46+
[INFO][rank 0][cvStep][begin-step-attempt] step = 3, t_n = 0.0002059720512190168, h = 0.0002810714214160092, q = 2
47+
[INFO][rank 0][cvNls][begin-nonlinear-solve] tol = 0.4217683352485581
48+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-solver] solver = Newton
49+
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
50+
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
51+
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
52+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 1, total-iters = 1, update-norm = 2.482483008202045
53+
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = continue
54+
[INFO][rank 0][SUNNonlinSolSolve_Newton][begin-nonlinear-iterate]
55+
[INFO][rank 0][cvLsSolve][begin-linear-solve] iterative = 0
56+
[INFO][rank 0][cvLsSolve][end-linear-solve] status = success, iters = 0, p-solves = 0
57+
[INFO][rank 0][SUNNonlinSolSolve_Newton][nonlinear-iterate] cur-iter = 2, total-iters = 2, update-norm = 1.190779226370788e-05
58+
[INFO][rank 0][SUNNonlinSolSolve_Newton][end-nonlinear-iterate] status = success
59+
[INFO][rank 0][cvNls][end-nonlinear-solve] status = success, iters = 2
60+
[INFO][rank 0][cvStep][end-step-attempt] status = success, dsm = 0.5885920559914323
61+
4.870434726350260e-04 1.224744841922706e+00 1.732034367987314e+00 5.258602220337139e-09 2.744232363793842e-06
62+
------------------------------------------------------------------------------------------------------------------------------
63+
Current time = 0.000487043472635026
64+
Steps = 3
65+
Error test fails = 1
66+
NLS step fails = 0
67+
Initial step size = 0.0001029860256095084
68+
Last step size = 0.0002810714214160092
69+
Current step size = 0.0002810714214160092
70+
Last method order = 2
71+
Current method order = 2
72+
Stab. lim. order reductions = 0
73+
RHS fn evals = 10
74+
NLS iters = 7
75+
NLS fails = 0
76+
NLS iters per step = 2.333333333333333
77+
LS setups = 3
78+
Jac fn evals = 1
79+
LS RHS fn evals = 0
80+
Prec setup evals = 0
81+
Prec solves = 0
82+
LS iters = 0
83+
LS fails = 0
84+
Jac-times setups = 0
85+
Jac-times evals = 0
86+
LS iters per NLS iter = 0
87+
Jac evals per NLS iter = 0.1428571428571428
88+
Prec evals per NLS iter = 0
89+
Root fn evals = 0

0 commit comments

Comments
 (0)