Skip to content

Commit

Permalink
use python script to test CVODE, for approximate solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jcschaff committed Jul 28, 2024
1 parent 5af4288 commit 57a7007
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 50 deletions.
6 changes: 4 additions & 2 deletions IDAWin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ enable_testing()

if (MINGW)
set(test_sundials_exe ${CMAKE_BINARY_DIR}/bin/${EXE_FILE}.exe)
set(python_cmd py)
else (MINGW)
set(test_sundials_exe ${CMAKE_BINARY_DIR}/bin/${EXE_FILE})
set(python_cmd python3)
endif (MINGW)
set(test_dir ${CMAKE_CURRENT_SOURCE_DIR}/tests/smoke)

# smoke test as a bash script, for python test example, see NFsim/tests/smoke
add_test(NAME ${EXE_FILE}_smoke COMMAND bash -c "${test_dir}/smoke.sh ${test_sundials_exe}" WORKING_DIRECTORY ${test_dir})
# smoke test as a python script, for bash test example, see NFsim/tests/smoke
add_test(NAME ${EXE_FILE}_smoke COMMAND ${python_cmd} ${test_dir}/smoke.py ${test_sundials_exe} WORKING_DIRECTORY ${test_dir})


add_executable(
Expand Down
70 changes: 70 additions & 0 deletions IDAWin/tests/smoke/smoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3

import os
import posixpath
import subprocess
import sys
from pathlib import Path


# function to numerically compare the contents of two text files to determine approximate equality
# each line is a space delimited list of numbers, compare them to within 8 significant figures
def compare_files(file1: Path, file2: Path, tolerance: float):
with open(file1, 'r') as f1, open(file2, 'r') as f2:
for line1, line2 in zip(f1, f2):
line1 = [float(x) for x in line1.split()]
line2 = [float(x) for x in line2.split()]
for x, y in zip(line1, line2):
if x != y and abs(x - y) > tolerance * max(abs(x), abs(y)):
return False
return True


# get the directory of this script
test_dir = os.path.dirname(os.path.realpath(__file__))
# in the path replace \ with /, D:\ with /d/
test_dir = test_dir.replace("\\", "/")
# tell os.path.join to use / as the path separator
os.path.sep = "/"
exe = sys.argv[1]

print(f"test_dir: {test_dir}")
print(f"exe: {exe}")

input_file = posixpath.join(test_dir, "SimID_1489333437_0_.cvodeInput")
output_file = posixpath.join(test_dir, "SimID_1489333437_0_.ida")
expected_output_file = posixpath.join(test_dir, "SimID_1489333437_0_.ida.expected")

if not posixpath.exists(exe):
print(f"SundialsSolverStandalone_x64 executable {exe} not found. Exiting...")
sys.exit(1)

if not posixpath.exists(input_file):
print(f"Input file {input_file} not found. Exiting...")
sys.exit(1)

if not posixpath.exists(expected_output_file):
print(f"Expected output file {expected_output_file} not found. Exiting...")
sys.exit(1)

command = [exe, input_file, output_file]
print(" ".join(command))

try:
subprocess.check_call(command)
except subprocess.CalledProcessError:
print("SundialsSolverStandalone_x64 failed to run. Exiting...")
sys.exit(1)

# verify that the output files exist
if not os.path.isfile(output_file):
print(f"Output file {output_file} not found. Exiting...")
sys.exit(1)

# verify that the output files match the expected output files
if not compare_files(file1=Path(output_file), file2=Path(expected_output_file), tolerance=1e-8):
print(f"Output file {output_file} does not match expected output {expected_output_file}. Exiting...")
sys.exit(1)

print("SundialsSolverStandalone_x64 solver completed and solution matched expected output. Exiting...")
sys.exit(0)
48 changes: 0 additions & 48 deletions IDAWin/tests/smoke/smoke.sh

This file was deleted.

0 comments on commit 57a7007

Please sign in to comment.