-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatcoup.cpp
More file actions
433 lines (354 loc) · 14.5 KB
/
flatcoup.cpp
File metadata and controls
433 lines (354 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*******************************************************************
*
* DESCRIPTION: class FlatCoupledCell
*
* AUTHOR: Amir Barylko & Jorge Beyoglonian
* Version 2: Daniel Rodriguez.
* Version 3: Gabriel Wainer
*
* EMAIL: mailto://amir@dc.uba.ar
* mailto://jbeyoglo@dc.uba.ar
* mailto://drodrigu@dc.uba.ar
* mailto://gabrielw@dc.uba.ar
*
* DATE: 27/06/1998
* DATE: 12/11/1999 (v2)
*
*******************************************************************/
// ** include files **//
#include <algorithm>
#include "flatcoup.h" // header
#include "cellstate.h" // CellState
#include "neighval.h" // NeighborhoodValue
#include "strutil.h" // lowerCase
#include "realfunc.h" // calculateWithQuantum
using namespace std;
// ** private data ** //
#define OUT_PORT "out"
// ** public ** //
inline long *selectPos( long *select, nTupla *dim, const CellPosition &pos, bool verifyIncludes )
{
return &(select[ pos.calculateIndex( *dim, verifyIncludes ) ]);
}
/*******************************************************************
* Method: createCells
********************************************************************/
CoupledCell &FlatCoupledCell::createCells( const CellPositionList &n, const CellPositionList &selectList )
{
neighbors = n;
cellState( new CellState( *((nTupla *) &(dimension())), borderWrapped() ) );
register long cantElems = dimension().totalElements();
select = new long[ cantElems ];
xList = new VirtualPortList[ cantElems ];
yList = new VirtualPortList[ cantElems ];
nTupla dimen( dimension() );
register long posi( 0 );
// let's fill the order
for ( CellPositionList::const_iterator cursor = selectList.begin(); cursor != selectList.end(); cursor++ )
*(selectPos( select, &dimen, *cursor, true )) = posi++;
///////////////////////
// Creates the cells
///////////////////////
MASSERTMSG( !dimen.contains(0), "Attempt to create a FlatCoupledCell model with a dimension containing the value 0.");
CellPosition counter( dimen.dimension(), 0 );
register bool overflow = false;
while (!overflow){
(*cellState())[ counter ] = initialCellValue();
if( find( selectList.begin(), selectList.end(), counter ) == selectList.end())
*(selectPos( select, &dimen, counter, false )) = posi++;
overflow = counter.next( dimen );
}
MASSERTMSG( posi == cantElems, "Invalid cell creation!" );
return *this ;
}
/*******************************************************************
* Method: setCellValue
********************************************************************/
CoupledCell &FlatCoupledCell::setCellValue( const CellPosition &pos, const Real &val )
{
CellPosition *cp = (CellPosition *) &pos;
(*cellState())[ *cp ] = val ;
return *this ;
}
/*******************************************************************
* Method: localTransition
********************************************************************/
CoupledCell &FlatCoupledCell::setLocalTransition( const CellPosition &pos, const LocalTransAdmin::Function &fn )
{
funcZones[ pos ] = fn;
return *this ;
}
/*******************************************************************
* Method: initFunction
********************************************************************/
Model &FlatCoupledCell::initFunction()
{
typedef map< long, CellPosition > SelectList ;
SelectList posList ;
nTupla dimen( dimension() );
MASSERTMSG( !dimen.contains(0), "Attempt to initialize a FlatCoupledCell model with a dimension containing the value 0.");
register bool overflow = false;
CellPosition counter( dimen.dimension(), 0 );
while (!overflow){
posList[ *(selectPos( select, &dimen, counter, false )) ] = counter;
overflow = counter.next( dimen );
}
for( SelectList::iterator cursor = posList.begin(); cursor != posList.end(); cursor ++ )
externalFunction( VTime::Zero, cursor->second );
return *this;
}
/*******************************************************************
* Method: internalFunction
*******************************************************************/
Model &FlatCoupledCell::internalFunction( const VTime &time )
{
typedef map< long, CellPosition > Influenced;
Influenced influenced;
nTupla dimen( dimension() );
VirtualPortList::iterator cursor;
CellPositionList::iterator links;
CellPosition aux;
register long posCell;
while( !eventList.empty() && eventList.front().time == time )
{
NextEvent &nextEvent( eventList.front() );
(*cellState())[ nextEvent.pos ] = nextEvent.value;
/*************************************************************/
// let's analize the output
// Find the "OUT" port
posCell = nextEvent.pos.calculateIndex( dimension() );
for (cursor = yList[posCell].begin(); cursor != yList[posCell].end(); cursor++)
if (cursor->first == OUT_PORT)
sendOutput( time, *(cursor->second), nextEvent.value.value() );
/*************************************************************/
// let's save the external messages to the local links
for (links = neighbors.begin(); links != neighbors.end(); links++ )
{
aux = nextEvent.pos;
aux -= *links; // REVERSE NEIGHBOR LIST
// For NORMAL NEIGHBOR LIST do +=
if( cellState()->includes( aux ) )
{
cellState()->calcRealPos( aux );
// inserting ordered by select
influenced[ *(selectPos( select, &dimen, aux, false )) ] = aux;
}
}
eventList.pop_front();
}
// let's send the external messages
for (Influenced::iterator cursor = influenced.begin(); cursor != influenced.end(); cursor++ )
externalFunction( time, cursor->second );
return *this;
}
/*******************************************************************
* Function Name: addInfluence
* Description: agrega una influencia entre dos ports
********************************************************************/
Model &FlatCoupledCell::addInfluence( const string &sourceName, const string &sourcePort, const string &destName, const string &destPort )
{
MASSERTMSG( sourceName==description() || destName==description(), "Invalid Link!" );
if( sourceName==description() )
{
Port &port( port( sourcePort ) );
xList[ CellPosition(destName).calculateIndex( dimension() )].insert( VirtualPortList::value_type( lowerCase(destPort), &port ) );
// Now adds the default function in the port function list
setPortInFunction( CellPosition(destName), destPort, DEFAULT_FUNCTION_InPort );
// Now set the last value arrived to the port with a undefined
setLastValuePortIn( CellPosition(destName), destPort, Real::tundef );
} else
{
/************************************************************/
// Now, adds the output ports to the YList
Port &port( port( destPort ) );
yList[ CellPosition(sourceName).calculateIndex( dimension() )].insert( VirtualPortList::value_type( lowerCase(sourcePort), &port ) );
/************************************************************/
}
return *this ;
}
/*******************************************************************
* Function Name: setPortInFunction
* Description: agrega o establece una function para un puerto especifico de una celda
********************************************************************/
void FlatCoupledCell::setPortInFunction( const CellPosition &cellPos, const string &portIn, const string &functionName )
{
FlatPortInFunction::iterator cursor = getPortFunction().begin();
register bool salgo = false;
// search for the (cellPos, portIn)
while ( !salgo && cursor != getPortFunction().end() )
if ( cursor->first.cellPos() == cellPos && cursor->first.inPort() == portIn )
salgo = true;
else
cursor++;
// if not exists
if (cursor == getPortFunction().end() )
{
CellDescription *cellDesc = new CellDescription;
cellDesc->cellPos( cellPos );
cellDesc->inPort( portIn );
getPortFunction()[ *cellDesc ] = functionName;
}
else // the entry exists
{
CellDescription cellDesc;
cellDesc.cellPos( cellPos );
cellDesc.inPort( portIn );
getPortFunction()[ cellDesc ] = functionName;
}
}
/*******************************************************************
* Function Name: getPortInFunction
* Description: obtiene el nombre de la function para un puerto especifico de una celda
********************************************************************/
const string &FlatCoupledCell::getPortInFunction( const CellPosition &cellPos, const string &portIn )
{
register bool salgo = false;
// search for the (cellPos(), portIn)
FlatPortInFunction::iterator cursor = getPortFunction().begin();
while (!salgo && cursor != getPortFunction().end())
if ( cursor->first.cellPos() == cellPos && cursor->first.inPort() == portIn )
salgo = true;
else
cursor++;
MASSERTMSG( cursor != getPortFunction().end(), "Invalid reference to the PortIn function: " + portIn);
CellDescription cellDesc;
cellDesc.cellPos( cellPos );
cellDesc.inPort( portIn );
return getPortFunction()[ cellDesc ];
}
/*******************************************************************
* Function Name: setLastValuePortIn
* Description: agrega o establece el ultimo valor ingresado por un puerto especifico de una celda
********************************************************************/
void FlatCoupledCell::setLastValuePortIn( const CellPosition &cellPos, const string &portIn, const Real &value )
{
FlatPortValues::iterator cursor = getPortValues().begin();
register bool salgo = false;
// search for the (cellPos, portIn)
while (!salgo && cursor != getPortValues().end())
if ( cursor->first.cellPos() == cellPos && cursor->first.inPort() == portIn )
salgo = true;
else
cursor++;
// if not exists
if (cursor == getPortValues().end() )
{
CellDescription *cellDesc = new CellDescription;
cellDesc->cellPos( cellPos );
cellDesc->inPort( portIn );
portValues[ *cellDesc ] = value;
}
else // the entry exists
{
CellDescription cellDesc;
cellDesc.cellPos( cellPos );
cellDesc.inPort( portIn );
portValues[ cellDesc ] = value;
}
}
/*******************************************************************
* Function Name: getInputPortValues
* Description: obtiene a partir de la lista de valores de los ports
* de todas las celdas, una lista con los valores de los
* ports de una celda especifica
********************************************************************/
void FlatCoupledCell::getInputPortValues( PortValues *pv, const CellPosition &cellPos, const string &portIn )
{
// search for the cellPos
for (FlatPortValues::const_iterator cursor = getPortValues().begin(); cursor != getPortValues().end(); cursor++)
if ( cursor->first.cellPos() == cellPos )
(*pv)[ cursor->first.inPort() ] = cursor->second;
}
/*******************************************************************
* Function Name: getOutputPorts
* Description: obtiene una lista de los puertos de salida de la celda
* en cuestion
********************************************************************/
void FlatCoupledCell::getOutputPorts( VirtualPortList **vpl, const CellPosition &cellPos )
{
*vpl = &(yList[ cellPos.calculateIndex( dimension() ) ]);
// register long posCell = cellPos.calculateIndex( dimension() );
// for (VirtualPortList::const_iterator cursor = yList[posCell].begin(); cursor != yList[posCell].end(); cursor++)
// vpl->insert( VirtualPortList::value_type( cursor->first, cursor->second ) );
}
/*******************************************************************
* Method: externalFunction
********************************************************************/
Model &FlatCoupledCell::externalFunction( const VTime &time, const CellPosition &pos, bool external, Real mtv, const string &portIn )
{
VTime delay( defaultDelay() );
VTime actualTime( time );
Real tv( mtv );
if( !external )
{
LocalTransAdmin::Function idFn( localTransition() );
// Busco
FunctionZones::iterator cursor = funcZones.begin();
while ( cursor->first != pos && cursor != funcZones.end() )
cursor++;
if( cursor != funcZones.end() )
idFn = cursor->second;
NeighborhoodValue *nVal = new NeighborhoodValue(*cellState(), neighbors, pos);
VirtualPortList *vpl;
getOutputPorts( &vpl, pos );
tv = SingleLocalTransAdmin::Instance().evaluate(idFn, *nVal, NULL, delay, actualTime, vpl, this);
delete nVal;
}
else // the message comes from an IN Port
{
string functionName = getPortInFunction(pos, portIn);
// first we set the new port value in the list of FlatPortValues
setLastValuePortIn( pos, portIn, mtv );
if (functionName != DEFAULT_FUNCTION_InPort)
{
// Obtengo el vecindario
NeighborhoodValue *nVal = new NeighborhoodValue(*cellState(), neighbors, pos);
// Obtengo los valores de todos los puertos de entrada de la celda en cuestion
PortValues *portInValues = new PortValues;
getInputPortValues( portInValues, pos, portIn );
VirtualPortList *vpl;
getOutputPorts( &vpl, pos );
tv = SingleLocalTransAdmin::Instance().evaluate( functionName, *nVal, portInValues, delay, actualTime, vpl, this, portIn );
delete portInValues;
delete nVal;
}
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//if (UseQuantum().Active())
// tv = valueWithQuantum(tv, Real(UseQuantum().Value()));
/////////////////////////////////////////////////////////////////////
NextEventList::iterator lastFound( nextEventList().end() );
nTupla dimen( dimension() );
// Replaced all time + delay with nextTime
VTime nextTime( time + delay );
NextEventList::iterator cursor = nextEventList().begin();
for (; ( cursor->time < nextTime || ( cursor->time == nextTime && *(selectPos(select, &dimen, cursor->pos, false)) < *(selectPos(select, &dimen, pos, false)) ) ) && cursor != nextEventList().end() ; cursor++ )
if (cursor->pos == pos)
lastFound = cursor;
CellPosition *cp = (CellPosition *) &pos;
Real ltv( lastFound == this->nextEventList().end() ? (*cellState())[ *cp ] : lastFound->value );
if( ( UseQuantum().Active() &&
( valueWithQuantum( tv, Real(UseQuantum().Value()) ) !=
valueWithQuantum( ltv, Real(UseQuantum().Value()) )
)
)
|| (!UseQuantum().Active() && ltv != tv ) )
// if( tv != ltv )
{
NextEvent next;
next.time = nextTime;
next.pos = pos;
next.value = tv;
cursor = nextEventList().insert( cursor, next );
cursor++;
if (inertialDelay() && lastFound != nextEventList().end())
nextEventList().erase( lastFound );
// ESTO ESTA MAL. POR ESO FUE COMENTADO
//else if (!inertialDelay()) // DEMORA DE TRANSPORTE
// for ( ; cursor != nextEventList().end(); cursor++)
// if (cursor->pos == pos)
// cursor = nextEventList().erase(cursor);
}
return *this;
}