|
|
|
|
||
|
DEFINITION MODULE CAModBase; (******************************************************************* Module CAModBase (Version 1.0) Copyright (c) 1991-2006 by Thomas Nemecek and ETH Zurich. Purpose General support for 2D cellular automatons with discrete and continuous states. Remarks Terminology: CA = cellular automaton update state = auxiliary variable used for update (see below) C = continuous D = discrete The following 3 "UpdateModes" are supported: - withNewState: calling PROC UpdateCAState provokes an overwriting of the state of the CA with the updateState state(k+1) := updateState - withStateChange: calling PROC UpdateCAState provokes an addition of the state of the CA and the updateState state(k+1) := state(k) + updateState - noUpdate: calling PROC UpdateCAState provokes no action at all. This mode should be chosen, if the variable caMod does not design a CA-model but an auxiliary array, which is not updated. In this mode, no update-state array is allocated, thus memory is saved. Limitations: The maximal size of the array is 8000x8000 cells. Programming o Design Thomas Nemecek 23/04/1991 o Implementation Thomas Nemecek 23/04/1991 ETH Zurich Systems Ecology CHN E 35.1 Universitaetstrasse 16 8092 Zurich SWITZERLAND URLs: <mailto:RAMSES@env.ethz.ch> <http://www.sysecol.ethz.ch> <http://www.sysecol.ethz.ch/SimSoftware/RAMSES> Last revision of definition: 21/05/2002 FG *******************************************************************) (*. FROM CAModBase IMPORT maxLen,CAMod,CAArC,CAArD,ColCPtr,ColDPtr,RowC,RowD,ColC,ColD,UpdateMode, State,CellProc,ConditionProc,MappingProc,RemoveProc,nonexistentCAMod,DeclCAMod, GetStatePtrsC,GetStatePtrsD,GetCASpecs,RemoveCAMod,AddRemoveProc,DeleteRemoveProc, CAExists,SetCellC,GetCellC,CellC,SetUpdCellC,GetUpdCellC,SetCellD,GetCellD, CellD,SetUpdCellD,GetUpdCellD,InitCAState,UpdateCAState,ResetCAUpdateState, CAFrequencies,CAMean,SetStatsCond,GetStatsCond,SetMappingProc,GetMappingProc, SetIndexCheckMode,GetIndexCheckMode,DoForAllCells,FillCAModC,FillCAModD; .*) FROM DMFiles IMPORT TextFile; CONST maxLen = 8000; TYPE CAMod; CAArC = POINTER TO RowC; CAArD = POINTER TO RowD; ColCPtr = POINTER TO ColC; ColDPtr = POINTER TO ColD; RowC = ARRAY [1..maxLen] OF ColCPtr; RowD = ARRAY [1..maxLen] OF ColDPtr; ColC = ARRAY [1..maxLen] OF REAL; ColD = ARRAY [1..maxLen] OF INTEGER; UpdateMode = (withNewState, withStateChange, noUpdate); StateType = (continuous, discrete); (* continuous: REAL -values discrete: INTEGER-values *) CellProc = PROCEDURE(CAMod, INTEGER, INTEGER); ConditionProc = PROCEDURE(CAMod, INTEGER, INTEGER): BOOLEAN; (* used as condition for the statistical evaluation *) MappingProc = PROCEDURE(CAMod, REAL): INTEGER; (* used to classify REAL-values into discrete classes for graphical monitoring and statistics *) RemoveProc = PROCEDURE(CAMod); CAModProc = PROCEDURE(CAMod); VAR nonexistentCAMod: CAMod; (* read only *) (*******************) (* CA management *) (*******************) PROCEDURE DeclCAMod (VAR caMod: CAMod; stateType: StateType; x, y, minState, maxState: INTEGER; mode: UpdateMode); (* caMod: the reference variable for the access of the model stateType: see Remarks x,y: the # of rows (x) and columns (y) minState, maxState: the first resp. last state. These parameters are of importance for the monitoring of the state in a graph. mode: see Remarks *) PROCEDURE GetStatePtrsC(caMod: CAMod; VAR stateC, updStateC: CAArC); PROCEDURE GetStatePtrsD(caMod: CAMod; VAR stateD, updStateD: CAArD); (* stateC, updStateC, resp. stateD, updStateD: These variables allow to access directly the array elements. Direct access is about 5 times faster than the call of the procedures SetCellC/D, resp. GetCellC/D This can e.g. be done as follows: cPtr^[r]^[c] is the element in the row r and the column c ------------------------------------------------ | CAUTION: no index range checking is done | | in the case of the direct access! | ------------------------------------------------ If mode = noUpdate, the variables updStateC resp. updStateD are NIL *) PROCEDURE GetCASpecs ( caMod: CAMod; VAR stateType: StateType; VAR x, y, minState, maxState: INTEGER; VAR mode: UpdateMode); PROCEDURE RemoveCAMod(VAR caMod: CAMod); PROCEDURE AddRemoveProc (caMod: CAMod; rp: RemoveProc); PROCEDURE DeleteRemoveProc(caMod: CAMod; rp: RemoveProc); (* the remove procedure allows to remove objects, associated with caMod. This procedure is called BEFORE the CAMod is removed.*) PROCEDURE CAExists(caMod: CAMod): BOOLEAN; (*****************) (* Cell state *) (*****************) (* The following procedures do not test for the existence of caMod. If required, the test can be performed with PROC CAExists. This is typically done at the beginning of a loop. *) PROCEDURE SetCellC (caMod: CAMod; x, y: INTEGER; state: REAL); PROCEDURE GetCellC (caMod: CAMod; x, y: INTEGER; VAR state: REAL); PROCEDURE CellC (caMod: CAMod; x, y: INTEGER): REAL; (* for easy access *) PROCEDURE SetUpdCellC(caMod: CAMod; x, y: CARDINAL; updateSt: REAL); PROCEDURE GetUpdCellC(caMod: CAMod; x, y: CARDINAL; VAR updateSt: REAL); PROCEDURE SetCellD (caMod: CAMod; x, y: INTEGER; state: INTEGER); PROCEDURE GetCellD (caMod: CAMod; x, y: INTEGER; VAR state: INTEGER); PROCEDURE CellD (caMod: CAMod; x, y: INTEGER): INTEGER; (* for easy access *) (* The last 2 procedures can be used to convert the state of a CAMod with continuous states into a discrete value using the current mapping procedure *) PROCEDURE SetUpdCellD(caMod: CAMod; x, y: CARDINAL; updateSt: INTEGER); PROCEDURE GetUpdCellD(caMod: CAMod; x, y: CARDINAL; VAR updateSt: INTEGER); (*****************) (* CA state *) (*****************) PROCEDURE InitCAState(caMod: CAMod; VAR f: TextFile); (* not yet implemented *) (* reads the initial state from the current position of the file f, and assigns it to the current state of caMod *) PROCEDURE FillCAModC(caMod: CAMod; val: REAL); PROCEDURE FillCAModD(caMod: CAMod; val: INTEGER); PROCEDURE UpdateCAState(caMod: CAMod); (* updates the state of the cellular automaton, according to the current update mode *) PROCEDURE ResetCAUpdateState(caMod: CAMod); (* the elements of the update state array are reset as follows: updSt[i] := 0 if mode = withStateChange updSt[i] := st[i] if mode = withNewState *) (*******************) (* statistics *) (*******************) PROCEDURE CAFrequencies(caMod: CAMod; VAR nInState: ARRAY OF LONGINT; VAR propInState: ARRAY OF REAL; useStatsCond: BOOLEAN); (* calculates the statistics of the cellular automaton: nInState = number of cells in states 0…nStates-1 propInState = proportion of cells in states 0…nStates-1 If the stateType of the CA is continuous, the current mapping procedure is used to calculate the frequency of the dicrete states. useStatsCond determines whether the statistics are calculated for a previously defined subset of cells only (TRUE) or for all cells (FALSE) *) PROCEDURE CAMean(caMod: CAMod; VAR mean: REAL; useStatsCond:BOOLEAN); (* calculates the mean of the cellular automaton states *) PROCEDURE SetStatsCond(caMod: CAMod; scp: ConditionProc); PROCEDURE GetStatsCond(caMod: CAMod; VAR scp: ConditionProc); (* scp can be used to select a range of cells for statistical evaluation *) PROCEDURE SetMappingProc(caMod: CAMod; mp: MappingProc); PROCEDURE GetMappingProc(caMod: CAMod; VAR mp: MappingProc); (* The default mapping procedure is: VAR c: REAL; d: INTEGER; d := TRUNC(MAX(FLOAT(minState), MIN(FLOAT(maxState), c))+0.5); *) (*******************) (* auxiliary *) (*******************) PROCEDURE SetIndexCheckMode( doCheck: BOOLEAN ); PROCEDURE GetIndexCheckMode(VAR doCheck: BOOLEAN ); PROCEDURE DoForAllCells(caMod: CAMod; p: CellProc; useStatsCond: BOOLEAN); (* if only the indices of the pocedure p are used, index range checking is unnecessary *) PROCEDURE DoForAllCAMods(cp: CAModProc); END CAModBase.
|
||
|
|
|