Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-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_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.134 $
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_Vcvs.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 
70 namespace Vcvs {
71 
72 
74 {
75 // Set up double precision variables:
76  p.addPar ("G", 0.0, false, ParameterType::NO_DEP,
78  NULL,U_NONE,CAT_NONE,"Gain");
79 }
80 
82 {
83 }
84 
85 
86 
87 std::vector< std::vector<int> > Instance::jacStamp;
88 
89 //-----------------------------------------------------------------------------
90 // Function : Instance::Instance
91 // Purpose : "instance block" constructor
92 // Special Notes :
93 // Scope : public
94 // Creator : Tom Russo, SNL, Component Information and Models
95 // Creation Date : 12/20/00
96 //-----------------------------------------------------------------------------
98  const Configuration & configuration,
99  const InstanceBlock & IBref,
100  Model & Viter,
101  const FactoryBlock & factory_block)
102  : DeviceInstance(IBref, configuration.getInstanceParameters(), factory_block),
103  IB(IBref),
104  model_(Viter),
105  Gain(1.0),
106  li_Pos(-1),
107  li_Neg(-1),
108  li_Bra(-1),
109  li_ContPos(-1),
110  li_ContNeg(-1),
111  ABraEquPosNodeOffset(-1),
112  ABraEquNegNodeOffset(-1),
113  ABraEquContPosNodeOffset(-1),
114  ABraEquContNegNodeOffset(-1),
115  APosEquBraVarOffset(-1),
116  ANegEquBraVarOffset(-1),
117 
118  f_BraEquPosNodePtr(0),
119  f_BraEquNegNodePtr(0),
120  f_BraEquContPosNodePtr(0),
121  f_BraEquContNegNodePtr(0),
122  f_PosEquBraVarPtr(0),
123  f_NegEquBraVarPtr(0)
124 {
125  numIntVars = 1;
126  numExtVars = 4;
127  numStateVars = 0;
128 
129  if( jacStamp.empty() )
130  {
131  jacStamp.resize(5);
132  jacStamp[0].resize(1);
133  jacStamp[0][0]=4;
134  jacStamp[1].resize(1);
135  jacStamp[1][0]=4;
136  jacStamp[4].resize(4);
137  jacStamp[4][0]=0;
138  jacStamp[4][1]=1;
139  jacStamp[4][2]=2;
140  jacStamp[4][3]=3;
141  }
142 
143 
144  // Set params to constant default values:
145  setDefaultParams ();
146 
147  // Set params according to instance line and constant defaults from metadata:
148  setParams (IB.params);
149 
150  // Set any non-constant parameter defaults:
151  if (!given("G"))
152  {
153  UserError0(*this) << "Could not find Gain parameter in instance.";
154  }
155 }
156 
157 //-----------------------------------------------------------------------------
158 // Function : Instance::~Instance
159 // Purpose : destructor
160 // Special Notes :
161 // Scope : public
162 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
163 // Creation Date : 3/16/00
164 //-----------------------------------------------------------------------------
166 {
167 
168 }
169 
170 //-----------------------------------------------------------------------------
171 // Function : Instance::registerLIDs
172 // Purpose :
173 // Special Notes :
174 // Scope : public
175 // Creator : Robert Hoekstra, SNL, Computational Science
176 // Creation Date : 6/21/02
177 //-----------------------------------------------------------------------------
178 void Instance::registerLIDs ( const std::vector<int> & intLIDVecRef,
179  const std::vector<int> & extLIDVecRef )
180 {
181  std::string msg;
182 
183 #ifdef Xyce_DEBUG_DEVICE
184  if (getDeviceOptions().debugLevel > 0)
185  {
186  Xyce::dout() << std::endl << section_divider << std::endl;
187  Xyce::dout() << " VcvsInstance::registerLIDs" << std::endl;
188  Xyce::dout() << " name = " << getName() << std::endl;
189  }
190 #endif
191 
192  // Check if the size of the ID lists corresponds to the
193  // proper number of internal and external variables.
194  int numInt = intLIDVecRef.size();
195  int numExt = extLIDVecRef.size();
196 
197  if (numInt != numIntVars)
198  {
199  msg = "Instance::registerLIDs:";
200  msg += "numInt != numIntVars";
201  N_ERH_ErrorMgr::report ( N_ERH_ErrorMgr::DEV_FATAL,msg);
202  }
203 
204  if (numExt != numExtVars)
205  {
206  msg = "Instance::registerLIDs:";
207  msg += "numExt != numExtVars";
208  N_ERH_ErrorMgr::report ( N_ERH_ErrorMgr::DEV_FATAL,msg);
209  }
210 
211  // copy over the global ID lists.
212  intLIDVec = intLIDVecRef;
213  extLIDVec = extLIDVecRef;
214 
215  // Now use these lists to obtain the indices into the
216  // linear algebra entities. This assumes an order.
217  // For the matrix indices, first do the rows.
218 
219  li_Pos = extLIDVec[0];
220  li_Neg = extLIDVec[1];
221  li_ContPos = extLIDVec[2];
222  li_ContNeg = extLIDVec[3];
223 
224 #ifdef Xyce_DEBUG_DEVICE
225  if (getDeviceOptions().debugLevel > 0)
226  {
227  Xyce::dout() << " li_Pos = " << li_Pos << std::endl;
228  Xyce::dout() << " li_Neg = " << li_Neg << std::endl;
229  Xyce::dout() << " li_ContPos = " << li_ContPos << std::endl;
230  Xyce::dout() << " li_ContNeg = " << li_ContNeg << std::endl;
231  }
232 #endif
233 
234  li_Bra = intLIDVec[0];
235 
236 #ifdef Xyce_DEBUG_DEVICE
237  if (getDeviceOptions().debugLevel > 0)
238  Xyce::dout() << " li_Bra = " << li_Bra << std::endl;
239 #endif
240 
241 #ifdef Xyce_DEBUG_DEVICE
242  if (getDeviceOptions().debugLevel > 0)
243  Xyce::dout() << section_divider << std::endl;
244 #endif
245 }
246 
247 //-----------------------------------------------------------------------------
248 // Function : Instance::getIntNameMap
249 // Purpose :
250 // Special Notes :
251 // Scope : public
252 // Creator : Eric R. Keiter, SNL, Parallel Computational Sciences
253 // Creation Date : 05/13/05
254 //-----------------------------------------------------------------------------
255 std::map<int,std::string> & Instance::getIntNameMap ()
256 {
257  // set up the internal name map, if it hasn't been already.
258  if (intNameMap.empty ())
259  {
260  intNameMap[ li_Bra ] = spiceInternalName(getName(), "branch");
261  }
262 
263  return intNameMap;
264 }
265 
266 //-----------------------------------------------------------------------------
267 // Function : Instance::registerStateGIDs
268 // Purpose :
269 // Special Notes :
270 // Scope : public
271 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
272 // Creation Date : 6/21/02
273 //-----------------------------------------------------------------------------
274 void Instance::registerStateLIDs( const std::vector<int> & staLIDVecRef )
275 {
276  AssertLIDs(staLIDVecRef.size() == numStateVars);
277 }
278 
279 //-----------------------------------------------------------------------------
280 // Function : Instance::jacobianStamp
281 // Purpose :
282 // Special Notes :
283 // Scope : public
284 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
285 // Creation Date : 9/2/02
286 //-----------------------------------------------------------------------------
287 const std::vector< std::vector<int> > & Instance::jacobianStamp() const
288 {
289  return jacStamp;
290 }
291 
292 //-----------------------------------------------------------------------------
293 // Function : Instance::registerJacLIDs
294 // Purpose :
295 // Special Notes :
296 // Scope : public
297 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
298 // Creation Date : 9/2/02
299 //-----------------------------------------------------------------------------
300 void Instance::registerJacLIDs( const std::vector< std::vector<int> > & jacLIDVec )
301 {
302  DeviceInstance::registerJacLIDs( jacLIDVec );
303 
304  APosEquBraVarOffset = jacLIDVec[0][0];
305  ANegEquBraVarOffset = jacLIDVec[1][0];
306  ABraEquPosNodeOffset = jacLIDVec[4][0];
307  ABraEquNegNodeOffset = jacLIDVec[4][1];
308  ABraEquContPosNodeOffset = jacLIDVec[4][2];
309  ABraEquContNegNodeOffset = jacLIDVec[4][3];
310 }
311 
312 //-----------------------------------------------------------------------------
313 // Function : Instance::setupPointers
314 // Purpose :
315 // Special Notes :
316 // Scope : public
317 // Creator : Eric Keiter, SNL
318 // Creation Date : 12/12/08
319 //-----------------------------------------------------------------------------
321 {
322 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
323  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
324 
331 #endif
332 }
333 
334 //-----------------------------------------------------------------------------
335 // Function : Instance::updatePrimaryState
336 // Purpose :
337 // Special Notes :
338 // Scope : public
339 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
340 // Creation Date : 01/29/01
341 //-----------------------------------------------------------------------------
343 {
344  return true;
345 }
346 
347 //-----------------------------------------------------------------------------
348 // Function : Instance::loadDAEFVector
349 //
350 // Purpose : Loads the F-vector contributions for a single
351 // Vcvs instance.
352 //
353 // Special Notes :
354 //
355 // Scope : public
356 // Creator : Eric R. Keiter, SNL, Parallel Computational Sciences
357 // Creation Date : 02/19/05
358 //-----------------------------------------------------------------------------
360 {
361  double * fVec = extData.daeFVectorRawPtr;
362  double * solVec = extData.nextSolVectorRawPtr;
363 
364  double v_pos = solVec[li_Pos];
365  double v_neg = solVec[li_Neg];
366  double v_cont_pos = solVec[li_ContPos];
367  double v_cont_neg = solVec[li_ContNeg];
368 
369  double i_bra = solVec[li_Bra];
370 
371  fVec[li_Pos] += i_bra;
372  fVec[li_Neg] += -i_bra;
373 
374  double src = Gain * ( v_cont_pos - v_cont_neg ) - v_pos + v_neg;
375  fVec[li_Bra] += -src;
376 
377  return true;
378 }
379 
380 //-----------------------------------------------------------------------------
381 // Function : Instance::loadDAEdFdx ()
382 //
383 // Purpose : Loads the F-vector contributions for a single
384 // resistor instance.
385 //
386 // Special Notes : The F-vector is an algebraic constaint.
387 //
388 // Scope : public
389 // Creator : Eric R. Keiter, SNL, Parallel Computational Sciences
390 // Creation Date : 02/19/05
391 //-----------------------------------------------------------------------------
393 {
394  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
395 
396  dFdx[li_Pos][APosEquBraVarOffset] += 1.0;
397  dFdx[li_Neg][ANegEquBraVarOffset] -= 1.0;
398 
399  dFdx[li_Bra][ABraEquPosNodeOffset] += 1.0;
400  dFdx[li_Bra][ABraEquNegNodeOffset] -= 1.0;
403 
404  return true;
405 }
406 
407 //-----------------------------------------------------------------------------
408 // Function : Instance::varTypes
409 // Purpose :
410 // Special Notes :
411 // Scope : public
412 // Creator : Rob Hoekstra, SNL, Parallel Computational Sciences
413 // Creation Date : 02/17/04
414 //-----------------------------------------------------------------------------
415 void Instance::varTypes( std::vector<char> & varTypeVec )
416 {
417  varTypeVec.resize(1);
418  varTypeVec[0] = 'I';
419 }
420 
421 // Class Model
422 
423 //-----------------------------------------------------------------------------
424 // Function : Model::Model
425 // Purpose : "Model block" constructor
426 // Special Notes :
427 // Scope : public
428 // Creator : Tom Russo, SNL, Component Information and Models
429 // Creation Date : 12/21/00
430 //-----------------------------------------------------------------------------
432  const Configuration & configuration,
433  const ModelBlock & MB,
434  const FactoryBlock & factory_block)
435  : DeviceModel(MB, configuration.getModelParameters(), factory_block)
436 {
437 }
438 
439 //-----------------------------------------------------------------------------
440 // Function : Model::~Model
441 // Purpose : destructor
442 // Special Notes :
443 // Scope : public
444 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
445 // Creation Date : 3/16/00
446 //-----------------------------------------------------------------------------
448 {
449  std::vector<Instance*>::iterator iter;
450  std::vector<Instance*>::iterator first = instanceContainer.begin();
451  std::vector<Instance*>::iterator last = instanceContainer.end();
452 
453  for (iter=first; iter!=last; ++iter)
454  {
455  delete (*iter);
456  }
457 }
458 
459 //-----------------------------------------------------------------------------
460 // Function : Model::printOutInstances
461 // Purpose : debugging tool.
462 // Special Notes :
463 // Scope : public
464 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
465 // Creation Date : 4/03/00
466 //-----------------------------------------------------------------------------
467 std::ostream &Model::printOutInstances(std::ostream &os) const
468 {
469  std::vector<Instance*>::const_iterator iter;
470  std::vector<Instance*>::const_iterator first = instanceContainer.begin();
471  std::vector<Instance*>::const_iterator last = instanceContainer.end();
472 
473  int i;
474  os << std::endl;
475  os << " name model name Parameters" << std::endl;
476  for (i=0, iter=first; iter!=last; ++iter, ++i)
477  {
478  os << " " << i << ": " << (*iter)->getName() << " ";
479  os << getName();
480  os << std::endl;
481  }
482 
483  os << std::endl;
484 
485  return os;
486 }
487 
488 //-----------------------------------------------------------------------------
489 // Function : Model::forEachInstance
490 // Purpose :
491 // Special Notes :
492 // Scope : public
493 // Creator : David Baur
494 // Creation Date : 2/4/2014
495 //-----------------------------------------------------------------------------
496 /// Apply a device instance "op" to all instances associated with this
497 /// model
498 ///
499 /// @param[in] op Operator to apply to all instances.
500 ///
501 ///
502 void Model::forEachInstance(DeviceInstanceOp &op) const /* override */
503 {
504  for (std::vector<Instance *>::const_iterator it = instanceContainer.begin(); it != instanceContainer.end(); ++it)
505  op(*it);
506 }
507 
508 
509 // Vcvs Master functions:
510 
511 //-----------------------------------------------------------------------------
512 // Function : Master::updateState
513 // Purpose :
514 // Special Notes :
515 // Scope : public
516 // Creator : Eric Keiter, SNL
517 // Creation Date : 11/26/08
518 //-----------------------------------------------------------------------------
519 bool Master::updateState (double * solVec, double * staVec, double * stoVec)
520 {
521  return true;
522 }
523 
524 //-----------------------------------------------------------------------------
525 // Function : Master::loadDAEVectors
526 // Purpose :
527 // Special Notes :
528 // Scope : public
529 // Creator : Eric Keiter, SNL
530 // Creation Date : 11/26/08
531 //-----------------------------------------------------------------------------
532 bool Master::loadDAEVectors (double * solVec, double * fVec, double *qVec, double * bVec, double * storeLeadF, double * storeLeadQ)
533 {
534 #ifdef _OMP
535 #pragma omp parallel for
536 #endif
537  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
538  {
539  Instance & vi = *(*it);
540 
541  double v_pos = solVec[vi.li_Pos];
542  double v_neg = solVec[vi.li_Neg];
543  double v_cont_pos = solVec[vi.li_ContPos];
544  double v_cont_neg = solVec[vi.li_ContNeg];
545 
546  double i_bra = solVec[vi.li_Bra];
547 
548  fVec[vi.li_Pos] += i_bra;
549  fVec[vi.li_Neg] += -i_bra;
550 
551  double src = vi.Gain * ( v_cont_pos - v_cont_neg ) - v_pos + v_neg;
552  fVec[vi.li_Bra] += -src;
553 
554  }
555 
556  return true;
557 }
558 
559 //-----------------------------------------------------------------------------
560 // Function : Master::loadDAEMatrices
561 // Purpose :
562 // Special Notes :
563 // Scope : public
564 // Creator : Eric Keiter, SNL
565 // Creation Date : 11/26/08
566 //-----------------------------------------------------------------------------
567 bool Master::loadDAEMatrices (N_LAS_Matrix & dFdx, N_LAS_Matrix & dQdx)
568 {
569  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
570  {
571  Instance & vi = *(*it);
572 
573 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
574 
575  *vi.f_PosEquBraVarPtr += 1.0;
576  *vi.f_NegEquBraVarPtr -= 1.0;
577 
578  *vi.f_BraEquPosNodePtr += 1.0;
579  *vi.f_BraEquNegNodePtr -= 1.0;
580  *vi.f_BraEquContPosNodePtr -= vi.Gain;
581  *vi.f_BraEquContNegNodePtr += vi.Gain;
582 #else
583 
584  dFdx[vi.li_Pos][vi.APosEquBraVarOffset] += 1.0;
585  dFdx[vi.li_Neg][vi.ANegEquBraVarOffset] -= 1.0;
586 
587  dFdx[vi.li_Bra][vi.ABraEquPosNodeOffset] += 1.0;
588  dFdx[vi.li_Bra][vi.ABraEquNegNodeOffset] -= 1.0;
589  dFdx[vi.li_Bra][vi.ABraEquContPosNodeOffset] -= vi.Gain;
590  dFdx[vi.li_Bra][vi.ABraEquContNegNodeOffset] += vi.Gain;
591 #endif
592  }
593  return true;
594 }
595 
596 Device *Traits::factory(const Configuration &configuration, const FactoryBlock &factory_block)
597 {
598 
599  return new Master(configuration, factory_block, factory_block.solverState_, factory_block.deviceOptions_);
600 }
601 
603 {
605  .registerDevice("e", 1)
606  .registerModelType("e", 1);
607 }
608 
609 } // namespace Vcvs
610 } // namespace Device
611 } // namespace Xyce