-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomcell.cpp
More file actions
460 lines (363 loc) · 13.7 KB
/
atomcell.cpp
File metadata and controls
460 lines (363 loc) · 13.7 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*******************************************************************
*
* DESCRIPTION: class AtomicCell
*
* AUTHOR: Amir Barylko & Jorge Beyoglonian
* Version 2: Daniel Rodriguez.
*
* EMAIL: mailto://amir@dc.uba.ar
* mailto://jbeyoglo@dc.uba.ar
* mailto://drodrigu@dc.uba.ar
*
* DATE: 27/06/1998
* DATE: 25/04/1999 (v2)
*
*******************************************************************/
// ** include files **
#include "atomcell.h" // base header
#include "message.h" // InternalMessage
#include "except.h" // MASSERTMSG
#include "strutil.h" // function lowerCase
#include "pmodeladm.h"
using namespace std;
// ** public data **
const string AtomicCell::cellClassName( "AtomicCell" ) ;
const string AtomicCell::outPort( "out" ) ;
const string AtomicCell::neighborChangePort( "neighborChange" ) ;
const string AtomicCell::NCInPrefix( "in_" );
const string AtomicCell::NCOutPrefix( "out_" );
/***************************************
* ATOMIC CELL STATE
***************************************/
AtomicCellState &AtomicCellState::operator=(AtomicCellState& thisState)
{
(AtomicState &)*this = (AtomicState &)thisState;
inValues = thisState.inValues;
Quantum = thisState.Quantum;
neighborhood = thisState.neighborhood;
return *this;
}
void AtomicCellState::copyState(AtomicCellState *thisState)
{
*this = *(AtomicCellState *) thisState;
}
int AtomicCellState::getSize() const
{
return sizeof(AtomicCellState);
}
/***************************************
* ATOMIC CELL
***************************************/
void AtomicCell::setPortInFunction( const string portName, const string functionName )
{
inputPortFunction()[ portName ] = functionName;
}
/* This function is overloaded by those in tdcell.cpp and idcell.cpp */
Model &AtomicCell::outputFunction( const CollectMessage &msg )
{
sendOutput( msg.time(), outputPort(outPort), this->value(neighborChangePort).value() );
return *this;
}
/*******************************************************************
* Function Name: ~AtomicCell
********************************************************************/
AtomicCell::~AtomicCell()
{}
/*******************************************************************
* Method: AtomicCell
* Description: Constructor
********************************************************************/
AtomicCell::AtomicCell( const CellPosition& pos, const string &name, const LocalTransAdmin::Function &lf )
: Atomic( name )
, localFn( lf )
{
cellPos = pos;
}
/*******************************************************************
* Method: initializeCell
* Description: This function is called once the current processor state
* has been create. The cell can now create it's neighborhood.
********************************************************************/
void AtomicCell::initializeCell()
{
//Create the neighborhood
const CoupledCell& coupled = (CoupledCell&)(*parent());
neighborhood().create( coupled , coupled.neighborhood(), cellPosition(), NCPortNames);
//Initialize the PortValues list
PortList::iterator cursor;
for (cursor = inputPort().begin() ;cursor != inputPort().end();
cursor++ )
{
inputPortValues()[ cursor->second->name() ] = Real();
}
}
/*******************************************************************
* Function Name: addInPort
* Description: Adds an input port if not exists.
********************************************************************/
bool AtomicCell::addInPort( string portName )
{
// See if the port exists
string name( lowerCase( portName ) );
PortList::iterator cursor;
// search the port
for (cursor = inputPort().begin() ;
cursor != inputPort().end() && cursor->second->name() != name ;
cursor++ )
;
// If not exists
if (cursor == inputPort().end() )
{
Port *port = &( addInputPort( name ) ); // Call to the Model's method
// Adds the port in the IN list
inputPort()[ port->id() ] = port;
// Now adds the default function in the port function list
inputPortFunction()[ name ] = DEFAULT_FUNCTION_InPort;
// The last value arrived to the port will be set to Undefined
// in afterProcessorInitialize
return true;
}
return false;
}
/*******************************************************************
* Function Name: addOutPort
* Description: Adds an output port if not exists.
********************************************************************/
bool AtomicCell::addOutPort( string portName )
{
// See if the port exists
string name( lowerCase( portName ) );
if (name == outPort) // Si es el puerto "OUT"
return false;
PortList::iterator cursor;
// search the port
for (cursor = outPortList().begin() ;
cursor != outPortList().end() && cursor->second->name() != name ;
cursor++ )
;
// If not exists
if (cursor == outPortList().end() )
{
Port *port = &( addOutputPort( name ) ); // Call to the Model's method
// Adds the port in the IN list
outPortList()[ port->id() ] = port;
return true;
}
return false;
}
/*******************************************************************
* Function Name: addInputNCPort
* Description: Adds a NC port if it is not already in the list.
* The prefix must be part of the portname
********************************************************************/
bool AtomicCell::addInputNCPort( string portName )
{
string name(lowerCase(portName));
// If it doesn't exists
if (!isInNCPort(name)) {
Port &port = addInputPort(name);
// Adds the port in the list
inNCPortList()[port.id()] = inputPorts()[port.id()];
return true;
}
return false;
}
/*******************************************************************
* Function Name: addOutputNCPort
* Description: Adds a NC port if it is not already in the list
* The prefix must be part of the portname
********************************************************************/
bool AtomicCell::addOutputNCPort( string portName)
{
// See if the port exists
string name(lowerCase(portName));
// If it doesn't exists
if (!isOutNCPort(name)) {
Port &port = addOutputPort(name);
// Adds the port in the list
outNCPortList()[port.id()] = outputPorts()[port.id()];
return true;
}
return false;
}
/*******************************************************************
* Function Name: createNCPorts
* Description: Creates all the in and out NC ports
********************************************************************/
void AtomicCell::createNCPorts( std::list<string> &portNames)
{
list<string>::iterator cursor;
NCPortNames = portNames;
addInputNCPort(neighborChangePort);
addOutputNCPort(outPort);
for (cursor = NCPortNames.begin(); cursor != NCPortNames.end(); cursor++) {
MASSERTMSG(addInputNCPort(NCInPrefix + *cursor),
"NeighborChange input port duplicated: " + *cursor);
MASSERTMSG(addOutputNCPort(NCOutPrefix + *cursor),
"NeighborChange output port duplicated: " + *cursor);
}
}
/*******************************************************************
* Function Name: setPortValue
* Description: set the last value arrived to a port
********************************************************************/
void AtomicCell::setPortValue( const string portName, const Real portValue )
{
// See if the port exists
string name( lowerCase( portName ) );
PortList::iterator cursor;
// search the port
for (cursor = inputPort().begin() ;
cursor != inputPort().end() && cursor->second->name() != name ;
cursor++ )
;
// If doesn't exists, then STOP !!
MASSERTMSG( cursor != inputPort().end(), "The port does not exist, in method AtomicCell::setPortValue(port, value)" );
// Now set the last value arrived to the port with a undefined value
inputPortValues()[ name ] = portValue;
}
/*******************************************************************
* Function Name: getOutPort
* Description: gets an output port
********************************************************************/
void AtomicCell::getOutPorts(VirtualPortList *vpl)
{
PortList::iterator cursor = outPortList().begin();
while (cursor != outPortList().end() )
{
vpl->insert( VirtualPortList::value_type( cursor->second->name(), cursor->second ) );
cursor++;
}
}
/*******************************************************************
* Function Name: localTransitionConfluent
* Description: this functions receives all the external messages
* and sets the neighborhood accordingly. This is the function
* that defines the policy for how to handle multiple external
* messages from a same cell
********************************************************************/
AtomicCell &AtomicCell::localTransitionConfluent( const MessageBag& msgs)
{
const CoupledCell* coupcell = (CoupledCell*) parent();
//Default localTransitionConfluent will do the following:
//Set neighbor's values to the last value received
//Set all port values
//Which local transition function to call will be
//decided in the externalFunction
for ( MessageBag::iterator cursor = msgs.begin(); cursor != msgs.end(); cursor++)
{
ExternalMessage msg = *((ExternalMessage *)(*cursor));
if (isInNCPort(msg.port().name())) {
NeighborPosition npos;
AtomicCell& cell = (AtomicCell &)SingleParallelModelAdm::Instance().model( msg.senderModelId() );
npos = coupcell->inverseNeighborhood( cellPosition(), cell.cellPosition());
//cout << "Cell " << cellPosition().print() << "received from " << cell.cellPosition().print() << " which is " << npos.print() << " value: " << msg.value() << endl;
neighborhood().set(npos, msg.port().name(), msg.value());
} else {
// first we set the new port value in the list of PortValues
setPortValue( msg.port().name(), msg.value() );
}
}//if
return *this;
}//localTransitionConfluent
/*******************************************************************
* Function Name: inputPort
* Description: Retorna el input port solicitado, por default is neighborChange
********************************************************************/
const Port &AtomicCell::neighborPort(const string &portName = neighborChangePort) const
{
const Port *port;
string intName(lowerCase(portName));
if (portName != neighborChangePort)
intName = NCInPrefix + intName;
port = getNCPortByName(Xports, intName);
MASSERTMSG(port != NULL, "Reference to unexisting port " + portName);
return *port;
}
/*******************************************************************
* Function Name: outputPort
* Description: Retorna el output port solicitado, por default is out
********************************************************************/
const Port &AtomicCell::outputPort(const string &portName = outPort) const
{
const Port *port;
string intName(lowerCase(portName));
/* If it isn't "out" and the name isn't already prefixed, prefix it */
if (portName != outPort && intName.find(NCOutPrefix) != 0)
intName = NCOutPrefix + intName;
port = getNCPortByName(Yports, intName);
MASSERTMSG(port != NULL, "Reference to unexisting port " + portName);
return *port;
}
/*******************************************************************
* Function Name: getNCPortByName
* Description: Retorns the output port required. Default is out.
* The prefix must be part of the portname
********************************************************************/
const Port *AtomicCell::getNCPortByName(const PortList &pl, const string portName) const
{
string name(lowerCase(portName));
PortList::const_iterator cursor = pl.begin();
while (cursor != pl.end() && cursor->second->name() != name)
cursor++;
return (cursor != pl.end() ? cursor->second : NULL);
}
/*******************************************************************
* Function Name: calculateInPort
* Description: Creates an input port name out of the available port name.
********************************************************************/
string AtomicCell::calculateInPort( string &portName )
{
/* Check whether it is the port default */
if (portName == outPort)
return neighborChangePort;
/* Check whether it is a port from the coupled model */
PortList &pl = inputPort();
PortList::iterator cursor = pl.begin();
while (cursor != pl.end() && lowerCase(cursor->second->name()) != lowerCase(portName))
cursor++;
if (cursor != pl.end())
return neighborChangePort;
/* Check if it is an output NC port */
string result(portName);
string::size_type pos;
pos = result.find(NCOutPrefix);
if (pos != string::npos)
result.replace(pos, NCOutPrefix.length(), NCInPrefix);
return result;
}
/*******************************************************************
* Function Name: calculateOutPort
* Description: Creates an output port name out of the available port name.
********************************************************************/
string AtomicCell::calculateOutPort( string &portName )
{
/* Check whether it is the port default */
if (portName == neighborChangePort)
return outPort;
/* Check whether it is a port from the coupled model */
PortList &pl = inputPort();
PortList::iterator cursor = pl.begin();
while (cursor != pl.end() && lowerCase(cursor->second->name()) != lowerCase(portName))
cursor++;
if (cursor != pl.end())
return outPort;
/* Check if it is an input NC port */
string result(portName);
string::size_type pos;
pos = result.find(NCInPrefix);
if (pos != string::npos)
result.replace(pos, NCInPrefix.length(), NCOutPrefix);
return result;
}
/*******************************************************************
* Function Name: setAllNCPortsValues
* Description: set the same value for all the input NC ports.
********************************************************************/
AtomicCell &AtomicCell::setAllNCPortsValues( const Real &val )
{
PortList::iterator cursor;
for (cursor = inNCPortList().begin(); cursor != inNCPortList().end(); cursor++)
value( val, cursor->second->name() );
return *this;
}