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.13 $
43 //
44 // Revision Date : $Date: 2015/10/14 19:31:37 $
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 {
83  d.forEachInstance(op);
84 }
85 
86 
87 //-----------------------------------------------------------------------------
88 // Function : forEachModel
89 // Purpose :
90 // Special Notes :
91 // Scope : public
92 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
93 // Creation Date : Tue Apr 22 12:04:01 2014
94 //-----------------------------------------------------------------------------
95 ///
96 /// Call forEachModel on object d, passing operator op
97 ///
98 /// @param d Object to call forEachModel on
99 /// @param op Operator to call passing in DeviceModel pointer
100 ///
101 template <class D, class Op>
102 void forEachModel(const D &d, Op op)
103 {
104  d.forEachModel(op);
105 }
106 
107 
108 //-----------------------------------------------------------------------------
109 // Function : getName
110 // Purpose :
111 // Special Notes :
112 // Scope : public
113 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
114 // Creation Date : Tue Apr 22 11:12:37 2014
115 //-----------------------------------------------------------------------------
116 ///
117 /// Returns the name of the specified object
118 ///
119 /// @param c object to return associated name
120 ///
121 /// @return name associated with the object
122 ///
123 template <class C>
124 const std::string &getName(const C *c);
125 
126 
127 //-----------------------------------------------------------------------------
128 // Class : DeviceInstanceOutIteratorOp
129 // Purpose :
130 // Special Notes :
131 // Scope : public
132 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
133 // Creation Date : Tue Apr 22 10:58:03 2014
134 //-----------------------------------------------------------------------------
135 ///
136 /// Operator to populate a container via an output iterator with device instances.
137 ///
138 template <class Out>
140 {
141  //-----------------------------------------------------------------------------
142  // Function : DeviceInstanceOutIteratorOp
143  // Purpose :
144  // Special Notes :
145  // Scope : public
146  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
147  // Creation Date : Tue Apr 22 10:59:27 2014
148  //-----------------------------------------------------------------------------
149  ///
150  /// Copies output iterator into operator
151  ///
153  : it_(it)
154  {}
155 
156  //-----------------------------------------------------------------------------
157  // Function : operator()
158  // Purpose :
159  // Special Notes :
160  // Scope : public
161  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
162  // Creation Date : Tue Apr 22 11:00:21 2014
163  //-----------------------------------------------------------------------------
164  ///
165  /// Copies instance pointer through output iterator
166  ///
167  virtual bool operator()(DeviceInstance *instance)
168  {
169  (*it_)++ = instance;
170 
171  return true;
172  }
173 
174  Out it_; ///< Output iterator
175 };
176 
177 
178 //-----------------------------------------------------------------------------
179 // Class : DeviceModelOutIteratorOp
180 // Purpose :
181 // Special Notes :
182 // Scope : public
183 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
184 // Creation Date : Tue Apr 22 10:58:03 2014
185 //-----------------------------------------------------------------------------
186 ///
187 /// Operator to populate a container via an output iterator with device models.
188 ///
189 template <class Out>
191 {
192  //-----------------------------------------------------------------------------
193  // Function : DeviceModelOutIteratorOp
194  // Purpose :
195  // Special Notes :
196  // Scope : public
197  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
198  // Creation Date : Tue Apr 22 10:59:27 2014
199  //-----------------------------------------------------------------------------
200  ///
201  /// Copies output iterator into operator
202  ///
204  : it_(it)
205  {}
206 
207  //-----------------------------------------------------------------------------
208  // Function : operator()
209  // Purpose :
210  // Special Notes :
211  // Scope : public
212  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
213  // Creation Date : Tue Apr 22 11:00:21 2014
214  //-----------------------------------------------------------------------------
215  ///
216  /// Copies model pointer through output iterator
217  ///
218  virtual bool operator()(DeviceModel *model)
219  {
220  (*it_)++ = model;
221 
222  return true;
223  }
224 
225  Out it_; ///< Output iterator
226 };
227 
228 
229 //-----------------------------------------------------------------------------
230 // Class : DeviceInstanceNameOp
231 // Purpose :
232 // Special Notes :
233 // Scope : public
234 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
235 // Creation Date : Tue Apr 22 10:58:03 2014
236 //-----------------------------------------------------------------------------
237 ///
238 /// Operator to populate a container of strings with the names of the device instances.
239 ///
240 template<class Out>
242 {
243  //-----------------------------------------------------------------------------
244  // Function : DeviceInstanceNameOp
245  // Purpose :
246  // Special Notes :
247  // Scope : public
248  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
249  // Creation Date : Tue Apr 22 10:59:27 2014
250  //-----------------------------------------------------------------------------
251  ///
252  /// Copies output iterator to it_
253  ///
255  : it_(it)
256  {}
257 
258  //-----------------------------------------------------------------------------
259  // Function : operator
260  // Purpose :
261  // Special Notes :
262  // Scope : public
263  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
264  // Creation Date : Tue Apr 22 12:08:10 2014
265  //-----------------------------------------------------------------------------
266  ///
267  /// Write the result of getName(instance) to the output iterator
268  ///
269  virtual bool operator()(DeviceInstance *device_instance)
270  {
271  (*it_)++ = getName(device_instance);
272 
273  return true;
274  }
275 
276  Out it_; ///< Output iterator
277 };
278 
279 
280 //-----------------------------------------------------------------------------
281 // Function : MapOp
282 // Purpose :
283 // Special Notes :
284 // Scope : public
285 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
286 // Creation Date : Tue Apr 22 12:19:13 2014
287 //-----------------------------------------------------------------------------
288 ///
289 /// operator to populate a map from device instance name to device instance pointer
290 ///
291 template<class C>
292 struct MapOp : public DeviceInstanceOp
293 {
294  //-----------------------------------------------------------------------------
295  // Function : MapOp
296  // Purpose :
297  // Special Notes :
298  // Scope : public
299  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
300  // Creation Date : Tue Apr 22 12:19:57 2014
301  //-----------------------------------------------------------------------------
302  ///
303  /// Destination map reference into the operator
304  ///
305  /// @param map map to insert name to instance mapping
306  ///
307  MapOp(std::map<std::string, C *> &map)
308  : map_(map)
309  {}
310 
311  //-----------------------------------------------------------------------------
312  // Function : operator()
313  // Purpose :
314  // Special Notes :
315  // Scope : public
316  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
317  // Creation Date : Tue Apr 22 12:21:47 2014
318  //-----------------------------------------------------------------------------
319  ///
320  ///
321  ///
322  virtual bool operator()(DeviceInstance *device_instance)
323  {
324  map_[getName(device_instance)] = static_cast<C *>(device_instance);
325  return true;
326  }
327 
328  std::map<std::string, C *> & map_;
329 };
330 
331 
332 //-----------------------------------------------------------------------------
333 // Function : getDeviceInstances
334 // Purpose :
335 // Special Notes :
336 // Scope : public
337 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
338 // Creation Date : Tue Apr 22 11:02:16 2014
339 //-----------------------------------------------------------------------------
340 ///
341 /// Calls forEachInstance() on d which iterates through all the instances copies them to the output iterator.
342 ///
343 /// @param d D to call forEachInstance
344 /// @param it output iterator
345 ///
346 template <class D, class Out>
347 void getDeviceInstances(const D &d, Out it)
348 {
350 }
351 
352 
353 //-----------------------------------------------------------------------------
354 // Function : getDeviceModels
355 // Purpose :
356 // Special Notes :
357 // Scope : public
358 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
359 // Creation Date : Tue Apr 22 11:02:16 2014
360 //-----------------------------------------------------------------------------
361 ///
362 /// Calls forEachModel() on d which iterates through all the models copies them to the output iterator.
363 ///
364 /// @param d D to call forEachModel
365 /// @param it output iterator
366 ///
367 template <class D, class Out>
368 void getDeviceModels(const D &d, Out it)
369 {
371 }
372 
373 
374 //-----------------------------------------------------------------------------
375 // Function : getDeviceInstanceNames
376 // Purpose :
377 // Special Notes :
378 // Scope : public
379 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
380 // Creation Date : Tue Apr 22 12:22:16 2014
381 //-----------------------------------------------------------------------------
382 ///
383 /// Calls forEachInstance() on d which iterates through all the instance and copies the name to the output iterator.
384 ///
385 /// @param d D to call forEachInstance
386 /// @param it output iterator
387 ///
388 template<class D, class Out>
389 void getDeviceInstanceNames(const D &d, Out it)
390 {
392 }
393 
394 
395 //-----------------------------------------------------------------------------
396 // Function : mapDeviceInstances
397 // Purpose :
398 // Special Notes :
399 // Scope : public
400 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
401 // Creation Date : Tue Apr 22 12:22:22 2014
402 //-----------------------------------------------------------------------------
403 ///
404 /// Calls forEachInstance() on d which iterates through all the instances and inserts the device instance name to pointer mapping.
405 ///
406 /// @param d D to call forEachInstance
407 /// @param it output iterator
408 ///
409 template<class D, class X>
410 void mapDeviceInstances(const D &d, std::map<std::string, X *> &map)
411 {
412  forEachInstance(d, MapOp<X>(map));
413 }
414 
415 // //-----------------------------------------------------------------------------
416 // // Function : allDevicesConverged
417 // // Purpose :
418 // // Special Notes :
419 // // Scope : public
420 // // Creator : Eric Keiter, SNL
421 // // Creation Date : 03/22/06
422 // //-----------------------------------------------------------------------------
423 // // convergence: allow devices to signal back to the solvers that
424 // // they've played some game that invalidates normal convergence tests,
425 // // and so the solution should be considered unconverged no matter how
426 // // small the various norms are.
427 // bool allDevicesConverged();
428 
429 //-----------------------------------------------------------------------------
430 // Function : innerDevsConverged
431 // Purpose :
432 // Special Notes :
433 // Scope : public
434 // Creator : Eric Keiter, SNL
435 // Creation Date : 03/22/06
436 //-----------------------------------------------------------------------------
437 bool devicesConverged(Parallel::Machine comm, const InstanceVector &extern_devices);
438 
439 } // namespace Device
440 } // namespace Xyce
441 
442 #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:193
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.