diff --git a/.gitignore b/.gitignore index 628cb20..a20656a 100755 --- a/.gitignore +++ b/.gitignore @@ -224,5 +224,10 @@ temp/ nrtestsuite/ tests/data/ -#Cmake generated export headers +#Cmake stuff +buildprod*/ *_export.h + + +# git merge +*.orig diff --git a/CMakeLists.txt b/CMakeLists.txt index 2756b71..268f55e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,8 +69,8 @@ ENDIF (MSVC) # configure file groups -file(GLOB EPANET_SOURCES src/*.c) -file(GLOB EPANET_LIB_ALL src/*.c src/*.h) +file(GLOB EPANET_SOURCES src/*.c src/util/*.c) +file(GLOB EPANET_LIB_ALL src/* src/util/*) source_group("Library" FILES ${EPANET_LIB_ALL}) diff --git a/include/epanet2_2.h b/include/epanet2_2.h index ae21a60..161f122 100644 --- a/include/epanet2_2.h +++ b/include/epanet2_2.h @@ -68,7 +68,7 @@ typedef struct Project *EN_Project; @brief Creates an EPANET project. @param[out] ph an EPANET project handle that is passed into all other API functions. @return an error code. - + EN_createproject must be called before any other API functions are used. */ int DLLEXPORT EN_createproject(EN_Project *ph); @@ -129,9 +129,9 @@ typedef struct Project *EN_Project; @param rptFile the name of a report file to be created (or "" if not needed). @param outFile the name of a binary output file to be created (or "" if not needed). @return an error code. - + This function should be called immediately after ::EN_createproject if an EPANET-formatted - input file will be used to supply network data. + input file will be used to supply network data. */ int DLLEXPORT EN_open(EN_Project ph, const char *inpFile, const char *rptFile, const char *outFile); @@ -163,19 +163,19 @@ typedef struct Project *EN_Project; /** @brief Runs a complete hydraulic simulation with results for all time periods - written to a temporary hydraulics file. + written to a temporary hydraulics file. @param ph an EPANET project handle. @return an error code. Use ::EN_solveH to generate a complete hydraulic solution which can stand alone - or be used as input to a water quality analysis. This function will not allow one to + or be used as input to a water quality analysis. This function will not allow one to examine intermediate hydraulic results as they are generated. It can also be followed by calls to ::EN_saveH and ::EN_report to write hydraulic results to the report file. - - The sequence ::EN_openH - ::EN_initH - ::EN_runH - ::EN_nextH - ::EN_closeH + + The sequence ::EN_openH - ::EN_initH - ::EN_runH - ::EN_nextH - ::EN_closeH can be used instead to gain access to results at intermediate time periods and directly adjust link status and control settings as a simulation proceeds. - + Example: \code {.c} EN_Project ph; @@ -211,7 +211,7 @@ typedef struct Project *EN_Project; Call ::EN_openH prior to running the first hydraulic analysis using the ::EN_initH - ::EN_runH - ::EN_nextH sequence. Multiple analyses can be made before calling ::EN_closeH to close the hydraulic solver. - + Do not call this function if ::EN_solveH is being used to run a complete hydraulic analysis or if hydraulics are being supplied by a previously saved hydraulics file using ::EN_usehydfile. @@ -219,14 +219,14 @@ typedef struct Project *EN_Project; int DLLEXPORT EN_openH(EN_Project ph); /** - @brief Initializes a network prior to running a hydraulic analysis. + @brief Initializes a network prior to running a hydraulic analysis. @param ph an EPANET project handle. @param initFlag a 2-digit initialization flag (see @ref EN_InitHydOption). @return an error code. This function initializes storage tank levels, link status and settings, and the simulation time clock prior to running a hydraulic analysis. - + The initialization flag is a two digit number where the 1st (left) digit indicates if link flows should be re-initialized (1) or not (0), and the 2nd digit indicates if hydraulic results should be saved to a temporary @@ -239,7 +239,7 @@ typedef struct Project *EN_Project; - making a subsequent water quality run, - using ::EN_report to generate a report - using ::EN_savehydfile to save the binary hydraulics file. - + There is no need to save hydraulics if you will be writing custom code to process hydraulic results as they are generated using the functions ::EN_getnodevalue and ::EN_getlinkvalue. @@ -257,7 +257,7 @@ typedef struct Project *EN_Project; should be treated as a read-only variable. ::EN_initH must have been called prior to running the ::EN_runH - ::EN_nextH loop. - + See ::EN_nextH for an example of using this function. */ int DLLEXPORT EN_runH(EN_Project ph, long *currentTime); @@ -280,25 +280,25 @@ typedef struct Project *EN_Project; - the time interval until the next change in demands occurs - the time interval until a tank becomes full or empty - the time interval until a control or rule fires. - + Example: \code {.c} long t, tstep; - EN_openH(ph); - EN_initH(ph, EN_NOSAVE); - do { - EN_runH(ph, &t); + EN_openH(ph); + EN_initH(ph, EN_NOSAVE); + do { + EN_runH(ph, &t); // Retrieve hydraulic results for time t - EN_nextH(ph, &tstep); - } while (tstep > 0); - EN_closeH(ph); + EN_nextH(ph, &tstep); + } while (tstep > 0); + EN_closeH(ph); \endcode */ int DLLEXPORT EN_nextH(EN_Project ph, long *tStep); /** @brief Transfers a project's hydraulics results from its temporary hydraulics file - to its binary output file, where results are only reported at uniform reporting intervals. + to its binary output file, where results are only reported at uniform reporting intervals. @param ph an EPANET project handle. @return an error code. @@ -316,20 +316,20 @@ typedef struct Project *EN_Project; Use this function to save the current set of hydraulics results to a file, either for post-processing or to be used at a later time by calling the ::EN_usehydfile function. - + The hydraulics file contains nodal demands and heads and link flows, status, and settings for all hydraulic time steps, even intermediate ones. - + Before calling this function hydraulic results must have been generated and saved by having called ::EN_solveH or the ::EN_initH - ::EN_runH - ::EN_nextH sequence with the initflag - argument of ::EN_initH set to `EN_SAVE` or `EN_SAVE_AND_INIT`. + argument of ::EN_initH set to `EN_SAVE` or `EN_SAVE_AND_INIT`. */ int DLLEXPORT EN_savehydfile(EN_Project ph, char *filename); /** @brief Closes the hydraulic solver freeing all of its allocated memory. @return an error code. - + Call ::EN_closeH after all hydraulics analyses have been made using ::EN_initH - ::EN_runH - ::EN_nextH. Do not call this function if ::EN_solveH is being used. */ @@ -343,10 +343,10 @@ typedef struct Project *EN_Project; /** @brief Runs a complete water quality simulation with results at uniform - reporting intervals written to the project's binary output file. + reporting intervals written to the project's binary output file. @param ph an EPANET project handle. @return an error code. - + A hydraulic analysis must have been run and saved to a hydraulics file before calling ::EN_solveQ. This function will not allow one to examine intermediate water quality results as they are generated. It can be followed by a call to ::EN_report @@ -355,7 +355,7 @@ typedef struct Project *EN_Project; One can instead use the ::EN_openQ - ::EN_initQ - ::EN_runQ - ::EN_nextQ - ::EN_closeQ sequence to gain access to gain access to water quality results at intermediate time periods. - + Example: see ::EN_solveH. */ int DLLEXPORT EN_solveQ(EN_Project ph); @@ -376,7 +376,7 @@ typedef struct Project *EN_Project; int DLLEXPORT EN_openQ(EN_Project ph); /** - @brief Initializes a network prior to running a water quality analysis. + @brief Initializes a network prior to running a water quality analysis. @param ph n EPANET project handle. @param saveFlag set to `EN_SAVE` (1) if results are to be saved to the project's binary output file, or to `EN_NOSAVE` (0) if not. @@ -384,16 +384,16 @@ typedef struct Project *EN_Project; Call ::EN_initQ prior to running a water quality analysis using ::EN_runQ in conjunction with either ::EN_nextQ or ::EN_stepQ. - + ::EN_openQ must have been called prior to calling EN_initQ. - + Do not call ::EN_initQ if a complete water quality analysis will be made using ::EN_solveQ. */ int DLLEXPORT EN_initQ(EN_Project ph, int saveFlag); /** @brief Makes hydraulic and water quality results at the start of the current time - period available to a project's water quality solver. + period available to a project's water quality solver. @param ph an EPANET project handle. @param[out] currentTime current simulation time in seconds. @return an error code. @@ -402,10 +402,10 @@ typedef struct Project *EN_Project; start of each hydraulic period in an extended period simulation. Or use it in a loop with ::EN_stepQ to access results at the start of each water quality time step. See each of these functions for examples of how to code such loops. - + ::EN_initQ must have been called prior to running an ::EN_runQ - ::EN_nextQ (or ::EN_stepQ) loop. - + The current time of the simulation is determined from information saved with the hydraulic analysis that preceded the water quality analysis. Treat it as a read-only variable. @@ -415,7 +415,7 @@ typedef struct Project *EN_Project; /** @brief Advances a water quality simulation over the time until the next hydraulic event. @param ph an EPANET project handle. - @param[out] tStep time (in seconds) until the next hydraulic event or 0 if at the end + @param[out] tStep time (in seconds) until the next hydraulic event or 0 if at the end of the full simulation duration. @return an error code. @@ -426,20 +426,20 @@ typedef struct Project *EN_Project; The value of `tStep` is determined from information produced by the hydraulic analysis that preceded the water quality analysis. Treat it as a read-only variable. - + Example: \code {.c} long t, tStep; EN_solveH(ph); // Generate & save hydraulics EN_openQ(ph); - EN_initQ(ph, EN_NOSAVE); - do { - EN_runQ(ph, &t); + EN_initQ(ph, EN_NOSAVE); + do { + EN_runQ(ph, &t); // Monitor results at time t, which // begins a new hydraulic time period - EN_nextQ(ph, &tStep); - } while (tStep > 0); - EN_closeQ(ph); + EN_nextQ(ph, &tStep); + } while (tStep > 0); + EN_closeQ(ph); \endcode */ int DLLEXPORT EN_nextQ(EN_Project ph, long *tStep); @@ -454,7 +454,7 @@ typedef struct Project *EN_Project; quality simulation. It allows one to generate water quality results at each water quality time step of the simulation, rather than over each hydraulic event period as with ::EN_nextQ. - + Use the argument `timeLeft` to determine when no more calls to ::EN_runQ are needed because the end of the simulation period has been reached (i.e., when `timeLeft = 0`). */ @@ -464,11 +464,11 @@ typedef struct Project *EN_Project; @brief Closes the water quality solver, freeing all of its allocated memory. @param ph an EPANET project handle. @return an error code. - + Call ::EN_closeQ after all water quality analyses have been made using the ::EN_initQ - ::EN_runQ - ::EN_nextQ (or ::EN_stepQ) sequence of function calls. - - Do not call this function if ::EN_solveQ is being used. + + Do not call this function if ::EN_solveQ is being used. */ int DLLEXPORT EN_closeQ(EN_Project ph); @@ -490,12 +490,12 @@ typedef struct Project *EN_Project; @brief Writes simulation results in a tabular format to a project's report file. @param ph an EPANET project handle. @return an error code - + Either a full hydraulic analysis or full hydraulic and water quality analysis must have been run, with results saved to file, before ::EN_report is called. In the former case, ::EN_saveH must also be called first to transfer results from the - project's intermediate hydraulics file to its output file. - + project's intermediate hydraulics file to its output file. + The format of the report is controlled by commands issued with ::EN_setreport. */ int DLLEXPORT EN_report(EN_Project ph); @@ -504,15 +504,15 @@ typedef struct Project *EN_Project; @brief Resets a project's report options to their default values. @param ph an EPANET project handle. @return an error code - - After calling this function the default reporting options are in effect. These are: - - no status report - - no energy report - - no nodes reported on - - no links reported on - - node variables reported to 2 decimal places - - link variables reported to 2 decimal places (3 for friction factor) - - node variables reported are elevation, head, pressure, and quality + + After calling this function the default reporting options are in effect. These are: + - no status report + - no energy report + - no nodes reported on + - no links reported on + - node variables reported to 2 decimal places + - link variables reported to 2 decimal places (3 for friction factor) + - node variables reported are elevation, head, pressure, and quality - link variables reported are flow, velocity, and head loss. */ int DLLEXPORT EN_resetreport(EN_Project ph); @@ -522,10 +522,10 @@ typedef struct Project *EN_Project; @param ph an EPANET project handle. @param format a report formatting command. @return an error code - + Acceptable report formatting commands are described in Appendix C of the EPANET 2 Users Manual. - + Formatted results of a simulation can be written to a project's report file using the ::EN_report function. */ @@ -536,18 +536,18 @@ typedef struct Project *EN_Project; @param ph an EPANET project handle. @param level a status reporting level code (see @ref EN_StatusReport). @return an error code. - + Status reporting writes changes in the hydraulics status of network elements to a project's report file as a hydraulic simulation unfolds. There are three levels of reporting: `EN_NO_REPORT` (no status reporting), `EN_NORMAL_REPORT` (normal reporting) `EN_FULL_REPORT` (full status reporting). - + The full status report contains information at each trial of the solution to the system hydraulic equations at each time step of a simulation. It is useful mainly - for debugging purposes. - + for debugging purposes. + If many hydraulic analyses will be run in the application it is recommended that - status reporting be turned off (`level = EN_NO_REPORT`). + status reporting be turned off (`level = EN_NO_REPORT`). */ int DLLEXPORT EN_setstatusreport(EN_Project ph, int level); @@ -576,7 +576,7 @@ typedef struct Project *EN_Project; @param[out] errmsg the error message generated by the error code @param maxLen maximum number of characters that errmsg can hold @return an error code - + Error message strings should be at least @ref EN_MAXMSG characters in length. */ int DLLEXPORT EN_geterror(int errcode, char *errmsg, int maxLen); @@ -620,9 +620,9 @@ typedef struct Project *EN_Project; @param ph an EPANET project handle. @param[out] units a flow units code (see @ref EN_FlowUnits) @return an error code. - + Flow units in liters or cubic meters implies that SI metric units are used for all - other quantities in addition to flow. Otherwise US Customary units are employed. + other quantities in addition to flow. Otherwise US Customary units are employed. */ int DLLEXPORT EN_getflowunits(EN_Project ph, int *units); @@ -631,9 +631,9 @@ typedef struct Project *EN_Project; @param ph an EPANET project handle. @param units a flow units code (see @ref EN_FlowUnits) @return an error code. - + Flow units in liters or cubic meters implies that SI metric units are used for all - other quantities in addition to flow. Otherwise US Customary units are employed. + other quantities in addition to flow. Otherwise US Customary units are employed. */ int DLLEXPORT EN_setflowunits(EN_Project ph, int units); @@ -684,11 +684,11 @@ typedef struct Project *EN_Project; @param chemUnits the concentration units of the constituent. @param traceNode the ID name of the node being traced if `qualType = EN_TRACE`. @return an error code. - + Chemical name and units can be an empty string if the analysis is not for a chemical. - The same holds for the trace node if the analysis is not for source tracing. - - Note that the trace node is specified by ID name and not by index. + The same holds for the trace node if the analysis is not for source tracing. + + Note that the trace node is specified by ID name and not by index. */ int DLLEXPORT EN_setqualtype(EN_Project ph, int qualType, char *chemName, char *chemUnits, char *traceNode); @@ -705,7 +705,7 @@ typedef struct Project *EN_Project; @param id the ID name of the node to be added. @param nodeType the type of node being added (see @ref EN_NodeType) @return an error code. - + When a new node is created all of it's properties (see @ref EN_NodeProperty) are set to 0. */ int DLLEXPORT EN_addnode(EN_Project ph, char *id, int nodeType); @@ -769,10 +769,10 @@ typedef struct Project *EN_Project; @brief Retrieves a property value for a node. @param ph an EPANET project handle. @param index a node's index. - @param property the property to retrieve (see @ref EN_NodeProperty). + @param property the property to retrieve (see @ref EN_NodeProperty). @param[out] value the current value of the property. @return an error code. - + Values are returned in units that depend on the units used for flow rate (see @ref Units). */ @@ -785,7 +785,7 @@ typedef struct Project *EN_Project; @param property the property to set (see @ref EN_NodeProperty). @param value the new value for the property. @return an error code. - + Values are in units that depend on the units used for flow rate (see @ref Units). */ int DLLEXPORT EN_setnodevalue(EN_Project ph, int index, int property, double value); @@ -798,7 +798,7 @@ typedef struct Project *EN_Project; @param dmnd the value of the junction's primary base demand. @param dmndpat the ID name of the demand's time pattern ("" for no pattern) @return an error code. - + These properties have units that depend on the units used for flow rate (see @ref Units). */ int DLLEXPORT EN_setjuncdata(EN_Project ph, int index, double elev, double dmnd, @@ -816,7 +816,7 @@ typedef struct Project *EN_Project; @param minvol the volume of the tank at its minimum water level. @param volcurve the name of the tank's volume curve ("" for no curve) @return an error code. - + These properties have units that depend on the units used for flow rate (see @ref Units). */ int DLLEXPORT EN_settankdata(EN_Project ph, int index, double elev, double initlvl, @@ -856,7 +856,7 @@ typedef struct Project *EN_Project; @param[out] preq Pressure required to deliver full demand. @param[out] pexp Pressure exponent in demand function. @return an error code. - + Parameters `pmin`, `preq`, and `pexp` are only used when the demand model is `EN_PDA`. */ int DLLEXPORT EN_getdemandmodel(EN_Project ph, int *type, double *pmin, @@ -870,13 +870,13 @@ typedef struct Project *EN_Project; @param preq Pressure required to deliver full demand. @param pexp Pressure exponent in demand function. @return an error code. - + Set `type` to `EN_DDA` for a traditional demand driven analysis (in which case the remaining three parameter values are ignored) or to `EN_PDA` for a pressure driven analysis. In the latter case a node's demand is computed as: > `Dfull * [ (P - pmin) / (preq - pmin) ] ^ pexp` where `Dfull` is the full demand and `P` is the current pressure. - + Setting `preq` equal to `pmin` will result in a solution with the smallest amount of demand reductions needed to insure that no node delivers positive demand at a pressure below `pmin`. @@ -922,7 +922,7 @@ typedef struct Project *EN_Project; @param demandIndex the index of a demand category for the node (starting from 1). @param[out] patIndex the index of the category's time pattern. @return an error code. - + A returned pattern index of 0 indicates that no time pattern has been assigned to the demand category. */ @@ -936,7 +936,7 @@ typedef struct Project *EN_Project; @param demandIndex the index of one of the node's demand categories (starting from 1). @param patIndex the index of the time pattern assigned to the category. @return an error code. - + Specifying a pattern index of 0 indicates that no time pattern is assigned to the demand category. */ @@ -949,7 +949,7 @@ typedef struct Project *EN_Project; @param demandIndex the index of one of the node's demand categories (starting from 1). @param[out] demandName The name of the selected category. @return an error code. - + `demandName` must be sized to contain at least @ref EN_MAXID characters. */ int DLLEXPORT EN_getdemandname(EN_Project ph, int nodeIndex, int demandIndex, char *demandName); @@ -961,7 +961,7 @@ typedef struct Project *EN_Project; @param demandIdx the index of one of the node's demand categories (starting from 1). @param demandName the new name assigned to the category. @return Error code. - + The category name must contain no more than @ref EN_MAXID characters. */ int DLLEXPORT EN_setdemandname(EN_Project ph, int nodeIndex, int demandIdx, char *demandName); @@ -980,15 +980,15 @@ typedef struct Project *EN_Project; @param fromNode The ID name of the link's starting node. @param toNode The ID name of the link's ending node. @return an error code. - + A new pipe is assigned a diameter of 10 inches (or 254 mm), a length of 100 feet (or meters), a roughness coefficient of 100 and 0 for all other properties. - + A new pump has a status of `EN_OPEN`, a speed setting of 1, and has no pump curve or power rating assigned to it. - + A new valve has a diameter of 10 inches (or 254 mm) and all other properties set to 0. - + See @ref EN_LinkProperty. */ int DLLEXPORT EN_addlink(EN_Project ph, char *id, int linkType, char *fromNode, char *toNode); @@ -1088,7 +1088,7 @@ typedef struct Project *EN_Project; @param property the property to retrieve (see @ref EN_LinkProperty). @param[out] value the current value of the property. @return an error code. - + Values are returned in units that depend on the units used for flow rate (see @ref Units). */ int DLLEXPORT EN_getlinkvalue(EN_Project ph, int index, int property, double *value); @@ -1100,7 +1100,7 @@ typedef struct Project *EN_Project; @param property the property to set (see @ref EN_LinkProperty). @param value the new value for the property. @return an error code. - + Values are in units that depend on the units used for flow rate (see @ref Units). */ int DLLEXPORT EN_setlinkvalue(EN_Project ph, int index, int property, double value); @@ -1114,7 +1114,7 @@ typedef struct Project *EN_Project; @param rough the pipe's roughness coefficient. @param mloss the pipe's minor loss coefficient. @return an error code. - + These properties have units that depend on the units used for flow rate (see @ref Units). */ int DLLEXPORT EN_setpipedata(EN_Project ph, int index, double length, double diam, @@ -1165,7 +1165,7 @@ typedef struct Project *EN_Project; @param ph an EPANET project handle. @param id the ID name of the pattern to add. @return an error code. - + The new pattern contains a single time period whose factor is 1.0. */ int DLLEXPORT EN_addpattern(EN_Project ph, char *id); @@ -1235,11 +1235,11 @@ typedef struct Project *EN_Project; @param values an array of new pattern factor values. @param len the number of factor values supplied. @return an error code. - - `values` is a zero-based array that contains `len` elements. - + + `values` is a zero-based array that contains `len` elements. + Use this function to redefine (and resize) a time pattern all at once; - use @ref EN_setpatternvalue to revise pattern factors one at a time. + use @ref EN_setpatternvalue to revise pattern factors one at a time. */ int DLLEXPORT EN_setpattern(EN_Project ph, int index, double *values, int len); @@ -1254,7 +1254,7 @@ typedef struct Project *EN_Project; @param ph an EPANET project handle. @param id The ID name of the curve to be added. @return an error code. - + The new curve contains a single data point (1.0, 1.0). */ int DLLEXPORT EN_addcurve(EN_Project ph, char *id); @@ -1346,11 +1346,11 @@ typedef struct Project *EN_Project; @param yValues an array of new y-values for the curve. @param nPoints the new number of data points for the curve. @return an error code. - - `xValues` and `yValues` are zero-based arrays that contains `nPoints` elements. - + + `xValues` and `yValues` are zero-based arrays that contains `nPoints` elements. + Use this function to redefine (and resize) a curve all at once; - use @ref EN_setcurvevalue to revise a curve's data points one at a time. + use @ref EN_setcurvevalue to revise a curve's data points one at a time. */ int DLLEXPORT EN_setcurve(EN_Project ph, int index, double *xValues, double *yValues, int nPoints); @@ -1429,7 +1429,7 @@ typedef struct Project *EN_Project; @param ph an EPANET project handle. @param rule text of the rule following the format used in an EPANET input file. @return an error code. - + Consult Appendix C of the EPANET 2 Users Manual to learn about a rule's format. Each clause of the rule must end with a newline character `\n`. */ @@ -1462,8 +1462,8 @@ typedef struct Project *EN_Project; @param index the rule's index (starting from 1). @param[out] id the rule's ID name. @return Error code. - - The ID name must be sized to hold at least @ref EN_MAXID characters. + + The ID name must be sized to hold at least @ref EN_MAXID characters. */ int DLLEXPORT EN_getruleID(EN_Project ph, int index, char* id); diff --git a/include/epanet2_enums.h b/include/epanet2_enums.h index 656386c..a445846 100644 --- a/include/epanet2_enums.h +++ b/include/epanet2_enums.h @@ -191,7 +191,7 @@ typedef enum { EN_PUMP_CLOSED = 2, //!< Pump closed EN_PUMP_OPEN = 3, //!< Pump open EN_PUMP_XFLOW = 5 //!< Pump open - cannot supply flow -} EN_PumpStateType; +} EN_PumpStateType; /// Types of water quality analyses /** @@ -249,7 +249,7 @@ typedef enum { EN_LPM = 6, //!< Liters per minute EN_MLD = 7, //!< Million liters per day EN_CMH = 8, //!< Cubic meters per hour - EN_CMD = 9 //!< Cubic meters per day + EN_CMD = 9 //!< Cubic meters per day } EN_FlowUnits; /// Types of demand models @@ -261,7 +261,7 @@ A demand driven analysis requires that a junction's full demand be supplied in each time period independent of how much pressure is available. A pressure driven analysis makes demand be a power function of pressure, up to the point where the full demand is met. -*/ +*/ typedef enum { EN_DDA = 0, //!< Demand driven analysis EN_PDA = 1 //!< Pressure driven analysis @@ -272,7 +272,7 @@ typedef enum { These options specify hydraulic convergence criteria, choice of head loss formula, and several other parameters applied on a network-wide basis. They are accessed using the @ref EN_getoption and @ref EN_setoption functions. -*/ +*/ typedef enum { EN_TRIALS = 0, //!< Maximum hydraulic trials allowed EN_ACCURACY = 1, //!< Maximum total relative flow change for hydraulic convergence @@ -308,7 +308,7 @@ These options determine what kind of statistical post-processing should be done the time series of simulation results generated before they are reported using @ref EN_report. An option can be chosen by using `STATISTIC option` as the argument to @ref EN_setreport. -*/ +*/ typedef enum { EN_SERIES = 0, //!< Report all time series points EN_AVERAGE = 1, //!< Report average value over simulation period @@ -408,7 +408,7 @@ typedef enum { EN_R_TIME = 9, //!< Elapsed simulation time EN_R_CLOCKTIME = 10, //!< Time of day EN_R_FILLTIME = 11, //!< Time to fill a tank - EN_R_DRAINTIME = 12 //!< Time to drain a tank + EN_R_DRAINTIME = 12 //!< Time to drain a tank } EN_RuleVariable; /// Comparison operators used in rule-based controls @@ -422,7 +422,7 @@ typedef enum { EN_R_IS = 6, //!< Is equal to EN_R_NOT = 7, //!< Is not equal to EN_R_BELOW = 8, //!< Is below - EN_R_ABOVE = 9 //!< Is above + EN_R_ABOVE = 9 //!< Is above } EN_RuleOperator; /// Link status codes used in rule-based controls diff --git a/include/epanet_py.h b/include/epanet_py.h new file mode 100644 index 0000000..f63e472 --- /dev/null +++ b/include/epanet_py.h @@ -0,0 +1,175 @@ +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: epanet_py.h + Description: EPANET API functions for Python SWIG wrap + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 12/10/2018 + ****************************************************************************** +*/ + + +#ifndef EPANET_PY_H +#define EPANET_PY_H + + +// Opaque pointer to project +typedef void *Handle; + + +#include "epanet2_enums.h" +#include "epanet2_export.h" + + +#if defined(__cplusplus) +extern "C" { +#endif + + +int DLLEXPORT proj_create(Handle *ph_out); +int DLLEXPORT proj_delete(Handle *ph_inout); +int DLLEXPORT proj_run(Handle ph, const char *input_path, const char *report_path, const char *output_path); +int DLLEXPORT proj_init(Handle ph, const char *rptFile, const char *outFile, EN_FlowUnits unitsType, EN_HeadLossType headLossType); +int DLLEXPORT proj_open(Handle ph, const char *inpFile, const char *rptFile, const char *binOutFile); +int DLLEXPORT proj_savefile(Handle ph, const char *inpfilename); +int DLLEXPORT proj_close(Handle ph); + + +int DLLEXPORT hydr_solve(Handle ph); +int DLLEXPORT hydr_save(Handle ph); +int DLLEXPORT hydr_open(Handle ph); +int DLLEXPORT hydr_init(Handle ph, EN_InitHydOption saveFlag); +int DLLEXPORT hydr_run(Handle ph, long *currentTime); +int DLLEXPORT hydr_next(Handle ph, long *tStep); +int DLLEXPORT hydr_close(Handle ph); +int DLLEXPORT hydr_savefile(Handle ph, char *filename); +int DLLEXPORT hydr_usefile(Handle ph, char *filename); + + +int DLLEXPORT qual_solve(Handle ph); +int DLLEXPORT qual_open(Handle ph); +int DLLEXPORT qual_init(Handle ph, EN_InitHydOption saveFlag); +int DLLEXPORT qual_run(Handle ph, long *currentTime); +int DLLEXPORT qual_next(Handle ph, long *tStep); +int DLLEXPORT qual_step(Handle ph, long *timeLeft); +int DLLEXPORT qual_close(Handle ph); + + +int DLLEXPORT rprt_writeline(Handle ph, char *line); +int DLLEXPORT rprt_writeresults(Handle ph); +int DLLEXPORT rprt_reset(Handle ph); +int DLLEXPORT rprt_set(Handle ph, char *reportCommand); +int DLLEXPORT rprt_setlevel(Handle ph, EN_StatusReport code); +int DLLEXPORT rprt_getcount(Handle ph, EN_CountType code, int *count); +int DLLEXPORT rprt_anlysstats(Handle ph, EN_AnalysisStatistic code, double* value); + + +int DLLEXPORT anlys_getoption(Handle ph, EN_Option code, double *value); +int DLLEXPORT anlys_setoption(Handle ph, EN_Option code, double value); +int DLLEXPORT anlys_getflowunits(Handle ph, int *code); +int DLLEXPORT anlys_setflowunits(Handle ph, EN_FlowUnits code); +int DLLEXPORT anlys_gettimeparam(Handle ph, EN_TimeParameter code, long *value); +int DLLEXPORT anlys_settimeparam(Handle ph, EN_TimeParameter code, long value); +int DLLEXPORT anlys_getqualinfo(Handle ph, int *qualcode, char *chemname, char *chemunits, int *tracenode); +int DLLEXPORT anlys_getqualtype(Handle ph, int *qualcode, int *tracenode); +int DLLEXPORT anlys_setqualtype(Handle ph, EN_QualityType qualcode, char *chemname, char *chemunits, char *tracenode); + + +int DLLEXPORT node_add(Handle ph, char *id, EN_NodeType nodeType); +int DLLEXPORT node_delete(Handle ph, int index, int actionCode); +int DLLEXPORT node_getindex(Handle ph, char *id, int *index); +int DLLEXPORT node_getid(Handle ph, int index, char *id); +int DLLEXPORT node_setid(Handle ph, int index, char *newid); +int DLLEXPORT node_gettype(Handle ph, int index, int *code); +int DLLEXPORT node_getvalue(Handle ph, int index, EN_NodeProperty code, double *value); +int DLLEXPORT node_setvalue(Handle ph, int index, EN_NodeProperty code, double value); +int DLLEXPORT node_getcoord(Handle ph, int index, double *x, double *y); +int DLLEXPORT node_setcoord(Handle ph, int index, double x, double y); + + +int DLLEXPORT dmnd_getmodel(Handle ph, int *type, double *pmin, double *preq, double *pexp); +int DLLEXPORT dmnd_setmodel(Handle ph, int type, double pmin, double preq, double pexp); +int DLLEXPORT dmnd_getcount(Handle ph, int nodeIndex, int *numDemands); +int DLLEXPORT dmnd_getbase(Handle ph, int nodeIndex, int demandIndex, double *baseDemand); +int DLLEXPORT dmnd_setbase(Handle ph, int nodeIndex, int demandIndex, double baseDemand); +int DLLEXPORT dmnd_getpattern(Handle ph, int nodeIndex, int demandIndex, int *pattIndex); +int DLLEXPORT dmnd_setpattern(Handle ph, int nodeIndex, int demandIndex, int patIndex); +int DLLEXPORT dmnd_getname(Handle ph, int nodeIndex, int demandIdx, char *demandName); +int DLLEXPORT dmnd_setname(Handle ph, int nodeIndex, int demandIdx, char *demandName); + + +int DLLEXPORT link_add(Handle ph, char *id, EN_LinkType linkType, char *fromNode, char *toNode); +int DLLEXPORT link_delete(Handle ph, int index, int actionCode); +int DLLEXPORT link_getindex(Handle ph, char *id, int *index); +int DLLEXPORT link_getid(Handle ph, int index, char *id); +int DLLEXPORT link_setid(Handle ph, int index, char *newid); +int DLLEXPORT link_gettype(Handle ph, int index, int *code); +int DLLEXPORT link_settype(Handle ph, int *index, EN_LinkType type, int actionCode); +int DLLEXPORT link_getnodes(Handle ph, int index, int *node1, int *node2); +int DLLEXPORT link_setnodes(Handle ph, int index, int node1, int node2); +int DLLEXPORT link_getvalue(Handle ph, int index, EN_LinkProperty code, double *value); +int DLLEXPORT link_setvalue(Handle ph, int index, int code, double v); + + +int DLLEXPORT pump_gettype(Handle ph, int linkIndex, int *outType); +int DLLEXPORT pump_getheadcurveindex(Handle ph, int pumpIndex, int *curveIndex); +int DLLEXPORT pump_setheadcurveindex(Handle ph, int pumpIndex, int curveIndex); + + +int DLLEXPORT ptrn_add(Handle ph, char *id); +int DLLEXPORT ptrn_getindex(Handle ph, char *id, int *index); +int DLLEXPORT ptrn_getid(Handle ph, int index, char *id); +int DLLEXPORT ptrn_getlength(Handle ph, int index, int *len); +int DLLEXPORT ptrn_getvalue(Handle ph, int index, int period, double *value); +int DLLEXPORT ptrn_setvalue(Handle ph, int index, int period, double value); +int DLLEXPORT ptrn_getavgvalue(Handle ph, int index, double *value); +int DLLEXPORT ptrn_set(Handle ph, int index, double *f, int len); + + +int DLLEXPORT curv_add(Handle ph, char *id); +int DLLEXPORT curv_getindex(Handle ph, char *id, int *index); +int DLLEXPORT curv_getid(Handle ph, int index, char *id); +int DLLEXPORT curv_getlength(Handle ph, int index, int *len); +int DLLEXPORT curv_gettype(Handle ph, int curveIndex, int *outType); +int DLLEXPORT curv_getvalue(Handle ph, int curveIndex, int pointIndex, double *x, double *y); +int DLLEXPORT curv_setvalue(Handle ph, int curveIndex, int pointIndex, double x, double y); +int DLLEXPORT curv_get(Handle ph, int curveIndex, char* id, int *nValues, double **xValues, double **yValues); +int DLLEXPORT curv_set(Handle ph, int index, double *x, double *y, int len); + +int DLLEXPORT scntl_add(Handle ph, int type, int linkIndex, double setting, int nodeIndex, double level, int *index); +int DLLEXPORT scntl_delete(Handle ph, int index); +int DLLEXPORT scntl_get(Handle ph, int controlIndex, int *controlType, int *linkIndex, double *setting, int *nodeIndex, double *level); +int DLLEXPORT scntl_set(Handle ph, int cindex, int ctype, int lindex, double setting, int nindex, double level); + + +int DLLEXPORT rcntl_add(Handle ph, char *rule); +int DLLEXPORT rcntl_delete(Handle ph, int index); +int DLLEXPORT rcntl_get(Handle ph, int index, int *nPremises, int *nThenActions, int *nElseActions, double *priority); +int DLLEXPORT rcntl_getid(Handle ph, int index, char* id); +int DLLEXPORT rcntl_getpremise(Handle ph, int ruleIndex, int premiseIndex, int *logop, int *object, int *objIndex, int *variable, int *relop, int *status, double *value); +int DLLEXPORT rcntl_setpremise(Handle ph, int ruleIndex, int premiseIndex, int logop, int object, int objIndex, int variable, int relop, int status, double value); +int DLLEXPORT rcntl_setpremiseindex(Handle ph, int ruleIndex, int premiseIndex, int objIndex); +int DLLEXPORT rcntl_setpremisestatus(Handle ph, int ruleIndex, int premiseIndex, int status); +int DLLEXPORT rcntl_setpremisevalue(Handle ph, int ruleIndex, int premiseIndex, double value); +int DLLEXPORT rcntl_getthenaction(Handle ph, int ruleIndex, int actionIndex, int *linkIndex, int *status, double *setting); +int DLLEXPORT rcntl_setthenaction(Handle ph, int ruleIndex, int actionIndex, int linkIndex, int status, double setting); +int DLLEXPORT rcntl_getelseaction(Handle ph, int ruleIndex, int actionIndex, int *linkIndex, int *status, double *setting); +int DLLEXPORT rcntl_setelseaction(Handle ph, int ruleIndex, int actionIndex, int linkIndex, int status, double setting); +int DLLEXPORT rcntl_setrulepriority(Handle ph, int index, double priority); + + +void DLLEXPORT err_clear(Handle ph); +int DLLEXPORT err_check(Handle ph, char** msg_buffer); +void DLLEXPORT toolkit_free(void **memory); +int DLLEXPORT toolkit_getversion(int *version); + + +#if defined(__cplusplus) +} +#endif + + +#endif //EPANET_PY_H diff --git a/src/epanet_py.c b/src/epanet_py.c new file mode 100644 index 0000000..a5d2ff2 --- /dev/null +++ b/src/epanet_py.c @@ -0,0 +1,807 @@ +/* + ****************************************************************************** + Project: OWA EPANET + Version: 2.2 + Module: epanet_py.c + Description: EPANET API functions for Python SWIG wrap + Authors: see AUTHORS + Copyright: see AUTHORS + License: see LICENSE + Last Updated: 12/10/2018 + ****************************************************************************** +*/ + + +#include +#include + +#include "epanet_py.h" +#include "util/errormanager.h" +#include "epanet2_2.h" +#include "text.h" +#include "types.h" + + +typedef struct { + Project *project; + error_handle_t *error; +}handle_t; +// Extern functions +extern char *geterrmsg(int, char *); +// Local functions +void error_lookup(int errcode, char *errmsg, int len); + + +int DLLEXPORT proj_create(Handle *ph) +{ + handle_t *handle = (handle_t *)calloc(1, sizeof(handle_t)); + + if (handle != NULL) + { + EN_createproject(&handle->project); + handle->error = error_new_manager(&error_lookup); + *ph = handle; + return 0; + } + return -1; +} + +int DLLEXPORT proj_delete(Handle *ph) +{ + handle_t *handle = (handle_t *)*ph; + + if (handle == NULL) + return -1; + else + { + EN_deleteproject(&handle->project); + error_dst_manager(handle->error); + } + free(handle); + *ph = NULL; + + return 0; +} + +int DLLEXPORT proj_run(Handle ph, const char *input_path, + const char *report_path, const char *output_path) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_runproject(pr->project, input_path, report_path, output_path, NULL)); +} + +int DLLEXPORT proj_init(Handle ph, const char *rptFile, const char *outFile, + EN_FlowUnits unitsType, EN_HeadLossType headLossType) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_init(pr->project, rptFile, outFile, unitsType, headLossType)); +} + +int DLLEXPORT proj_open(Handle ph, const char *inpFile, const char *rptFile, + const char *binOutFile) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_open(pr->project, inpFile, rptFile, binOutFile)); +} + +int DLLEXPORT proj_savefile(Handle ph, const char *filename) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_saveinpfile(pr->project, filename)); +} + +int DLLEXPORT proj_close(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_close(pr->project)); +} + + + + +int DLLEXPORT hydr_solve(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_solveH(pr->project)); +} + +int DLLEXPORT hydr_save(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_saveH(pr->project)); +} + +int DLLEXPORT hydr_open(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_openH(pr->project)); +} + +int DLLEXPORT hydr_init(Handle ph, EN_InitHydOption saveFlag) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_initH(pr->project, saveFlag)); +} + +int DLLEXPORT hydr_run(Handle ph, long *currentTime) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_runH(pr->project, currentTime)); +} + +int DLLEXPORT hydr_next(Handle ph, long *tStep) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_nextH(pr->project, tStep)); +} + +int DLLEXPORT hydr_close(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_closeH(pr->project)); +} + +int DLLEXPORT hydr_savefile(Handle ph, char *filename) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_savehydfile(pr->project, filename)); +} + +int DLLEXPORT hydr_usefile(Handle ph, char *filename) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_usehydfile(pr->project, filename)); +} + + + + +int DLLEXPORT qual_solve(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_solveQ(pr->project)); +} + +int DLLEXPORT qual_open(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_openQ(pr->project)); +} + +int DLLEXPORT qual_init(Handle ph, EN_InitHydOption saveFlag) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_initQ(pr->project, saveFlag)); +} + +int DLLEXPORT qual_run(Handle ph, long *currentTime) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_runQ(pr->project, currentTime)); +} + +int DLLEXPORT qual_next(Handle ph, long *tStep) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_nextQ(pr->project, tStep)); +} + +int DLLEXPORT qual_step(Handle ph, long *timeLeft) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_stepQ(pr->project, timeLeft)); +} + +int DLLEXPORT qual_close(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_closeQ(pr->project)); +} + + + + +int DLLEXPORT rprt_writeline(Handle ph, char *line) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_writeline(pr->project, line)); +} + +int DLLEXPORT rprt_writeresults(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_report(pr->project)); +} + +int DLLEXPORT rprt_reset(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_resetreport(pr->project)); +} + +int DLLEXPORT rprt_set(Handle ph, char *reportCommand) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setreport(pr->project, reportCommand)); +} + +int DLLEXPORT rprt_setlevel(Handle ph, EN_StatusReport code) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setstatusreport(pr->project, code)); +} + +int DLLEXPORT rprt_getcount(Handle ph, EN_CountType code, int *count) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getcount(pr->project, code, count)); +} + +int DLLEXPORT rprt_anlysstats(Handle ph, EN_AnalysisStatistic code, double* value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getstatistic(pr->project, code, value)); +} + + + + +int DLLEXPORT anlys_getoption(Handle ph, EN_Option code, double *value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getoption(pr->project, (int)code, value)); +} + +int DLLEXPORT anlys_setoption(Handle ph, EN_Option code, double value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setoption(pr->project, (int)code, value)); +} + +int DLLEXPORT anlys_getflowunits(Handle ph, int *code) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getflowunits(pr->project, code)); +} + +int DLLEXPORT anlys_setflowunits(Handle ph, EN_FlowUnits code) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setflowunits(pr->project, code)); +} + +int DLLEXPORT anlys_gettimeparam(Handle ph, EN_TimeParameter code, long *value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_gettimeparam(pr->project, code, value)); +} + +int DLLEXPORT anlys_settimeparam(Handle ph, EN_TimeParameter code, long value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_settimeparam(pr->project, code, value)); +} + +int DLLEXPORT anlys_getqualinfo(Handle ph, int *qualcode, char *chemname, char *chemunits, int *tracenode) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getqualinfo(pr->project, qualcode, chemname, chemunits, tracenode)); +} + +int DLLEXPORT anlys_getqualtype(Handle ph, int *qualcode, int *tracenode) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getqualtype(pr->project, qualcode, tracenode)); +} + +int DLLEXPORT anlys_setqualtype(Handle ph, EN_QualityType qualcode, char *chemname, char *chemunits, char *tracenode) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setqualtype(pr->project, qualcode, chemname, chemunits, tracenode)); +} + + + + +int DLLEXPORT node_add(Handle ph, char *id, EN_NodeType nodeType) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_addnode(pr->project, id, nodeType)); +} + +int DLLEXPORT node_delete(Handle ph, int index, int actionCode) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_deletenode(pr->project, index, actionCode)); +} + +int DLLEXPORT node_getindex(Handle ph, char *id, int *index) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getnodeindex(pr->project, id, index)); +} + +int DLLEXPORT node_getid(Handle ph, int index, char *id) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getnodeid(pr->project, index, id)); +} + +int DLLEXPORT node_setid(Handle ph, int index, char *newid) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getnodeid(pr->project, index, newid)); +} + +int DLLEXPORT node_gettype(Handle ph, int index, int *code) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getnodetype(pr->project, index, code)); +} + +int DLLEXPORT node_getvalue(Handle ph, int index, EN_NodeProperty code, double *value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getnodevalue(pr->project, index, (int)code, value)); +} + +int DLLEXPORT node_setvalue(Handle ph, int index, EN_NodeProperty code, double value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setnodevalue(pr->project, index, (int)code, value)); +} + +int DLLEXPORT node_getcoord(Handle ph, int index, double *x, double *y) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getcoord(pr->project, index, x, y)); +} + +int DLLEXPORT node_setcoord(Handle ph, int index, double x, double y) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setcoord(pr->project, index, x, y)); +} + + + + +int DLLEXPORT dmnd_getmodel(Handle ph, int *type, double *pmin, double *preq, double *pexp) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getdemandmodel(pr->project, type, pmin, preq, pexp)); +} + +int DLLEXPORT dmnd_setmodel(Handle ph, int type, double pmin, double preq, double pexp) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setdemandmodel(pr->project, type, pmin, preq, pexp)); +} + +int DLLEXPORT dmnd_getcount(Handle ph, int nodeIndex, int *numDemands) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getnumdemands(pr->project, nodeIndex, numDemands)); +} + +int DLLEXPORT dmnd_getbase(Handle ph, int nodeIndex, int demandIndex, double *baseDemand) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getbasedemand(pr->project, nodeIndex, demandIndex, baseDemand)); +} + +int DLLEXPORT dmnd_setbase(Handle ph, int nodeIndex, int demandIndex, double baseDemand) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setbasedemand(pr->project, nodeIndex, demandIndex, baseDemand)); +} + +int DLLEXPORT dmnd_getpattern(Handle ph, int nodeIndex, int demandIndex, int *patIndex) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getdemandpattern(pr->project, nodeIndex, demandIndex, patIndex)); +} + +int DLLEXPORT dmnd_setpattern(Handle ph, int nodeIndex, int demandIndex, int patIndex) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setdemandpattern(pr->project, nodeIndex, demandIndex, patIndex)); +} + +int DLLEXPORT dmnd_getname(Handle ph, int nodeIndex, int demandIdx, char *demandName) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getdemandname(pr->project, nodeIndex, demandIdx, demandName)); +} + +int DLLEXPORT dmnd_setname(Handle ph, int nodeIndex, int demandIdx, char *demandName) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setdemandname(pr->project, nodeIndex, demandIdx, demandName)); +} + + + + +int DLLEXPORT link_add(Handle ph, char *id, EN_LinkType linkType, char *fromNode, char *toNode) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_addlink(pr->project, id, linkType, fromNode, toNode)); +} + +int DLLEXPORT link_delete(Handle ph, int index, int actionCode) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_deletelink(pr->project, index, actionCode)); +} + +int DLLEXPORT link_getindex(Handle ph, char *id, int *index) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getlinkindex(pr->project, id, index)); +} + +int DLLEXPORT link_getid(Handle ph, int index, char *id) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getlinkid(pr->project, index, id)); +} + +int DLLEXPORT link_setid(Handle ph, int index, char *newid) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setlinkid(pr->project, index, newid)); +} + +int DLLEXPORT link_gettype(Handle ph, int index, int *code) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getlinktype(pr->project, index, code)); +} + +int DLLEXPORT link_settype(Handle ph, int *index, EN_LinkType type, int actionCode) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setlinktype(pr->project, index, type, actionCode)); +} + +int DLLEXPORT link_getnodes(Handle ph, int index, int *node1, int *node2) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getlinknodes(pr->project, index, node1, node2)); +} + +int DLLEXPORT link_setnodes(Handle ph, int index, int node1, int node2) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setlinknodes(pr->project, index, node1, node2)); +} + +int DLLEXPORT link_getvalue(Handle ph, int index, EN_LinkProperty code, double *value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getlinkvalue(pr->project, index, code, value)); +} + +int DLLEXPORT link_setvalue(Handle ph, int index, int code, double value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setlinkvalue(pr->project, index, code, value)); +} + + + + +int DLLEXPORT pump_gettype(Handle ph, int linkIndex, int *outType) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getpumptype(pr->project, linkIndex, outType)); +} + +int DLLEXPORT pump_getheadcurveindex(Handle ph, int pumpIndex, int *curveIndex) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getheadcurveindex(pr->project, pumpIndex, curveIndex)); +} + +int DLLEXPORT pump_setheadcurveindex(Handle ph, int pumpIndex, int curveIndex) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setheadcurveindex(pr->project, pumpIndex, curveIndex)); +} + + + + +int DLLEXPORT ptrn_add(Handle ph, char *id) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_addpattern(pr->project, id)); +} + +int DLLEXPORT ptrn_getindex(Handle ph, char *id, int *index) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getpatternindex(pr->project, id, index)); +} + +int DLLEXPORT ptrn_getid(Handle ph, int index, char *id) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getpatternid(pr->project, index, id)); +} + +int DLLEXPORT ptrn_getlength(Handle ph, int index, int *len) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getpatternlen(pr->project, index, len)); +} + +int DLLEXPORT ptrn_getvalue(Handle ph, int index, int period, double *value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getpatternvalue(pr->project, index, period, value)); +} + +int DLLEXPORT ptrn_setvalue(Handle ph, int index, int period, double value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setpatternvalue(pr->project, index, period, value)); +} + +int DLLEXPORT ptrn_getavgvalue(Handle ph, int index, double *value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getaveragepatternvalue(pr->project, index, value)); +} + +int DLLEXPORT ptrn_set(Handle ph, int index, double *values, int len) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setpattern(pr->project, index, values, len)); +} + + + + +int DLLEXPORT curv_add(Handle ph, char *id) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_addcurve(pr->project, id)); +} + +int DLLEXPORT curv_getindex(Handle ph, char *id, int *index) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getcurveindex(pr->project, id, index)); +} + +int DLLEXPORT curv_getid(Handle ph, int index, char *id) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getcurveid(pr->project, index, id)); +} + +int DLLEXPORT curv_getlength(Handle ph, int index, int *len) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getcurvelen(pr->project, index, len)); +} + +int DLLEXPORT curv_gettype(Handle ph, int index, int *type) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getcurvetype(pr->project, index, type)); +} + +int DLLEXPORT curv_getvalue(Handle ph, int curveIndex, int pointIndex, double *x, double *y) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getcurvevalue(pr->project, curveIndex, pointIndex, x, y)); +} + +int DLLEXPORT curv_setvalue(Handle ph, int curveIndex, int pointIndex, double x, double y) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setcurvevalue(pr->project, curveIndex, pointIndex, x, y)); +} + +int DLLEXPORT curv_get(Handle ph, int curveIndex, char* id, int *nValues, double **xValues, double **yValues) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getcurve(pr->project, curveIndex, id, nValues, xValues, yValues)); +} + +int DLLEXPORT curv_set(Handle ph, int index, double *x, double *y, int len) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setcurve(pr->project, index, x, y, len)); +} + + + + +int DLLEXPORT scntl_add(Handle ph, int type, int linkIndex, double setting, int nodeIndex, double level, int *index) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_addcontrol(pr->project, type, linkIndex, setting, nodeIndex, level, index)); +} + +int DLLEXPORT scntl_delete(Handle ph, int index) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_deletecontrol(pr->project, index)); +} + +int DLLEXPORT scntl_get(Handle ph, int controlIndex, int *controlType, int *linkIndex, double *setting, int *nodeIndex, double *level) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getcontrol(pr->project, controlIndex, controlType, linkIndex, setting, nodeIndex, level)); +} + +int DLLEXPORT scntl_set(Handle ph, int cindex, int ctype, int lindex, double setting, int nindex, double level) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setcontrol(pr->project, cindex, ctype, lindex, setting, nindex, level)); +} + + + + +int DLLEXPORT rcntl_add(Handle ph, char *rule) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_addrule(pr->project, rule)); +} + +int DLLEXPORT rcntl_delete(Handle ph, int index) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_deleterule(pr->project, index)); +} + +int DLLEXPORT rcntl_get(Handle ph, int index, int *nPremises, int *nThenActions, int *nElseActions, double *priority) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getrule(pr->project, index, nPremises, nThenActions, nElseActions, priority)); +} + +int DLLEXPORT rcntl_getid(Handle ph, int index, char *id) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getruleID(pr->project, index, id)); +} + +int DLLEXPORT rcntl_getpremise(Handle ph, int ruleIndex, int premiseIndex, int *logop, int *object, int *objIndex, int *variable, int *relop, int *status, double *value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getpremise(pr->project, ruleIndex, premiseIndex, logop, object, objIndex, variable, relop, status, value)); +} + +int DLLEXPORT rcntl_setpremise(Handle ph, int ruleIndex, int premiseIndex, int logop, int object, int objIndex, int variable, int relop, int status, double value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setpremise(pr->project, ruleIndex, premiseIndex, logop, object, objIndex, variable, relop, status, value)); +} + +int DLLEXPORT rcntl_setpremiseindex(Handle ph, int ruleIndex, int premiseIndex, int objIndex) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setpremiseindex(pr->project, ruleIndex, premiseIndex, objIndex)); +} + +int DLLEXPORT rcntl_setpremisestatus(Handle ph, int ruleIndex, int premiseIndex, int status) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setpremisestatus(pr->project, ruleIndex, premiseIndex, status)); +} + +int DLLEXPORT rcntl_setpremisevalue(Handle ph, int ruleIndex, int premiseIndex, double value) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setpremisevalue(pr->project, ruleIndex, premiseIndex, value)); +} + +int DLLEXPORT rcntl_getthenaction(Handle ph, int ruleIndex, int actionIndex, int *linkIndex, int *status, double *setting) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getthenaction(pr->project, ruleIndex, actionIndex, linkIndex, status, setting)); +} + +int DLLEXPORT rcntl_setthenaction(Handle ph, int ruleIndex, int actionIndex, int linkIndex, int status, double setting) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setthenaction(pr->project, ruleIndex, actionIndex, linkIndex, status, setting)); +} + +int DLLEXPORT rcntl_getelseaction(Handle ph, int ruleIndex, int actionIndex, int *linkIndex, int *status, double *setting) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_getelseaction(pr->project, ruleIndex, actionIndex, linkIndex, status, setting)); +} + +int DLLEXPORT rcntl_setelseaction(Handle ph, int ruleIndex, int actionIndex, int linkIndex, int status, double setting) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setelseaction(pr->project, ruleIndex, actionIndex, linkIndex, status, setting)); +} + +int DLLEXPORT rcntl_setrulepriority(Handle ph, int index, double priority) +{ + handle_t *pr = (handle_t *)ph; + return error_set(pr->error, EN_setrulepriority(pr->project, index, priority)); +} + + + + +void DLLEXPORT err_clear(Handle ph) +{ + handle_t *pr = (handle_t *)ph; + error_clear(pr->error); +} + +int DLLEXPORT err_check(Handle ph, char** msg_buffer) +// +// Purpose: Returns the error message or NULL. +// +// Note: Caller must free memory allocated by EN_check_error +// +{ + int errorcode = 0; + char *temp = NULL; + + handle_t *pr = (handle_t *)ph; + + + if (pr == NULL) return -1; + else + { + errorcode = pr->error->error_status; + if (errorcode) + temp = error_check(pr->error); + + *msg_buffer = temp; + } + + return errorcode; +} + +int DLLEXPORT toolkit_getversion(int *version) +{ + return EN_getversion(version); +} + +void DLLEXPORT toolkit_free(void **memory) +{ + free(*memory); + *memory = NULL; +} + +void error_lookup(int errcode, char *dest_msg, int dest_len) +// Purpose: takes error code returns error message +{ + char *msg = NULL; + + switch (errcode) + { + case 1: msg = WARN1; + break; + case 2: msg = WARN2; + break; + case 3: msg = WARN3; + break; + case 4: msg = WARN4; + break; + case 5: msg = WARN5; + break; + case 6: msg = WARN6; + break; + default: + { + char new_msg[MAXMSG + 1]; + msg = geterrmsg(errcode, new_msg); + } + } + strncpy(dest_msg, msg, dest_len); +} diff --git a/src/util/errormanager.c b/src/util/errormanager.c new file mode 100644 index 0000000..3f4f47c --- /dev/null +++ b/src/util/errormanager.c @@ -0,0 +1,74 @@ +//----------------------------------------------------------------------------- +// +// errormanager.c +// +// Purpose: Provides a simple interface for managing runtime error messages. +// +// Date: 08/25/2017 +// +// Author: Michael E. Tryby +// US EPA - ORD/NRMRL +//----------------------------------------------------------------------------- +#include +#include +#include "errormanager.h" + +error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int)) +// +// Purpose: Constructs a new error handle. +// +{ + error_handle_t* error_handle; + error_handle = (error_handle_t*)calloc(1, sizeof(error_handle_t)); + + error_handle->p_msg_lookup = p_error_message; + + return error_handle; +} + +void error_dst_manager(error_handle_t* error_handle) +// +// Purpose: Destroys the error handle. +// +{ + free(error_handle); +} + +int error_set(error_handle_t* error_handle, int errorcode) +// +// Purpose: Sets an error code in the handle. +// +{ + // If the error code is 0 no action is taken and 0 is returned. + // This is a feature not a bug. + if (errorcode) + error_handle->error_status = errorcode; + + return errorcode; +} + +char* error_check(error_handle_t* error_handle) +// +// Purpose: Returns the error message or NULL. +// +// Note: Caller must free memory allocated by check_error +// +{ + char* temp = NULL; + + if (error_handle->error_status != 0) { + temp = (char*) calloc(ERR_MAXMSG, sizeof(char)); + + if (temp) + error_handle->p_msg_lookup(error_handle->error_status, temp, ERR_MAXMSG); + } + return temp; +} + +void error_clear(error_handle_t* error_handle) +// +// Purpose: Clears the error from the handle. +// +{ + error_handle->error_status = 0; +} diff --git a/src/util/errormanager.h b/src/util/errormanager.h new file mode 100644 index 0000000..2a8099f --- /dev/null +++ b/src/util/errormanager.h @@ -0,0 +1,27 @@ +/* + * errormanager.h + * + * Created on: Aug 25, 2017 + * + * Author: Michael E. Tryby + * US EPA - ORD/NRMRL + */ + +#ifndef ERRORMANAGER_H_ +#define ERRORMANAGER_H_ + +#define ERR_MAXMSG 256 + +typedef struct error_s { + int error_status; + void (*p_msg_lookup)(int, char*, int); +} error_handle_t; + +error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int)); +void error_dst_manager(error_handle_t* error_handle); + +int error_set(error_handle_t* error_handle, int errorcode); +char* error_check(error_handle_t* error_handle); +void error_clear(error_handle_t* error_handle); + +#endif /* ERRORMANAGER_H_ */