Xyce  6.1
N_DEV_Algorithm.h
Go to the documentation of this file.
1 //-------------------------------------------------------------------------
2 // Copyright Notice
3 //
4 // Copyright 2002 Sandia Corporation. Under the terms
5 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
6 // Government retains certain rights in this software.
7 //
8 // Xyce(TM) Parallel Electrical Simulator
9 // Copyright (C) 2002-2015 Sandia Corporation
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //-------------------------------------------------------------------------
24 
25 //-------------------------------------------------------------------------
26 // Filename : $RCSfile: N_DEV_Algorithm.h,v $
27 //
28 // Purpose :
29 //
30 //
31 //
32 // Special Notes :
33 //
34 //
35 // Creator :
36 //
37 // Creation Date :
38 //
39 // Revision Information:
40 // ---------------------
41 //
42 // Revision Number: $Revision: 1.11.2.1 $
43 //
44 // Revision Date : $Date: 2015/04/02 18:20:09 $
45 //
46 // Current Owner : $Author: tvrusso $
47 //-------------------------------------------------------------------------
48 
49 #ifndef Xyce_N_DEV_Algorithm_h
50 #define Xyce_N_DEV_Algorithm_h
51 
52 #include <iterator>
53 #include <vector>
54 
55 #include <N_DEV_fwd.h>
56 #include <N_PDS_fwd.h>
57 
58 #include <N_DEV_Device.h>
59 
60 namespace Xyce {
61 namespace Device {
62 
63 
64 // Ultimately making std::for_each work would be most awesome. But that will take some doing.
65 
66 //-----------------------------------------------------------------------------
67 // Function : forEachInstance
68 // Purpose :
69 // Special Notes :
70 // Scope : public
71 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
72 // Creation Date : Tue Apr 22 12:04:01 2014
73 //-----------------------------------------------------------------------------
74 ///
75 /// Call forEachInstance on object d, passing operator op
76 ///
77 /// @param d Object to call forEachInstance on
78 /// @param op Operator to call passing in DeviceInstance pointer
79 ///
80 template <class D, class Op>
81 void forEachInstance(const D &d, Op op) {
82  d.forEachInstance(op);
83 }
84 
85 
86 //-----------------------------------------------------------------------------
87 // Function : forEachModel
88 // Purpose :
89 // Special Notes :
90 // Scope : public
91 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
92 // Creation Date : Tue Apr 22 12:04:01 2014
93 //-----------------------------------------------------------------------------
94 ///
95 /// Call forEachModel on object d, passing operator op
96 ///
97 /// @param d Object to call forEachModel on
98 /// @param op Operator to call passing in DeviceModel pointer
99 ///
100 template <class D, class Op>
101 void forEachModel(const D &d, Op op) {
102  d.forEachModel(op);
103 }
104 
105 
106 //-----------------------------------------------------------------------------
107 // Function : getName
108 // Purpose :
109 // Special Notes :
110 // Scope : public
111 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
112 // Creation Date : Tue Apr 22 11:12:37 2014
113 //-----------------------------------------------------------------------------
114 ///
115 /// Returns the name of the specified object
116 ///
117 /// @param c object to return associated name
118 ///
119 /// @return name associated with the object
120 ///
121 template <class C>
122 const std::string &getName(const C *c);
123 
124 
125 //-----------------------------------------------------------------------------
126 // Class : DeviceInstanceOutIteratorOp
127 // Purpose :
128 // Special Notes :
129 // Scope : public
130 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
131 // Creation Date : Tue Apr 22 10:58:03 2014
132 //-----------------------------------------------------------------------------
133 ///
134 /// Operator to populate a container via an output iterator with device instances.
135 ///
136 template <class Out>
138 {
139  //-----------------------------------------------------------------------------
140  // Function : DeviceInstanceOutIteratorOp
141  // Purpose :
142  // Special Notes :
143  // Scope : public
144  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
145  // Creation Date : Tue Apr 22 10:59:27 2014
146  //-----------------------------------------------------------------------------
147  ///
148  /// Copies output iterator into operator
149  ///
151  : it_(it)
152  {}
153 
154  //-----------------------------------------------------------------------------
155  // Function : operator()
156  // Purpose :
157  // Special Notes :
158  // Scope : public
159  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
160  // Creation Date : Tue Apr 22 11:00:21 2014
161  //-----------------------------------------------------------------------------
162  ///
163  /// Copies instance pointer through output iterator
164  ///
165  virtual bool operator()(DeviceInstance *instance) {
166  (*it_)++ = instance;
167 
168  return true;
169  }
170 
171  Out it_; ///< Output iterator
172 };
173 
174 
175 //-----------------------------------------------------------------------------
176 // Class : DeviceModelOutIteratorOp
177 // Purpose :
178 // Special Notes :
179 // Scope : public
180 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
181 // Creation Date : Tue Apr 22 10:58:03 2014
182 //-----------------------------------------------------------------------------
183 ///
184 /// Operator to populate a container via an output iterator with device models.
185 ///
186 template <class Out>
188 {
189  //-----------------------------------------------------------------------------
190  // Function : DeviceModelOutIteratorOp
191  // Purpose :
192  // Special Notes :
193  // Scope : public
194  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
195  // Creation Date : Tue Apr 22 10:59:27 2014
196  //-----------------------------------------------------------------------------
197  ///
198  /// Copies output iterator into operator
199  ///
201  : it_(it)
202  {}
203 
204  //-----------------------------------------------------------------------------
205  // Function : operator()
206  // Purpose :
207  // Special Notes :
208  // Scope : public
209  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
210  // Creation Date : Tue Apr 22 11:00:21 2014
211  //-----------------------------------------------------------------------------
212  ///
213  /// Copies model pointer through output iterator
214  ///
215  virtual bool operator()(DeviceModel *model) {
216  (*it_)++ = model;
217 
218  return true;
219  }
220 
221  Out it_; ///< Output iterator
222 };
223 
224 
225 //-----------------------------------------------------------------------------
226 // Class : DeviceInstanceNameOp
227 // Purpose :
228 // Special Notes :
229 // Scope : public
230 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
231 // Creation Date : Tue Apr 22 10:58:03 2014
232 //-----------------------------------------------------------------------------
233 ///
234 /// Operator to populate a container of strings with the names of the device instances.
235 ///
236 template<class Out>
238 {
239  //-----------------------------------------------------------------------------
240  // Function : DeviceInstanceNameOp
241  // Purpose :
242  // Special Notes :
243  // Scope : public
244  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
245  // Creation Date : Tue Apr 22 10:59:27 2014
246  //-----------------------------------------------------------------------------
247  ///
248  /// Copies output iterator to it_
249  ///
251  : it_(it)
252  {}
253 
254  //-----------------------------------------------------------------------------
255  // Function : operator
256  // Purpose :
257  // Special Notes :
258  // Scope : public
259  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
260  // Creation Date : Tue Apr 22 12:08:10 2014
261  //-----------------------------------------------------------------------------
262  ///
263  /// Write the result of getName(instance) to the output iterator
264  ///
265  virtual bool operator()(DeviceInstance *device_instance) {
266  (*it_)++ = getName(device_instance);
267 
268  return true;
269  }
270 
271  Out it_; ///< Output iterator
272 };
273 
274 
275 //-----------------------------------------------------------------------------
276 // Function : MapOp
277 // Purpose :
278 // Special Notes :
279 // Scope : public
280 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
281 // Creation Date : Tue Apr 22 12:19:13 2014
282 //-----------------------------------------------------------------------------
283 ///
284 /// operator to populate a map from device instance name to device instance pointer
285 ///
286 template<class C>
287 struct MapOp : public DeviceInstanceOp
288 {
289  //-----------------------------------------------------------------------------
290  // Function : MapOp
291  // Purpose :
292  // Special Notes :
293  // Scope : public
294  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
295  // Creation Date : Tue Apr 22 12:19:57 2014
296  //-----------------------------------------------------------------------------
297  ///
298  /// Destination map reference into the operator
299  ///
300  /// @param map map to insert name to instance mapping
301  ///
302  MapOp(std::map<std::string, C *> &map)
303  : map_(map)
304  {}
305 
306  //-----------------------------------------------------------------------------
307  // Function : operator()
308  // Purpose :
309  // Special Notes :
310  // Scope : public
311  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
312  // Creation Date : Tue Apr 22 12:21:47 2014
313  //-----------------------------------------------------------------------------
314  ///
315  ///
316  ///
317  virtual bool operator()(DeviceInstance *device_instance)
318  {
319  map_[getName(device_instance)] = static_cast<C *>(device_instance);
320  return true;
321  }
322 
323  std::map<std::string, C *> & map_;
324 };
325 
326 
327 //-----------------------------------------------------------------------------
328 // Function : getDeviceInstances
329 // Purpose :
330 // Special Notes :
331 // Scope : public
332 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
333 // Creation Date : Tue Apr 22 11:02:16 2014
334 //-----------------------------------------------------------------------------
335 ///
336 /// Calls forEachInstance() on d which iterates through all the instances copies them to the output iterator.
337 ///
338 /// @param d D to call forEachInstance
339 /// @param it output iterator
340 ///
341 template <class D, class Out>
342 void getDeviceInstances(const D &d, Out it) {
344 }
345 
346 
347 //-----------------------------------------------------------------------------
348 // Function : getDeviceModels
349 // Purpose :
350 // Special Notes :
351 // Scope : public
352 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
353 // Creation Date : Tue Apr 22 11:02:16 2014
354 //-----------------------------------------------------------------------------
355 ///
356 /// Calls forEachModel() on d which iterates through all the models copies them to the output iterator.
357 ///
358 /// @param d D to call forEachModel
359 /// @param it output iterator
360 ///
361 template <class D, class Out>
362 void getDeviceModels(const D &d, Out it)
363 {
365 }
366 
367 
368 //-----------------------------------------------------------------------------
369 // Function : getDeviceInstanceNames
370 // Purpose :
371 // Special Notes :
372 // Scope : public
373 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
374 // Creation Date : Tue Apr 22 12:22:16 2014
375 //-----------------------------------------------------------------------------
376 ///
377 /// Calls forEachInstance() on d which iterates through all the instance and copies the name to the output iterator.
378 ///
379 /// @param d D to call forEachInstance
380 /// @param it output iterator
381 ///
382 template<class D, class Out>
383 void getDeviceInstanceNames(const D &d, Out it)
384 {
386 }
387 
388 
389 //-----------------------------------------------------------------------------
390 // Function : mapDeviceInstances
391 // Purpose :
392 // Special Notes :
393 // Scope : public
394 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
395 // Creation Date : Tue Apr 22 12:22:22 2014
396 //-----------------------------------------------------------------------------
397 ///
398 /// Calls forEachInstance() on d which iterates through all the instances and inserts the device instance name to pointer mapping.
399 ///
400 /// @param d D to call forEachInstance
401 /// @param it output iterator
402 ///
403 template<class D, class X>
404 void mapDeviceInstances(const D &d, std::map<std::string, X *> &map) {
405  forEachInstance(d, MapOp<X>(map));
406 }
407 
408 // //-----------------------------------------------------------------------------
409 // // Function : allDevicesConverged
410 // // Purpose :
411 // // Special Notes :
412 // // Scope : public
413 // // Creator : Eric Keiter, SNL
414 // // Creation Date : 03/22/06
415 // //-----------------------------------------------------------------------------
416 // // convergence: allow devices to signal back to the solvers that
417 // // they've played some game that invalidates normal convergence tests,
418 // // and so the solution should be considered unconverged no matter how
419 // // small the various norms are.
420 // bool allDevicesConverged();
421 
422 //-----------------------------------------------------------------------------
423 // Function : innerDevsConverged
424 // Purpose :
425 // Special Notes :
426 // Scope : public
427 // Creator : Eric Keiter, SNL
428 // Creation Date : 03/22/06
429 //-----------------------------------------------------------------------------
430 bool devicesConverged(Parallel::Machine comm, const InstanceVector &extern_devices);
431 
432 } // namespace Device
433 } // namespace Xyce
434 
435 #endif // Xyce_N_DEV_Algorithm_h
std::map< std::string, C * > & map_
void forEachModel(const D &d, Op op)
Call forEachModel on object d, passing operator op.
operator to populate a map from device instance name to device instance pointer
Pure virtual class to augment a linear system.
void forEachInstance(const D &d, Op op)
Call forEachInstance on object d, passing operator op.
void getDeviceInstances(const D &d, Out it)
Calls forEachInstance() on d which iterates through all the instances copies them to the output itera...
Operator to populate a container of strings with the names of the device instances.
Operator to populate a container via an output iterator with device instances.
DeviceInstanceNameOp(Out it)
Copies output iterator to it_.
void getDeviceModels(const D &d, Out it)
Calls forEachModel() on d which iterates through all the models copies them to the output iterator...
const std::string & getName(const C *c)
Returns the name of the specified object.
virtual bool operator()(DeviceModel *model)
Copies model pointer through output iterator.
Operator to populate a container via an output iterator with device models.
bool devicesConverged(Parallel::Machine comm, const InstanceVector &extern_devices)
virtual bool operator()(DeviceInstance *device_instance)
DeviceInstanceOutIteratorOp(Out it)
Copies output iterator into operator.
void mapDeviceInstances(const D &d, std::map< std::string, X * > &map)
Calls forEachInstance() on d which iterates through all the instances and inserts the device instance...
DeviceModelOutIteratorOp(Out it)
Copies output iterator into operator.
virtual bool operator()(DeviceInstance *instance)
Copies instance pointer through output iterator.
std::vector< DeviceInstance * > InstanceVector
Definition: N_DEV_fwd.h:182
virtual bool operator()(DeviceInstance *device_instance)
Write the result of getName(instance) to the output iterator.
void getDeviceInstanceNames(const D &d, Out it)
Calls forEachInstance() on d which iterates through all the instance and copies the name to the outpu...
MapOp(std::map< std::string, C * > &map)
Destination map reference into the operator.