Skip to content

MSX Functions

These are the methods exposed by EPANET-MSX, the multi-species extension of EPANET. It enables performing simulations of chemical species transport and concentration changes through a network.

The typical sequence for a complete MSX run is:

  1. Load an -msx engine and create a Project.
  2. Open the EPANET project (open) and write the MSX configuration file into the workspace.
  3. Call msxOpen with the path of the MSX file.
  4. Run hydraulics — either msxSolveH for a single run, or msxUseHydFile to reuse a previously saved hydraulics file.
  5. Run quality — either msxSolveQ for a single run, or the msxInitmsxStep loop to step through the simulation.
  6. Read results with msxGetQual, then call msxClose.
FunctionVersionsDescription
msxOpenMSX 2.2+Opens an MSX configuration file and binds it to the EPANET project.
msxCloseMSX 2.2+Releases all memory used by the MSX solver.
msxSolveHMSX 2.2+Solves hydraulics for the EPANET project.
msxUseHydFileMSX 2.2+Re-uses a previously saved hydraulics file.
msxSolveQMSX 2.2+Runs a complete MSX water-quality simulation.
msxInitMSX 2.2+Initializes the MSX solver before stepping through the simulation.
msxStepMSX 2.2+Advances the MSX simulation by one water-quality step.
msxReportMSX 2.2+Writes a formatted MSX report to the project’s report file.
msxSaveOutFileMSX 2.2+Saves the binary MSX output file.
msxSaveMsxFileMSX 2.2+Saves the current MSX configuration to a text file.
msxGetCountMSX 2.2+Returns the number of MSX objects of a given type.
msxGetIndexMSX 2.2+Looks up the index of an MSX object by its ID.
msxGetIdMSX 2.2+Looks up the ID of an MSX object by its index.
msxGetIdLenMSX 2.2+Returns the length of an MSX object’s ID.
msxGetSpeciesMSX 2.2+Returns type, units and tolerances of a chemical species.
msxGetConstantMSX 2.2+Reads the value of an MSX reaction constant.
msxSetConstantMSX 2.2+Writes the value of an MSX reaction constant.
msxGetParameterMSX 2.2+Reads an MSX reaction parameter for a node/link.
msxSetParameterMSX 2.2+Writes an MSX reaction parameter for a node/link.
msxGetInitQualMSX 2.2+Reads the initial concentration of a species at a node or link.
msxSetInitQualMSX 2.2+Writes the initial concentration of a species at a node or link.
msxGetQualMSX 2.2+Reads the current computed concentration of a species at a node or link.
msxGetSourceMSX 2.2+Reads the source definition of a species at a node.
msxSetSourceMSX 2.2+Writes the source definition of a species at a node.
msxAddPatternMSX 2.2+Adds a new MSX time pattern.
msxSetPatternMSX 2.2+Replaces all multipliers of an MSX time pattern.
msxSetPatternValueMSX 2.2+Updates a single period of an MSX time pattern.
msxGetPatternLenMSX 2.2+Returns the number of periods in an MSX time pattern.
msxGetPatternValueMSX 2.2+Returns a single multiplier from an MSX time pattern.

Opens an MSX configuration file and binds it to the underlying EPANET project.

msxOpen(msxFilePath: string): void;

Parameters

ParameterTypeDescription
msxFilePathstringpath of the .msx file inside the workspace filesystem.

open on the EPANET project must have been called first.


Releases all memory used by the MSX solver.

msxClose(): void;

Runs a full hydraulic simulation and saves its results so MSX can consume them. Equivalent to solveH for the MSX solver.

msxSolveH(): void;

Uses a previously saved EPANET hydraulics file as the input to the MSX solver instead of re-running hydraulics.

msxUseHydFile(hydFilePath: string): void;

Parameters

ParameterTypeDescription
hydFilePathstringpath of the hydraulics file inside the workspace.

Runs a complete MSX quality simulation in one call. After it returns, results can be inspected with msxGetQual.

msxSolveQ(): void;

Use the msxInitmsxStep loop instead if you want to read intermediate concentrations.


Initializes the MSX quality solver before stepping through the simulation.

msxInit(saveFlag: number): void;

Parameters

ParameterTypeDescription
saveFlagnumberset to 1 to save intermediate results to the MSX binary file, 0 not.

Advances the MSX simulation by one water-quality step. Returns the current simulation time and the time remaining.

msxStep(): {
t: number;
tleft: number;
};

Returns

Object
PropertyTypeDescription
tnumbercurrent simulation time in seconds.
tleftnumbertime remaining in the simulation, in seconds (0 = done).

Drive a full simulation by calling msxStep in a loop until tleft reaches 0.


Writes a formatted MSX report to the project’s report file.

msxReport(): void;

Saves the MSX binary output file to disk (within the workspace virtual filesystem).

msxSaveOutFile(outFilePath: string): void;

Parameters

ParameterTypeDescription
outFilePathstringpath of the file to create in the workspace.

Saves the current MSX configuration (species, reactions, sources, patterns, …) back out to a text .msx file.

msxSaveMsxFile(msxFilePath: string): void;

Parameters

ParameterTypeDescription
msxFilePathstringpath of the file to create in the workspace.

Returns the number of MSX objects of a given type currently registered with the project.

msxGetCount(type: number): number;

Parameters

ParameterTypeDescription
typenumberone of the MsxConstant codes.

Returns

Number the number of objects of that type.


Looks up the index of an MSX object by its ID name.

msxGetIndex(type: number, id: string): number;

Parameters

ParameterTypeDescription
typenumberone of the MsxConstant codes.
idstringthe object’s ID name.

Looks up the ID name of an MSX object by index.

msxGetId(type: number, index: number): string;

Parameters

ParameterTypeDescription
typenumberone of the MsxConstant codes.
indexnumberthe object’s index (starting from 1).

Returns the length, in characters, of an MSX object’s ID name.

msxGetIdLen(type: number, id: number): number;

Parameters

ParameterTypeDescription
typenumberone of the MsxConstant codes.
idnumberthe object’s index (starting from 1).

Returns metadata about a chemical species.

msxGetSpecies(index: number): {
type: MsxChemicalSpeciesType;
units: string;
aTol: number;
rTol: number;
};

Parameters

ParameterTypeDescription
indexnumberspecies index (starting from 1).

Returns

Object
PropertyTypeDescription
typeMsxChemicalSpeciesTypebulk or wall species (see MsxChemicalSpeciesType).
unitsstringconcentration units.
aTolnumberabsolute concentration tolerance.
rTolnumberrelative concentration tolerance.

Reads the value of an MSX reaction constant.

msxGetConstant(index: number): number;

Parameters

ParameterTypeDescription
indexnumberconstant index (starting from 1).

Writes the value of an MSX reaction constant.

msxSetConstant(index: number, value: number): void;

Parameters

ParameterTypeDescription
indexnumberconstant index (starting from 1).
valuenumbernew value for the constant.

Reads an MSX reaction parameter for a node or link.

msxGetParameter(type: number, index: number, param: number): number;

Parameters

ParameterTypeDescription
typenumbereither MsxConstant.MSX_NODE or MsxConstant.MSX_LINK.
indexnumberthe node or link index.
paramnumberthe parameter index.

Writes an MSX reaction parameter for a node or link.

msxSetParameter(type: number, index: number, param: number, value: number): void;

Parameters

ParameterTypeDescription
typenumbereither MsxConstant.MSX_NODE or MsxConstant.MSX_LINK.
indexnumberthe node or link index.
paramnumberthe parameter index.
valuenumbernew value for the parameter.

Reads the initial concentration of a species at a node or link.

msxGetInitQual(type: number, index: number, species: number): number;

Parameters

ParameterTypeDescription
typenumbereither MsxConstant.MSX_NODE or MsxConstant.MSX_LINK.
indexnumberthe node or link index.
speciesnumberthe species index (starting from 1).

Writes the initial concentration of a species at a node or link.

msxSetInitQual(type: number, index: number, species: number, value: number): void;

Parameters

ParameterTypeDescription
typenumbereither MsxConstant.MSX_NODE or MsxConstant.MSX_LINK.
indexnumberthe node or link index.
speciesnumberthe species index (starting from 1).
valuenumberinitial concentration.

Reads the current computed concentration of a species at a node or link. Call after msxStep or once msxSolveQ has finished.

msxGetQual(type: number, index: number, species: number): number;

Parameters

ParameterTypeDescription
typenumbereither MsxConstant.MSX_NODE or MsxConstant.MSX_LINK.
indexnumberthe node or link index.
speciesnumberthe species index (starting from 1).

Reads the current source definition of a species at a node.

msxGetSource(node: number, species: number): {
type: MsxChemicalSpeciesSourceType;
level: number;
pat: number;
};

Parameters

ParameterTypeDescription
nodenumberthe node index (starting from 1).
speciesnumberthe species index (starting from 1).

Returns

Object
PropertyTypeDescription
typeMsxChemicalSpeciesSourceTypethe source’s kind (see MsxChemicalSpeciesSourceType).
levelnumbersource’s baseline concentration or mass-rate.
patnumberindex of the time pattern that scales the source (0 if none).

Writes the source definition of a species at a node.

msxSetSource(
node: number,
species: number,
type: MsxChemicalSpeciesSourceType,
level: number,
pat: number,
): void;

Parameters

ParameterTypeDescription
nodenumberthe node index (starting from 1).
speciesnumberthe species index (starting from 1).
typeMsxChemicalSpeciesSourceTypethe source kind (see MsxChemicalSpeciesSourceType).
levelnumbersource’s baseline concentration or mass-rate.
patnumberindex of the time pattern that scales the source (0 for none).

Adds a new (empty) MSX time pattern with the given ID.

msxAddPattern(id: string): void;

Parameters

ParameterTypeDescription
idstringthe ID name of the new pattern.

Replaces all multipliers of an existing MSX pattern in one call.

msxSetPattern(pat: number, mult: number[]): void;

Parameters

ParameterTypeDescription
patnumberthe pattern’s index (starting from 1).
multnumber[]the new array of multipliers.

msxSetPatternValue MSX 2.2+

Section titled “msxSetPatternValue  ”

Updates a single period of an MSX time pattern.

msxSetPatternValue(pat: number, period: number, value: number): void;

Parameters

ParameterTypeDescription
patnumberthe pattern’s index (starting from 1).
periodnumberthe period within the pattern (starting from 1).
valuenumbernew multiplier for that period.

Returns the number of periods in an MSX time pattern.

msxGetPatternLen(pat: number): number;

Parameters

ParameterTypeDescription
patnumberthe pattern’s index (starting from 1).

msxGetPatternValue MSX 2.2+

Section titled “msxGetPatternValue  ”

Returns a single multiplier from an MSX time pattern.

msxGetPatternValue(pat: number, period: number): number;

Parameters

ParameterTypeDescription
patnumberthe pattern’s index (starting from 1).
periodnumberthe period within the pattern (starting from 1).