Xyce  6.1
N_DEV_Vcvs.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-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_Vcvs.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.140.2.1 $
40 //
41 // Revision Date : $Date: 2015/04/02 18:20:11 $
42 //
43 // Current Owner : $Author: tvrusso $
44 //-------------------------------------------------------------------------
45 #include <Xyce_config.h>
46 
47 // ---------- Standard Includes ----------
48 
49 // ---------- Xyce Includes ----------
50 #include <N_DEV_DeviceOptions.h>
51 #include <N_DEV_ExternData.h>
52 #include <N_DEV_MatrixLoadData.h>
53 #include <N_DEV_SolverState.h>
54 #include <N_DEV_Vcvs.h>
55 #include <N_DEV_Message.h>
56 #include <N_ERH_ErrorMgr.h>
57 
58 #include <N_LAS_Vector.h>
59 #include <N_LAS_Matrix.h>
60 #include <N_UTL_FeatureTest.h>
61 
62 namespace Xyce {
63 namespace Device {
64 
65 namespace Vcvs {
66 
68 {
69 // Set up double precision variables:
70  p.addPar ("G",0.0,&Vcvs::Instance::Gain)
71  .setUnit(U_NONE)
72  .setCategory(CAT_NONE)
73  .setDescription("Gain");
74 }
75 
77 {
78 }
79 
80 std::vector< std::vector<int> > Instance::jacStamp;
81 
82 //-----------------------------------------------------------------------------
83 // Function : Instance::Instance
84 // Purpose : "instance block" constructor
85 // Special Notes :
86 // Scope : public
87 // Creator : Tom Russo, SNL, Component Information and Models
88 // Creation Date : 12/20/00
89 //-----------------------------------------------------------------------------
91  const Configuration & configuration,
92  const InstanceBlock & IBref,
93  Model & Viter,
94  const FactoryBlock & factory_block)
95  : DeviceInstance(IBref, configuration.getInstanceParameters(), factory_block),
96  IB(IBref),
97  model_(Viter),
98  Gain(1.0),
99  li_Pos(-1),
100  li_Neg(-1),
101  li_Bra(-1),
102  li_ContPos(-1),
103  li_ContNeg(-1),
104  ABraEquPosNodeOffset(-1),
105  ABraEquNegNodeOffset(-1),
106  ABraEquContPosNodeOffset(-1),
107  ABraEquContNegNodeOffset(-1),
108  APosEquBraVarOffset(-1),
109  ANegEquBraVarOffset(-1),
110 
111  f_BraEquPosNodePtr(0),
112  f_BraEquNegNodePtr(0),
113  f_BraEquContPosNodePtr(0),
114  f_BraEquContNegNodePtr(0),
115  f_PosEquBraVarPtr(0),
116  f_NegEquBraVarPtr(0)
117 {
118  numIntVars = 1;
119  numExtVars = 4;
120  numStateVars = 0;
121 
122  if( jacStamp.empty() )
123  {
124  jacStamp.resize(5);
125  jacStamp[0].resize(1);
126  jacStamp[0][0]=4;
127  jacStamp[1].resize(1);
128  jacStamp[1][0]=4;
129  jacStamp[4].resize(4);
130  jacStamp[4][0]=0;
131  jacStamp[4][1]=1;
132  jacStamp[4][2]=2;
133  jacStamp[4][3]=3;
134  }
135 
136 
137  // Set params to constant default values:
138  setDefaultParams ();
139 
140  // Set params according to instance line and constant defaults from metadata:
141  setParams (IB.params);
142 
143  // Set any non-constant parameter defaults:
144  if (!given("G"))
145  {
146  UserError0(*this) << "Could not find Gain parameter in instance.";
147  }
148 }
149 
150 //-----------------------------------------------------------------------------
151 // Function : Instance::~Instance
152 // Purpose : destructor
153 // Special Notes :
154 // Scope : public
155 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
156 // Creation Date : 3/16/00
157 //-----------------------------------------------------------------------------
159 {
160 
161 }
162 
163 //-----------------------------------------------------------------------------
164 // Function : Instance::registerLIDs
165 // Purpose :
166 // Special Notes :
167 // Scope : public
168 // Creator : Robert Hoekstra, SNL, Computational Science
169 // Creation Date : 6/21/02
170 //-----------------------------------------------------------------------------
171 void Instance::registerLIDs ( const std::vector<int> & intLIDVecRef,
172  const std::vector<int> & extLIDVecRef )
173 {
174  std::string msg;
175 
176  if (DEBUG_DEVICE && isActive(Diag::DEVICE_PARAMETERS))
177  {
178  Xyce::dout() << std::endl << section_divider << std::endl;
179  Xyce::dout() << " VcvsInstance::registerLIDs" << std::endl;
180  Xyce::dout() << " name = " << getName() << std::endl;
181  }
182 
183  // Check if the size of the ID lists corresponds to the
184  // proper number of internal and external variables.
185  int numInt = intLIDVecRef.size();
186  int numExt = extLIDVecRef.size();
187 
188  if (numInt != numIntVars)
189  {
190  msg = "Instance::registerLIDs:";
191  msg += "numInt != numIntVars";
192  N_ERH_ErrorMgr::report ( N_ERH_ErrorMgr::DEV_FATAL,msg);
193  }
194 
195  if (numExt != numExtVars)
196  {
197  msg = "Instance::registerLIDs:";
198  msg += "numExt != numExtVars";
199  N_ERH_ErrorMgr::report ( N_ERH_ErrorMgr::DEV_FATAL,msg);
200  }
201 
202  // copy over the global ID lists.
203  intLIDVec = intLIDVecRef;
204  extLIDVec = extLIDVecRef;
205 
206  // Now use these lists to obtain the indices into the
207  // linear algebra entities. This assumes an order.
208  // For the matrix indices, first do the rows.
209 
210  li_Pos = extLIDVec[0];
211  li_Neg = extLIDVec[1];
212  li_ContPos = extLIDVec[2];
213  li_ContNeg = extLIDVec[3];
214 
215  if (DEBUG_DEVICE && isActive(Diag::DEVICE_PARAMETERS))
216  {
217  Xyce::dout() << " li_Pos = " << li_Pos << std::endl;
218  Xyce::dout() << " li_Neg = " << li_Neg << std::endl;
219  Xyce::dout() << " li_ContPos = " << li_ContPos << std::endl;
220  Xyce::dout() << " li_ContNeg = " << li_ContNeg << std::endl;
221  }
222 
223  li_Bra = intLIDVec[0];
224 
225  if (DEBUG_DEVICE && isActive(Diag::DEVICE_PARAMETERS))
226  Xyce::dout() << " li_Bra = " << li_Bra << std::endl;
227 
228  if (DEBUG_DEVICE && isActive(Diag::DEVICE_PARAMETERS))
229  Xyce::dout() << section_divider << std::endl;
230 }
231 
232 //-----------------------------------------------------------------------------
233 // Function : Instance::loadNodeSymbols
234 // Purpose :
235 // Special Notes :
236 // Scope : public
237 // Creator : Eric R. Keiter, SNL, Parallel Computational Sciences
238 // Creation Date : 05/13/05
239 //-----------------------------------------------------------------------------
240 void Instance::loadNodeSymbols(Util::SymbolTable &symbol_table) const
241 {
242  addInternalNode(symbol_table, li_Bra, getName(), "branch");
243 }
244 
245 //-----------------------------------------------------------------------------
246 // Function : Instance::registerStateGIDs
247 // Purpose :
248 // Special Notes :
249 // Scope : public
250 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
251 // Creation Date : 6/21/02
252 //-----------------------------------------------------------------------------
253 void Instance::registerStateLIDs( const std::vector<int> & staLIDVecRef )
254 {
255  AssertLIDs(staLIDVecRef.size() == numStateVars);
256 }
257 
258 //-----------------------------------------------------------------------------
259 // Function : Instance::jacobianStamp
260 // Purpose :
261 // Special Notes :
262 // Scope : public
263 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
264 // Creation Date : 9/2/02
265 //-----------------------------------------------------------------------------
266 const std::vector< std::vector<int> > & Instance::jacobianStamp() const
267 {
268  return jacStamp;
269 }
270 
271 //-----------------------------------------------------------------------------
272 // Function : Instance::registerJacLIDs
273 // Purpose :
274 // Special Notes :
275 // Scope : public
276 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
277 // Creation Date : 9/2/02
278 //-----------------------------------------------------------------------------
279 void Instance::registerJacLIDs( const std::vector< std::vector<int> > & jacLIDVec )
280 {
281  DeviceInstance::registerJacLIDs( jacLIDVec );
282 
283  APosEquBraVarOffset = jacLIDVec[0][0];
284  ANegEquBraVarOffset = jacLIDVec[1][0];
285  ABraEquPosNodeOffset = jacLIDVec[4][0];
286  ABraEquNegNodeOffset = jacLIDVec[4][1];
287  ABraEquContPosNodeOffset = jacLIDVec[4][2];
288  ABraEquContNegNodeOffset = jacLIDVec[4][3];
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  Linear::Matrix & dFdx = *(extData.dFdxMatrixPtr);
303 
310 #endif
311 }
312 
313 //-----------------------------------------------------------------------------
314 // Function : Instance::updatePrimaryState
315 // Purpose :
316 // Special Notes :
317 // Scope : public
318 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
319 // Creation Date : 01/29/01
320 //-----------------------------------------------------------------------------
322 {
323  return true;
324 }
325 
326 //-----------------------------------------------------------------------------
327 // Function : Instance::loadDAEFVector
328 //
329 // Purpose : Loads the F-vector contributions for a single
330 // Vcvs instance.
331 //
332 // Special Notes :
333 //
334 // Scope : public
335 // Creator : Eric R. Keiter, SNL, Parallel Computational Sciences
336 // Creation Date : 02/19/05
337 //-----------------------------------------------------------------------------
339 {
340  double * fVec = extData.daeFVectorRawPtr;
341  double * solVec = extData.nextSolVectorRawPtr;
342 
343  double v_pos = solVec[li_Pos];
344  double v_neg = solVec[li_Neg];
345  double v_cont_pos = solVec[li_ContPos];
346  double v_cont_neg = solVec[li_ContNeg];
347 
348  double i_bra = solVec[li_Bra];
349 
350  fVec[li_Pos] += i_bra;
351  fVec[li_Neg] += -i_bra;
352 
353  double src = Gain * ( v_cont_pos - v_cont_neg ) - v_pos + v_neg;
354  fVec[li_Bra] += -src;
355 
356  return true;
357 }
358 
359 //-----------------------------------------------------------------------------
360 // Function : Instance::loadDAEdFdx ()
361 //
362 // Purpose : Loads the F-vector contributions for a single
363 // resistor instance.
364 //
365 // Special Notes : The F-vector is an algebraic constaint.
366 //
367 // Scope : public
368 // Creator : Eric R. Keiter, SNL, Parallel Computational Sciences
369 // Creation Date : 02/19/05
370 //-----------------------------------------------------------------------------
372 {
373  Linear::Matrix & dFdx = *(extData.dFdxMatrixPtr);
374 
375  dFdx[li_Pos][APosEquBraVarOffset] += 1.0;
376  dFdx[li_Neg][ANegEquBraVarOffset] -= 1.0;
377 
378  dFdx[li_Bra][ABraEquPosNodeOffset] += 1.0;
379  dFdx[li_Bra][ABraEquNegNodeOffset] -= 1.0;
382 
383  return true;
384 }
385 
386 //-----------------------------------------------------------------------------
387 // Function : Instance::varTypes
388 // Purpose :
389 // Special Notes :
390 // Scope : public
391 // Creator : Rob Hoekstra, SNL, Parallel Computational Sciences
392 // Creation Date : 02/17/04
393 //-----------------------------------------------------------------------------
394 void Instance::varTypes( std::vector<char> & varTypeVec )
395 {
396  varTypeVec.resize(1);
397  varTypeVec[0] = 'I';
398 }
399 
400 // Class Model
401 
402 //-----------------------------------------------------------------------------
403 // Function : Model::Model
404 // Purpose : "Model block" constructor
405 // Special Notes :
406 // Scope : public
407 // Creator : Tom Russo, SNL, Component Information and Models
408 // Creation Date : 12/21/00
409 //-----------------------------------------------------------------------------
411  const Configuration & configuration,
412  const ModelBlock & MB,
413  const FactoryBlock & factory_block)
414  : DeviceModel(MB, configuration.getModelParameters(), factory_block)
415 {
416 }
417 
418 //-----------------------------------------------------------------------------
419 // Function : Model::~Model
420 // Purpose : destructor
421 // Special Notes :
422 // Scope : public
423 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
424 // Creation Date : 3/16/00
425 //-----------------------------------------------------------------------------
427 {
428  std::vector<Instance*>::iterator iter;
429  std::vector<Instance*>::iterator first = instanceContainer.begin();
430  std::vector<Instance*>::iterator last = instanceContainer.end();
431 
432  for (iter=first; iter!=last; ++iter)
433  {
434  delete (*iter);
435  }
436 }
437 
438 //-----------------------------------------------------------------------------
439 // Function : Model::printOutInstances
440 // Purpose : debugging tool.
441 // Special Notes :
442 // Scope : public
443 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
444 // Creation Date : 4/03/00
445 //-----------------------------------------------------------------------------
446 std::ostream &Model::printOutInstances(std::ostream &os) const
447 {
448  std::vector<Instance*>::const_iterator iter;
449  std::vector<Instance*>::const_iterator first = instanceContainer.begin();
450  std::vector<Instance*>::const_iterator last = instanceContainer.end();
451 
452  int i;
453  os << std::endl;
454  os << " name model name Parameters" << std::endl;
455  for (i=0, iter=first; iter!=last; ++iter, ++i)
456  {
457  os << " " << i << ": " << (*iter)->getName() << " ";
458  os << getName();
459  os << std::endl;
460  }
461 
462  os << std::endl;
463 
464  return os;
465 }
466 
467 //-----------------------------------------------------------------------------
468 // Function : Model::forEachInstance
469 // Purpose :
470 // Special Notes :
471 // Scope : public
472 // Creator : David Baur
473 // Creation Date : 2/4/2014
474 //-----------------------------------------------------------------------------
475 /// Apply a device instance "op" to all instances associated with this
476 /// model
477 ///
478 /// @param[in] op Operator to apply to all instances.
479 ///
480 ///
481 void Model::forEachInstance(DeviceInstanceOp &op) const /* override */
482 {
483  for (std::vector<Instance *>::const_iterator it = instanceContainer.begin(); it != instanceContainer.end(); ++it)
484  op(*it);
485 }
486 
487 
488 // Vcvs Master functions:
489 
490 //-----------------------------------------------------------------------------
491 // Function : Master::updateState
492 // Purpose :
493 // Special Notes :
494 // Scope : public
495 // Creator : Eric Keiter, SNL
496 // Creation Date : 11/26/08
497 //-----------------------------------------------------------------------------
498 bool Master::updateState (double * solVec, double * staVec, double * stoVec)
499 {
500  return true;
501 }
502 
503 //-----------------------------------------------------------------------------
504 // Function : Master::loadDAEVectors
505 // Purpose :
506 // Special Notes :
507 // Scope : public
508 // Creator : Eric Keiter, SNL
509 // Creation Date : 11/26/08
510 //-----------------------------------------------------------------------------
511 bool Master::loadDAEVectors (double * solVec, double * fVec, double *qVec, double * bVec, double * storeLeadF, double * storeLeadQ, double * leadF, double * leadQ, double * junctionV)
512 {
513 #ifdef _OMP
514 #pragma omp parallel for
515 #endif
516  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
517  {
518  Instance & vi = *(*it);
519 
520  double v_pos = solVec[vi.li_Pos];
521  double v_neg = solVec[vi.li_Neg];
522  double v_cont_pos = solVec[vi.li_ContPos];
523  double v_cont_neg = solVec[vi.li_ContNeg];
524 
525  double i_bra = solVec[vi.li_Bra];
526 
527  fVec[vi.li_Pos] += i_bra;
528  fVec[vi.li_Neg] += -i_bra;
529 
530  double src = vi.Gain * ( v_cont_pos - v_cont_neg ) - v_pos + v_neg;
531  fVec[vi.li_Bra] += -src;
532 
533  }
534 
535  return true;
536 }
537 
538 //-----------------------------------------------------------------------------
539 // Function : Master::loadDAEMatrices
540 // Purpose :
541 // Special Notes :
542 // Scope : public
543 // Creator : Eric Keiter, SNL
544 // Creation Date : 11/26/08
545 //-----------------------------------------------------------------------------
546 bool Master::loadDAEMatrices (Linear::Matrix & dFdx, Linear::Matrix & dQdx)
547 {
548  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
549  {
550  Instance & vi = *(*it);
551 
552 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
553 
554  *vi.f_PosEquBraVarPtr += 1.0;
555  *vi.f_NegEquBraVarPtr -= 1.0;
556 
557  *vi.f_BraEquPosNodePtr += 1.0;
558  *vi.f_BraEquNegNodePtr -= 1.0;
559  *vi.f_BraEquContPosNodePtr -= vi.Gain;
560  *vi.f_BraEquContNegNodePtr += vi.Gain;
561 #else
562 
563  dFdx[vi.li_Pos][vi.APosEquBraVarOffset] += 1.0;
564  dFdx[vi.li_Neg][vi.ANegEquBraVarOffset] -= 1.0;
565 
566  dFdx[vi.li_Bra][vi.ABraEquPosNodeOffset] += 1.0;
567  dFdx[vi.li_Bra][vi.ABraEquNegNodeOffset] -= 1.0;
568  dFdx[vi.li_Bra][vi.ABraEquContPosNodeOffset] -= vi.Gain;
569  dFdx[vi.li_Bra][vi.ABraEquContNegNodeOffset] += vi.Gain;
570 #endif
571  }
572  return true;
573 }
574 
575 Device *Traits::factory(const Configuration &configuration, const FactoryBlock &factory_block)
576 {
577 
578  return new Master(configuration, factory_block, factory_block.solverState_, factory_block.deviceOptions_);
579 }
580 
582 {
584  .registerDevice("e", 1)
585  .registerModelType("e", 1);
586 }
587 
588 } // namespace Vcvs
589 } // namespace Device
590 } // namespace Xyce
Instance(const Configuration &configuration, const InstanceBlock &IBref, Model &Viter, const FactoryBlock &factory_block)
Definition: N_DEV_Vcvs.C:90
const InstanceName & getName() const
const DeviceOptions & deviceOptions_
Descriptor & addPar(const char *parName, T default_value, T U::*varPtr)
Adds the parameter description to the parameter map.
Definition: N_DEV_Pars.h:1429
void registerStateLIDs(const std::vector< int > &staLIDVecRef)
Definition: N_DEV_Vcvs.C:253
virtual bool loadDAEVectors(double *solVec, double *fVec, double *qVec, double *bVec, double *storeLeadF, double *storeLeadQ, double *leadF, double *leadQ, double *junctionV)
Populates the device's ExternData object with these pointers.
Definition: N_DEV_Vcvs.C:511
bool given(const std::string &parameter_name) const
Pure virtual class to augment a linear system.
void addInternalNode(Util::SymbolTable &symbol_table, int index, const InstanceName &instance_name, const std::string &lead_name)
static void loadInstanceParameters(ParametricData< Instance > &instance_parameters)
Definition: N_DEV_Vcvs.C:67
static std::vector< std::vector< int > > jacStamp
Definition: N_DEV_Vcvs.h:138
virtual bool updateState(double *solVec, double *staVec, double *stoVec)
Updates the devices state information.
Definition: N_DEV_Vcvs.C:498
InstanceVector::const_iterator getInstanceEnd() const
Returns an iterator to the ending of the vector of all instances created for this device...
#define AssertLIDs(cmp)
void registerJacLIDs(const std::vector< std::vector< int > > &jacLIDVec)
Definition: N_DEV_Vcvs.C:279
virtual void registerJacLIDs(const JacobianStamp &jacLIDVec)
const std::vector< std::vector< int > > & jacobianStamp() const
Definition: N_DEV_Vcvs.C:266
virtual void forEachInstance(DeviceInstanceOp &op) const
Apply a device instance "op" to all instances associated with this model.
Definition: N_DEV_Vcvs.C:481
InstanceVector::const_iterator getInstanceBegin() const
Returns an iterator to the beginning of the vector of all instances created for this device...
void setParams(const std::vector< Param > &params)
const std::string & getName() const
static void loadModelParameters(ParametricData< Model > &model_parameters)
Definition: N_DEV_Vcvs.C:76
The FactoryBlock contains parameters needed by the device, instance and model creation functions...
void loadNodeSymbols(Util::SymbolTable &symbol_table) const
Populates and returns the store name map.
Definition: N_DEV_Vcvs.C:240
static Device * factory(const Configuration &configuration, const FactoryBlock &factory_block)
Definition: N_DEV_Vcvs.C:575
static Config< T > & addConfiguration()
Adds the device to the Xyce device configuration.
void registerLIDs(const std::vector< int > &intLIDVecRef, const std::vector< int > &extLIDVecRef)
Definition: N_DEV_Vcvs.C:171
void varTypes(std::vector< char > &varTypeVec)
Definition: N_DEV_Vcvs.C:394
Linear::Matrix * dFdxMatrixPtr
The Device class is an interface for device implementations.
Definition: N_DEV_Device.h:101
const SolverState & solverState_
Class Configuration contains device configuration data.
virtual bool loadDAEMatrices(Linear::Matrix &dFdx, Linear::Matrix &dQdx)
Populates the device's Jacobian object with these pointers.
Definition: N_DEV_Vcvs.C:546
virtual std::ostream & printOutInstances(std::ostream &os) const
Definition: N_DEV_Vcvs.C:446
ModelBlock represents a .MODEL line from the netlist.
Manages parameter binding for class C.
Definition: N_DEV_Pars.h:214
InstanceBlock represent a device instance line from the netlist.
std::vector< Param > params
std::vector< Instance * > instanceContainer
Definition: N_DEV_Vcvs.h:224