Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
N_DEV_VCCS.C
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-2014 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_VCCS.C,v $
27 //
28 // Purpose :
29 //
30 // Special Notes :
31 //
32 // Creator : Eric R. Keiter, SNL, Parallel Computational Sciences
33 //
34 // Creation Date : 02/28/00
35 //
36 // Revision Information:
37 // ---------------------
38 //
39 // Revision Number: $Revision: 1.132 $
40 //
41 // Revision Date : $Date: 2014/05/22 17:40:30 $
42 //
43 // Current Owner : $Author: erkeite $
44 //-------------------------------------------------------------------------
45 #include <Xyce_config.h>
46 
47 // ---------- Standard Includes ----------
48 #ifdef HAVE_CSTDIO
49 #include <cstdio>
50 #else
51 #include <stdio.h>
52 #endif
53 
54 // ---------- Xyce Includes ----------
55 #include <N_DEV_DeviceOptions.h>
56 #include <N_DEV_ExternData.h>
57 #include <N_DEV_MatrixLoadData.h>
58 #include <N_DEV_SolverState.h>
59 #include <N_DEV_VCCS.h>
60 #include <N_DEV_Message.h>
61 #include <N_ERH_ErrorMgr.h>
62 
63 #include <N_LAS_Vector.h>
64 #include <N_LAS_Matrix.h>
65 
66 namespace Xyce {
67 namespace Device {
68 
69 namespace VCCS {
70 
71 
73 {
75  .setDescription("Transconductance");
76 }
77 
79 {
80 }
81 
82 // static member components
83 std::vector< std::vector<int> > Instance::jacStamp;
84 
85 //-----------------------------------------------------------------------------
86 // Function : ::
87 // Purpose : "instance block" constructor
88 // Special Notes :
89 // Scope : public
90 // Creator : Tom Russo, SNL, Component Information and Models
91 // Creation Date : 12/20/00
92 //-----------------------------------------------------------------------------
94  const Configuration & configuration,
95  const InstanceBlock & instance_block,
96  Model & model,
97  const FactoryBlock & factory_block)
98  : DeviceInstance(instance_block, configuration.getInstanceParameters(), factory_block),
99  model_(model),
100  Transconductance(1.0),
101  li_Pos(-1),
102  li_Neg(-1),
103  li_ContPos(-1),
104  li_ContNeg(-1),
105  li_store_dev_i(-1),
106  APosEquContPosVarOffset(-1),
107  APosEquContNegVarOffset(-1),
108  ANegEquContPosVarOffset(-1),
109  ANegEquContNegVarOffset(-1),
110  f_PosEquContPosVarPtr(0),
111  f_PosEquContNegVarPtr(0),
112  f_NegEquContPosVarPtr(0),
113  f_NegEquContNegVarPtr(0)
114 {
115  numIntVars = 0;
116  numExtVars = 4;
117  numStateVars = 0;
118  numLeadCurrentStoreVars = 1; // lead current
119 
120  if( jacStamp.empty() )
121  {
122  jacStamp.resize(4);
123  jacStamp[0].resize(2);
124  jacStamp[0][0]=2;
125  jacStamp[0][1]=3;
126  jacStamp[1].resize(2);
127  jacStamp[1][0]=2;
128  jacStamp[1][1]=3;
129  }
130 
131  // Set params to constant default values:
133 
134  // Set params according to instance line and constant defaults from metadata:
135  setParams(instance_block.params);
136 
137  // Set any non-constant parameter defaults:
138  if (!given("T"))
139  {
140  UserError0(*this) << "Could not find Transconductance parameter in instance.";
141  }
142 }
143 
144 //-----------------------------------------------------------------------------
145 // Function : Instance::~Instance
146 // Purpose : destructor
147 // Special Notes :
148 // Scope : public
149 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
150 // Creation Date : 3/16/00
151 //-----------------------------------------------------------------------------
153 {
154 
155 }
156 
157 //-----------------------------------------------------------------------------
158 // Function : Instance::registerLIDs
159 // Purpose :
160 // Special Notes :
161 // Scope : public
162 // Creator : Robert Hoekstra, SNL, Component Information and Models
163 // Creation Date : 6/21/01
164 //-----------------------------------------------------------------------------
165 void Instance::registerLIDs( const std::vector<int> & intLIDVecRef,
166  const std::vector<int> & extLIDVecRef )
167 {
168  AssertLIDs(intLIDVecRef.size() == numIntVars);
169  AssertLIDs(extLIDVecRef.size() == numExtVars);
170 
171 #ifdef Xyce_DEBUG_DEVICE
172 
173  if (getDeviceOptions().debugLevel > 0)
174  {
175  Xyce::dout() << std::endl << section_divider << std::endl;
176  Xyce::dout() << " VCCSInstance::registerLIDs" << std::endl;
177  Xyce::dout() << " name = " << getName() << std::endl;
178  }
179 #endif
180 
181  // copy over the global ID lists.
182  intLIDVec = intLIDVecRef;
183  extLIDVec = extLIDVecRef;
184 
185  // Now use these lists to obtain the indices into the
186  // linear algebra entities. This assumes an order.
187  // For the matrix indices, first do the rows.
188 
189  li_Pos = extLIDVec[0];
190  li_Neg = extLIDVec[1];
191  li_ContPos = extLIDVec[2];
192  li_ContNeg = extLIDVec[3];
193 
194 #ifdef Xyce_DEBUG_DEVICE
195  if (getDeviceOptions().debugLevel > 0)
196  {
197  Xyce::dout() << " li_Pos = " << li_Pos << std::endl;
198  Xyce::dout() << " li_Neg = " << li_Neg << std::endl;
199  Xyce::dout() << " li_ContPos = " << li_ContPos << std::endl;
200  Xyce::dout() << " li_ContNeg = " << li_ContNeg << std::endl;
201 
202  Xyce::dout() << section_divider << std::endl;
203  }
204 #endif
205 }
206 
207 //-----------------------------------------------------------------------------
208 // Function : Instance::registerStateLIDs
209 // Purpose :
210 // Special Notes :
211 // Scope : public
212 // Creator : Robert Hoektra, SNL, Parallel Computational Sciences
213 // Creation Date : 6/21/02
214 //-----------------------------------------------------------------------------
215 void Instance::registerStateLIDs( const std::vector<int> & staLIDVecRef)
216 {
217  AssertLIDs(staLIDVecRef.size() == numStateVars);
218 }
219 
220 
221 //-----------------------------------------------------------------------------
222 // Function : Instance::registerStoreLIDs
223 // Purpose :
224 // Special Notes :
225 // Scope : public
226 // Creator : Richard Schiek, Electrical Systems Modeling
227 // Creation Date : 4/23/2013
228 //-----------------------------------------------------------------------------
229 void Instance::registerStoreLIDs( const std::vector<int> & stoLIDVecRef )
230 {
231  AssertLIDs(stoLIDVecRef.size() == getNumStoreVars());
232 
233  // Copy over the global ID lists:
234  stoLIDVec = stoLIDVecRef;
235  if( loadLeadCurrent )
236  {
237  li_store_dev_i = stoLIDVec[0];
238  }
239 }
240 
241 
242 //-----------------------------------------------------------------------------
243 // Function : Instance::getStoreNameMap
244 // Purpose :
245 // Special Notes :
246 // Scope : public
247 // Creator : Richard Schiek, Electrical Systems Modeling
248 // Creation Date : 4/23/2013
249 //-----------------------------------------------------------------------------
250 std::map<int,std::string> & Instance::getStoreNameMap ()
251 {
252  // set up the internal name map, if it hasn't been already.
253  if( loadLeadCurrent && storeNameMap.empty ())
254  {
256  }
257 
258  return storeNameMap;
259 }
260 //-----------------------------------------------------------------------------
261 // Function : Instance::jacobianStamp
262 // Purpose :
263 // Special Notes :
264 // Scope : public
265 // Creator : Robert Hoektra, SNL, Parallel Computational Sciences
266 // Creation Date : 9/2/02
267 //-----------------------------------------------------------------------------
268 const std::vector< std::vector<int> > & Instance::jacobianStamp() const
269 {
270  return jacStamp;
271 }
272 
273 //-----------------------------------------------------------------------------
274 // Function : Instance::registerJacLIDs
275 // Purpose :
276 // Special Notes :
277 // Scope : public
278 // Creator : Robert Hoektra, SNL, Parallel Computational Sciences
279 // Creation Date : 9/2/02
280 //-----------------------------------------------------------------------------
281 void Instance::registerJacLIDs( const std::vector< std::vector<int> > & jacLIDVec )
282 {
283  DeviceInstance::registerJacLIDs( jacLIDVec );
284 
285  APosEquContPosVarOffset = jacLIDVec[0][0];
286  APosEquContNegVarOffset = jacLIDVec[0][1];
287  ANegEquContPosVarOffset = jacLIDVec[1][0];
288  ANegEquContNegVarOffset = jacLIDVec[1][1];
289 }
290 
291 //-----------------------------------------------------------------------------
292 // Function : Instance::setupPointers
293 // Purpose :
294 // Special Notes :
295 // Scope : public
296 // Creator : Eric Keiter, SNL
297 // Creation Date : 12/12/08
298 //-----------------------------------------------------------------------------
300 {
301 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
302  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
303 
308 #endif
309 }
310 
311 //-----------------------------------------------------------------------------
312 // Function : Instance::updatePrimaryState
313 // Purpose :
314 // Special Notes :
315 // Scope : public
316 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
317 // Creation Date : 01/29/01
318 //-----------------------------------------------------------------------------
320 {
321  return true;
322 }
323 
324 //-----------------------------------------------------------------------------
325 // Function : Instance::loadDAEFVector
326 //
327 // Purpose : Loads the F-vector contributions for a single
328 // VCCS instance.
329 //
330 // Special Notes :
331 //
332 // Scope : public
333 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
334 // Creation Date : 01/24/03
335 //-----------------------------------------------------------------------------
337 {
338  double * solVec = extData.nextSolVectorRawPtr;
339  double * fVec = extData.daeFVectorRawPtr;
340 
341  double v_cont_pos = solVec[li_ContPos];
342  double v_cont_neg = solVec[li_ContNeg];
343 
344  fVec[li_Pos] += Transconductance * ( v_cont_pos - v_cont_neg );
345  fVec[li_Neg] += -Transconductance * ( v_cont_pos - v_cont_neg );
346 
347  if( loadLeadCurrent )
348  {
349  double * storeLeadF = extData.nextStoVectorRawPtr;
350  storeLeadF[li_store_dev_i] = Transconductance * ( v_cont_pos - v_cont_neg );
351  }
352 
353  return true;
354 }
355 
356 //-----------------------------------------------------------------------------
357 // Function : Instance::loadDAEdFdx ()
358 //
359 // Purpose : Loads the F-vector contributions for a single
360 // VCCS instance.
361 //
362 // Special Notes : See the special notes for loadDAEFVector.
363 //
364 // Scope : public
365 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
366 // Creation Date : 03/05/04
367 //-----------------------------------------------------------------------------
369 {
370  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
371 
376 
377  return true;
378 }
379 
380 // Class Model
381 //-----------------------------------------------------------------------------
382 // Function : Model::Model
383 // Purpose : "Model block" constructor
384 // Special Notes :
385 // Scope : public
386 // Creator : Tom Russo, SNL, Component Information and Models
387 // Creation Date : 12/21/00
388 //-----------------------------------------------------------------------------
390  const Configuration & configuration,
391  const ModelBlock & MB,
392  const FactoryBlock & factory_block)
393  : DeviceModel(MB, configuration.getModelParameters(), factory_block)
394 {
395 }
396 
397 //-----------------------------------------------------------------------------
398 // Function : Model::~Model
399 // Purpose : destructor
400 // Special Notes :
401 // Scope : public
402 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
403 // Creation Date : 3/16/00
404 //-----------------------------------------------------------------------------
406 {
407  std::vector<Instance*>::iterator iter;
408  std::vector<Instance*>::iterator first = instanceContainer.begin();
409  std::vector<Instance*>::iterator last = instanceContainer.end();
410 
411  for (iter=first; iter!=last; ++iter)
412  {
413  delete (*iter);
414  }
415 }
416 
417 //-----------------------------------------------------------------------------
418 // Function : Model::printOutInstances
419 // Purpose : debugging tool.
420 // Special Notes :
421 // Scope : public
422 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
423 // Creation Date : 4/03/00
424 //-----------------------------------------------------------------------------
425 std::ostream &Model::printOutInstances(std::ostream &os) const
426 {
427  std::vector<Instance*>::const_iterator iter;
428  std::vector<Instance*>::const_iterator first = instanceContainer.begin();
429  std::vector<Instance*>::const_iterator last = instanceContainer.end();
430 
431  int i;
432  os << std::endl;
433  os << " name model name Parameters" << std::endl;
434  for (i=0, iter=first; iter!=last; ++iter, ++i)
435  {
436  os << " " << i << ": " << (*iter)->getName() << " ";
437  os << getName();
438  os << std::endl;
439  }
440 
441  os << std::endl;
442 
443  return os;
444 }
445 
446 //-----------------------------------------------------------------------------
447 // Function : Model::forEachInstance
448 // Purpose :
449 // Special Notes :
450 // Scope : public
451 // Creator : David Baur
452 // Creation Date : 2/4/2014
453 //-----------------------------------------------------------------------------
454 /// Apply a device instance "op" to all instances associated with this
455 /// model
456 ///
457 /// @param[in] op Operator to apply to all instances.
458 ///
459 ///
460 void Model::forEachInstance(DeviceInstanceOp &op) const /* override */
461 {
462  for (std::vector<Instance *>::const_iterator it = instanceContainer.begin(); it != instanceContainer.end(); ++it)
463  op(*it);
464 }
465 
466 
467 // VCCS Master functions:
468 
469 //-----------------------------------------------------------------------------
470 // Function : Master::updateState
471 // Purpose :
472 // Special Notes :
473 // Scope : public
474 // Creator : Eric Keiter, SNL
475 // Creation Date : 12/12/08
476 //-----------------------------------------------------------------------------
477 bool Master::updateState (double * solVec, double * staVec, double * stoVec)
478 {
479  return true;
480 }
481 
482 //-----------------------------------------------------------------------------
483 // Function : Master::loadDAEVectors
484 // Purpose :
485 // Special Notes :
486 // Scope : public
487 // Creator : Eric Keiter, SNL
488 // Creation Date : 12/12/08
489 //-----------------------------------------------------------------------------
490 bool Master::loadDAEVectors (double * solVec, double * fVec, double *qVec, double * bVec, double * storeLeadF, double * storeLeadQ)
491 {
492  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
493  {
494  Instance & vi = *(*it);
495  double v_cont_pos = solVec[vi.li_ContPos];
496  double v_cont_neg = solVec[vi.li_ContNeg];
497 
498 
499  fVec[vi.li_Pos] += vi.Transconductance * ( v_cont_pos - v_cont_neg );
500  fVec[vi.li_Neg] += -vi.Transconductance * ( v_cont_pos - v_cont_neg );
501  if( vi.loadLeadCurrent )
502  {
503  storeLeadF[vi.li_store_dev_i] = vi.Transconductance * ( v_cont_pos - v_cont_neg );
504  }
505  }
506 
507  return true;
508 }
509 
510 //-----------------------------------------------------------------------------
511 // Function : Master::loadDAEMatrices
512 // Purpose :
513 // Special Notes :
514 // Scope : public
515 // Creator : Eric Keiter, SNL
516 // Creation Date : 12/12/08
517 //-----------------------------------------------------------------------------
518 bool Master::loadDAEMatrices (N_LAS_Matrix & dFdx, N_LAS_Matrix & dQdx)
519 {
520  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
521  {
522  Instance & vi = *(*it);
523 
524 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
529 #else
530 
535 #endif
536  }
537 
538  return true;
539 }
540 
541 Device *Traits::factory(const Configuration &configuration, const FactoryBlock &factory_block)
542 {
543 
544  return new Master(configuration, factory_block, factory_block.solverState_, factory_block.deviceOptions_);
545 }
546 
548 {
550  .registerDevice("g", 1);
551 }
552 
553 } // namespace VCCS
554 } // namespace Device
555 } // namespace Xyce