Skip to content

Commit

Permalink
WIP on expanding unit tests #162, #150; other minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cbuahin committed Feb 20, 2024
1 parent 58e330b commit 66e9f61
Show file tree
Hide file tree
Showing 22 changed files with 3,238 additions and 220 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
cmake_minimum_required (VERSION 3.13)

if("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
message(FATAL_ERROR "In-source builds are disabled.")
message(FATAL_ERROR "In-source builds are disabled.")
endif()


Expand All @@ -34,7 +34,7 @@ endif()


# Define install locations (will be prepended by install prefix)
set(TOOL_DIST "bin")
set(TOOL_DIST "bin")
set(INCLUDE_DIST "include")
set(LIBRARY_DIST "lib")
set(CONFIG_DIST "cmake")
Expand Down
6 changes: 3 additions & 3 deletions extern/boost.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@


if(WIN32)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_LIBS ON)
else()
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_STATIC_LIBS OFF)
add_definitions(-DBOOST_ALL_DYN_LINK)
endif()

Expand All @@ -25,4 +25,4 @@ find_package(

include_directories(
${Boost_INCLUDE_DIRS}
)
)
2 changes: 1 addition & 1 deletion src/outfile/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ if(BUILD_TESTS)
add_custom_command(TARGET swmm-output POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:swmm-output>
${CMAKE_BINARY_DIR}/bin/$<CONFIGURATION>/$<TARGET_FILE_NAME:swmm-output>
${CMAKE_BINARY_DIR}/bin/$<CONFIG>/$<TARGET_FILE_NAME:swmm-output>
)
endif()
2 changes: 1 addition & 1 deletion src/run/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ install(TARGETS runswmm
add_custom_command(TARGET runswmm POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:runswmm>
${CMAKE_BINARY_DIR}/bin/$<CONFIGURATION>/$<TARGET_FILE_NAME:runswmm>
${CMAKE_BINARY_DIR}/bin/$<CONFIG>/$<TARGET_FILE_NAME:runswmm>
)
2 changes: 1 addition & 1 deletion src/solver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@ install(
add_custom_command(TARGET swmm5 POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:swmm5>
${CMAKE_BINARY_DIR}/bin/$<CONFIGURATION>/$<TARGET_FILE_NAME:swmm5>
${CMAKE_BINARY_DIR}/bin/$<CONFIG>/$<TARGET_FILE_NAME:swmm5>
)
1 change: 1 addition & 0 deletions src/solver/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ void iface_saveOutletResults(DateTime reportDate, FILE* file);
int hotstart_open(void);
void hotstart_save(void);
int hotstart_save_to_file(const char* hotstartFile);
int hotstart_is_valid(const char* hotstartFile, int *inputFileVersion);
void hotstart_close(void);

//-----------------------------------------------------------------------------
Expand Down
112 changes: 80 additions & 32 deletions src/solver/hotstart.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,25 @@ static int initializeSaveHotstartFile(TFile *hotstartFile);
static void readRunoff(void);
static void saveRunoff(TFile *hotstartFile);
static void readRouting(void);
static void saveRouting(TFile* hotstartFile);
static int readFloat(float *x, FILE* f);
static int readDouble(double* x, FILE* f);
static void saveRouting(TFile *hotstartFile);
static int readFloat(float *x, FILE *f);
static int readDouble(double *x, FILE *f);

//=============================================================================

int hotstart_open()
{
int i;
int errorCode = 0;

// --- open hot start files
if ( !initializeFromHotstartFile() ) return FALSE; //input hot start file

for (i = 0; i < MAXHOTSTARTFILES; i++)
{
if (initializeSaveHotstartFile(&FhotstartOutputs[i]))
if ((errorCode = initializeSaveHotstartFile(&FhotstartOutputs[i])))
{
report_writeErrorMsg(ERR_HOTSTART_FILE_OPEN, FhotstartOutputs[i].name);
report_writeErrorMsg(errorCode, FhotstartOutputs[i].name);
return FALSE;
}
}
Expand Down Expand Up @@ -119,7 +120,7 @@ void hotstart_save()

//=============================================================================

int hotstart_save_to_file(const char* hotstartFile)
int hotstart_save_to_file(const char *hotstartFile)
//
// Input: hotStartFile = filepath to hotstart file to use
// Output: returns error code
Expand Down Expand Up @@ -154,6 +155,49 @@ int hotstart_save_to_file(const char* hotstartFile)

//=============================================================================

int hotstart_is_valid(const char *hotstartFile, int *inputFileVersion)
//
// Input: hotStartFile = filepath to hotstart file to check
// Output: inputFileVersion = version of hotstart file
// returns error code
{
char fStamp[] = "SWMM5-HOTSTART";
char fileStamp[] = "SWMM5-HOTSTART";
char fStampx[] = "SWMM5-HOTSTARTx";
char fileStamp2[] = "SWMM5-HOTSTART2";
char fileStamp3[] = "SWMM5-HOTSTART3";
char fileStamp4[] = "SWMM5-HOTSTART4";
FILE* f;

// --- try to open the file
if ( (f = fopen(hotstartFile, "r+b")) == NULL)
{
return ERR_HOTSTART_FILE_OPEN;
}

// --- check that file contains proper header records
fread(fStampx, sizeof(char), strlen(fileStamp2), f);
if ( strcmp(fStampx, fileStamp4) == 0 ) *inputFileVersion = 4;
else if ( strcmp(fStampx, fileStamp3) == 0 ) *inputFileVersion = 3;
else if ( strcmp(fStampx, fileStamp2) == 0 ) *inputFileVersion = 2;
else
{
rewind(f);
fread(fStamp, sizeof(char), strlen(fileStamp), f);
if ( strcmp(fStamp, fileStamp) != 0 )
{
fclose(f);
return ERR_HOTSTART_FILE_FORMAT;
}
*inputFileVersion = 1;
}

fclose(f);
return 0;
}

//=============================================================================

void hotstart_close()
//
// Input: none
Expand All @@ -179,7 +223,7 @@ void hotstart_close()
int initializeFromHotstartFile()
//
// Input: none
// Output: none
// Output: True for successful initialization of hotstart file or False otherwise
// Purpose: initializes model state a previously saved routing hotstart file.
//
{
Expand All @@ -189,38 +233,40 @@ int initializeFromHotstartFile()
int nPollut;
int nLandUses;
int flowUnits;
char fStamp[] = "SWMM5-HOTSTART";
char fileStamp[] = "SWMM5-HOTSTART";
char fStampx[] = "SWMM5-HOTSTARTx";
char fileStamp2[] = "SWMM5-HOTSTART2";
char fileStamp3[] = "SWMM5-HOTSTART3";
char fileStamp4[] = "SWMM5-HOTSTART4";
int errorCode;

// --- try to open the file
if ( FhotstartInput.mode != USE_FILE ) return TRUE;
if ( (FhotstartInput.file = fopen(FhotstartInput.name, "r+b")) == NULL)
{
report_writeErrorMsg(
ERR_HOTSTART_FILE_OPEN,
FhotstartInput.name);
errorCode = hotstart_is_valid(FhotstartInput.name, &fileVersion);

if (errorCode)
{
if (errorCode == ERR_HOTSTART_FILE_OPEN)
{
report_writeErrorMsg(
ERR_HOTSTART_FILE_OPEN,
FhotstartInput.name);
}
else
{
report_writeErrorMsg(errorCode, "");
}
return FALSE;
}

// --- check that file contains proper header records
fread(fStampx, sizeof(char), strlen(fileStamp2), FhotstartInput.file);
if ( strcmp(fStampx, fileStamp4) == 0 ) fileVersion = 4;
else if ( strcmp(fStampx, fileStamp3) == 0 ) fileVersion = 3;
else if ( strcmp(fStampx, fileStamp2) == 0 ) fileVersion = 2;
FhotstartInput.file = fopen(FhotstartInput.name, "r+b");
// --- read header records from the file
if (fileVersion > 1)
{
char fStampx[] = "SWMM5-HOTSTARTx";
char fileStamp2[] = "SWMM5-HOTSTART2";
fread(fStampx, sizeof(char), strlen(fileStamp2), FhotstartInput.file);
}
else
{
rewind(FhotstartInput.file);
fread(fStamp, sizeof(char), strlen(fileStamp), FhotstartInput.file);
if ( strcmp(fStamp, fileStamp) != 0 )
{
report_writeErrorMsg(ERR_HOTSTART_FILE_FORMAT, "");
return FALSE;
}
fileVersion = 1;
char fStamp[] = "SWMM5-HOTSTART";
char fileStamp[] = "SWMM5-HOTSTART";
fread(fStamp, sizeof(char), strlen(fileStamp), FhotstartInput.file);
}

nSubcatch = -1;
Expand Down Expand Up @@ -281,7 +327,9 @@ int initializeSaveHotstartFile(TFile *hotstartFile)
char fileStamp[] = "SWMM5-HOTSTART4";

// --- try to open file
if (hotstartFile->mode != SAVE_FILE ) return TRUE;
if (hotstartFile->mode != SAVE_FILE )
return 0;

if ( (hotstartFile->file = fopen(hotstartFile->name, "w+b")) == NULL)
{
error_code = ERR_HOTSTART_FILE_OPEN;
Expand Down
5 changes: 3 additions & 2 deletions src/solver/include/swmm5.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ typedef enum {
ERR_API_PROPERTY_VALUE = -999908,
ERR_API_TIME_PERIOD = -999909,
ERR_API_HOTSTART_FILE_OPEN = -999910,
ERR_API_HOTSTART_FILE_FORMAT= -999911,
} swmm_API_Errors;


int DLLEXPORT swmm_run(const char *f1, const char *f2, const char *f3);
int DLLEXPORT swmm_open(const char *f1, const char *f2, const char *f3);
int DLLEXPORT swmm_run(const char *inpuFile, const char *reportFile, const char *outputFile);
int DLLEXPORT swmm_open(const char *inputFile, const char *reportFile, const char *outputFile);
int DLLEXPORT swmm_start(int saveFlag);
int DLLEXPORT swmm_step(double *elapsedTime);
int DLLEXPORT swmm_stride(int strideStep, double *elapsedTime);
Expand Down
55 changes: 27 additions & 28 deletions src/solver/swmm5.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ static int xfilter(int xc, char* module, double elapsedTime, long step);

//=============================================================================

int DLLEXPORT swmm_run(const char *f1, const char *f2, const char *f3)
int DLLEXPORT swmm_run(const char* inpuFile, const char* reportFile, const char* outputFile)
//
// Input: f1 = name of input file
// f2 = name of report file
// f3 = name of binary output file
// Input: inputFile = name of input file
// reportFile = name of report file
// outputFile = name of binary output file
// Output: returns error code
// Purpose: runs a SWMM simulation.
//
Expand All @@ -209,7 +209,7 @@ int DLLEXPORT swmm_run(const char *f1, const char *f2, const char *f3)
// --- open the files & read input data
ErrorCode = 0;
writecon("\n o Retrieving project data");
swmm_open(f1, f2, f3);
swmm_open(inpuFile, reportFile, outputFile);

// --- run the simulation if input data OK
if ( !ErrorCode )
Expand Down Expand Up @@ -258,11 +258,11 @@ int DLLEXPORT swmm_run(const char *f1, const char *f2, const char *f3)

//=============================================================================

int DLLEXPORT swmm_open(const char *f1, const char *f2, const char *f3)
int DLLEXPORT swmm_open(const char* inpuFile, const char* reportFile, const char* outputFile)
//
// Input: f1 = name of input file
// f2 = name of report file
// f3 = name of binary output file
// Input: inputFile = name of input file
// reportFile = name of report file
// outputFile = name of binary output file
// Output: returns error code
// Purpose: opens a SWMM project.
//
Expand All @@ -289,8 +289,8 @@ int DLLEXPORT swmm_open(const char *f1, const char *f2, const char *f3)

// --- open a SWMM project
strcpy(InpDir, "");
project_open(f1, f2, f3);
getAbsolutePath(f1, InpDir, sizeof(InpDir));
project_open(inpuFile, reportFile, outputFile);
getAbsolutePath(inpuFile, InpDir, sizeof(InpDir));
if ( ErrorCode ) return ErrorCode;
IsOpenFlag = TRUE;
report_writeLogo();
Expand Down Expand Up @@ -527,8 +527,8 @@ int DLLEXPORT swmm_useHotStart(const char* hotStartFile)
// Purpose: Sets the hotstart file to use for simulation. Errors does not terminate simulation unless
// there is a prior terminating error.
{
int error_code = 0;
FILE *f;
int errorCode = 0;
int fileVersion = 0;
char fname[MAXFNAME + 1];

if (ErrorCode)
Expand All @@ -540,20 +540,19 @@ int DLLEXPORT swmm_useHotStart(const char* hotStartFile)

sstrncpy(fname, hotStartFile, MAXFNAME);

// Try to open the hotstart file first to see if it exists
if ((f = fopen(addAbsolutePath(fname), "r")) == NULL)
// Try to open the hotstart file first to see if it is valid
errorCode = hotstart_is_valid(addAbsolutePath(fname), &fileVersion);
if (errorCode)
{
return (error_code = ERR_API_HOTSTART_FILE_OPEN);
return errorCode;
}
else
{
fclose(f);
}

FhotstartInput.mode = USE_FILE;
sstrncpy(FhotstartInput.name, addAbsolutePath(fname), MAXFNAME);
FhotstartInput.mode = USE_FILE;
sstrncpy(FhotstartInput.name, addAbsolutePath(fname), MAXFNAME);
}

return error_code;
return errorCode;
}

//=============================================================================
Expand All @@ -564,7 +563,7 @@ int DLLEXPORT swmm_saveHotStart(const char* hotStartFile)
// Output: returns error code
// Purpose: Saves hotstart file at the current simulation time
{
int error_code = 0;
int errorCode = 0;

if (ErrorCode)
return ErrorCode;
Expand All @@ -573,9 +572,9 @@ int DLLEXPORT swmm_saveHotStart(const char* hotStartFile)
else if (!IsStartedFlag)
return (ErrorCode = ERR_API_NOT_STARTED);

error_code = hotstart_save_to_file(hotStartFile);
errorCode = hotstart_save_to_file(hotStartFile);

return error_code;
return errorCode;

}

Expand Down Expand Up @@ -847,15 +846,15 @@ int DLLEXPORT swmm_getError(char *errMsg, int msgLen)

//=============================================================================

int DLLEXPORT swmm_getErrorFromCode(int error_code, char **outErrMsg)
int DLLEXPORT swmm_getErrorFromCode(int errorCode, char **outErrMsg)
//
// Input: error_code = error code number
// Input: errorCode = error code number
// Output: outErrMsg = error message text
// Purpose: retrieves the text of the error message that corresponds to the
// error code number.
{
char err_msg[MAXMSG];
error_getMsg(error_code, err_msg);
error_getMsg(errorCode, err_msg);
sstrncpy(*outErrMsg, err_msg, MAXMSG);

return 0;
Expand Down
Loading

0 comments on commit 66e9f61

Please sign in to comment.