Skip to content

Commit

Permalink
WIP addressing #162, #150
Browse files Browse the repository at this point in the history
  • Loading branch information
cbuahin committed Feb 15, 2024
1 parent e3309b6 commit 9cad38b
Show file tree
Hide file tree
Showing 13 changed files with 453 additions and 156 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Build and Test

on:
push:
branches: [ master, develop, release, lew-develop ]
branches: [ master, develop, release, bug_fixes ]
pull_request:
branches: [ master, develop, release, lew-develop ]
branches: [ master, develop, release ]

env:
OMP_NUM_THREADS: 1
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ add_subdirectory(src/solver)
add_subdirectory(src/run)

if(BUILD_TESTS)
enable_testing()
enable_testing()
add_subdirectory(tests)
endif()

Expand Down
6 changes: 3 additions & 3 deletions src/outfile/swmm_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ int EXPORT_OUT_API SMO_open(SMO_Handle p_handle, const char *path)
if (p_data == NULL)
return -1;
else {
strncpy(p_data->name, path, MAXFILENAME);
strncpy_s(p_data->name, path, MAXFILENAME);

// Attempt to open binary output file for reading only
if ((_fopen(&(p_data->file), path, "rb")) != 0)
Expand Down Expand Up @@ -526,7 +526,7 @@ int EXPORT_OUT_API SMO_getElementName(SMO_Handle p_handle, SMO_elementType type,
*length = p_data->elementNames[idx].length;
*name = newCharArray(*length + 1);
// Writes IDname and an additional null character to name
strncpy(*name, p_data->elementNames[idx].IDname,
strncpy_s(*name, p_data->elementNames[idx].IDname,
(*length + 1) * sizeof(char));
}
}
Expand Down Expand Up @@ -1036,7 +1036,7 @@ void errorLookup(int errcode, char *dest_msg, int dest_len)
msg = ERR440;
}

strncpy(dest_msg, msg, MAXMSG);
strncpy_s(dest_msg, msg, MAXMSG);
}

// Local functions:
Expand Down
2 changes: 2 additions & 0 deletions src/solver/consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// Biuld 5.3.0
// - Added MAXHOTSTARTFILES constant to support saving multiple hotstart files
// at different times.
// - Added const for MAX_ERR_MSG_LENGTH to define the maximum length of an error message.
//
//-----------------------------------------------------------------------------

Expand All @@ -28,6 +29,7 @@
#define EOFMARK 0x1A // Use 0x04 for UNIX systems
#define MAXTITLE 3 // Max. # title lines
#define MAXMSG 1024 // Max. # characters in message text
#define MAX_ERR_MSG_LENGTH 256 // Max. # characters in error message text
#define MAXLINE 1024 // Max. # characters per input line
#define MAXFNAME 259 // Max. # characters in file name
#define MAXTOKS 40 // Max. items per line of input
Expand Down
3 changes: 2 additions & 1 deletion src/solver/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
#define _CRT_SECURE_NO_DEPRECATE

#include <string.h>
#include "consts.h"
#include "error.h"

char ErrString[256];
char ErrString[MAX_ERR_MSG_LENGTH];

char* error_getMsg(int errCode, char* msg)
{
Expand Down
22 changes: 8 additions & 14 deletions src/solver/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
// Author: L. Rossman
//
// Error codes
//
//
// Update History:
// ==============
// Build 5.3.0:
// - Moved API error codes to swmm.h so that they can be accessed for
// interpretation
//-----------------------------------------------------------------------------

#ifndef ERROR_H
#define ERROR_H

enum ErrorType {
enum ErrorType {

// ... Runtime Errors
ERR_NONE = 0,
Expand Down Expand Up @@ -161,19 +166,8 @@ enum ErrorType {
// ... Runtime Errors
ERR_SYSTEM = 500,

// ... API Errors
ERR_API_NOT_OPEN = 501,
ERR_API_NOT_STARTED = 502,
ERR_API_NOT_ENDED = 503,
ERR_API_OBJECT_TYPE = 504,
ERR_API_OBJECT_INDEX = 505,
ERR_API_OBJECT_NAME = 506,
ERR_API_PROPERTY_TYPE = 507,
ERR_API_PROPERTY_VALUE = 508,
ERR_API_TIME_PERIOD = 509,

// ... Additional Errors
MAXERRMSG = 1000
MAXERRMSG = 1000,
};

char* error_getMsg(int i, char* msg);
Expand Down
22 changes: 12 additions & 10 deletions src/solver/error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,16 @@ ERR(357,"\n ERROR 357: inflows and outflows interface files have same name.")
ERR(361,"\n ERROR 361: could not open external file used for Time Series %s.")
ERR(363,"\n ERROR 363: invalid data in external file used for Time Series %s.")

ERR(500, "\n ERROR 500: System exception thrown.")

// API Error Keys
ERR(500,"\n ERROR 500: System exception thrown.")
ERR(501,"\n API Error 501: project not opened.")
ERR(502,"\n API Error 502: simulation not started.")
ERR(503,"\n API Error 503: simulation not ended.")
ERR(504,"\n API Error 504: invalid object type.")
ERR(505,"\n API Error 505: invalid object index.")
ERR(506,"\n API Error 506: invalid object name.")
ERR(507,"\n API Error 507: invalid property type.")
ERR(508,"\n API Error 508: invalid property value.")
ERR(509,"\n API Error 509: invalid time period.")
ERR(-999901,"\n API Error -999901: project not opened.")
ERR(-999902,"\n API Error -999902: simulation not started.")
ERR(-999903,"\n API Error -999903: simulation not ended.")
ERR(-999904,"\n API Error -999904: invalid object type.")
ERR(-999905,"\n API Error -999905: invalid object index.")
ERR(-999906,"\n API Error -999906: invalid object name.")
ERR(-999907,"\n API Error -999907: invalid property type.")
ERR(-999908,"\n API Error -999908: invalid property value.")
ERR(-999909,"\n API Error -999909: invalid time period.")
ERR(-999910,"\n API Error -999910: cannot open hot start interface file %s.")
7 changes: 5 additions & 2 deletions src/solver/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#ifndef FUNCS_H
#define FUNCS_H



//-----------------------------------------------------------------------------
// Project Methods
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -165,8 +167,8 @@ void output_saveResults(double reportTime);
void output_updateAvgResults(void);
void output_readDateTime(long period, DateTime *aDate);
void output_readSubcatchResults(long period, int index);
void output_readNodeResults(int long, int index);
void output_readLinkResults(int long, int index);
void output_readNodeResults(long period, int index);
void output_readLinkResults(long period, int index);

//-----------------------------------------------------------------------------
// Groundwater Methods
Expand Down Expand Up @@ -401,6 +403,7 @@ void iface_saveOutletResults(DateTime reportDate, FILE* file);
//-----------------------------------------------------------------------------
int hotstart_open(void);
void hotstart_save(void);
int hotstart_save_to_file(const char* hotstartFile);
void hotstart_close(void);

//-----------------------------------------------------------------------------
Expand Down
88 changes: 67 additions & 21 deletions src/solver/hotstart.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ static int fileVersion;
//-----------------------------------------------------------------------------
// Function declarations
//-----------------------------------------------------------------------------
static int initializeFromHostartFile(void);
static int initializeSaveHostartFile(TFile *hotstartFile);
static int initializeFromHotstartFile(void);
static int initializeSaveHotstartFile(TFile *hotstartFile);
static void readRunoff(void);
static void saveRunoff(TFile *hotstartFile);
static void readRouting(void);
Expand All @@ -77,15 +77,22 @@ static int readDouble(double* x, FILE* f);
int hotstart_open()
{
int i;

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

for (int i = 0; i < MAXHOTSTARTFILES; i++)
for (i = 0; i < MAXHOTSTARTFILES; i++)
{
if (!initializeSaveHostartFile(&FhotstartOutputs[i])) return FALSE; //output hot start file
if (initializeSaveHotstartFile(&FhotstartOutputs[i]))
{
report_writeErrorMsg(ERR_HOTSTART_FILE_OPEN, FhotstartOutputs[i].name);
return FALSE;
}
}

return TRUE;
}

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

void hotstart_save()
Expand All @@ -109,6 +116,42 @@ void hotstart_save()
}
}
}

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

int hotstart_save_to_file(const char* hotstartFile)
//
// Input: hotStartFile = filepath to hotstart file to use
// Output: returns error code
// Purpose: Saves and closes hotstart files that are to be written at end of simulation.
{
int error_code = 0;
char fname[MAXFNAME + 1];
TFile *hotstart = (TFile *) calloc(1, sizeof(TFile));

hotstart->file = NULL;
hotstart->mode = SAVE_FILE;
hotstart->saveDateTime = 0;

sstrncpy(fname, hotstartFile, MAXFNAME);
sstrncpy(hotstart->name, addAbsolutePath(fname), MAXFNAME);

if(error_code = initializeSaveHotstartFile(hotstart))
{
FREE(hotstart);
return error_code;
}
else
{
saveRunoff(hotstart);
saveRouting(hotstart);
fclose(hotstart->file);
hotstart->file = NULL;
FREE(hotstart);
return error_code;
}
}

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

void hotstart_close()
Expand All @@ -133,7 +176,7 @@ void hotstart_close()

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

int initializeFromHostartFile()
int initializeFromHotstartFile()
//
// Input: none
// Output: none
Expand All @@ -157,7 +200,9 @@ int initializeFromHostartFile()
if ( FhotstartInput.mode != USE_FILE ) return TRUE;
if ( (FhotstartInput.file = fopen(FhotstartInput.name, "r+b")) == NULL)
{
report_writeErrorMsg(ERR_HOTSTART_FILE_OPEN, FhotstartInput.name);
report_writeErrorMsg(
ERR_HOTSTART_FILE_OPEN,
FhotstartInput.name);
return FALSE;
}

Expand Down Expand Up @@ -219,13 +264,14 @@ int initializeFromHostartFile()

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

int initializeSaveHostartFile(TFile *hotstartFile)
int initializeSaveHotstartFile(TFile *hotstartFile)
//
// Input: none
// Output: none
// Input: hotStartFile = filepath to hotstart file to use
// Output: returns error code
// Purpose: opens a new routing hotstart file to save results to.
//
{
int error_code = 0;
int nSubcatch;
int nLandUses;
int nNodes;
Expand All @@ -238,8 +284,8 @@ int initializeSaveHostartFile(TFile *hotstartFile)
if (hotstartFile->mode != SAVE_FILE ) return TRUE;
if ( (hotstartFile->file = fopen(hotstartFile->name, "w+b")) == NULL)
{
report_writeErrorMsg(ERR_HOTSTART_FILE_OPEN, hotstartFile->name);
return FALSE;
error_code = ERR_HOTSTART_FILE_OPEN;
return error_code;
}

// --- write file stamp & number of objects to file
Expand All @@ -256,20 +302,20 @@ int initializeSaveHostartFile(TFile *hotstartFile)
fwrite(&nLinks, sizeof(int), 1, hotstartFile->file);
fwrite(&nPollut, sizeof(int), 1, hotstartFile->file);
fwrite(&flowUnits, sizeof(int), 1, hotstartFile->file);
return TRUE;
return error_code;
}

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

void saveRouting(TFile *hotstartFile)
//
// Input: none
// Input: hotStartFile = hotstart file to use
// Output: none
// Purpose: saves current state of all nodes and links to hotstart file.
//
{
int i, j;
float x[3];
float x[3] = { 0 };

for (i = 0; i < Nobjects[NODE]; i++)
{
Expand Down Expand Up @@ -316,7 +362,7 @@ void readRouting()
{
int i, j;
float x;
double xgw[4];
double xgw[4] = { 0 };
FILE* f = FhotstartInput.file;

// --- for file format 2, assign GW moisture content and lower depth
Expand Down Expand Up @@ -395,15 +441,15 @@ void readRouting()

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

void saveRunoff(TFile *hotstartFile)
void saveRunoff(TFile *hotstartFile)
//
// Input: none
// Input: hotStartFile = hotstart file to use
// Output: none
// Purpose: saves current state of all subcatchments to hotstart file.
//
{
int i, j, k;
double x[6];
double x[6] = { 0 };
FILE* f = hotstartFile->file;

for (i = 0; i < Nobjects[SUBCATCH]; i++)
Expand Down Expand Up @@ -469,15 +515,15 @@ void saveRunoff(TFile *hotstartFile)

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

void readRunoff()
void readRunoff()
//
// Input: none
// Output: none
// Purpose: reads saved state of all subcatchments from a hot start file.
//
{
int i, j, k;
double x[6];
double x[6] = { 0 };
FILE* f = FhotstartInput.file;

for (i = 0; i < Nobjects[SUBCATCH]; i++)
Expand Down
Loading

0 comments on commit 9cad38b

Please sign in to comment.