-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoupcell.cpp
More file actions
425 lines (339 loc) · 13 KB
/
coupcell.cpp
File metadata and controls
425 lines (339 loc) · 13 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
/*******************************************************************
*
* DESCRIPTION: class CoupledCell
*
* AUTHOR: Amir Barylko & Jorge Beyoglonian
* Version 2: Daniel Rodriguez.
* Version 3: Alejandro Troccoli
*
* EMAIL: mailto://amir@dc.uba.ar
* mailto://jbeyoglo@dc.uba.ar
* mailto://drodrigu@dc.uba.ar
* mailto://atroccol@dc.uba.ar
*
* DATE: 27/06/1998
* DATE: 06/03/1999 (v2)
* DATE: 12/02/2001 (v3)
*
*******************************************************************/
/** include files **/
#include <algorithm>
#include "coupcell.h" // CoupledCell
#include "strutil.h" // class int2Str
#include "atomcell.h" // class AtomicCell
#include "pprocadm.h" // SingleParallelProcessorAdmin
#include "pmodeladm.h" // SingleParallelModelAdm
#include "cellpartition.h" //CellPartition
#include "idcell.h"
#include "tdcell.h"
#include "except.h"
using namespace std;
/** methods **/
CoupledCell::CoupledCell( const string &name )
: Coupled( name )
, partition ( NULL )
, localFn( LocalTransAdmin::InvalidFn )
{}
/*******************************************************************
* Function Name: ~CoupledCell
********************************************************************/
CoupledCell::~CoupledCell()
{
if (partition )
delete partition;
}
/*******************************************************************
* Function Name: addInfluence
* Description: add a link between two ports
********************************************************************/
Model &CoupledCell::addInfluence( const string &sourceName, const string &sourcePort, const string &destName, const string &destPort)
{
Model& source = SingleParallelModelAdm::Instance().model( sourceName );
Model& destination = SingleParallelModelAdm::Instance().model( destName );
//If the either modelName is the name of the coupled, the port
//has already been created. If the model is a cell, the port
//must be created.
if ( sourceName != description() )
static_cast< AtomicCell& >(source).addOutPort( sourcePort );
if ( destName != description() )
static_cast< AtomicCell& >(destination).addInPort( destPort );
Port &port1( source.port( sourcePort ) );
Port &port2( destination.port( destPort ) );
port1.addInfluence( port2 );
return *this;
}
/*******************************************************************
* Function Name: afterProcessorInitialize
********************************************************************/
void CoupledCell::afterProcessorInitialize()
{
//Two things must be done now that we have an initial state:
//1. Create all the local cell's neighborhoods and set the
// default values
PModelList::const_iterator childrenCursor;
for ( childrenCursor = childModels().begin();
childrenCursor != childModels().end();
childrenCursor++) {
if( (*childrenCursor)->localProc() != ParallelProcessor::InvalidId )
{
AtomicCell* cell = (AtomicCell*)(*childrenCursor);
cell->initializeCell();
cell->setAllNCPortsValues( initialCellValue());
cell->variables( initialStateVars() );
}
}
//2. Load the initial cell values.
ParallelMainSimulator::Instance().loadInitialCellValues( *this );
ParallelMainSimulator::Instance().loadInitialVariablesValues( *this );
}
/*******************************************************************
* Function Name: createCells
********************************************************************/
CoupledCell &CoupledCell::createCells( const CellPositionList &neighborList,
CellPartition *part,
std::list<string> NCPorts)
{
unsigned long cellIndex;
MASSERTMSG( !dimension().contains(0), "Attemp to create a Coupled Cell Model with a dimension containing the value 0.");
partition = part;
neighbors = neighborList;
AtomicCell **matrix = new AtomicCell*[ dimension().totalElements() ] ;
//Set all the pointers to NULL. This will help to check if the cell has
//already been created.
for ( long i = 0; i < dimension().totalElements(); i++ ) {
matrix[i] = NULL;
}
//////////////////////
// cell creation
//////////////////////
AtomicCell *atomicCell;
CellPositionList::const_iterator cursor;
CellPosition counter( dimension().dimension(),0);
register bool overflow = false;
while ( !overflow ){
cellIndex = counter.calculateIndex( dimension() );
//Right now, I have decided to create all the models
//Later, only does models that are required will be created
if (matrix [cellIndex] == NULL) // no encontre
matrix[ cellIndex ] = &( SingleParallelModelAdm::Instance().newAtomicCell( counter, inertial, cellName(counter) ) );
atomicCell = matrix[ cellIndex ];
atomicCell->localFunction( localTransition() );
atomicCell->createNCPorts( NCPorts );
// inserts ordered by id
addModel( *atomicCell );
overflow = counter.next( dimension() );
}//while
/////////////////////
// links creation
/////////////////////
counter.fill( dimension().dimension(), 0 );
overflow = false;
CellPosition auxi;
unsigned long cellIndex2;
list<string>::iterator cursor2;
while ( !overflow )
{
cellIndex = counter.calculateIndex( dimension() );
MASSERTMSG( matrix[ cellIndex ] != NULL, "Incorrect value for matrix in CoupledCell::CreateCells");
for (cursor = neighbors.begin(); cursor != neighbors.end(); cursor++)
{
// get the real position
auxi = *cursor;
auxi += counter;
if ( borderWrapped() )
auxi.canonizar( dimension() );
if ( dimension().includes( auxi ) ) {
cellIndex2 = auxi.calculateIndex( dimension() );
// Add the neighbor port
matrix[ cellIndex2 ]->port( AtomicCell::outPort ).addInfluence(
matrix[ cellIndex ]->port( AtomicCell::neighborChangePort ) );
// Add the other neighbor change ports
for (cursor2 = matrix[ cellIndex2 ]->NCPorts().begin();
cursor2 != matrix[ cellIndex2 ]->NCPorts().end();
cursor2++) {
matrix[ cellIndex2 ]->port( AtomicCell::NCOutPrefix + *cursor2 ).addInfluence(
matrix[ cellIndex ]->port( AtomicCell::NCInPrefix + *cursor2 ) );
}
//addInfluence( matrix[ cellIndex2 ]->description(), AtomicCell::outPort,
//matrix[ cellIndex ]->description(), AtomicCell::neighborChangePort );
}
}
overflow = counter.next( dimension() );
}
delete [] matrix;
return *this;
}
/*******************************************************************
* Method: localTransition
********************************************************************/
CoupledCell &CoupledCell::setLocalTransition( const CellPosition &pos, const LocalTransAdmin::Function &fName )
{
//Attempt to register the transition.
//It might be the case the cell does not belong to the partition.
//In that case, do nothing
unsigned long cellIndex;
cellIndex = cellPartition().cellIndex( pos );
if( cellIndex != CellPartition::InvalidCellIndex ) {
AtomicCell &cell( static_cast< AtomicCell & >( SingleParallelModelAdm::Instance().model( cellName( pos ) ) ) );
cell.localFunction( fName );
}
return *this ;
}
/*******************************************************************
* Method: setCellMachine
********************************************************************/
CoupledCell& CoupledCell::setCellMachine( const CellPosition &pos, const MachineId& m, const ProcId& p)
{
AtomicCell &cell( static_cast< AtomicCell & >( SingleParallelModelAdm::Instance().model( cellName( pos ) ) ) );
cell.setMachineProcId( m , p);;
//Set the parent/child relationship
if ( cellPartition().localCellsInclude( pos ) ) {
cell.parentId( localProc() );
} else {
cell.parentId( procInMachine(m) );
}
return *this ;
}
/*******************************************************************
* Method: setPortInFunction
********************************************************************/
void CoupledCell::setPortInFunction( const CellPosition &pos, const string &sourcePort, const string &actionName )
{
AtomicCell &cell( static_cast< AtomicCell & >( SingleParallelModelAdm::Instance().model( cellName( pos ) ) ) );
cell.setPortInFunction( sourcePort, actionName ); ;
}
/*******************************************************************
* Function Name: setCellValue
* This function will fail if the cell does not have a local processor
********************************************************************/
CoupledCell &CoupledCell::setCellValue( const ModelId &model, const string &port, Value v )
{
AtomicCell &cell( static_cast< AtomicCell & >( SingleParallelModelAdm::Instance().model( model ) ) );
cell.value( Real(v), port );
return *this ;
}
/*******************************************************************
* Function Name: setCellValue
********************************************************************/
CoupledCell &CoupledCell::setCellValue( const CellPosition &pos, const string &port, const Real &v )
{
if ( cellPartition().localCellsInclude( pos ) )
{
string sCellPos = cellName( pos );
setCellValue( sCellPos, port, v);
}
return *this;
}
/*******************************************************************
* Function Name: setCellValue
* This function will fail if the cell does not have a local processor
********************************************************************/
CoupledCell &CoupledCell::setCellValue( const string &sCellPos, const string &port, const Real &v )
{
AtomicCell &cell( static_cast< AtomicCell & >( SingleParallelModelAdm::Instance().model( sCellPos ) ) );
cell.value( v, port );
return *this ;
}
/*******************************************************************
* Function Name: setCellAllValues
* This function will fail if the cell does not have a local processor
********************************************************************/
CoupledCell &CoupledCell::setCellAllValues( const ModelId &model, Value v )
{
AtomicCell &cell( static_cast< AtomicCell & >( SingleParallelModelAdm::Instance().model( model ) ) );
cell.setAllNCPortsValues( Real(v) );
return *this ;
}
/*******************************************************************
* Function Name: setCellAllValues
********************************************************************/
CoupledCell &CoupledCell::setCellAllValues( const CellPosition &pos, const Real &v )
{
if ( cellPartition().localCellsInclude( pos ) )
{
string sCellPos = cellName( pos );
setCellAllValues( sCellPos, v);
}
return *this;
}
/*******************************************************************
* Function Name: setCellAllValues
* This function will fail if the cell does not have a local processor
********************************************************************/
CoupledCell &CoupledCell::setCellAllValues( const string &sCellPos, const Real &v )
{
AtomicCell &cell( static_cast< AtomicCell & >( SingleParallelModelAdm::Instance().model( sCellPos ) ) );
cell.setAllNCPortsValues( v );
return *this ;
}
/*******************************************************************
* Function Name: setVariablesValue
********************************************************************/
CoupledCell &CoupledCell::setVariablesValue( const CellPosition &pos, const char *v )
{
if ( cellPartition().localCellsInclude( pos ) )
{
string sCellPos = cellName( pos );
setVariablesValue( sCellPos, v );
}
return *this;
}
/*******************************************************************
* Function Name: setVariablesValue
* This function will fail if the cell does not have a local processor
********************************************************************/
CoupledCell &CoupledCell::setVariablesValue( const string &sCellPos, const char *v )
{
AtomicCell &cell( static_cast< AtomicCell & >( SingleParallelModelAdm::Instance().model( sCellPos ) ) );
cell.variables().setValues( string( v ) );
return *this ;
}
/*******************************************************************
* Function Name: totalProcCount
********************************************************************/
unsigned long CoupledCell::totalProcCount() const
{
long count = 0;
count = dimension().totalElements();
count += modelPartition().size();
return count;
}
/*******************************************************************
* Function Name: localProcCount
********************************************************************/
unsigned long CoupledCell::localProcCount() const
{
long count = 0;
count = cellPartition().totalLocalElements();
if ( localProc() != ParallelProcessor::InvalidId )
count++;
return count;
}
/*******************************************************************
* Function Name: inverseNeighborhood
********************************************************************/
CellPosition CoupledCell::inverseNeighborhood( const CellPosition& center, const CellPosition& other) const
{
if ( !borderWrapped() )
{
CellPosition nPos = other;
nPos -= center;
return nPos;
}
else
{
//if the border is wrapped, check each neighbor
for( CellPositionList::const_iterator cursor = neighborhood().begin();
cursor != neighborhood().end(); cursor++)
{
CellPosition nPos = *cursor;
nPos += center;
nPos.canonizar( dimension() );
if (nPos == other)
return *cursor;
}//for
}//if
MException e;
e.addText( "inverseNeighborhood: No neighbor found!");
MTHROW ( e );
}