MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Msg.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2009 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include "header.h"
11 #include "SingleMsg.h"
12 #include "DiagonalMsg.h"
13 #include "OneToOneMsg.h"
14 #include "OneToOneDataIndexMsg.h"
15 #include "OneToAllMsg.h"
16 #include "SparseMatrix.h"
17 #include "SparseMsg.h"
18 #include "../shell/Shell.h" // For the myNode() and numNodes() definitions
19 #include "MsgElement.h"
20 
21 #include "../shell/Shell.h"
22 
24 
25 // Static field declaration.
27 bool Msg::lastTrump_ = false;
28 const Msg* Msg::lastMsg_ = 0;
29 
30 Msg::Msg( ObjId mid, Element* e1, Element* e2 )
31  : mid_( mid), e1_( e1 ), e2_( e2 )
32 {
33  e1->addMsg( mid_ );
34  e2->addMsg( mid_ );
35  lastMsg_ = this;
36 }
37 
39 {
40  if ( !lastTrump_ ) {
41  e1_->dropMsg( mid_ );
42  e2_->dropMsg( mid_ );
43  }
44 
45  /*
46  if ( mid_ > 1 )
47  garbageMsg_.push_back( mid_ );
48  */
49 }
50 
51 // Static func
52 void Msg::deleteMsg( ObjId mid )
53 {
54  const Msg* msg = getMsg( mid );
55  delete( msg );
56 }
57 
58 // Static func
59 const Msg* Msg::getMsg( ObjId m )
60 {
61  return reinterpret_cast< const Msg* >( m.data() );
62 }
63 
67 Id Msg::getE1() const
68 {
69  return e1_->id();
70 }
71 
75 Id Msg::getE2() const
76 {
77  return e2_->id();
78 }
79 
80 vector< string > Msg::getSrcFieldsOnE1() const
81 {
82  vector< pair< BindIndex, FuncId > > ids;
83  vector< string > ret;
84 
86 
87  for ( unsigned int i = 0; i < ids.size(); ++i ) {
88  string name = e1_->cinfo()->srcFinfoName( ids[i].first );
89  if ( name == "" ) {
90  cout << "Error: Msg::getSrcFieldsOnE1: Failed to find field on msg " <<
91  e1_->getName() << "-->" << e2_->getName() << endl;
92  } else {
93  ret.push_back( name );
94  }
95  }
96  return ret;
97 }
98 
99 vector< string > Msg::getDestFieldsOnE2() const
100 {
101  vector< pair< BindIndex, FuncId > > ids;
102  vector< string > ret;
103 
105 
106  for ( unsigned int i = 0; i < ids.size(); ++i ) {
107  string name = e2_->cinfo()->destFinfoName( ids[i].second );
108  if ( name == "" ) {
109  cout << "Error: Msg::getDestFieldsOnE2: Failed to find field on msg " <<
110  e1_->getName() << "-->" << e2_->getName() << endl;
111  } else {
112  ret.push_back( name );
113  }
114  }
115  return ret;
116 }
117 
118 vector< string > Msg::getSrcFieldsOnE2() const
119 {
120  vector< pair< BindIndex, FuncId > > ids;
121  vector< string > ret;
122 
124 
125  for ( unsigned int i = 0; i < ids.size(); ++i ) {
126  string name = e2_->cinfo()->srcFinfoName( ids[i].first );
127  if ( name == "" ) {
128  cout << "Error: Msg::getSrcFieldsOnE2: Failed to find field on msg " <<
129  e1_->getName() << "-->" << e2_->getName() << endl;
130  } else {
131  ret.push_back( name );
132  }
133  }
134  return ret;
135 }
136 
137 vector< string > Msg::getDestFieldsOnE1() const
138 {
139  vector< pair< BindIndex, FuncId > > ids;
140  vector< string > ret;
141 
143 
144  for ( unsigned int i = 0; i < ids.size(); ++i ) {
145  string name = e1_->cinfo()->destFinfoName( ids[i].second );
146  if ( name == "" ) {
147  cout << "Error: Msg::getDestFieldsOnE1: Failed to find field on msg " <<
148  e1_->getName() << "-->" << e2_->getName() << endl;
149  } else {
150  ret.push_back( name );
151  }
152  }
153  return ret;
154 }
155 
157 {
158  return findOtherEnd(obj);
159 }
160 
162 // Here we set up the Element related stuff for Msgs.
164 
166 {
168  // Field definitions.
171  "e1",
172  "Id of source Element.",
173  &Msg::getE1
174  );
176  "e2",
177  "Id of source Element.",
178  &Msg::getE2
179  );
180 
181  static ReadOnlyValueFinfo< Msg, vector< string > > srcFieldsOnE1(
182  "srcFieldsOnE1",
183  "Names of SrcFinfos for messages going from e1 to e2. There are"
184  "matching entries in the destFieldsOnE2 vector",
186  );
187  static ReadOnlyValueFinfo< Msg, vector< string > > destFieldsOnE2(
188  "destFieldsOnE2",
189  "Names of DestFinfos for messages going from e1 to e2. There are"
190  "matching entries in the srcFieldsOnE1 vector",
192  );
193  static ReadOnlyValueFinfo< Msg, vector< string > > srcFieldsOnE2(
194  "srcFieldsOnE2",
195  "Names of SrcFinfos for messages going from e2 to e1. There are"
196  "matching entries in the destFieldsOnE1 vector",
198  );
199  static ReadOnlyValueFinfo< Msg, vector< string > > destFieldsOnE1(
200  "destFieldsOnE1",
201  "Names of destFinfos for messages going from e2 to e1. There are"
202  "matching entries in the srcFieldsOnE2 vector",
204  );
205 
207  "adjacent",
208  "The element adjacent to the specified element",
210 
211  static Finfo* msgFinfos[] = {
212  &e1, // readonly value
213  &e2, // readonly value
214  &srcFieldsOnE1, // readonly value
215  &destFieldsOnE2, // readonly value
216  &srcFieldsOnE2, // readonly value
217  &destFieldsOnE1, // readonly value
218  &adjacent, // readonly lookup value
219  };
220 
221  static Cinfo msgCinfo (
222  "Msg", // name
223  Neutral::initCinfo(), // base class
224  msgFinfos,
225  sizeof( msgFinfos ) / sizeof( Finfo* ), // num Fields
226  0
227  // new Dinfo< Msg >()
228  );
229 
230  return &msgCinfo;
231 }
232 
233 static const Cinfo* msgCinfo = Msg::initCinfo();
234 
235 // Static func. Returns the index to use for msgIndex.
236 unsigned int Msg::initMsgManagers()
237 {
238  Dinfo< short > dummyDinfo;
239 
240  // This is to be the parent of all the msg managers.
242  new GlobalDataElement(
243  msgManagerId_, Neutral::initCinfo(), "Msgs", 1 );
244 
247  "singleMsg", &SingleMsg::numMsg, &SingleMsg::lookupMsg );
248 
251  "oneToOneMsg", &OneToOneMsg::numMsg, &OneToOneMsg::lookupMsg );
252 
255  "oneToAllMsg", &OneToAllMsg::numMsg, &OneToAllMsg::lookupMsg );
256 
259  "diagonalMsg", &DiagonalMsg::numMsg, &DiagonalMsg::lookupMsg );
260 
263  "sparseMsg", &SparseMsg::numMsg, &SparseMsg::lookupMsg );
264 
268  "oneToOneDataIndexMsg",
271 
272  // Do the 'adopt' only after all the message managers exist - we need
273  // the OneToAll manager for the adoption messages themselves.
274  assert( OneToAllMsg::numMsg() == 0 );
275  unsigned int n = 1;
276  Shell::adopt( Id(), msgManagerId_, n++ );
282 
283  return n;
284 }
285 
287 {
288  lastTrump_ = true;
289  for ( unsigned int i = 0; i < SingleMsg::numMsg(); ++i ) {
290  Msg* m = reinterpret_cast< Msg* >( SingleMsg::lookupMsg( i ) );
291  if ( m ) delete m;
292  }
293  for ( unsigned int i = 0; i < OneToOneMsg::numMsg(); ++i ) {
294  Msg* m = reinterpret_cast< Msg* >( OneToOneMsg::lookupMsg( i ) );
295  if ( m ) delete m;
296  }
297  for ( unsigned int i = 0; i < OneToAllMsg::numMsg(); ++i ) {
298  Msg* m = reinterpret_cast< Msg* >( OneToAllMsg::lookupMsg( i ) );
299  if ( m ) delete m;
300  }
301  for ( unsigned int i = 0; i < DiagonalMsg::numMsg(); ++i ) {
302  Msg* m = reinterpret_cast< Msg* >( DiagonalMsg::lookupMsg( i ) );
303  if ( m ) delete m;
304  }
305  for ( unsigned int i = 0; i < SparseMsg::numMsg(); ++i ) {
306  Msg* m = reinterpret_cast< Msg* >( SparseMsg::lookupMsg( i ) );
307  if ( m ) delete m;
308  }
309 }
310 
316 {
317  return lastMsg_;
318 }
319 
321 {
322  return lastTrump_;
323 }
static char * lookupMsg(unsigned int index)
Static function for Msg access.
virtual ObjId findOtherEnd(ObjId) const =0
Element * e2() const
Definition: Msg.h:68
static Id managerId_
Definition: OneToOneMsg.h:50
ObjId getAdjacent(ObjId) const
Definition: Msg.cpp:156
static char * lookupMsg(unsigned int index)
Static function for Msg access.
static const Cinfo * initCinfo()
Setup function for Element-style access to Msg fields.
char * data() const
Definition: ObjId.cpp:113
Id getE1() const
Definition: Msg.cpp:67
Definition: Dinfo.h:60
vector< string > getDestFieldsOnE2() const
Definition: Msg.cpp:99
Definition: ObjId.h:20
static const Cinfo * initCinfo()
Setup function for Element-style access to Msg fields.
void dropMsg(ObjId mid)
Definition: Element.cpp:113
static const Cinfo * initCinfo()
Setup function for Element-style access to Msg fields.
Id id() const
Definition: Element.cpp:71
unsigned int getFieldsOfOutgoingMsg(ObjId mid, vector< pair< BindIndex, FuncId > > &ret) const
Definition: Element.cpp:1009
static Id managerId_
Definition: OneToAllMsg.h:50
static Id nextId()
Definition: Id.cpp:132
static unsigned int numMsg()
Msg lookup functions.
static bool isLastTrump()
True when MOOSE has been terminated and is being cleaned up.
Definition: Msg.cpp:320
static char * lookupMsg(unsigned int index)
Static function for Msg access.
static bool lastTrump_
Flag to indicate termination of program.
Definition: Msg.h:189
static void deleteMsg(ObjId mid)
Definition: Msg.cpp:52
static unsigned int numMsg()
Msg lookup functions.
static char * lookupMsg(unsigned int index)
Static function for Msg access.
Definition: SparseMsg.cpp:643
static Id msgManagerId_
Element 2 attached to Msg.
Definition: Msg.h:186
Id getE2() const
Definition: Msg.cpp:75
static const Cinfo * msgCinfo
Definition: Msg.cpp:233
static const Cinfo * initCinfo()
Setup function for Element-style access to Msg fields.
const string & destFinfoName(FuncId fid) const
Definition: Cinfo.cpp:569
static Id managerId_
Definition: SparseMsg.h:148
virtual ~Msg()
Destructor.
Definition: Msg.cpp:38
static unsigned int numMsg()
Msg lookup functions.
Definition: SparseMsg.cpp:637
const Cinfo * cinfo() const
Definition: Element.cpp:66
void addMsg(ObjId mid)
Definition: Element.cpp:79
Definition: Msg.h:18
Element * e1_
Index of this Msg on the msg_ vector.
Definition: Msg.h:180
ObjId mid_
Definition: Msg.h:178
Element * e1() const
Definition: Msg.h:61
static const Msg * lastMsg()
Definition: Msg.cpp:315
static char name[]
Definition: mfield.cpp:401
static unsigned int numMsg()
Msg lookup functions.
static const Cinfo * initCinfo()
Definition: SparseMsg.cpp:24
static const Cinfo * initCinfo()
Definition: SingleMsg.cpp:148
static unsigned int initMsgManagers()
Definition: Msg.cpp:236
vector< string > getSrcFieldsOnE1() const
Definition: Msg.cpp:80
static bool adopt(ObjId parent, Id child, unsigned int msgIndex)
Definition: Shell.cpp:654
vector< string > getSrcFieldsOnE2() const
Definition: Msg.cpp:118
Definition: Id.h:17
static char * lookupMsg(unsigned int index)
Static function for Msg access.
static const Msg * getMsg(ObjId m)
Definition: Msg.cpp:59
const string & srcFinfoName(BindIndex bid) const
Definition: Cinfo.cpp:552
Element * e2_
Element 1 attached to Msg.
Definition: Msg.h:181
static const Cinfo * initCinfo()
Definition: Neutral.cpp:16
static unsigned int numMsg()
Msg lookup functions.
Definition: SingleMsg.cpp:223
vector< string > getDestFieldsOnE1() const
Definition: Msg.cpp:137
static Id managerId_
Definition: DiagonalMsg.h:71
const string & getName() const
Definition: Element.cpp:56
Definition: Cinfo.h:18
static const Msg * lastMsg_
Definition: Msg.h:192
Msg(ObjId mid, Element *e1, Element *e2)
Constructor.
Definition: Msg.cpp:30
static char * lookupMsg(unsigned int index)
Static function for Msg access.
Definition: SingleMsg.cpp:229
static Id managerId_
Definition: SingleMsg.h:61
static const Cinfo * initCinfo()
Definition: Msg.cpp:165
static void clearAllMsgs()
Definition: Msg.cpp:286
static unsigned int numMsg()
Assign the first DataId.
Definition: Finfo.h:12