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.129.2.2 $
40 //
41 // Revision Date : $Date: 2014/03/06 23:33:43 $
42 //
43 // Current Owner : $Author: tvrusso $
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  {
255  // change subcircuitname:devicetype_deviceName to
256  // devicetype:subcircuitName:deviceName
257  std::string modName(getName());
258  spiceInternalName(modName);
259  std::string tmpstr;
260  tmpstr = modName+":DEV_I";
261  storeNameMap[ li_store_dev_i ] = tmpstr;
262  }
263 
264  return storeNameMap;
265 }
266 //-----------------------------------------------------------------------------
267 // Function : Instance::jacobianStamp
268 // Purpose :
269 // Special Notes :
270 // Scope : public
271 // Creator : Robert Hoektra, SNL, Parallel Computational Sciences
272 // Creation Date : 9/2/02
273 //-----------------------------------------------------------------------------
274 const std::vector< std::vector<int> > & Instance::jacobianStamp() const
275 {
276  return jacStamp;
277 }
278 
279 //-----------------------------------------------------------------------------
280 // Function : Instance::registerJacLIDs
281 // Purpose :
282 // Special Notes :
283 // Scope : public
284 // Creator : Robert Hoektra, SNL, Parallel Computational Sciences
285 // Creation Date : 9/2/02
286 //-----------------------------------------------------------------------------
287 void Instance::registerJacLIDs( const std::vector< std::vector<int> > & jacLIDVec )
288 {
289  DeviceInstance::registerJacLIDs( jacLIDVec );
290 
291  APosEquContPosVarOffset = jacLIDVec[0][0];
292  APosEquContNegVarOffset = jacLIDVec[0][1];
293  ANegEquContPosVarOffset = jacLIDVec[1][0];
294  ANegEquContNegVarOffset = jacLIDVec[1][1];
295 }
296 
297 //-----------------------------------------------------------------------------
298 // Function : Instance::setupPointers
299 // Purpose :
300 // Special Notes :
301 // Scope : public
302 // Creator : Eric Keiter, SNL
303 // Creation Date : 12/12/08
304 //-----------------------------------------------------------------------------
306 {
307 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
308  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
309 
314 #endif
315 }
316 
317 //-----------------------------------------------------------------------------
318 // Function : Instance::updatePrimaryState
319 // Purpose :
320 // Special Notes :
321 // Scope : public
322 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
323 // Creation Date : 01/29/01
324 //-----------------------------------------------------------------------------
326 {
327  return true;
328 }
329 
330 //-----------------------------------------------------------------------------
331 // Function : Instance::loadDAEFVector
332 //
333 // Purpose : Loads the F-vector contributions for a single
334 // VCCS instance.
335 //
336 // Special Notes :
337 //
338 // Scope : public
339 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
340 // Creation Date : 01/24/03
341 //-----------------------------------------------------------------------------
343 {
344  double * solVec = extData.nextSolVectorRawPtr;
345  double * fVec = extData.daeFVectorRawPtr;
346 
347  double v_cont_pos = solVec[li_ContPos];
348  double v_cont_neg = solVec[li_ContNeg];
349 
350  fVec[li_Pos] += Transconductance * ( v_cont_pos - v_cont_neg );
351  fVec[li_Neg] += -Transconductance * ( v_cont_pos - v_cont_neg );
352 
353  if( loadLeadCurrent )
354  {
355  double * storeLeadF = extData.nextStoVectorRawPtr;
356  storeLeadF[li_store_dev_i] = Transconductance * ( v_cont_pos - v_cont_neg );
357  }
358 
359  return true;
360 }
361 
362 //-----------------------------------------------------------------------------
363 // Function : Instance::loadDAEdFdx ()
364 //
365 // Purpose : Loads the F-vector contributions for a single
366 // VCCS instance.
367 //
368 // Special Notes : See the special notes for loadDAEFVector.
369 //
370 // Scope : public
371 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
372 // Creation Date : 03/05/04
373 //-----------------------------------------------------------------------------
375 {
376  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
377 
382 
383  return true;
384 }
385 
386 // Class Model
387 //-----------------------------------------------------------------------------
388 // Function : Model::Model
389 // Purpose : "Model block" constructor
390 // Special Notes :
391 // Scope : public
392 // Creator : Tom Russo, SNL, Component Information and Models
393 // Creation Date : 12/21/00
394 //-----------------------------------------------------------------------------
396  const Configuration & configuration,
397  const ModelBlock & MB,
398  const FactoryBlock & factory_block)
399  : DeviceModel(MB, configuration.getModelParameters(), factory_block)
400 {
401 }
402 
403 //-----------------------------------------------------------------------------
404 // Function : Model::~Model
405 // Purpose : destructor
406 // Special Notes :
407 // Scope : public
408 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
409 // Creation Date : 3/16/00
410 //-----------------------------------------------------------------------------
412 {
413  std::vector<Instance*>::iterator iter;
414  std::vector<Instance*>::iterator first = instanceContainer.begin();
415  std::vector<Instance*>::iterator last = instanceContainer.end();
416 
417  for (iter=first; iter!=last; ++iter)
418  {
419  delete (*iter);
420  }
421 }
422 
423 //-----------------------------------------------------------------------------
424 // Function : Model::printOutInstances
425 // Purpose : debugging tool.
426 // Special Notes :
427 // Scope : public
428 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
429 // Creation Date : 4/03/00
430 //-----------------------------------------------------------------------------
431 std::ostream &Model::printOutInstances(std::ostream &os) const
432 {
433  std::vector<Instance*>::const_iterator iter;
434  std::vector<Instance*>::const_iterator first = instanceContainer.begin();
435  std::vector<Instance*>::const_iterator last = instanceContainer.end();
436 
437  int i;
438  os << std::endl;
439  os << " name model name Parameters" << std::endl;
440  for (i=0, iter=first; iter!=last; ++iter, ++i)
441  {
442  os << " " << i << ": " << (*iter)->getName() << " ";
443  os << getName();
444  os << std::endl;
445  }
446 
447  os << std::endl;
448 
449  return os;
450 }
451 
452 //-----------------------------------------------------------------------------
453 // Function : Model::forEachInstance
454 // Purpose :
455 // Special Notes :
456 // Scope : public
457 // Creator : David Baur
458 // Creation Date : 2/4/2014
459 //-----------------------------------------------------------------------------
460 /// Apply a device instance "op" to all instances associated with this
461 /// model
462 ///
463 /// @param[in] op Operator to apply to all instances.
464 ///
465 ///
466 void Model::forEachInstance(DeviceInstanceOp &op) const /* override */
467 {
468  for (std::vector<Instance *>::const_iterator it = instanceContainer.begin(); it != instanceContainer.end(); ++it)
469  op(*it);
470 }
471 
472 
473 // VCCS Master functions:
474 
475 //-----------------------------------------------------------------------------
476 // Function : Master::updateState
477 // Purpose :
478 // Special Notes :
479 // Scope : public
480 // Creator : Eric Keiter, SNL
481 // Creation Date : 12/12/08
482 //-----------------------------------------------------------------------------
483 bool Master::updateState (double * solVec, double * staVec, double * stoVec)
484 {
485  return true;
486 }
487 
488 //-----------------------------------------------------------------------------
489 // Function : Master::loadDAEVectors
490 // Purpose :
491 // Special Notes :
492 // Scope : public
493 // Creator : Eric Keiter, SNL
494 // Creation Date : 12/12/08
495 //-----------------------------------------------------------------------------
496 bool Master::loadDAEVectors (double * solVec, double * fVec, double *qVec, double * storeLeadF, double * storeLeadQ)
497 {
498  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
499  {
500  Instance & vi = *(*it);
501  double v_cont_pos = solVec[vi.li_ContPos];
502  double v_cont_neg = solVec[vi.li_ContNeg];
503 
504 
505  fVec[vi.li_Pos] += vi.Transconductance * ( v_cont_pos - v_cont_neg );
506  fVec[vi.li_Neg] += -vi.Transconductance * ( v_cont_pos - v_cont_neg );
507  if( vi.loadLeadCurrent )
508  {
509  storeLeadF[vi.li_store_dev_i] = vi.Transconductance * ( v_cont_pos - v_cont_neg );
510  }
511  }
512 
513  return true;
514 }
515 
516 //-----------------------------------------------------------------------------
517 // Function : Master::loadDAEMatrices
518 // Purpose :
519 // Special Notes :
520 // Scope : public
521 // Creator : Eric Keiter, SNL
522 // Creation Date : 12/12/08
523 //-----------------------------------------------------------------------------
524 bool Master::loadDAEMatrices (N_LAS_Matrix & dFdx, N_LAS_Matrix & dQdx)
525 {
526  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
527  {
528  Instance & vi = *(*it);
529 
530 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
535 #else
536 
541 #endif
542  }
543 
544  return true;
545 }
546 
547 Device *Traits::factory(const Configuration &configuration, const FactoryBlock &factory_block)
548 {
549 
550  return new Master(configuration, factory_block, factory_block.solverState_, factory_block.deviceOptions_);
551 }
552 
554 {
556  .registerDevice("g", 1);
557 }
558 
559 } // namespace VCCS
560 } // namespace Device
561 } // namespace Xyce