From 05db1e06e572d8c8a8e0c00393b095d678946f10 Mon Sep 17 00:00:00 2001 From: Petros Aristidou Date: Fri, 20 Nov 2026 12:22:10 +0200 Subject: [PATCH 1/2] Raise when a run stops before its STOP time RAMSES ends a run on "Divergence. Simulation stopped" by setting its end-of-simulation flag alone, so ramses() returns 0, the same flag as a run that reached STOP. execSim() took that 0 as success, which is how the reduced GB network passed for three months while its trajectory ended at 0.34 s. execSim() and contSim() now compare getSimTime() with the STOP time of the disturbance file when the engine returns 0, and raise RAMSESError when the run fell short, quoting the engine's last "t = ..." line from the part of the trace file the call wrote. endSim() skips the check, as does $LATENCY early stopping, which the engine logs as "All equipment is latent". A STOP added through addDisturb() moves the time checked against. tests/test_divergence.py reaches the engine's divergence branch on the Nordic case with an unreachable $NEWTON_TOLER, and checks that a run reaching STOP and an endSim() before STOP still pass. --- CLAUDE.md | 9 + src/stepss/cases.py | 27 + src/stepss/simulator.py | 2396 ++++++++++++++++++++------------------ tests/test_divergence.py | 156 +++ 4 files changed, 1427 insertions(+), 1161 deletions(-) create mode 100644 tests/test_divergence.py diff --git a/CLAUDE.md b/CLAUDE.md index e53d539..90fc38f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -128,6 +128,15 @@ against `tests/baselines/nordic_baseline.npz` via `tools/compare_trj.py`. `-1` and a message naming `sim_minmaxvolt`; a run that completes cleanly is the regression. `RAMSESError` is not exported at package level: import it from `stepss.globals`. +- **A 0 flag does not mean the run reached STOP.** The engine ends a run on + `Divergence. Simulation stopped` by setting its end flag alone, so + `ramses()` returns 0 exactly as for a finished run. `execSim()` and + `contSim()` therefore compare `getSimTime()` with the STOP time of the + `.dst` and raise `RAMSESError`, quoting the engine's last `t = ...` line + from the trace file, when the run fell short. `endSim()` skips the check + (ending early is its job), and so does `$LATENCY` early stopping, which the + engine logs as `All equipment is latent`. `tests/test_divergence.py` drives + the engine's divergence branch with an unreachable `$NEWTON_TOLER`. - **Do not call `endSim()` after the trip.** It raises a second, unrelated `RAMSESError` ("Load records") that masks the real result. `obs.trj` is already complete without it. diff --git a/src/stepss/cases.py b/src/stepss/cases.py index 81cf38b..9530552 100644 --- a/src/stepss/cases.py +++ b/src/stepss/cases.py @@ -57,6 +57,33 @@ def _dstProblem(dstfile): 'ends at its STOP time, so add a line such as "240.0 STOP".' % dstfile) +def _dstStopTime(dstfile): + """Return the time of the first STOP record in *dstfile*, or None. + + Reads the file the way :func:`_dstProblem` does. None means there is no + STOP time to compare against (unreadable file, no STOP, or a STOP whose + time is not a number), which callers take as "do not check". + + :param str dstfile: path to the disturbance file. + :rtype: float or None + """ + try: + with open(dstfile, 'r', encoding='utf-8', errors='replace') as records_file: + for line in records_file: + text = line.strip() + if not text or text[0] in '#!': + continue + time, space, description = text.partition(' ') + if space and 'STOP' in description: + try: + return float(time) + except ValueError: + return None + except IOError: + pass + return None + + class cfg(object): """Simulation case configuration. diff --git a/src/stepss/simulator.py b/src/stepss/simulator.py index aed9de4..5b16fdb 100644 --- a/src/stepss/simulator.py +++ b/src/stepss/simulator.py @@ -1,1161 +1,1235 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -"""Python interface to the RAMSES dynamic power-system simulator. - -Provides :class:`sim`, a ctypes-based wrapper around the RAMSES shared library -(``ramses.dll`` on Windows, ``ramses.so`` on Linux and macOS). Function -signatures are read automatically from the bundled ``ramses.h`` C header at -instantiation time, so the wrapper is self-describing and does not hard-code -argument types. -""" - -import ctypes -import _ctypes -import os -import sys -import warnings -import datetime -import numpy as np -from scipy.sparse import coo_matrix - -from .cases import cfg -from .globals import RAMSESError, CustomWarning, __libdir__, __platlibdir__, wrapToList - -class sim(object): - """Interface to a RAMSES solver instance. - - Each :class:`sim` object loads the RAMSES shared library into the current - process and exposes the solver's C API through Python methods. The library - is unloaded (via :func:`ctypes.FreeLibrary` / :func:`ctypes._ctypes.dlclose`) - when the instance is garbage-collected. - - **Thread safety:** RAMSES maintains internal global state; running multiple - :class:`sim` instances concurrently in separate threads is not supported. - Multiple sequential instances within the same process are safe. - - **Class attributes:** - - - ``ramsesCount`` (*int*) — number of :class:`sim` instances currently alive. - Incremented in :meth:`__init__` and decremented in :meth:`__del__`. - """ - - warnings.showwarning = CustomWarning - - ramsesCount = 0 # number of active sim instances - os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" - - def __init__(self, custLibDir = None): - """Load the RAMSES shared library and initialise C function signatures. - - On Windows the library is ``ramses.dll``; on all other platforms - (Linux and macOS) it is ``ramses.so``. By default the library bundled - with the package for the current platform is used - (``src/stepss/libs/win/``, ``libs/lin/`` or ``libs/mac/``). A custom - directory can be supplied via *custLibDir* to override that library — - useful for testing pre-release solver builds; the directory must - contain the library file directly (no platform subfolder). - - After loading the library, :meth:`_setcalls` is invoked to parse - ``ramses.h`` and configure the ctypes argument/return types for every - exported function. - - :param custLibDir: path to a directory containing an alternative - ``ramses.dll`` / ``ramses.so``. The directory must - exist. When provided, the library file in that - location will be locked by the OS until this instance - is deleted. - :type custLibDir: str or None - :raises RAMSESError: if *custLibDir* is given but does not exist or is - not a directory. - :raises ImportError: if the shared library cannot be loaded (e.g. missing - system runtime libraries such as OpenBLAS on Linux). - """ - if custLibDir is None: - ramLibDir = __platlibdir__ - else: - try: - if os.path.exists(custLibDir) and os.path.isdir(custLibDir): - ramLibDir = custLibDir - warnings.warn("Overwriting the internal DLL with the one at %s. The DLL in that location will be locked. To unlock it, you need to del() this instance of stepss.sim() or restart the kernel." % ramLibDir) - else: - raise RAMSESError('RAMSES: The path %s does not exist or it is not a directory.' % (custLibDir)) - except OSError as e: - raise RAMSESError('RAMSES: The path %s gave an error.' % custLibDir) from e - - try: - if sys.platform in ('win32', 'cygwin'): - self._ramseslib = ctypes.CDLL(os.path.join(ramLibDir, "ramses.dll")) - else: - self._ramseslib = ctypes.CDLL(os.path.join(ramLibDir, "ramses.so")) - except OSError as e: - raise ImportError('RAMSES: Could not load shared library from %s.' % ramLibDir) from e - - sim.ramsesCount += 1 - self._ramsesNum = sim.ramsesCount # This is the current instance of Ramses - # How many small-signal analyses this instance has completed. The - # engine retains one state matrix at a time and get_state_matrix - # refuses only an order mismatch, so two runs agreeing on nx, which is - # what re-running one case with changed data produces, would let a - # stale result hand back the newer matrix. A stepss.ssa.Results - # compares against this to refuse that instead. - self._ssaGeneration = 0 - self._setcalls() - - def __del__(self): - """Unload the RAMSES shared library and decrement the instance counter. - - Called automatically by the garbage collector. Releases the OS-level - handle to the DLL/SO so the file can be replaced or deleted afterwards. - Guards against partially-initialised instances (e.g. if ``__init__`` - raised before the library was loaded). - """ - if not hasattr(self, '_ramseslib'): - return - warnings.warn("Simulator with number %i was deleted." % getattr(self, '_ramsesNum', -1)) - if sys.platform in ('win32', 'cygwin'): - _ctypes.FreeLibrary(self._ramseslib._handle) - else: - _ctypes.dlclose(self._ramseslib._handle) - sim.ramsesCount -= 1 - - def _c_func_wrapper(self, cdecl_text): - """Parse a single C function declaration and configure its ctypes binding. - - Reads one line from ``ramses.h``, extracts the return type, function - name, and parameter types, then sets ``restype`` and ``argtypes`` on the - corresponding function object retrieved from ``self._ramseslib``. - - :param str cdecl_text: a single-line C function declaration in the form - `` ( , ...)`` - :raises KeyError: (internally caught) if a C type has no ctypes mapping. - - .. note:: Pointer qualifiers (``*``) attached to the variable name are - moved to the type string before lookup, so both ``char* x`` - and ``char *x`` are handled correctly. - """ - - def move_pointer_and_strip(type_def, name): - """Move any trailing ``*`` from *name* onto *type_def* and strip whitespace.""" - if '*' in name: - type_def += ' ' + name[:name.rindex('*') + 1] - name = name.rsplit('*', 1)[1] - return type_def.strip(), name.strip() - - def type_lookup(type_def): - '''Supported C variable types and their ctypes equivalents.''' - types = { - 'void': None, - 'char *': ctypes.c_char_p, - 'int': ctypes.c_int, - 'int *': ctypes.POINTER(ctypes.c_int), - 'void *': ctypes.c_void_p, - 'size_t': ctypes.c_size_t, - 'size_t *': ctypes.POINTER(ctypes.c_size_t), - 'double': ctypes.c_double, - 'double *': ctypes.POINTER(ctypes.c_double) - } - type_def_without_const = type_def.replace('const ', '') - if type_def_without_const in types: - return types[type_def_without_const] - elif (type_def_without_const.endswith('*') and type_def_without_const[:-1] in types): - return ctypes.POINTER(types[type_def_without_const[:-1]]) - else: - raise KeyError(type_def) - - a, b = [i.strip() for i in cdecl_text.split('(', 1)] - params, _ = b.rsplit(')', 1) - rtn_type, name = move_pointer_and_strip(*a.rsplit(' ', 1)) - param_spec = [] - for param in params.split(','): - if param != 'void': - param_spec.append(move_pointer_and_strip(*param.rsplit(' ', 1))) - - try: - func = getattr(self._ramseslib, name) # get the function from the dll - setattr(func, 'restype', type_lookup(rtn_type)) # set the return type - setattr(func, 'argtypes', [type_lookup(type_def) for type_def, _ in param_spec]) # set the argument types - except AttributeError as e: - warnings.warn('RAMSES: Function %s is listed in ramses.h but cannot be found in the library.' % (name)) - warnings.warn(str(e)) - - def _setcalls(self): - """Parse ``ramses.h`` and configure ctypes bindings for all exported functions. - - Reads the C header file bundled with the package line by line, skipping - blank lines and ``//`` comments, and delegates each declaration to - :meth:`_c_func_wrapper`. - - :raises IOError: if ``ramses.h`` cannot be opened. - """ - try: - with open(os.path.join(__libdir__, "ramses.h"), 'r') as f: - _C_HEADER = f.read() - for cdecl_text in _C_HEADER.splitlines(): - if cdecl_text.strip(): - if not cdecl_text.startswith("//"): - self._c_func_wrapper(cdecl_text) - except IOError as e: - raise IOError("RAMSES: Cannot open ramses.h files", e) - - def getLastErr(self): - """Return the last error message issued by RAMSES. - - :rtype: str - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0) # start simulation paused - >>> ram.getLastErr() - 'This is an error msg' - - """ - - errMsg = ctypes.create_string_buffer(1024) - - try: - retval = self._ramseslib.get_last_err_log(errMsg) - except KeyError: - retval = 1 - - if (retval != 0): - raise RAMSESError('RAMSES: Function getLastErr failed.') - - return errMsg.value.decode() - - def getJac(self): - """Return the system Jacobian matrices written by RAMSES to temporary files. - - Triggers the RAMSES ``get_Jac`` routine, which writes two sparse matrix - files to the current working directory (``py_eqs.dat`` for the structural - incidence matrix *E* and ``py_val.dat`` for the Jacobian values *A*). - Both files are then parsed and returned as SciPy CSC sparse matrices. - - The simulation must have been initialised (i.e. :meth:`execSim` called - with ``pause=0.0``) before this method is called. - - :returns: tuple ``(A, E)`` where *A* is the Jacobian value matrix and - *E* is the structural incidence matrix, both as - :class:`scipy.sparse.csc_matrix` of shape ``(N, N)``. - :rtype: tuple - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0) # start simulation paused - >>> ram.getJac() - - """ - try: - retval = self._ramseslib.get_Jac() - except KeyError: - retval = 1 - - if (retval != 0) and (retval != 112): - raise RAMSESError('RAMSES: Function get_Jac failed.') - - Nmat = 0 - try: - with open('py_eqs.dat', 'r') as f: - lines = f.readlines() - rowl = [] - coll = [] - datal = [] - for x in lines: - xsplit = x.split() - if int(xsplit[0]) > Nmat: - Nmat = int(xsplit[0]) - if int(xsplit[5]) > 0: - rowl.append(int(xsplit[0])-1) - coll.append(int(xsplit[5])-1) - datal.append(1) - row = np.array( rowl, dtype=int ) - col = np.array( coll, dtype=int ) - data = np.array( datal, dtype=float ) - E = coo_matrix((data,(row,col)), shape=(Nmat,Nmat)).tocsc() - except Exception: - raise RAMSESError('RAMSES: Function get_Jac failed while reading E.') - - try: - with open('py_val.dat', 'r') as f: - lines = f.readlines() - row = np.array( [ int(x.split()[0])-1 for x in lines ], dtype=int ) - col = np.array( [ int(x.split()[1])-1 for x in lines ], dtype=int ) - data = np.array( [ x.split()[2] for x in lines ], dtype=float ) - A = coo_matrix((data,(row,col)), shape=(Nmat,Nmat)).tocsc() - except Exception: - raise RAMSESError('RAMSES: Function get_Jac failed while reading A.') - - return A, E - - def _noteSsaAnalysis(self): - """Record that a small-signal analysis has completed on this instance. - - Called by :meth:`runSsa` and by :func:`stepss.ssa.run`'s disturbance - route, which are the two ways an analysis can be triggered. Both - replace the state matrix the engine retains. - """ - self._ssaGeneration += 1 - - def runSsa(self, basename, t=None): - """Run the small-signal analysis and write its three results files. - - The engine linearises about the operating point the run is currently - paused at, reduces the Jacobian to the state matrix, solves the dense - eigenproblem, and writes ``_modes.dat``, ``_pf.dat`` and - ``_ms.dat`` in the working directory. - - The C entry takes a basename and nothing else, so *t* is honoured here: - when it is later than the simulated time already reached, the run is - advanced to it first, with whatever disturbances the case carries. A - *t* that has already passed is refused rather than silently answered - with the current operating point, because the difference is invisible - in the results. - - The analysis needs ``$SCHEME DE`` and ``$OMEGA_REF SYN``. Under - ``$SCHEME IN`` the global Jacobian is algebraized and carries the step - size; under the centre-of-inertia reference frame the COI equations are - computed by finite differences at export time and never enter the - assembled Jacobian. The engine refuses both rather than producing a - plausible, wrong spectrum. :func:`stepss.ssa.run` supplies both itself. - - :param str basename: names the three results files and is written into - the engine's own record, so it may contain only - letters, digits, dot, underscore and hyphen. - :param t: when to linearise, in seconds; defaults to - :data:`stepss.ssa.MIN_TIME`. - :type t: float or None - :returns: *basename*, so the call can be chained. - :rtype: str - :raises RAMSESError: if the basename or the time is rejected, or the - engine refuses the analysis. The engine's own - reason is included, from :meth:`getLastErr`. - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0.0) # initialise and pause at t = 0 - >>> ram.runSsa("ssa") - 'ssa' - """ - from . import ssa as _ssa - - if t is None: - t = _ssa.MIN_TIME - if not _ssa.valid_basename(basename): - raise RAMSESError( - 'RAMSES: the results basename %r cannot be used. It names the ' - 'three results files and is written into the analysis record, ' - 'so it may contain only letters, digits, dot, underscore and ' - 'hyphen.' % (basename,)) - when = _ssa.check_time(t) - now = self.getSimTime() - if when < now - 1e-12: - raise RAMSESError( - 'RAMSES: the analysis time %g s has already passed; the ' - 'simulation is at %g s. The engine linearises about wherever ' - 'the run currently sits, so this would answer a different ' - 'question silently.' % (when, now)) - if when > now: - self.contSim(when) - - try: - retval = self._ramseslib.run_ssa(basename.encode('utf-8')) - except (AttributeError, KeyError): - raise RAMSESError('RAMSES: the bundled library exports no run_ssa; ' - 'small-signal analysis needs RAMSES 3.79 or newer.') - # 112 is ramses() reporting that it paused again after completing the - # request, exactly as get_Jac's return can also be 112. - if (retval != 0) and (retval != 112): - raise RAMSESError('RAMSES: Function runSsa() failed with the flag %i. ' - 'Last message was: %s' % (retval, self.getLastErr())) - self._noteSsaAnalysis() - return basename - - def getStateMatrix(self): - """Return the state matrix of the last small-signal analysis. - - This is the Schur complement the engine formed before solving the - eigenproblem, retained by the library rather than written to any file. - - The engine keeps one at a time, so read this before starting the next - analysis. - - :returns: the matrix, of order ``nstates`` - :rtype: numpy.ndarray - :raises RAMSESError: if no analysis has been run in this process, or - the last one refused. - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0.0) - >>> ram.runSsa("ssa") - 'ssa' - >>> A = ram.getStateMatrix() - >>> A.shape[0] == A.shape[1] - True - """ - nx = ctypes.c_int(0) - try: - self._ramseslib.get_state_matrix_size(ctypes.byref(nx)) - except (AttributeError, KeyError): - raise RAMSESError('RAMSES: the bundled library exports no ' - 'get_state_matrix_size; small-signal analysis ' - 'needs RAMSES 3.79 or newer.') - order = nx.value - if order <= 0: - raise RAMSESError('RAMSES: no small-signal analysis is retained. Run ' - 'one with runSsa() first; a refused analysis ' - 'retains nothing.') - buffer = (ctypes.c_double * (order * order))() - retval = self._ramseslib.get_state_matrix(order, buffer) - if retval != 0: - raise RAMSESError('RAMSES: Function getStateMatrix() failed with the ' - 'flag %i. Last message was: %s' - % (retval, self.getLastErr())) - # Column-major: entry (i, j) of the nx by nx matrix sits at - # a_sys[(j-1)*nx + (i-1)], which is what order='F' reconstructs. The - # copy detaches the array from the ctypes buffer, which is freed with - # this call's frame. - return np.reshape(np.frombuffer(buffer, dtype=np.float64, - count=order * order), - (order, order), order='F').copy() - - def getCompName(self, comp_type, num): - """Return the name of the *num*-th component of the given type. - - :param str comp_type: component type string — one of ``'BUS'``, - ``'SYNC'``, ``'INJ'``, ``'DCTL'``, ``'BRANCH'``, - ``'TWOP'``, ``'SHUNT'``, ``'LOAD'``. - :param int num: 1-based index of the component. - :returns: component name as returned by RAMSES (trailing whitespace stripped). - :rtype: str - :raises RAMSESError: if the component does not exist or the call fails. - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0) # start simulation paused - >>> ram.getCompName("BUS",1) - 'B1' - - """ - get_name_func = { - 'BUS': self._ramseslib.get_bus_name, - 'SYNC': self._ramseslib.get_sync_name, - 'INJ': self._ramseslib.get_inj_name, - 'DCTL': self._ramseslib.get_dctl_name, - 'BRANCH': self._ramseslib.get_branch_name, - 'TWOP': self._ramseslib.get_twop_name, - 'SHUNT': self._ramseslib.get_shunt_name, - 'LOAD': self._ramseslib.get_load_name - } - - name = ctypes.create_string_buffer(21) - - try: - retval = get_name_func[comp_type](num, name) - except KeyError: - retval = 1 - - if (retval != 0): - raise RAMSESError( - 'RAMSES: Function getCompName(%s,%i) failed. Does this component exist?' % (comp_type, num)) - - return name.value.decode() - - def getAllCompNames(self, comp_type): - """Return the names of all components of the given type. - - :param str comp_type: component type string — one of ``'BUS'``, - ``'SYNC'``, ``'INJ'``, ``'DCTL'``, ``'BRANCH'``, - ``'TWOP'``, ``'SHUNT'``, ``'LOAD'``. - :returns: list of component names (empty list if none exist). - :rtype: list of str - :raises RAMSESError: if *comp_type* is not recognised. - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0) # start simulation paused - >>> ram.getAllCompNames("BUS") - ['B1', - 'B2', - 'B3'] - - """ - get_nb_func = { - 'BUS': self._ramseslib.get_nbbus, - 'SYNC': self._ramseslib.get_nbsync, - 'INJ': self._ramseslib.get_nbinj, - 'DCTL': self._ramseslib.get_nbdctl, - 'BRANCH': self._ramseslib.get_nbbra, - 'TWOP': self._ramseslib.get_nbtwop, - 'SHUNT': self._ramseslib.get_nbshunt, - 'LOAD': self._ramseslib.get_nbload - } - - try: - Nb = get_nb_func[comp_type]() - except KeyError: - raise RAMSESError( - 'RAMSES: Function getAllCompName(%s) failed. Does this component type exists?' % (comp_type)) - - names = [] - if Nb > 0: - for comp in range(1, Nb + 1): - names.append(self.getCompName(comp_type, comp)) - return names - - def execSim(self, cmd, pause=None): - """Execute a RAMSES simulation. - - Serialises *cmd* to a RAMSES command file (via :meth:`~stepss.cfg.writeCmdFile`), - then calls the RAMSES solver. If *pause* is supplied, the simulation is - scheduled to stop at that simulated time and can be resumed later with - :meth:`contSim`. Pass ``pause=0.0`` to initialise only (power-flow - solution) without running the dynamic simulation. - - :param cmd: case configuration describing all input and output files. - :type cmd: :class:`stepss.cfg` - :param pause: simulated time (seconds) at which to pause, or ``None`` - to run until the end of the disturbance scenario. - :type pause: float or None - :returns: ``0`` on success. - :rtype: int - :raises TypeError: if *cmd* is not a :class:`stepss.cfg` instance. - :raises RAMSESError: if the solver returns a non-zero, non-112 flag. - - .. note:: Return code ``112`` means the simulation paused normally; it - is treated as success by this method. - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case) # run to end - >>> ram.execSim(case, pause=0.0) # initialise only, then pause - - .. note:: If you have an existing command file, you can load it with - ``stepss.cfg("cmd.txt")`` before passing it here. - """ - if not isinstance(cmd, cfg): - raise TypeError('RAMSES: Function execSim failed because the command file is not of type stepss.cfg()') - if pause is not None: - self.pauseSim(pause) - cmdfilename = cmd.writeCmdFile() - if not cmd._out: - outfilename = os.path.join(os.getcwd(),'output '+datetime.datetime.now().strftime("%d.%m.%Y-%H.%M.%S")+'.trace') - else: - outfilename = cmd._out[0] - retval = self._ramseslib.ramses(cmdfilename.encode('utf-8'), outfilename.encode('utf-8')) - if (retval != 0) and (retval != 112): - raise RAMSESError('RAMSES: Function execSim() failed with the flag %i. Last message was: %s' % (retval, self.getLastErr())) - return 0 - - def contSim(self, pause=None): - """Continue a paused simulation until *pause* seconds of simulated time. - - If *pause* is given, :meth:`pauseSim` is called first to schedule the - next stop. Pass ``self.getInfTime()`` to run until the end of the - disturbance scenario. - - :param pause: simulated time (in seconds) at which to pause again, or - ``None`` to keep the currently scheduled pause time. - :type pause: float or None - :returns: ``0`` on success. - :rtype: int - :raises RAMSESError: if the RAMSES call returns a non-zero, non-112 flag. - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0.0) # initialise and pause at t=0 - >>> ram.contSim(ram.getInfTime()) # run to end of scenario - """ - if pause is not None: - self.pauseSim(pause) - retval = self._ramseslib.continue_simul() - if (retval != 0) and (retval != 112): - raise RAMSESError('RAMSES: Function contSim() failed with the flag %i. Last message was: %s' % (retval, self.getLastErr())) - return 0 - - def getBusVolt(self, busNames): - """Return the voltage magnitude of a list of buses - - :param busNames: the names of buses - :type busNames: list of str - :returns: list of bus voltage magnitudes - :rtype: list of floats - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause - >>> buses = ['g1','g2','g3'] - >>> ram.getBusVolt(buses) - [1.0736851673414456, - 1.0615442362180327, - 1.064702686997689] - """ - volts = [] - bus_volt = ctypes.c_double() - for bus in busNames: - retval = self._ramseslib.get_volt_mag(bus.encode('utf-8'), bus_volt) - if (retval != 0): - raise RAMSESError( - 'RAMSES: Function getBusVolt(%s) failed with the flag %i. Does the bus exist? Last message was: %s' % (bus, retval, self.getLastErr())) - volts.append(bus_volt.value) - return volts - - def getBranchPow(self, branchName): - """Return the active and reactive powers of a list of branches - - :param branchName: the names of branches - :type branchName: list of str - :returns: list of branch powers. These are active and reactive power at the origin and extremity respectively (p_orig, q_orig, p_extr, q_extr) - :rtype: list of floats - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause - >>> branchName = ['1011-1013','1012-1014','1021-1022'] - >>> ram.getBranchPow(branchName) - """ - pows = [] - p_orig = ctypes.c_double() - q_orig = ctypes.c_double() - p_extr = ctypes.c_double() - q_extr = ctypes.c_double() - for branch in branchName: - retval = self._ramseslib.get_line_pow(branch.encode('utf-8'), p_orig, q_orig, p_extr, q_extr) - if (retval != 0): - raise RAMSESError( - 'RAMSES: Function get_line_pow(%s) failed with the flag %i. Does the branch exist? Last message was: %s' % (branch, retval, self.getLastErr())) - thisBranch = [p_orig.value, q_orig.value, p_extr.value, q_extr.value] - pows.append(thisBranch) - return pows - - def getBranchCur(self, branchName): - """Return the currents of a list of branches - - :param branchName: the names of branches - :type branchName: list of str - :returns: list of branch currents. These are x-y components at the origin and extremity respectively (ix_orig, iy_orig, ix_extr, iy_extr) - :rtype: list of floats - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause - >>> branchName = ['1011-1013','1012-1014','1021-1022'] - >>> ram.getBranchCur(branchName) - - """ - curs = [] - ix_orig = ctypes.c_double() - iy_orig = ctypes.c_double() - ix_extr = ctypes.c_double() - iy_extr = ctypes.c_double() - for branch in branchName: - retval = self._ramseslib.get_line_cur(branch.encode('utf-8'), ix_orig, iy_orig, ix_extr, iy_extr) - if (retval != 0): - raise RAMSESError( - 'RAMSES: Function get_line_cur(%s) failed with the flag %i. Does the branch exist? Last message was: %s' % (branch, retval, self.getLastErr())) - thisBranch = [ix_orig.value, iy_orig.value, ix_extr.value, iy_extr.value] - curs.append(thisBranch) - return curs - - def getBusPha(self, busNames): - """Return the voltage phase of a list of buses - - :param busNames: the names of buses - :type busNames: list of str - :returns: list of bus voltage phase - :rtype: list of floats - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause - >>> buses = ['g1','g2','g3'] - >>> ram.getBusPha(buses) - [0.0000000000000000, - 10.0615442362180327, - 11.064702686997689] - - """ - pha = [] - bus_pha = ctypes.c_double() - for bus in busNames: - retval = self._ramseslib.get_volt_pha(bus.encode('utf-8'), bus_pha) - if (retval != 0): - raise RAMSESError( - 'RAMSES: Function getBusPha(%s) failed with the flag %i. Does the bus exist? Last message was: %s' % (bus, retval, self.getLastErr())) - pha.append(bus_pha.value) - return pha - - def endSim(self): - """Signal RAMSES to end the simulation and block until it terminates. - - Sets the internal RAMSES end-of-simulation flag and then calls - :meth:`contSim` with an infinite time horizon so that the solver - flushes its output and cleans up. - - :returns: return value of the underlying :meth:`contSim` call (``0`` on success). - :rtype: int - """ - - self._ramseslib.set_end_simul() - return self.contSim(self.getInfTime()) - - def getEndSim(self): - """Check whether the simulation has ended. - - :returns: ``0`` if the simulation is still running; ``1`` if it has ended. - :rtype: int - """ - - return self._ramseslib.get_end_simul() - - def getSimTime(self): - """Return the current simulated time reported by the RAMSES solver. - - Only meaningful while a simulation is paused (between :meth:`execSim` - and :meth:`contSim` calls). - - :returns: current simulation time in seconds. - :rtype: float - """ - return self._ramseslib.get_sim_time() - - def getInfTime(self): - """Return the largest representable double recognised by the RAMSES solver. - - Passing this value to :meth:`contSim` or :meth:`execSim` instructs - RAMSES to run until the end of the disturbance scenario. - - :returns: maximum double value used internally by RAMSES as a sentinel - for "simulate to the end". - :rtype: float - """ - return self._ramseslib.get_huge_double() - - def initObserv(self, traj_filenm): - """Initialise the runtime observable recording system. - - Must be called after :meth:`execSim` (with ``pause=0.0``) and before - :meth:`addObserv` / :meth:`finalObserv`. Sets the path of the output - trajectory file and prepares the internal observable structures. - - :param str traj_filenm: path of the ``.trj`` file where recorded - timeseries will be written. - :returns: return value from the underlying RAMSES call (non-zero on error). - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") # command file without any observables - >>> ram.execSim(case, 0.0) # start - >>> traj_filenm = 'obs.trj' - >>> ram.initObserv(traj_filenm) - """ - - return self._ramseslib.initObserv(traj_filenm.encode('utf-8')) - - def addObserv(self, string): - """Register one observable element for runtime recording. - - Must be called after :meth:`initObserv` and before :meth:`finalObserv`. - Accepts RAMSES observable selector strings such as ``'BUS *'`` (all - buses) or ``'SYNC g1'`` (a specific generator). - - :param str string: observable selector in RAMSES format. - :returns: return value from the underlying RAMSES call (non-zero on error). - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") # command file without any observables - >>> ram.execSim(case, 0.0) # start - >>> traj_filenm = 'obs.trj' - >>> ram.initObserv(traj_filenm) - >>> string = 'BUS *' # monitor all buses - >>> ram.addObserv(string) - """ - - return self._ramseslib.addObserv(string.encode('utf-8')) - - def finalObserv(self): - """Finalise observable selection, allocate recording buffers, and write the trajectory file header. - - Must be called after all :meth:`addObserv` calls. RAMSES will write - timeseries data to the ``.trj`` file specified in :meth:`initObserv` - as the simulation advances. - - :returns: return value from the underlying RAMSES call (non-zero on error). - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") # command file without any observables - >>> ram.execSim(case, 0.0) # start - >>> traj_filenm = 'obs.trj' - >>> ram.initObserv(traj_filenm) - >>> string = 'BUS *' # monitor all buses - >>> ram.addObserv(string) - >>> ram.finalObserv() - """ - - return self._ramseslib.finalObserv() - - def getPrm(self, comp_type, comp_name, prm_name): - """Get the value of a named parameter - - :param comp_type: the types of components (EXC, TOR, INJ, DCTL, TWOP) - :type comp_type: list of str - :param comp_name: the names of components - :type comp_name: list of str - :param prm_name: the names of parameters - :type prm_name: list of str - - :returns: list of parameter values - :rtype: list of floats - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause - >>> comp_type = ['EXC','EXC','EXC'] - >>> comp_name = ['g1','g2','g3'] - >>> prm_name = ['V0','V0','V0'] - >>> ram.getPrm(comp_type,comp_name, prm_name) - [1.0736851673414456, - 1.0615442362180327, - 1.064702686997689] - - """ - - comp_type = wrapToList(comp_type) - comp_name = wrapToList(comp_name) - prm_name = wrapToList(prm_name) - - if not (len(comp_type) == len(comp_name) == len(prm_name)): - raise ValueError('RAMSES: Function getPrm failed because the lists are not equal!') - prm_values = [] - for a, b, c in zip(comp_type, comp_name, prm_name): - prm_value = ctypes.c_double() - retval = self._ramseslib.get_named_prm(a.encode('utf-8'), b.encode('utf-8'), c.encode('utf-8'), - prm_value) - if retval != 0: - raise RAMSESError( - 'RAMSES: Function getPrm(%s,%s,%s) failed with the flag %i. Does the parameter or equipment exist? Last message was: %s' - % (a, b, c, retval, self.getLastErr())) - prm_values.append(prm_value.value) - if len(prm_values) == 1: - return prm_values[0] - else: - return prm_values - - def defineSS(self, ssID, filter1, filter2, filter3): - """Define a subsytem using three filters. The resulting list is an intersection of the filters. - - :param ssID: Number of the SS - :type ssID: int - :param filter1: Voltage levels to be included - :type filter1: list of str or str - :param filter2: Zones (which zone/zones will be included) - :type filter2: list of str or str - :param filter3: Bus names to be included - :type filter3: list of str or str - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0.0) - >>> ram.defineSS(1, ['735'], [], []) # SS 1 with all buses at 735 kV, no zones, no list of buses - - :raises RAMSESError: if *ssID*, *location*, or *in_service* are not valid, or the RAMSES call fails. - :returns: ``None`` - - .. note:: An empty filter means it is deactivated and discarded. - - """ - filter1 = wrapToList(filter1) - filter2 = wrapToList(filter2) - filter3 = wrapToList(filter3) - - strF1 = ' '.join(filter1) - strF2 = ' '.join(filter2) - strF3 = ' '.join(filter3) - - retval = self._ramseslib.define_SS(ctypes.c_int(ssID), strF1.encode('utf-8'), ctypes.c_int(len(filter1)), - strF2.encode('utf-8'), ctypes.c_int(len(filter2)), strF3.encode('utf-8'), - ctypes.c_int(len(filter3))) - if retval != 0: - raise RAMSESError('RAMSES: Function define_SS(%i,...) failed with the flag %i. Last message was: %s' % (ssID, retval, self.getLastErr())) - - def getSS(self, ssID): - """Retrieve the buses of a subsytem. - - :param ssID: Number of the SS - :type ssID: int - - :returns: list of buses - :rtype: list of str - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0.0) - >>> ram.defineSS(1, ['735'], [], []) # SS 1 with all buses at 735 kV - >>> ram.getSS(1) # get list of buses in SS 1 - - """ - - mxreclen = self._ramseslib.get_nbbus() - string_buffer = ctypes.create_string_buffer(19 * mxreclen) - - retval = self._ramseslib.get_SS(ctypes.c_int(ssID), ctypes.c_int(mxreclen), string_buffer) - if retval != 0: - raise RAMSESError('RAMSES: Function getSS(%i) failed with the flag %i. Last message was: %s' % (ssID, retval, self.getLastErr())) - - return string_buffer.value.split() - - def getTrfoSS(self, ssID, location, in_service, rettype): - """Retrieve transformer information of subsystem after applying some filters - - :param ssID: Number of the SS - :type ssID: int - :param location: 1 – both buses inside SS, 2 - tie transformers, 3 – 1 & 2 - :type location: int - :param in_service: 1 – transformers in service, 2 - all transformers - :type in_service: int - :param rettype: Type of response (NAME, From, To, Status, Tap, Currentf, Currentt, Pf, Qf, Pt, Qt). - :type rettype: str - - :returns: list of transformer names - :rtype: list of str - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0.0) - >>> ram.defineSS(1, ['735'], [], []) # SS 1 with all buses at 735 kV - >>> ram.getTrfoSS(1,3,2,'Status') - - .. note:: Tap is not implemented yet. - - .. todo:: Implement Taps after discussing with Lester. - """ - - if rettype not in ['NAME', 'From', 'To', 'Status', 'Tap', 'Currentf', 'Currentt', 'Pf', 'Qf', 'Pt', 'Qt']: - raise RAMSESError('RAMSES: Function getTrfoSS(%i,%i,%i,%s): rettype is not valid!' % ( - ssID, location, in_service, rettype)) - elif location not in [1, 2, 3]: - raise RAMSESError('RAMSES: Function getTrfoSS(%i,%i,%i,%s): location is not valid!' % ( - ssID, location, in_service, rettype)) - elif in_service not in [1, 2]: - raise RAMSESError('RAMSES: Function getTrfoSS(%i,%i,%i,%s): in_service is not valid!' % ( - ssID, location, in_service, rettype)) - - mxreclen = self._ramseslib.get_nbbra() - string_buffer = ctypes.create_string_buffer(21 * mxreclen) - dp_vec = (ctypes.c_double * mxreclen)() - dp_int = (ctypes.c_int * mxreclen)() - elem = ctypes.c_int() - - retval = self._ramseslib.get_transformer_in_SS(ctypes.c_int(ssID), ctypes.c_int(location), - ctypes.c_int(in_service), rettype.encode('utf-8'), - ctypes.c_int(mxreclen), string_buffer, dp_vec, dp_int, elem) - if retval != 0: - raise RAMSESError('RAMSES: Function getTrfoSS(%i,%i,%i,%s) failed with the flag %i.' % ( - ssID, location, in_service, rettype, retval)) - - if elem == 0: - return [] - elif rettype in ['Tap', 'Status']: - return [dp_int[i] for i in range(0, elem.value)] - elif rettype in ['Currentf', 'Currentt', 'Pf', 'Qf', 'Pt', 'Qt']: - return [dp_vec[i] for i in range(0, elem.value)] - else: - return string_buffer.value.split() - - def getPrmNames(self, comp_type, comp_name): - """Get the named parameters of a model - - :param comp_type: the types of components (EXC, TOR, INJ, DCTL, TWOP) - :type comp_type: list of str or str - :param comp_name: the names of component instances - :type comp_name: list of str or str - - :returns: list of parameter names - :rtype: list of lists of strings - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 0.0) # initialize and wait - >>> comp_type = ['EXC','EXC','EXC'] - >>> comp_name = ['g1','g2','g3'] # name of synchronous machines - >>> ram.getPrmNames(comp_type,comp_name) - - """ - comp_type = wrapToList(comp_type) - comp_name = wrapToList(comp_name) - - if not (len(comp_type) == len(comp_name)): - raise ValueError('RAMSES: Function getPrmNames failed because the lists are not equal!') - prm_names = [] - mxprm = self._ramseslib.get_mxprm() # get maximum number of parameters in any model - for a, b in zip(comp_type, comp_name): - string_buffer = ctypes.create_string_buffer(11 * mxprm) - retval = self._ramseslib.get_comp_prm_names(a.encode('utf-8'), b.encode('utf-8'), ctypes.c_int(mxprm), - string_buffer) - if retval != 0: - raise RAMSESError( - 'RAMSES: Function getPrm(%s,%s) failed with the flag %i. Does the parameter or equipment exist?' - % (a, b, retval)) - decodedstring = string_buffer.value.decode() - prm_names.append(decodedstring.split()) - if len(prm_names) == 1: - return prm_names[0] - else: - return prm_names - - def getObs(self, comp_type, comp_name, obs_name): - """Get the value of a named observable. - - :param comp_type: the types of components ('EXC','TOR','INJ','TWOP','DCTL','SYN') - :type comp_type: list of str - :param comp_name: the names of components - :type comp_name: list of str - :param obs_name: the names of observables - :type obs_name: list of str - - :returns: list of observable values - :rtype: list of floats - - .. note:: - - For the synchronous generator ('SYN') the accepted obs_name values are - - 'P': Active power (MW) - - 'Q': Reactive power (MVAr) - - ``'Omega'``: Machine speed (pu) - - 'S': Apparent power (MVA) - - 'SNOM': Nominal apparent power (MVA) - - 'PNOM': Nominal active power (MW) - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause - >>> comp_type = ['INJ','EXC','TOR'] - >>> comp_name = ['L_11','g2','g3'] - >>> obs_name = ['P','vf','Pm'] - >>> ram.getObs(comp_type,comp_name, obs_name) - [199.73704732259995, - 1.3372945282585218, - 0.816671075203357] - - """ - comp_type = wrapToList(comp_type) - comp_name = wrapToList(comp_name) - obs_name = wrapToList(obs_name) - - if not (len(comp_type) == len(comp_name) == len(obs_name)): - raise ValueError('RAMSES: Function getObs failed because the lists are not equal!') - obs_values = [] - for a, b, c in zip(comp_type, comp_name, obs_name): - obs_value = ctypes.c_double() - retval = self._ramseslib.get_named_obs(a.encode('utf-8'), b.encode('utf-8'), c.encode('utf-8'), - obs_value) - if retval != 0: - raise RAMSESError( - 'RAMSES: Function getObs(%s,%s,%s) failed with the flag %i. Does the observable or equipment exist?' - % (a, b, c, retval)) - obs_values.append(obs_value.value) - return obs_values - - def pauseSim(self, t_pause): - """Schedule a pause at *t_pause* seconds of simulated time. - - The pause is registered in the solver's internal state and takes effect - during the next :meth:`execSim` or :meth:`contSim` call. - - :param float t_pause: simulated time (seconds) at which to pause. - :returns: return value from the underlying RAMSES call (non-zero on error). - :rtype: int - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.pauseSim(10.0) - >>> ram.execSim(case) # will pause at t=10 s - """ - return self._ramseslib.set_pause_time(t_pause) - - def addDisturb(self, t_dist, disturb): - """Add a new disturbance at a specific time. Follows the same structure as the disturbances in the dst files. - - :param t_dist: time of the disturbance - :type t_dist: float - :param disturb: description of disturbance - :type disturb: str - - :Example: - - >>> import stepss - >>> ram = stepss.sim() - >>> case = stepss.cfg("cmd.txt") - >>> ram.execSim(case, 80.0) # simulate until 80 seconds and pause - >>> ram.addDisturb(100.000, 'CHGPRM DCTL 1-1041 Vsetpt -0.05 0') # Decrease the setpoint of the DCTL by 0.015 pu, at t=100 s - >>> ram.addDisturb(100.000, 'CHGPRM DCTL 2-1042 Vsetpt -0.05 0') - >>> ram.addDisturb(100.000, 'CHGPRM DCTL 3-1043 Vsetpt -0.05 0') - >>> ram.addDisturb(100.000, 'CHGPRM DCTL 4-1044 Vsetpt -0.05 0') - >>> ram.addDisturb(100.000, 'CHGPRM DCTL 5-1045 Vsetpt -0.05 0') - >>> ram.contSim(ram.getInfTime()) # continue the simulation - """ - return self._ramseslib.add_disturb(t_dist, disturb.encode('utf-8')) +#!/usr/bin/env python +# -*- coding: utf-8 -*- +"""Python interface to the RAMSES dynamic power-system simulator. + +Provides :class:`sim`, a ctypes-based wrapper around the RAMSES shared library +(``ramses.dll`` on Windows, ``ramses.so`` on Linux and macOS). Function +signatures are read automatically from the bundled ``ramses.h`` C header at +instantiation time, so the wrapper is self-describing and does not hard-code +argument types. +""" + +import ctypes +import _ctypes +import os +import sys +import warnings +import datetime +import numpy as np +from scipy.sparse import coo_matrix + +from .cases import cfg, _dstStopTime +from .globals import RAMSESError, CustomWarning, __libdir__, __platlibdir__, wrapToList + +class sim(object): + """Interface to a RAMSES solver instance. + + Each :class:`sim` object loads the RAMSES shared library into the current + process and exposes the solver's C API through Python methods. The library + is unloaded (via :func:`ctypes.FreeLibrary` / :func:`ctypes._ctypes.dlclose`) + when the instance is garbage-collected. + + **Thread safety:** RAMSES maintains internal global state; running multiple + :class:`sim` instances concurrently in separate threads is not supported. + Multiple sequential instances within the same process are safe. + + **Class attributes:** + + - ``ramsesCount`` (*int*) — number of :class:`sim` instances currently alive. + Incremented in :meth:`__init__` and decremented in :meth:`__del__`. + """ + + warnings.showwarning = CustomWarning + + ramsesCount = 0 # number of active sim instances + os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" + + def __init__(self, custLibDir = None): + """Load the RAMSES shared library and initialise C function signatures. + + On Windows the library is ``ramses.dll``; on all other platforms + (Linux and macOS) it is ``ramses.so``. By default the library bundled + with the package for the current platform is used + (``src/stepss/libs/win/``, ``libs/lin/`` or ``libs/mac/``). A custom + directory can be supplied via *custLibDir* to override that library — + useful for testing pre-release solver builds; the directory must + contain the library file directly (no platform subfolder). + + After loading the library, :meth:`_setcalls` is invoked to parse + ``ramses.h`` and configure the ctypes argument/return types for every + exported function. + + :param custLibDir: path to a directory containing an alternative + ``ramses.dll`` / ``ramses.so``. The directory must + exist. When provided, the library file in that + location will be locked by the OS until this instance + is deleted. + :type custLibDir: str or None + :raises RAMSESError: if *custLibDir* is given but does not exist or is + not a directory. + :raises ImportError: if the shared library cannot be loaded (e.g. missing + system runtime libraries such as OpenBLAS on Linux). + """ + if custLibDir is None: + ramLibDir = __platlibdir__ + else: + try: + if os.path.exists(custLibDir) and os.path.isdir(custLibDir): + ramLibDir = custLibDir + warnings.warn("Overwriting the internal DLL with the one at %s. The DLL in that location will be locked. To unlock it, you need to del() this instance of stepss.sim() or restart the kernel." % ramLibDir) + else: + raise RAMSESError('RAMSES: The path %s does not exist or it is not a directory.' % (custLibDir)) + except OSError as e: + raise RAMSESError('RAMSES: The path %s gave an error.' % custLibDir) from e + + try: + if sys.platform in ('win32', 'cygwin'): + self._ramseslib = ctypes.CDLL(os.path.join(ramLibDir, "ramses.dll")) + else: + self._ramseslib = ctypes.CDLL(os.path.join(ramLibDir, "ramses.so")) + except OSError as e: + raise ImportError('RAMSES: Could not load shared library from %s.' % ramLibDir) from e + + sim.ramsesCount += 1 + self._ramsesNum = sim.ramsesCount # This is the current instance of Ramses + # How many small-signal analyses this instance has completed. The + # engine retains one state matrix at a time and get_state_matrix + # refuses only an order mismatch, so two runs agreeing on nx, which is + # what re-running one case with changed data produces, would let a + # stale result hand back the newer matrix. A stepss.ssa.Results + # compares against this to refuse that instead. + self._ssaGeneration = 0 + # What a run must reach to count as finished: the STOP time of the + # case's disturbance file, and the trace file the engine logs to (its + # log unit is opened in append mode on that file). See _checkReachedStop. + self._stopTime = None + self._traceFile = None + self._setcalls() + + def __del__(self): + """Unload the RAMSES shared library and decrement the instance counter. + + Called automatically by the garbage collector. Releases the OS-level + handle to the DLL/SO so the file can be replaced or deleted afterwards. + Guards against partially-initialised instances (e.g. if ``__init__`` + raised before the library was loaded). + """ + if not hasattr(self, '_ramseslib'): + return + warnings.warn("Simulator with number %i was deleted." % getattr(self, '_ramsesNum', -1)) + if sys.platform in ('win32', 'cygwin'): + _ctypes.FreeLibrary(self._ramseslib._handle) + else: + _ctypes.dlclose(self._ramseslib._handle) + sim.ramsesCount -= 1 + + def _c_func_wrapper(self, cdecl_text): + """Parse a single C function declaration and configure its ctypes binding. + + Reads one line from ``ramses.h``, extracts the return type, function + name, and parameter types, then sets ``restype`` and ``argtypes`` on the + corresponding function object retrieved from ``self._ramseslib``. + + :param str cdecl_text: a single-line C function declaration in the form + `` ( , ...)`` + :raises KeyError: (internally caught) if a C type has no ctypes mapping. + + .. note:: Pointer qualifiers (``*``) attached to the variable name are + moved to the type string before lookup, so both ``char* x`` + and ``char *x`` are handled correctly. + """ + + def move_pointer_and_strip(type_def, name): + """Move any trailing ``*`` from *name* onto *type_def* and strip whitespace.""" + if '*' in name: + type_def += ' ' + name[:name.rindex('*') + 1] + name = name.rsplit('*', 1)[1] + return type_def.strip(), name.strip() + + def type_lookup(type_def): + '''Supported C variable types and their ctypes equivalents.''' + types = { + 'void': None, + 'char *': ctypes.c_char_p, + 'int': ctypes.c_int, + 'int *': ctypes.POINTER(ctypes.c_int), + 'void *': ctypes.c_void_p, + 'size_t': ctypes.c_size_t, + 'size_t *': ctypes.POINTER(ctypes.c_size_t), + 'double': ctypes.c_double, + 'double *': ctypes.POINTER(ctypes.c_double) + } + type_def_without_const = type_def.replace('const ', '') + if type_def_without_const in types: + return types[type_def_without_const] + elif (type_def_without_const.endswith('*') and type_def_without_const[:-1] in types): + return ctypes.POINTER(types[type_def_without_const[:-1]]) + else: + raise KeyError(type_def) + + a, b = [i.strip() for i in cdecl_text.split('(', 1)] + params, _ = b.rsplit(')', 1) + rtn_type, name = move_pointer_and_strip(*a.rsplit(' ', 1)) + param_spec = [] + for param in params.split(','): + if param != 'void': + param_spec.append(move_pointer_and_strip(*param.rsplit(' ', 1))) + + try: + func = getattr(self._ramseslib, name) # get the function from the dll + setattr(func, 'restype', type_lookup(rtn_type)) # set the return type + setattr(func, 'argtypes', [type_lookup(type_def) for type_def, _ in param_spec]) # set the argument types + except AttributeError as e: + warnings.warn('RAMSES: Function %s is listed in ramses.h but cannot be found in the library.' % (name)) + warnings.warn(str(e)) + + def _setcalls(self): + """Parse ``ramses.h`` and configure ctypes bindings for all exported functions. + + Reads the C header file bundled with the package line by line, skipping + blank lines and ``//`` comments, and delegates each declaration to + :meth:`_c_func_wrapper`. + + :raises IOError: if ``ramses.h`` cannot be opened. + """ + try: + with open(os.path.join(__libdir__, "ramses.h"), 'r') as f: + _C_HEADER = f.read() + for cdecl_text in _C_HEADER.splitlines(): + if cdecl_text.strip(): + if not cdecl_text.startswith("//"): + self._c_func_wrapper(cdecl_text) + except IOError as e: + raise IOError("RAMSES: Cannot open ramses.h files", e) + + def getLastErr(self): + """Return the last error message issued by RAMSES. + + :rtype: str + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0) # start simulation paused + >>> ram.getLastErr() + 'This is an error msg' + + """ + + errMsg = ctypes.create_string_buffer(1024) + + try: + retval = self._ramseslib.get_last_err_log(errMsg) + except KeyError: + retval = 1 + + if (retval != 0): + raise RAMSESError('RAMSES: Function getLastErr failed.') + + return errMsg.value.decode() + + def getJac(self): + """Return the system Jacobian matrices written by RAMSES to temporary files. + + Triggers the RAMSES ``get_Jac`` routine, which writes two sparse matrix + files to the current working directory (``py_eqs.dat`` for the structural + incidence matrix *E* and ``py_val.dat`` for the Jacobian values *A*). + Both files are then parsed and returned as SciPy CSC sparse matrices. + + The simulation must have been initialised (i.e. :meth:`execSim` called + with ``pause=0.0``) before this method is called. + + :returns: tuple ``(A, E)`` where *A* is the Jacobian value matrix and + *E* is the structural incidence matrix, both as + :class:`scipy.sparse.csc_matrix` of shape ``(N, N)``. + :rtype: tuple + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0) # start simulation paused + >>> ram.getJac() + + """ + try: + retval = self._ramseslib.get_Jac() + except KeyError: + retval = 1 + + if (retval != 0) and (retval != 112): + raise RAMSESError('RAMSES: Function get_Jac failed.') + + Nmat = 0 + try: + with open('py_eqs.dat', 'r') as f: + lines = f.readlines() + rowl = [] + coll = [] + datal = [] + for x in lines: + xsplit = x.split() + if int(xsplit[0]) > Nmat: + Nmat = int(xsplit[0]) + if int(xsplit[5]) > 0: + rowl.append(int(xsplit[0])-1) + coll.append(int(xsplit[5])-1) + datal.append(1) + row = np.array( rowl, dtype=int ) + col = np.array( coll, dtype=int ) + data = np.array( datal, dtype=float ) + E = coo_matrix((data,(row,col)), shape=(Nmat,Nmat)).tocsc() + except Exception: + raise RAMSESError('RAMSES: Function get_Jac failed while reading E.') + + try: + with open('py_val.dat', 'r') as f: + lines = f.readlines() + row = np.array( [ int(x.split()[0])-1 for x in lines ], dtype=int ) + col = np.array( [ int(x.split()[1])-1 for x in lines ], dtype=int ) + data = np.array( [ x.split()[2] for x in lines ], dtype=float ) + A = coo_matrix((data,(row,col)), shape=(Nmat,Nmat)).tocsc() + except Exception: + raise RAMSESError('RAMSES: Function get_Jac failed while reading A.') + + return A, E + + def _noteSsaAnalysis(self): + """Record that a small-signal analysis has completed on this instance. + + Called by :meth:`runSsa` and by :func:`stepss.ssa.run`'s disturbance + route, which are the two ways an analysis can be triggered. Both + replace the state matrix the engine retains. + """ + self._ssaGeneration += 1 + + def runSsa(self, basename, t=None): + """Run the small-signal analysis and write its three results files. + + The engine linearises about the operating point the run is currently + paused at, reduces the Jacobian to the state matrix, solves the dense + eigenproblem, and writes ``_modes.dat``, ``_pf.dat`` and + ``_ms.dat`` in the working directory. + + The C entry takes a basename and nothing else, so *t* is honoured here: + when it is later than the simulated time already reached, the run is + advanced to it first, with whatever disturbances the case carries. A + *t* that has already passed is refused rather than silently answered + with the current operating point, because the difference is invisible + in the results. + + The analysis needs ``$SCHEME DE`` and ``$OMEGA_REF SYN``. Under + ``$SCHEME IN`` the global Jacobian is algebraized and carries the step + size; under the centre-of-inertia reference frame the COI equations are + computed by finite differences at export time and never enter the + assembled Jacobian. The engine refuses both rather than producing a + plausible, wrong spectrum. :func:`stepss.ssa.run` supplies both itself. + + :param str basename: names the three results files and is written into + the engine's own record, so it may contain only + letters, digits, dot, underscore and hyphen. + :param t: when to linearise, in seconds; defaults to + :data:`stepss.ssa.MIN_TIME`. + :type t: float or None + :returns: *basename*, so the call can be chained. + :rtype: str + :raises RAMSESError: if the basename or the time is rejected, or the + engine refuses the analysis. The engine's own + reason is included, from :meth:`getLastErr`. + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0.0) # initialise and pause at t = 0 + >>> ram.runSsa("ssa") + 'ssa' + """ + from . import ssa as _ssa + + if t is None: + t = _ssa.MIN_TIME + if not _ssa.valid_basename(basename): + raise RAMSESError( + 'RAMSES: the results basename %r cannot be used. It names the ' + 'three results files and is written into the analysis record, ' + 'so it may contain only letters, digits, dot, underscore and ' + 'hyphen.' % (basename,)) + when = _ssa.check_time(t) + now = self.getSimTime() + if when < now - 1e-12: + raise RAMSESError( + 'RAMSES: the analysis time %g s has already passed; the ' + 'simulation is at %g s. The engine linearises about wherever ' + 'the run currently sits, so this would answer a different ' + 'question silently.' % (when, now)) + if when > now: + self.contSim(when) + + try: + retval = self._ramseslib.run_ssa(basename.encode('utf-8')) + except (AttributeError, KeyError): + raise RAMSESError('RAMSES: the bundled library exports no run_ssa; ' + 'small-signal analysis needs RAMSES 3.79 or newer.') + # 112 is ramses() reporting that it paused again after completing the + # request, exactly as get_Jac's return can also be 112. + if (retval != 0) and (retval != 112): + raise RAMSESError('RAMSES: Function runSsa() failed with the flag %i. ' + 'Last message was: %s' % (retval, self.getLastErr())) + self._noteSsaAnalysis() + return basename + + def getStateMatrix(self): + """Return the state matrix of the last small-signal analysis. + + This is the Schur complement the engine formed before solving the + eigenproblem, retained by the library rather than written to any file. + + The engine keeps one at a time, so read this before starting the next + analysis. + + :returns: the matrix, of order ``nstates`` + :rtype: numpy.ndarray + :raises RAMSESError: if no analysis has been run in this process, or + the last one refused. + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0.0) + >>> ram.runSsa("ssa") + 'ssa' + >>> A = ram.getStateMatrix() + >>> A.shape[0] == A.shape[1] + True + """ + nx = ctypes.c_int(0) + try: + self._ramseslib.get_state_matrix_size(ctypes.byref(nx)) + except (AttributeError, KeyError): + raise RAMSESError('RAMSES: the bundled library exports no ' + 'get_state_matrix_size; small-signal analysis ' + 'needs RAMSES 3.79 or newer.') + order = nx.value + if order <= 0: + raise RAMSESError('RAMSES: no small-signal analysis is retained. Run ' + 'one with runSsa() first; a refused analysis ' + 'retains nothing.') + buffer = (ctypes.c_double * (order * order))() + retval = self._ramseslib.get_state_matrix(order, buffer) + if retval != 0: + raise RAMSESError('RAMSES: Function getStateMatrix() failed with the ' + 'flag %i. Last message was: %s' + % (retval, self.getLastErr())) + # Column-major: entry (i, j) of the nx by nx matrix sits at + # a_sys[(j-1)*nx + (i-1)], which is what order='F' reconstructs. The + # copy detaches the array from the ctypes buffer, which is freed with + # this call's frame. + return np.reshape(np.frombuffer(buffer, dtype=np.float64, + count=order * order), + (order, order), order='F').copy() + + def getCompName(self, comp_type, num): + """Return the name of the *num*-th component of the given type. + + :param str comp_type: component type string — one of ``'BUS'``, + ``'SYNC'``, ``'INJ'``, ``'DCTL'``, ``'BRANCH'``, + ``'TWOP'``, ``'SHUNT'``, ``'LOAD'``. + :param int num: 1-based index of the component. + :returns: component name as returned by RAMSES (trailing whitespace stripped). + :rtype: str + :raises RAMSESError: if the component does not exist or the call fails. + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0) # start simulation paused + >>> ram.getCompName("BUS",1) + 'B1' + + """ + get_name_func = { + 'BUS': self._ramseslib.get_bus_name, + 'SYNC': self._ramseslib.get_sync_name, + 'INJ': self._ramseslib.get_inj_name, + 'DCTL': self._ramseslib.get_dctl_name, + 'BRANCH': self._ramseslib.get_branch_name, + 'TWOP': self._ramseslib.get_twop_name, + 'SHUNT': self._ramseslib.get_shunt_name, + 'LOAD': self._ramseslib.get_load_name + } + + name = ctypes.create_string_buffer(21) + + try: + retval = get_name_func[comp_type](num, name) + except KeyError: + retval = 1 + + if (retval != 0): + raise RAMSESError( + 'RAMSES: Function getCompName(%s,%i) failed. Does this component exist?' % (comp_type, num)) + + return name.value.decode() + + def getAllCompNames(self, comp_type): + """Return the names of all components of the given type. + + :param str comp_type: component type string — one of ``'BUS'``, + ``'SYNC'``, ``'INJ'``, ``'DCTL'``, ``'BRANCH'``, + ``'TWOP'``, ``'SHUNT'``, ``'LOAD'``. + :returns: list of component names (empty list if none exist). + :rtype: list of str + :raises RAMSESError: if *comp_type* is not recognised. + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0) # start simulation paused + >>> ram.getAllCompNames("BUS") + ['B1', + 'B2', + 'B3'] + + """ + get_nb_func = { + 'BUS': self._ramseslib.get_nbbus, + 'SYNC': self._ramseslib.get_nbsync, + 'INJ': self._ramseslib.get_nbinj, + 'DCTL': self._ramseslib.get_nbdctl, + 'BRANCH': self._ramseslib.get_nbbra, + 'TWOP': self._ramseslib.get_nbtwop, + 'SHUNT': self._ramseslib.get_nbshunt, + 'LOAD': self._ramseslib.get_nbload + } + + try: + Nb = get_nb_func[comp_type]() + except KeyError: + raise RAMSESError( + 'RAMSES: Function getAllCompName(%s) failed. Does this component type exists?' % (comp_type)) + + names = [] + if Nb > 0: + for comp in range(1, Nb + 1): + names.append(self.getCompName(comp_type, comp)) + return names + + def execSim(self, cmd, pause=None): + """Execute a RAMSES simulation. + + Serialises *cmd* to a RAMSES command file (via :meth:`~stepss.cfg.writeCmdFile`), + then calls the RAMSES solver. If *pause* is supplied, the simulation is + scheduled to stop at that simulated time and can be resumed later with + :meth:`contSim`. Pass ``pause=0.0`` to initialise only (power-flow + solution) without running the dynamic simulation. + + :param cmd: case configuration describing all input and output files. + :type cmd: :class:`stepss.cfg` + :param pause: simulated time (seconds) at which to pause, or ``None`` + to run until the end of the disturbance scenario. + :type pause: float or None + :returns: ``0`` on success. + :rtype: int + :raises TypeError: if *cmd* is not a :class:`stepss.cfg` instance. + :raises RAMSESError: if the solver returns a non-zero, non-112 flag, + or if it returns 0 but the simulation stopped + before the STOP time of the disturbance file + (for example on ``Divergence. Simulation + stopped``, which the engine does not flag). + + .. note:: Return code ``112`` means the simulation paused normally; it + is treated as success by this method. + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case) # run to end + >>> ram.execSim(case, pause=0.0) # initialise only, then pause + + .. note:: If you have an existing command file, you can load it with + ``stepss.cfg("cmd.txt")`` before passing it here. + """ + if not isinstance(cmd, cfg): + raise TypeError('RAMSES: Function execSim failed because the command file is not of type stepss.cfg()') + if pause is not None: + self.pauseSim(pause) + cmdfilename = cmd.writeCmdFile() + if not cmd._out: + outfilename = os.path.join(os.getcwd(),'output '+datetime.datetime.now().strftime("%d.%m.%Y-%H.%M.%S")+'.trace') + else: + outfilename = cmd._out[0] + self._stopTime = _dstStopTime(cmd._dstset[0]) + self._traceFile = outfilename + traceStart = self._traceSize() + retval = self._ramseslib.ramses(cmdfilename.encode('utf-8'), outfilename.encode('utf-8')) + if (retval != 0) and (retval != 112): + raise RAMSESError('RAMSES: Function execSim() failed with the flag %i. Last message was: %s' % (retval, self.getLastErr())) + if retval == 0: + self._checkReachedStop('execSim', traceStart) + return 0 + + def _traceSize(self): + """Current size of the trace file in bytes, or 0 if it cannot be read.""" + try: + return os.path.getsize(self._traceFile) + except (OSError, TypeError): + return 0 + + def _checkReachedStop(self, caller, traceStart): + """Raise :class:`RAMSESError` if a run that returned 0 ended before STOP. + + The engine ends a run on ``Divergence. Simulation stopped`` (Newton + failure at the minimum time step), and on a few other numerical + failures, by setting its end-of-simulation flag alone: ``ramses()`` + then returns 0, the same flag as a run that reached STOP. The + simulated time is what tells them apart. The engine's own reason is + only written to its log, so it is quoted from the part of the trace + file this call added. + + The engine also ends a run early on purpose when ``$LATENCY`` early + stopping finds every device latent, and says so in the log; that is + not a failure and is let through. + + :param str caller: name of the public method, for the message. + :param int traceStart: size of the trace file before the call. + """ + if self._stopTime is None: + return + reached = self.getSimTime() + if reached >= self._stopTime - 1e-9 * max(1.0, abs(self._stopTime)): + return + logged = '' + try: + with open(self._traceFile, 'r', encoding='utf-8', errors='replace') as trace: + trace.seek(traceStart) + logged = trace.read() + except (OSError, TypeError): + pass + if 'All equipment is latent' in logged: + return + reasons = [' '.join(line.split()) for line in logged.splitlines() + if line.strip().startswith('t =')] + reason = reasons[-1] if reasons else 'none in %s' % self._traceFile + raise RAMSESError('RAMSES: Function %s() returned without an error flag, but the ' + 'simulation stopped at t = %.4f s, before the STOP time %.4f s. ' + 'The engine reported: %s' + % (caller, reached, self._stopTime, reason)) + + def contSim(self, pause=None): + """Continue a paused simulation until *pause* seconds of simulated time. + + If *pause* is given, :meth:`pauseSim` is called first to schedule the + next stop. Pass ``self.getInfTime()`` to run until the end of the + disturbance scenario. + + :param pause: simulated time (in seconds) at which to pause again, or + ``None`` to keep the currently scheduled pause time. + :type pause: float or None + :returns: ``0`` on success. + :rtype: int + :raises RAMSESError: if the RAMSES call returns a non-zero, non-112 flag, + or returns 0 with the simulation stopped before + the STOP time (see :meth:`execSim`). + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0.0) # initialise and pause at t=0 + >>> ram.contSim(ram.getInfTime()) # run to end of scenario + """ + return self._contSim(pause, checkStop=True) + + def _contSim(self, pause, checkStop): + """Body of :meth:`contSim`; :meth:`endSim` passes ``checkStop=False``.""" + if pause is not None: + self.pauseSim(pause) + traceStart = self._traceSize() + retval = self._ramseslib.continue_simul() + if (retval != 0) and (retval != 112): + raise RAMSESError('RAMSES: Function contSim() failed with the flag %i. Last message was: %s' % (retval, self.getLastErr())) + if retval == 0 and checkStop: + self._checkReachedStop('contSim', traceStart) + return 0 + + def getBusVolt(self, busNames): + """Return the voltage magnitude of a list of buses + + :param busNames: the names of buses + :type busNames: list of str + :returns: list of bus voltage magnitudes + :rtype: list of floats + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause + >>> buses = ['g1','g2','g3'] + >>> ram.getBusVolt(buses) + [1.0736851673414456, + 1.0615442362180327, + 1.064702686997689] + """ + volts = [] + bus_volt = ctypes.c_double() + for bus in busNames: + retval = self._ramseslib.get_volt_mag(bus.encode('utf-8'), bus_volt) + if (retval != 0): + raise RAMSESError( + 'RAMSES: Function getBusVolt(%s) failed with the flag %i. Does the bus exist? Last message was: %s' % (bus, retval, self.getLastErr())) + volts.append(bus_volt.value) + return volts + + def getBranchPow(self, branchName): + """Return the active and reactive powers of a list of branches + + :param branchName: the names of branches + :type branchName: list of str + :returns: list of branch powers. These are active and reactive power at the origin and extremity respectively (p_orig, q_orig, p_extr, q_extr) + :rtype: list of floats + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause + >>> branchName = ['1011-1013','1012-1014','1021-1022'] + >>> ram.getBranchPow(branchName) + """ + pows = [] + p_orig = ctypes.c_double() + q_orig = ctypes.c_double() + p_extr = ctypes.c_double() + q_extr = ctypes.c_double() + for branch in branchName: + retval = self._ramseslib.get_line_pow(branch.encode('utf-8'), p_orig, q_orig, p_extr, q_extr) + if (retval != 0): + raise RAMSESError( + 'RAMSES: Function get_line_pow(%s) failed with the flag %i. Does the branch exist? Last message was: %s' % (branch, retval, self.getLastErr())) + thisBranch = [p_orig.value, q_orig.value, p_extr.value, q_extr.value] + pows.append(thisBranch) + return pows + + def getBranchCur(self, branchName): + """Return the currents of a list of branches + + :param branchName: the names of branches + :type branchName: list of str + :returns: list of branch currents. These are x-y components at the origin and extremity respectively (ix_orig, iy_orig, ix_extr, iy_extr) + :rtype: list of floats + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause + >>> branchName = ['1011-1013','1012-1014','1021-1022'] + >>> ram.getBranchCur(branchName) + + """ + curs = [] + ix_orig = ctypes.c_double() + iy_orig = ctypes.c_double() + ix_extr = ctypes.c_double() + iy_extr = ctypes.c_double() + for branch in branchName: + retval = self._ramseslib.get_line_cur(branch.encode('utf-8'), ix_orig, iy_orig, ix_extr, iy_extr) + if (retval != 0): + raise RAMSESError( + 'RAMSES: Function get_line_cur(%s) failed with the flag %i. Does the branch exist? Last message was: %s' % (branch, retval, self.getLastErr())) + thisBranch = [ix_orig.value, iy_orig.value, ix_extr.value, iy_extr.value] + curs.append(thisBranch) + return curs + + def getBusPha(self, busNames): + """Return the voltage phase of a list of buses + + :param busNames: the names of buses + :type busNames: list of str + :returns: list of bus voltage phase + :rtype: list of floats + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause + >>> buses = ['g1','g2','g3'] + >>> ram.getBusPha(buses) + [0.0000000000000000, + 10.0615442362180327, + 11.064702686997689] + + """ + pha = [] + bus_pha = ctypes.c_double() + for bus in busNames: + retval = self._ramseslib.get_volt_pha(bus.encode('utf-8'), bus_pha) + if (retval != 0): + raise RAMSESError( + 'RAMSES: Function getBusPha(%s) failed with the flag %i. Does the bus exist? Last message was: %s' % (bus, retval, self.getLastErr())) + pha.append(bus_pha.value) + return pha + + def endSim(self): + """Signal RAMSES to end the simulation and block until it terminates. + + Sets the internal RAMSES end-of-simulation flag and then calls + :meth:`contSim` with an infinite time horizon so that the solver + flushes its output and cleans up. + + :returns: return value of the underlying :meth:`contSim` call (``0`` on success). + :rtype: int + """ + + # Ending before STOP is the point here, so the STOP check is skipped. + self._ramseslib.set_end_simul() + return self._contSim(self.getInfTime(), checkStop=False) + + def getEndSim(self): + """Check whether the simulation has ended. + + :returns: ``0`` if the simulation is still running; ``1`` if it has ended. + :rtype: int + """ + + return self._ramseslib.get_end_simul() + + def getSimTime(self): + """Return the current simulated time reported by the RAMSES solver. + + Only meaningful while a simulation is paused (between :meth:`execSim` + and :meth:`contSim` calls). + + :returns: current simulation time in seconds. + :rtype: float + """ + return self._ramseslib.get_sim_time() + + def getInfTime(self): + """Return the largest representable double recognised by the RAMSES solver. + + Passing this value to :meth:`contSim` or :meth:`execSim` instructs + RAMSES to run until the end of the disturbance scenario. + + :returns: maximum double value used internally by RAMSES as a sentinel + for "simulate to the end". + :rtype: float + """ + return self._ramseslib.get_huge_double() + + def initObserv(self, traj_filenm): + """Initialise the runtime observable recording system. + + Must be called after :meth:`execSim` (with ``pause=0.0``) and before + :meth:`addObserv` / :meth:`finalObserv`. Sets the path of the output + trajectory file and prepares the internal observable structures. + + :param str traj_filenm: path of the ``.trj`` file where recorded + timeseries will be written. + :returns: return value from the underlying RAMSES call (non-zero on error). + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") # command file without any observables + >>> ram.execSim(case, 0.0) # start + >>> traj_filenm = 'obs.trj' + >>> ram.initObserv(traj_filenm) + """ + + return self._ramseslib.initObserv(traj_filenm.encode('utf-8')) + + def addObserv(self, string): + """Register one observable element for runtime recording. + + Must be called after :meth:`initObserv` and before :meth:`finalObserv`. + Accepts RAMSES observable selector strings such as ``'BUS *'`` (all + buses) or ``'SYNC g1'`` (a specific generator). + + :param str string: observable selector in RAMSES format. + :returns: return value from the underlying RAMSES call (non-zero on error). + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") # command file without any observables + >>> ram.execSim(case, 0.0) # start + >>> traj_filenm = 'obs.trj' + >>> ram.initObserv(traj_filenm) + >>> string = 'BUS *' # monitor all buses + >>> ram.addObserv(string) + """ + + return self._ramseslib.addObserv(string.encode('utf-8')) + + def finalObserv(self): + """Finalise observable selection, allocate recording buffers, and write the trajectory file header. + + Must be called after all :meth:`addObserv` calls. RAMSES will write + timeseries data to the ``.trj`` file specified in :meth:`initObserv` + as the simulation advances. + + :returns: return value from the underlying RAMSES call (non-zero on error). + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") # command file without any observables + >>> ram.execSim(case, 0.0) # start + >>> traj_filenm = 'obs.trj' + >>> ram.initObserv(traj_filenm) + >>> string = 'BUS *' # monitor all buses + >>> ram.addObserv(string) + >>> ram.finalObserv() + """ + + return self._ramseslib.finalObserv() + + def getPrm(self, comp_type, comp_name, prm_name): + """Get the value of a named parameter + + :param comp_type: the types of components (EXC, TOR, INJ, DCTL, TWOP) + :type comp_type: list of str + :param comp_name: the names of components + :type comp_name: list of str + :param prm_name: the names of parameters + :type prm_name: list of str + + :returns: list of parameter values + :rtype: list of floats + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause + >>> comp_type = ['EXC','EXC','EXC'] + >>> comp_name = ['g1','g2','g3'] + >>> prm_name = ['V0','V0','V0'] + >>> ram.getPrm(comp_type,comp_name, prm_name) + [1.0736851673414456, + 1.0615442362180327, + 1.064702686997689] + + """ + + comp_type = wrapToList(comp_type) + comp_name = wrapToList(comp_name) + prm_name = wrapToList(prm_name) + + if not (len(comp_type) == len(comp_name) == len(prm_name)): + raise ValueError('RAMSES: Function getPrm failed because the lists are not equal!') + prm_values = [] + for a, b, c in zip(comp_type, comp_name, prm_name): + prm_value = ctypes.c_double() + retval = self._ramseslib.get_named_prm(a.encode('utf-8'), b.encode('utf-8'), c.encode('utf-8'), + prm_value) + if retval != 0: + raise RAMSESError( + 'RAMSES: Function getPrm(%s,%s,%s) failed with the flag %i. Does the parameter or equipment exist? Last message was: %s' + % (a, b, c, retval, self.getLastErr())) + prm_values.append(prm_value.value) + if len(prm_values) == 1: + return prm_values[0] + else: + return prm_values + + def defineSS(self, ssID, filter1, filter2, filter3): + """Define a subsytem using three filters. The resulting list is an intersection of the filters. + + :param ssID: Number of the SS + :type ssID: int + :param filter1: Voltage levels to be included + :type filter1: list of str or str + :param filter2: Zones (which zone/zones will be included) + :type filter2: list of str or str + :param filter3: Bus names to be included + :type filter3: list of str or str + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0.0) + >>> ram.defineSS(1, ['735'], [], []) # SS 1 with all buses at 735 kV, no zones, no list of buses + + :raises RAMSESError: if *ssID*, *location*, or *in_service* are not valid, or the RAMSES call fails. + :returns: ``None`` + + .. note:: An empty filter means it is deactivated and discarded. + + """ + filter1 = wrapToList(filter1) + filter2 = wrapToList(filter2) + filter3 = wrapToList(filter3) + + strF1 = ' '.join(filter1) + strF2 = ' '.join(filter2) + strF3 = ' '.join(filter3) + + retval = self._ramseslib.define_SS(ctypes.c_int(ssID), strF1.encode('utf-8'), ctypes.c_int(len(filter1)), + strF2.encode('utf-8'), ctypes.c_int(len(filter2)), strF3.encode('utf-8'), + ctypes.c_int(len(filter3))) + if retval != 0: + raise RAMSESError('RAMSES: Function define_SS(%i,...) failed with the flag %i. Last message was: %s' % (ssID, retval, self.getLastErr())) + + def getSS(self, ssID): + """Retrieve the buses of a subsytem. + + :param ssID: Number of the SS + :type ssID: int + + :returns: list of buses + :rtype: list of str + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0.0) + >>> ram.defineSS(1, ['735'], [], []) # SS 1 with all buses at 735 kV + >>> ram.getSS(1) # get list of buses in SS 1 + + """ + + mxreclen = self._ramseslib.get_nbbus() + string_buffer = ctypes.create_string_buffer(19 * mxreclen) + + retval = self._ramseslib.get_SS(ctypes.c_int(ssID), ctypes.c_int(mxreclen), string_buffer) + if retval != 0: + raise RAMSESError('RAMSES: Function getSS(%i) failed with the flag %i. Last message was: %s' % (ssID, retval, self.getLastErr())) + + return string_buffer.value.split() + + def getTrfoSS(self, ssID, location, in_service, rettype): + """Retrieve transformer information of subsystem after applying some filters + + :param ssID: Number of the SS + :type ssID: int + :param location: 1 – both buses inside SS, 2 - tie transformers, 3 – 1 & 2 + :type location: int + :param in_service: 1 – transformers in service, 2 - all transformers + :type in_service: int + :param rettype: Type of response (NAME, From, To, Status, Tap, Currentf, Currentt, Pf, Qf, Pt, Qt). + :type rettype: str + + :returns: list of transformer names + :rtype: list of str + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0.0) + >>> ram.defineSS(1, ['735'], [], []) # SS 1 with all buses at 735 kV + >>> ram.getTrfoSS(1,3,2,'Status') + + .. note:: Tap is not implemented yet. + + .. todo:: Implement Taps after discussing with Lester. + """ + + if rettype not in ['NAME', 'From', 'To', 'Status', 'Tap', 'Currentf', 'Currentt', 'Pf', 'Qf', 'Pt', 'Qt']: + raise RAMSESError('RAMSES: Function getTrfoSS(%i,%i,%i,%s): rettype is not valid!' % ( + ssID, location, in_service, rettype)) + elif location not in [1, 2, 3]: + raise RAMSESError('RAMSES: Function getTrfoSS(%i,%i,%i,%s): location is not valid!' % ( + ssID, location, in_service, rettype)) + elif in_service not in [1, 2]: + raise RAMSESError('RAMSES: Function getTrfoSS(%i,%i,%i,%s): in_service is not valid!' % ( + ssID, location, in_service, rettype)) + + mxreclen = self._ramseslib.get_nbbra() + string_buffer = ctypes.create_string_buffer(21 * mxreclen) + dp_vec = (ctypes.c_double * mxreclen)() + dp_int = (ctypes.c_int * mxreclen)() + elem = ctypes.c_int() + + retval = self._ramseslib.get_transformer_in_SS(ctypes.c_int(ssID), ctypes.c_int(location), + ctypes.c_int(in_service), rettype.encode('utf-8'), + ctypes.c_int(mxreclen), string_buffer, dp_vec, dp_int, elem) + if retval != 0: + raise RAMSESError('RAMSES: Function getTrfoSS(%i,%i,%i,%s) failed with the flag %i.' % ( + ssID, location, in_service, rettype, retval)) + + if elem == 0: + return [] + elif rettype in ['Tap', 'Status']: + return [dp_int[i] for i in range(0, elem.value)] + elif rettype in ['Currentf', 'Currentt', 'Pf', 'Qf', 'Pt', 'Qt']: + return [dp_vec[i] for i in range(0, elem.value)] + else: + return string_buffer.value.split() + + def getPrmNames(self, comp_type, comp_name): + """Get the named parameters of a model + + :param comp_type: the types of components (EXC, TOR, INJ, DCTL, TWOP) + :type comp_type: list of str or str + :param comp_name: the names of component instances + :type comp_name: list of str or str + + :returns: list of parameter names + :rtype: list of lists of strings + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 0.0) # initialize and wait + >>> comp_type = ['EXC','EXC','EXC'] + >>> comp_name = ['g1','g2','g3'] # name of synchronous machines + >>> ram.getPrmNames(comp_type,comp_name) + + """ + comp_type = wrapToList(comp_type) + comp_name = wrapToList(comp_name) + + if not (len(comp_type) == len(comp_name)): + raise ValueError('RAMSES: Function getPrmNames failed because the lists are not equal!') + prm_names = [] + mxprm = self._ramseslib.get_mxprm() # get maximum number of parameters in any model + for a, b in zip(comp_type, comp_name): + string_buffer = ctypes.create_string_buffer(11 * mxprm) + retval = self._ramseslib.get_comp_prm_names(a.encode('utf-8'), b.encode('utf-8'), ctypes.c_int(mxprm), + string_buffer) + if retval != 0: + raise RAMSESError( + 'RAMSES: Function getPrm(%s,%s) failed with the flag %i. Does the parameter or equipment exist?' + % (a, b, retval)) + decodedstring = string_buffer.value.decode() + prm_names.append(decodedstring.split()) + if len(prm_names) == 1: + return prm_names[0] + else: + return prm_names + + def getObs(self, comp_type, comp_name, obs_name): + """Get the value of a named observable. + + :param comp_type: the types of components ('EXC','TOR','INJ','TWOP','DCTL','SYN') + :type comp_type: list of str + :param comp_name: the names of components + :type comp_name: list of str + :param obs_name: the names of observables + :type obs_name: list of str + + :returns: list of observable values + :rtype: list of floats + + .. note:: + + For the synchronous generator ('SYN') the accepted obs_name values are + - 'P': Active power (MW) + - 'Q': Reactive power (MVAr) + - ``'Omega'``: Machine speed (pu) + - 'S': Apparent power (MVA) + - 'SNOM': Nominal apparent power (MVA) + - 'PNOM': Nominal active power (MW) + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 10.0) # simulate until 10 seconds and pause + >>> comp_type = ['INJ','EXC','TOR'] + >>> comp_name = ['L_11','g2','g3'] + >>> obs_name = ['P','vf','Pm'] + >>> ram.getObs(comp_type,comp_name, obs_name) + [199.73704732259995, + 1.3372945282585218, + 0.816671075203357] + + """ + comp_type = wrapToList(comp_type) + comp_name = wrapToList(comp_name) + obs_name = wrapToList(obs_name) + + if not (len(comp_type) == len(comp_name) == len(obs_name)): + raise ValueError('RAMSES: Function getObs failed because the lists are not equal!') + obs_values = [] + for a, b, c in zip(comp_type, comp_name, obs_name): + obs_value = ctypes.c_double() + retval = self._ramseslib.get_named_obs(a.encode('utf-8'), b.encode('utf-8'), c.encode('utf-8'), + obs_value) + if retval != 0: + raise RAMSESError( + 'RAMSES: Function getObs(%s,%s,%s) failed with the flag %i. Does the observable or equipment exist?' + % (a, b, c, retval)) + obs_values.append(obs_value.value) + return obs_values + + def pauseSim(self, t_pause): + """Schedule a pause at *t_pause* seconds of simulated time. + + The pause is registered in the solver's internal state and takes effect + during the next :meth:`execSim` or :meth:`contSim` call. + + :param float t_pause: simulated time (seconds) at which to pause. + :returns: return value from the underlying RAMSES call (non-zero on error). + :rtype: int + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.pauseSim(10.0) + >>> ram.execSim(case) # will pause at t=10 s + """ + return self._ramseslib.set_pause_time(t_pause) + + def addDisturb(self, t_dist, disturb): + """Add a new disturbance at a specific time. Follows the same structure as the disturbances in the dst files. + + :param t_dist: time of the disturbance + :type t_dist: float + :param disturb: description of disturbance + :type disturb: str + + :Example: + + >>> import stepss + >>> ram = stepss.sim() + >>> case = stepss.cfg("cmd.txt") + >>> ram.execSim(case, 80.0) # simulate until 80 seconds and pause + >>> ram.addDisturb(100.000, 'CHGPRM DCTL 1-1041 Vsetpt -0.05 0') # Decrease the setpoint of the DCTL by 0.015 pu, at t=100 s + >>> ram.addDisturb(100.000, 'CHGPRM DCTL 2-1042 Vsetpt -0.05 0') + >>> ram.addDisturb(100.000, 'CHGPRM DCTL 3-1043 Vsetpt -0.05 0') + >>> ram.addDisturb(100.000, 'CHGPRM DCTL 4-1044 Vsetpt -0.05 0') + >>> ram.addDisturb(100.000, 'CHGPRM DCTL 5-1045 Vsetpt -0.05 0') + >>> ram.contSim(ram.getInfTime()) # continue the simulation + """ + if 'STOP' in disturb and (self._stopTime is None or t_dist < self._stopTime): + # A STOP added at run time moves the end the run is checked against. + self._stopTime = float(t_dist) + return self._ramseslib.add_disturb(t_dist, disturb.encode('utf-8')) diff --git a/tests/test_divergence.py b/tests/test_divergence.py new file mode 100644 index 0000000..37c97fa --- /dev/null +++ b/tests/test_divergence.py @@ -0,0 +1,156 @@ +"""A run that stops short of its STOP time must not pass as a finished one. + +RAMSES ends a run on "Divergence. Simulation stopped" (Newton failed at the +minimum time step) by setting its end-of-simulation flag and nothing else, so +``ramses()`` returns 0, the same flag as a run that reached STOP. ``execSim()`` +used to read that 0 as success: a reduced GB network diverged at t = 0.35 s +and its notebook reported it as running for three months, because the +trajectory simply ended there. + +The diverging case below is the Nordic test system with a Newton tolerance no +iteration can meet, so the very first time step fails at the minimum step size. +That is a reliable way to reach the engine's divergence branch, which is what +is being tested, without shipping a case that diverges for physical reasons. + +Every run happens in its own subprocess: RAMSES keeps its state in Fortran +module variables shared by every sim() in a process (see test_live_engine.py). +""" + +import json +import os +import shutil +import subprocess +import sys +import textwrap +from pathlib import Path + +import pytest + +REPO_ROOT = Path(__file__).resolve().parents[1] +CASE_DIR = REPO_ROOT / "tests" / "data" / "nordic" + +STOP = 3.0 + +FAULT_CLEARED = """ 0.000 CONTINUE SOLVER BD 0.020 0.001 0. ABL + 1.000 FAULT BUS 4032 0. 0.8 + 1.100 CLEAR BUS 4032 + 3.000 STOP +""" + +UNREACHABLE_TOLERANCE = "$NEWTON_TOLER 1e-15 1e-15 1e-15 ;\n" + +DRIVER = textwrap.dedent( + """ + import json + import os + import sys + import stepss + from stepss.globals import RAMSESError + + mode = sys.argv[1] + case = stepss.cfg() + for name in ("dyn_A.dat", "volt_rat_A.dat", "settings1.dat"): + case.addData(name) + if os.path.exists("extra.dat"): + case.addData("extra.dat") + case.addObs("obs.dat") + case.addDst("case.dst") + case.addTrj("obs.trj") + case.addOut("output.trace") + + ram = stepss.sim() + outcome = {"raised": None, "returned": None} + try: + if mode == "run": + outcome["returned"] = ram.execSim(case) + else: # pause part-way, then end the run on purpose + ram.execSim(case, 1.0) + outcome["returned"] = ram.endSim() + except RAMSESError as exc: + outcome["raised"] = str(exc) + outcome["sim_time"] = ram.getSimTime() + json.dump(outcome, open("outcome.json", "w")) + """ +) + + +def run_case(tmp_path, dst_text, extra=None, mode="run"): + """Run the Nordic case with *dst_text* in a fresh process; return its outcome.""" + for src in sorted(CASE_DIR.glob("*.dat")): + shutil.copy(src, tmp_path / src.name) + (tmp_path / "case.dst").write_text(dst_text) + if extra is not None: + (tmp_path / "extra.dat").write_text(extra) + (tmp_path / "driver.py").write_text(DRIVER) + + # Import the stepss this session imported (the wheel under CI), as + # test_live_engine.py does. + import stepss + + env = dict(os.environ) + env["MPLBACKEND"] = "Agg" + env["PYTHONPATH"] = os.pathsep.join( + [str(Path(stepss.__file__).resolve().parents[1])] + + ([env["PYTHONPATH"]] if "PYTHONPATH" in env else [])) + done = subprocess.run([sys.executable, "driver.py", mode], cwd=tmp_path, + env=env, capture_output=True, text=True) + assert done.returncode == 0, done.stdout + done.stderr + return json.loads((tmp_path / "outcome.json").read_text()) + + +# -- against the engine --------------------------------------------------- + +def test_a_diverging_run_raises(tmp_path): + outcome = run_case(tmp_path, FAULT_CLEARED, extra=UNREACHABLE_TOLERANCE) + assert outcome["sim_time"] < STOP # the engine really did stop short + assert outcome["raised"] is not None, outcome + assert "Divergence" in outcome["raised"] # the engine's own words + assert "3.0000" in outcome["raised"] # and the STOP it fell short of + + +def test_a_run_that_reaches_stop_does_not_raise(tmp_path): + outcome = run_case(tmp_path, FAULT_CLEARED) + assert outcome["raised"] is None, outcome["raised"] + assert outcome["returned"] == 0 + assert outcome["sim_time"] == pytest.approx(STOP) + + +def test_ending_a_paused_run_on_purpose_does_not_raise(tmp_path): + """endSim() stops before STOP by design; that is not a failure.""" + outcome = run_case(tmp_path, FAULT_CLEARED, mode="end") + assert outcome["raised"] is None, outcome["raised"] + assert outcome["sim_time"] < STOP + + +# -- reading the STOP time ------------------------------------------------ + +def stop_time(path): + from stepss.cases import _dstStopTime + return _dstStopTime(path) + + +def write(tmp_path, text): + target = tmp_path / "t.dst" + target.write_text(text) + return str(target) + + +def test_the_stop_time_is_read(tmp_path): + assert stop_time(write(tmp_path, FAULT_CLEARED)) == 3.0 + + +def test_the_first_stop_wins(tmp_path): + # The engine stops reading at the first STOP, as _dstProblem assumes. + text = " 1.0 FAULT BUS 4032 0. 0.8\n 9.0 STOP\n20.0 STOP\n" + assert stop_time(write(tmp_path, text)) == 9.0 + + +def test_comments_are_skipped(tmp_path): + text = "# 1.0 STOP\n! 2.0 STOP\n\n 5.5 STOP ;\n" + assert stop_time(write(tmp_path, text)) == 5.5 + + +def test_no_usable_stop_gives_none(tmp_path): + assert stop_time(write(tmp_path, " 1.0 FAULT BUS 4032 0. 0.8\n")) is None + assert stop_time(write(tmp_path, "abc STOP\n")) is None + assert stop_time(str(tmp_path / "absent.dst")) is None From 415f161630236879c422394b6f4ad9c1f0be1eb3 Mon Sep 17 00:00:00 2001 From: Petros Aristidou Date: Fri, 20 Nov 2026 12:48:39 +0200 Subject: [PATCH 2/2] Accept either route for a diverged run; pin the STOP check on a fake engine The engine now returns -1 for a diverged run, with the message in getLastErr(), which the non-zero-flag path already raises on. The engine test accepts either that or the STOP-time check, so it passes against the bundled 3.84 library and a newer one alike. Six fake-engine tests pin the STOP-time check on its own, whatever library is bundled: an unflagged early end raises and quotes the engine's line from this call's part of the trace only; reaching STOP, a latency early stop and a pause pass; a STOP added at run time moves the time checked against. The check stays as the backstop for older engines and for early ends the engine still does not flag. --- CLAUDE.md | 18 +++--- src/stepss/simulator.py | 9 +-- tests/test_divergence.py | 117 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 126 insertions(+), 18 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 90fc38f..19fb434 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -128,15 +128,19 @@ against `tests/baselines/nordic_baseline.npz` via `tools/compare_trj.py`. `-1` and a message naming `sim_minmaxvolt`; a run that completes cleanly is the regression. `RAMSESError` is not exported at package level: import it from `stepss.globals`. -- **A 0 flag does not mean the run reached STOP.** The engine ends a run on - `Divergence. Simulation stopped` by setting its end flag alone, so - `ramses()` returns 0 exactly as for a finished run. `execSim()` and - `contSim()` therefore compare `getSimTime()` with the STOP time of the - `.dst` and raise `RAMSESError`, quoting the engine's last `t = ...` line - from the trace file, when the run fell short. `endSim()` skips the check +- **A 0 flag does not mean the run reached STOP.** Up to RAMSES 3.84 the + engine ended a run on `Divergence. Simulation stopped` by setting its end + flag alone, so `ramses()` returned 0 exactly as for a finished run; later + engines return -1 with the message in `getLastErr()`. Older engines, and + any other early end the engine does not flag, are why `execSim()` and + `contSim()` also compare `getSimTime()` with the STOP time of the `.dst` + and raise `RAMSESError`, quoting the engine's last `t = ...` line from the + trace file, when the run fell short. Keep that check even once the bundled + engine flags divergence. `endSim()` skips the check (ending early is its job), and so does `$LATENCY` early stopping, which the engine logs as `All equipment is latent`. `tests/test_divergence.py` drives - the engine's divergence branch with an unreachable `$NEWTON_TOLER`. + the engine's divergence branch with an unreachable `$NEWTON_TOLER` and + accepts either route, and pins the STOP-time check against a fake engine. - **Do not call `endSim()` after the trip.** It raises a second, unrelated `RAMSESError` ("Load records") that masks the real result. `obs.trj` is already complete without it. diff --git a/src/stepss/simulator.py b/src/stepss/simulator.py index 5b16fdb..544fb6f 100644 --- a/src/stepss/simulator.py +++ b/src/stepss/simulator.py @@ -591,10 +591,11 @@ def _traceSize(self): def _checkReachedStop(self, caller, traceStart): """Raise :class:`RAMSESError` if a run that returned 0 ended before STOP. - The engine ends a run on ``Divergence. Simulation stopped`` (Newton - failure at the minimum time step), and on a few other numerical - failures, by setting its end-of-simulation flag alone: ``ramses()`` - then returns 0, the same flag as a run that reached STOP. The + Up to RAMSES 3.84 the engine ended a run on ``Divergence. Simulation + stopped`` (Newton failure at the minimum time step) by setting its + end-of-simulation flag alone, so ``ramses()`` returned 0, the same + flag as a run that reached STOP. Later engines return -1 for it, but + a few other early ends are still unflagged, so this check stays. The simulated time is what tells them apart. The engine's own reason is only written to its log, so it is quoted from the part of the trace file this call added. diff --git a/tests/test_divergence.py b/tests/test_divergence.py index 37c97fa..d6991dc 100644 --- a/tests/test_divergence.py +++ b/tests/test_divergence.py @@ -1,11 +1,17 @@ """A run that stops short of its STOP time must not pass as a finished one. -RAMSES ends a run on "Divergence. Simulation stopped" (Newton failed at the -minimum time step) by setting its end-of-simulation flag and nothing else, so -``ramses()`` returns 0, the same flag as a run that reached STOP. ``execSim()`` -used to read that 0 as success: a reduced GB network diverged at t = 0.35 s -and its notebook reported it as running for three months, because the -trajectory simply ended there. +Up to RAMSES 3.84 the engine ended a run on "Divergence. Simulation stopped" +(Newton failed at the minimum time step) by setting its end-of-simulation flag +and nothing else, so ``ramses()`` returned 0, the same flag as a run that +reached STOP. ``execSim()`` read that 0 as success: a reduced GB network +diverged at t = 0.35 s and its notebook reported it as running for three +months, because the trajectory simply ended there. + +Later engines return -1 with the message in ``getLastErr()``, which the +non-zero-flag path already raises on. Older engines, and any other early end +the engine does not flag, are caught by comparing the simulated time with the +STOP time. The engine tests below accept either route; the fake-engine tests +pin the STOP-time check on its own, whatever library is bundled. The diverging case below is the Nordic test system with a Newton tolerance no iteration can meet, so the very first time step fails at the minimum step size. @@ -105,7 +111,9 @@ def test_a_diverging_run_raises(tmp_path): assert outcome["sim_time"] < STOP # the engine really did stop short assert outcome["raised"] is not None, outcome assert "Divergence" in outcome["raised"] # the engine's own words - assert "3.0000" in outcome["raised"] # and the STOP it fell short of + # Either the engine flagged it (-1) or the STOP-time check caught it. + assert ("flag -1" in outcome["raised"] + or "before the STOP time 3.0000 s" in outcome["raised"]) def test_a_run_that_reaches_stop_does_not_raise(tmp_path): @@ -154,3 +162,98 @@ def test_no_usable_stop_gives_none(tmp_path): assert stop_time(write(tmp_path, " 1.0 FAULT BUS 4032 0. 0.8\n")) is None assert stop_time(write(tmp_path, "abc STOP\n")) is None assert stop_time(str(tmp_path / "absent.dst")) is None + + +# -- the STOP-time check, against a fake engine --------------------------- + +class FakeEngine: + """Stands in for the loaded library: returns *flag*, stops at *t_end*.""" + + def __init__(self, flag, t_end, trace_text=""): + self.flag, self.t_end, self.trace_text = flag, t_end, trace_text + self.trace = None + + def set_pause_time(self, t): + return 0 + + def ramses(self, cmd, out): + self.trace = out.decode("utf-8") + with open(self.trace, "a") as trace: + trace.write(self.trace_text) + return self.flag + + def continue_simul(self): + return self.flag + + def set_end_simul(self): + return 0 + + def get_sim_time(self): + return self.t_end + + def get_huge_double(self): + return 1e308 + + +def faked(tmp_path, engine): + import stepss + + (tmp_path / "d.dat").write_text("# data\n") + (tmp_path / "c.dst").write_text(FAULT_CLEARED) + case = stepss.cfg() + case.addData(str(tmp_path / "d.dat")) + case.addDst(str(tmp_path / "c.dst")) + case.addOut(str(tmp_path / "output.trace")) + ram = stepss.sim() + # sim.__del__ unloads the library through this handle, so the fake + # carries the real one and the real library is still released once. + engine._handle = ram._ramseslib._handle + ram._ramseslib = engine + return ram, case + + +def test_an_unflagged_early_end_raises_with_the_engine_line(tmp_path): + from stepss.globals import RAMSESError + + engine = FakeEngine(0, 0.35, "t = 0.3500 Divergence. Simulation stopped\n") + ram, case = faked(tmp_path, engine) + with pytest.raises(RAMSESError) as err: + ram.execSim(case) + assert "t = 0.3500 s, before the STOP time 3.0000 s" in str(err.value) + assert "t = 0.3500 Divergence. Simulation stopped" in str(err.value) + + +def test_only_this_calls_part_of_the_trace_is_quoted(tmp_path): + from stepss.globals import RAMSESError + + (tmp_path / "output.trace").write_text("t = 9.9999 an earlier run's line\n") + ram, case = faked(tmp_path, FakeEngine(0, 0.35)) + with pytest.raises(RAMSESError) as err: + ram.execSim(case) + assert "earlier run" not in str(err.value) + + +def test_reaching_stop_passes(tmp_path): + ram, case = faked(tmp_path, FakeEngine(0, 3.0)) + assert ram.execSim(case) == 0 + + +def test_a_latency_early_stop_passes(tmp_path): + engine = FakeEngine(0, 1.2, "All equipment is latent. So, the simulation stops.\n") + ram, case = faked(tmp_path, engine) + assert ram.execSim(case) == 0 + + +def test_a_pause_is_not_checked(tmp_path): + ram, case = faked(tmp_path, FakeEngine(112, 1.0)) + assert ram.execSim(case, pause=1.0) == 0 + + +def test_a_stop_added_at_run_time_moves_the_check(tmp_path): + engine = FakeEngine(112, 1.0) + ram, case = faked(tmp_path, engine) + ram.execSim(case, pause=1.0) + engine.add_disturb = lambda t, d: 0 + ram.addDisturb(2.0, "STOP") + engine.flag, engine.t_end = 0, 2.0 + assert ram.contSim(ram.getInfTime()) == 0