Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
N_DEV_Vsrc.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_Vsrc.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.235 $
40 //
41 // Revision Date : $Date: 2014/08/10 14:05:49 $
42 //
43 // Current Owner : $Author: erkeite $
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_SourceData.h>
55 #include <N_DEV_Vsrc.h>
56 #include <N_DEV_Message.h>
57 #include <N_ERH_ErrorMgr.h>
58 
59 #include <N_LAS_Vector.h>
60 #include <N_LAS_Matrix.h>
61 #include <N_UTL_BreakPoint.h>
62 
63 namespace Xyce {
64 namespace Device {
65 
66 
67 namespace Vsrc {
68 
69 
71 {
72  // Set up double precision variables:
73  // DC parameters
74  //p.addPar ("DCV0", 0.0, true, ParameterType::NO_DEP,
75  //&Vsrc::Instance::DCV0,
76  //NULL, U_VOLT, CAT_NONE, "DC Voltage")
77  p.addPar ("DCV0", 0.0, &Vsrc::Instance::DCV0)
78  .setOriginalValueStored(true)
79  .setUnit(U_VOLT)
80  .setDescription("DC Voltage")
81  .setAnalyticSensitivityAvailable(true)
82  .setSensitivityFunctor(&dcv0Sens);
83 
84  // Pulse parameters
85  p.addPar ("V0", 0.0, false, ParameterType::NO_DEP,
87  NULL, U_VOLT, CAT_NONE, "Offset Voltage");
88 
89  p.addPar ("V1", 0.0, false, ParameterType::NO_DEP,
91  NULL, U_VOLT, CAT_NONE, "Initial Voltage");
92 
93  p.addPar ("V2", 0.0, false, ParameterType::NO_DEP,
95  NULL, U_VOLT, CAT_NONE, "Pulsed Voltage");
96 
97  p.addPar ("TD", 0.0, false, ParameterType::NO_DEP,
99  NULL, U_SECOND, CAT_NONE, "Delay");
100 
101  p.addPar ("TR", 0.0, false, ParameterType::NO_DEP,
103  NULL, U_SECOND, CAT_NONE, "Rise Time");
104 
105  p.addPar ("TF", 0.0, false, ParameterType::NO_DEP,
107  NULL, U_SECOND, CAT_NONE, "Fall Time");
108 
109  p.addPar ("PW", 0.0, false, ParameterType::NO_DEP,
111  NULL, U_SECOND, CAT_NONE, "Pulse Width");
112 
113  p.addPar ("PER", 0.0, false, ParameterType::NO_DEP,
115  NULL, U_SECOND, CAT_NONE, "Period");
116 
117  p.addPar ("SF", 0.0, false, ParameterType::NO_DEP,
119  NULL, U_NONE, CAT_NONE, "Scale Factor -- smooth pulse only");
120 
121  // Sin parameters
122  p.addPar ("VA", 0.0, false, ParameterType::NO_DEP,
124  NULL, U_VOLT, CAT_NONE, "Amplitude");
125 
126  p.addPar ("FREQ", 0.0, false, ParameterType::NO_DEP,
128  NULL, U_SECM1, CAT_NONE, "Frequency");
129 
130  p.addPar ("THETA", 0.0, false, ParameterType::NO_DEP,
132  NULL, U_NONE, CAT_NONE, "Theta");
133 
134  p.addPar ("PHASE", 0.0, false, ParameterType::NO_DEP,
136  NULL, U_NONE, CAT_NONE, "Phase");
137 
138  // Exp parameters
139  p.addPar ("TD1", 0.0, false, ParameterType::NO_DEP,
141  NULL, U_SECOND, CAT_NONE, "Rise Delay Time");
142 
143  p.addPar ("TAU1", 0.0, false, ParameterType::NO_DEP,
145  NULL, U_SECOND, CAT_NONE, "Rise Time Constant");
146 
147  p.addPar ("TD2", 0.0, false, ParameterType::NO_DEP,
149  NULL, U_SECOND, CAT_NONE, "Fall Delay Time");
150 
151  p.addPar ("TAU2", 0.0, false, ParameterType::NO_DEP,
153  NULL, U_SECOND, CAT_NONE, "Fall Time Constant");
154 
155 // AC parameters
156  p.addPar ("ACMAG", 0.0, false, ParameterType::NO_DEP,
158  NULL, U_VOLT, CAT_NONE, "Amplitude");
159 
160  p.addPar ("ACPHASE", 0.0, false, ParameterType::NO_DEP,
162  NULL, U_NONE, CAT_NONE, "Phase");
163 
164  // SFFM parameters
165  p.addPar ("FC", 0.0, false, ParameterType::NO_DEP,
167  NULL, U_SECM1, CAT_NONE, "Carrier Frequency");
168 
169  p.addPar ("FS", 0.0, false, ParameterType::NO_DEP,
171  NULL, U_SECM1, CAT_NONE, "Signal Frequency");
172 
173  p.addPar ("MDI", 0.0, false, ParameterType::NO_DEP,
175  NULL, U_NONE, CAT_NONE, "Modulation Index");
176 
177  // PWL params
178  p.addPar ("R", 0.0, false, ParameterType::NO_DEP,
180  NULL, U_SECOND, CAT_NONE, "Repeat Time");
181 
182  p.addPar ("T", 0.0, false, ParameterType::NO_DEP,
184  NULL, U_SECOND, CAT_NONE, "Time"); // time-voltage pairs
185 
186  p.addPar ("V", 0.0, false, ParameterType::NO_DEP,
188  NULL, U_VOLT, CAT_NONE, "Voltage"); // time-voltage pairs
189 
190  // Set up exceptions (ie variables that are not doubles):
191  p.addPar ("TRANSIENTSOURCETYPE", (int) _DC_DATA, false, ParameterType::NO_DEP,
194  U_NONE, CAT_NONE, "" );
195 
196  p.addPar ("ACSOURCETYPE", (int) _AC_DATA, false, ParameterType::NO_DEP,
199  U_NONE, CAT_NONE, "" );
200 
201  p.addPar ("DCSOURCETYPE", (int) _DC_DATA, false, ParameterType::NO_DEP,
204  U_NONE, CAT_NONE, "" );
205 
206  p.addPar ("NUM", 0, false, ParameterType::NO_DEP,
208  NULL, U_NONE, CAT_NONE, "" );
209 }
210 
212 {
213 }
214 
215 
216 std::vector< std::vector<int> > Instance::jacStamp;
217 std::vector< std::vector<int> > Instance::jacStampPDE;
218 
219 // Class Instance
220 //-----------------------------------------------------------------------------
221 // Function : Instance::Instance
222 // Purpose : "instance block" constructor
223 // Special Notes :
224 // Scope : public
225 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
226 // Creation Date : 04/06/00
227 //-----------------------------------------------------------------------------
229  const Configuration & configuration,
230  const InstanceBlock & IB,
231  Model & Viter,
232  const FactoryBlock & factory_block)
233  : SourceInstance(IB, configuration.getInstanceParameters(), factory_block),
234  model_(Viter),
235  srcCurrent(0.0),
236  srcVoltage(0.0),
237  srcDrop(0.0),
238  srcBC(0.0),
239 
240  scale(1.0),
241  nlstep(-1),
242  DCV0(0.0),
243  par0(0.0),
244  par1(0.0),
245  par2(0.0),
246  par3(0.0),
247  par4(0.0),
248  par5(0.0),
249  par6(0.0),
250  par7(0.0),
251  REPEATTIME(),
252  T(0.0),
253  V(0.0),
254  ACMAG(1.0),
255  ACPHASE(0.0),
256  NUM(0),
257  REPEAT(false),
258  TRANSIENTSOURCETYPE(_DC_DATA),
259  TRANSIENTSOURCETYPEgiven(false),
260  ACSOURCETYPE(_AC_DATA),
261  ACSOURCETYPEgiven(false),
262  DCSOURCETYPE(_AC_DATA),
263  DCSOURCETYPEgiven(false),
264 
265  li_Pos(-1),
266  li_Neg(-1),
267  li_Bra(-1),
268 
269  gotParams(false),
270 
271  ABraEquPosNodeOffset(-1),
272  ABraEquNegNodeOffset(-1),
273  APosEquBraVarOffset(-1),
274  ANegEquBraVarOffset(-1),
275  ABraEquBraVarOffset(-1),
276  APosEquPosNodeOffset(-1),
277  ANegEquNegNodeOffset(-1),
278 
280  fBraEquPosNodePtr(0),
281  fBraEquNegNodePtr(0),
282  fPosEquBraVarPtr(0),
283  fNegEquBraVarPtr(0),
284  fPosEquPosNodePtr(0),
285  fNegEquNegNodePtr(0),
286  fBraEquBraVarPtr(0),
287 #endif
288 
289  source(0.0),
290  v_pos(0.0),
291  v_neg(0.0),
292  i_bra(0.0)
293 {
294  numIntVars = 1;
295  numExtVars = 2;
296  numStateVars = 0;
297 
298  if( jacStamp.empty() )
299  {
300  jacStamp.resize(3);
301  jacStamp[0].resize(1);
302  jacStamp[0][0] = 2;
303  jacStamp[1].resize(1);
304  jacStamp[1][0] = 2;
305  jacStamp[2].resize(2);
306  jacStamp[2][0] = 0;
307  jacStamp[2][1] = 1;
308 
309  // PDE supporting stamp. This includes diagonal elements, needed by the
310  // 2-level Newton.
311  jacStampPDE.resize(3);
312  jacStampPDE[0].resize(2);
313  jacStampPDE[0][0] = 0;
314  jacStampPDE[0][1] = 2;
315  jacStampPDE[1].resize(2);
316  jacStampPDE[1][0] = 1;
317  jacStampPDE[1][1] = 2;
318  jacStampPDE[2].resize(3);
319  jacStampPDE[2][0] = 0;
320  jacStampPDE[2][1] = 1;
321  jacStampPDE[2][2] = 2;
322  }
323 
324  // Set params to constant default values:
325  setDefaultParams ();
326 
327  // Set params according to instance line and constant defaults from metadata:
328  setParams (IB.params);
329 
330  // Set any non-constant parameter defaults:
331  if (getSolverState().ACspecified && ACSOURCETYPEgiven)
332  {
334  }
335 
336  if (DCSOURCETYPEgiven) // this will always be given, if the source spec was valid.
337  {
339  }
340 
341  if (getSolverState().HBspecified || TRANSIENTSOURCETYPEgiven)
342  {
343  switch (TRANSIENTSOURCETYPE)
344  {
345  case _SIN_DATA:
347  break;
348 
349  case _EXP_DATA:
351  break;
352 
353  case _PULSE_DATA:
355  break;
356 
357  case _PWL_DATA:
359  break;
360 
361  case _SFFM_DATA:
363  break;
364 
365  case _DC_DATA:
367  break;
368 
369  case _SMOOTH_PULSE_DATA:
371  break;
372 
373  default:
374  UserFatal0(*this) << "Cannot identify source data type for " << getName();
375  break;
376  }
377  }
378 
379  processParams();
380 
381  // Calculate any parameters specified as expressions:
383  processParams();
384 
385  // calculate dependent (ie computed) params and check for errors:
386 
387 }
388 
389 //-----------------------------------------------------------------------------
390 // Function : Instance::processParams
391 // Purpose :
392 // Special Notes :
393 // Scope : public
394 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
395 // Creation Date : 07/26/03
396 //-----------------------------------------------------------------------------
398 {
399  if (gotParams)
400  {
401  if (dcSourceData_ != 0)
402  {
404  }
405  if (acSourceData_ != 0)
406  {
408  }
409  if (tranSourceData_ != 0)
410  {
412  }
413  }
414  else
415  {
416  if (dcSourceData_ != 0)
417  {
419  }
420  if (acSourceData_ != 0)
421  {
423  }
424  if (tranSourceData_ != 0)
425  {
427  }
428  gotParams = true;
429  }
430 
431  return true;
432 }
433 
434 //-----------------------------------------------------------------------------
435 // Function : Instance::~Instance
436 // Purpose : destructor
437 // Special Notes :
438 // Scope : public
439 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
440 // Creation Date : 04/06/00
441 //-----------------------------------------------------------------------------
443 {
444  delete tranSourceData_;
445  delete acSourceData_;
446  delete dcSourceData_;
447 }
448 
449 // Additional Declarations
450 
451 //-----------------------------------------------------------------------------
452 // Function : Instance::registerLIDs
453 // Purpose :
454 // Special Notes :
455 // Scope : public
456 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
457 // Creation Date : 6/20/02
458 //-----------------------------------------------------------------------------
459 void Instance::registerLIDs ( const std::vector<int> & intLIDVecRef,
460  const std::vector<int> & extLIDVecRef)
461 {
462  std::string msg;
463 
464 #ifdef Xyce_DEBUG_DEVICE
465  if (getDeviceOptions().debugLevel > 0 )
466  {
467  Xyce::dout() << std::endl << section_divider << std::endl;
468  Xyce::dout() << " VsrcInstance::registerLIDs" << std::endl;
469  Xyce::dout() << " name = " << getName() << std::endl;
470  }
471 #endif
472 
473  // Check if the size of the ID lists corresponds to the
474  // proper number of internal and external variables.
475  int numInt = intLIDVecRef.size();
476  int numExt = extLIDVecRef.size();
477 
478  if (numInt != numIntVars)
479  {
480  msg = "Instance::registerLIDs:";
481  msg += "numInt != numIntVars";
482  N_ERH_ErrorMgr::report ( N_ERH_ErrorMgr::DEV_FATAL_0,msg);
483  }
484 
485  if (numExt != numExtVars)
486  {
487  msg = "Instance::registerLIDs:";
488  msg += "numExt != numExtVars";
489  N_ERH_ErrorMgr::report ( N_ERH_ErrorMgr::DEV_FATAL_0,msg);
490  }
491 
492  // copy over the global ID lists.
493  intLIDVec = intLIDVecRef;
494  extLIDVec = extLIDVecRef;
495 
496  // Now use these lists to obtain the indices into the
497  // linear algebra entities. This assumes an order.
498  // For the matrix indices, first do the rows.
499 
500  li_Pos = extLIDVec[0];
501  li_Neg = extLIDVec[1];
502  li_Bra = intLIDVec[0];
503 
504 #ifdef Xyce_DEBUG_DEVICE
505  if (getDeviceOptions().debugLevel > 0 )
506  {
507  Xyce::dout() << " li_Pos = " << li_Pos << std::endl;
508  Xyce::dout() << " li_Neg = " << li_Neg << std::endl;
509  Xyce::dout() << " li_Bra = " << li_Bra << std::endl;
510  Xyce::dout() << section_divider << std::endl;
511  }
512 #endif
513 }
514 
515 //-----------------------------------------------------------------------------
516 // Function : Instance::getIntNameMap
517 // Purpose :
518 // Special Notes :
519 // Scope : public
520 // Creator : Eric R. Keiter, SNL, Parallel Computational Sciences
521 // Creation Date : 05/13/05
522 //-----------------------------------------------------------------------------
523 std::map<int,std::string> & Instance::getIntNameMap ()
524 {
525  // set up the internal name map, if it hasn't been already.
526  if (intNameMap.empty ())
527  {
528  intNameMap[ li_Bra ] = spiceInternalName(getName(), "branch");
529  }
530 
531  return intNameMap;
532 }
533 
534 
535 
536 //-----------------------------------------------------------------------------
537 // Function : Instance::registerStateLIDs
538 // Purpose :
539 // Special Notes :
540 // Scope : public
541 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
542 // Creation Date : 6/20/02
543 //-----------------------------------------------------------------------------
544 void Instance::registerStateLIDs( const std::vector<int> & staLIDVecRef )
545 {
546  AssertLIDs(staLIDVecRef.size() == numStateVars);
547 }
548 
549 //-----------------------------------------------------------------------------
550 // Function : Instance::jacobianStamp
551 // Purpose :
552 // Special Notes :
553 // Scope : public
554 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
555 // Creation Date : 8/21/02
556 //-----------------------------------------------------------------------------
557 const std::vector< std::vector<int> > & Instance::jacobianStamp() const
558 {
559  if (getSolverState().PDESystemFlag)
560  return jacStampPDE;
561 
562  return jacStamp;
563 }
564 
565 //-----------------------------------------------------------------------------
566 // Function : Instance::registerJacLIDs
567 // Purpose :
568 // Special Notes :
569 // Scope : public
570 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
571 // Creation Date : 8/27/02
572 //-----------------------------------------------------------------------------
573 void Instance::registerJacLIDs( const std::vector< std::vector<int> > & jacLIDVec )
574 {
575  DeviceInstance::registerJacLIDs( jacLIDVec );
576  if (getSolverState().PDESystemFlag)
577  {
578  APosEquBraVarOffset = jacLIDVec[0][1];
579  ANegEquBraVarOffset = jacLIDVec[1][1];
580  ABraEquPosNodeOffset = jacLIDVec[2][0];
581  ABraEquNegNodeOffset = jacLIDVec[2][1];
582  }
583  else
584  {
585  APosEquBraVarOffset = jacLIDVec[0][0];
586  ANegEquBraVarOffset = jacLIDVec[1][0];
587  ABraEquPosNodeOffset = jacLIDVec[2][0];
588  ABraEquNegNodeOffset = jacLIDVec[2][1];
589  }
590 
591 }
592 
593 //-----------------------------------------------------------------------------
594 // Function : Instance::setupPointers
595 // Purpose :
596 // Special Notes :
597 // Scope : public
598 // Creator : Eric Keiter, SNL
599 // Creation Date : 11/30/08
600 //-----------------------------------------------------------------------------
602 {
603 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
604  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
605 
610 #endif
611 }
612 
613 //-----------------------------------------------------------------------------
614 // Function : Instance::updateIntermediateVars
615 // Purpose :
616 // Special Notes :
617 // Scope : public
618 // Creator : Eric Keiter, 9233, Computational Sciences
619 // Creation Date : 03/05/04
620 //-----------------------------------------------------------------------------
622 {
623  double * solVec = extData.nextSolVectorRawPtr;
624 
625  // Get the value for the source.
626  SourceData *dataPtr = dcSourceData_; // by default assume the DC value.
627  if ( (getSolverState().HBspecified || getSolverState().tranopFlag || getSolverState().transientFlag) && tranSourceData_ != 0 )
628  {
629  dataPtr = tranSourceData_;
630  }
631 
632  if (dataPtr != 0)
633  {
634  source = dataPtr->returnSource();
635  }
636  else
637  {
638  source = 0.0;
639  }
640 
641  // get the value for v_pos, v_neg, i_bra
642  v_pos = solVec[li_Pos];
643  v_neg = solVec[li_Neg];
644  i_bra = solVec[li_Bra];
645 
646  srcCurrent = i_bra;
647  srcDrop = (v_pos-v_neg);
648  srcBC = source;
650 
651  if( getDeviceOptions().scale_src != 0.0 )
652  {
653  srcCurrent *= scale;
654 
655  // first newton step, generate new scaling
656  //if( nlsMgrPtr->getNonLinearIter() != nlstep )
657  if( getSolverState().newtonIter != nlstep )
658  {
659  //nlstep = nlsMgrPtr->getNonLinearIter();
661  double new_scale = fabs(i_bra) * scale;
662 
663  scale = Xycemax( new_scale, getDeviceOptions().scale_src );
664  }
665 
666  srcVoltage *= scale;
667  srcDrop *= scale;
668  srcBC *= scale;
669  }
670 
671  return true;
672 }
673 
674 //-----------------------------------------------------------------------------
675 // Function : Instance::updatePrimaryState
676 // Purpose :
677 // Special Notes :
678 // Scope : public
679 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
680 // Creation Date : 01/29/01
681 //-----------------------------------------------------------------------------
683 {
684  bool bsuccess = updateIntermediateVars ();
685  return bsuccess;
686 }
687 
688 //-----------------------------------------------------------------------------
689 // Function : Instance::loadDAEFVector
690 // Purpose : Loads the F-vector contributions for a single vsrc instance.
691 // Special Notes :
692 // Scope : public
693 // Creator : Eric Keiter, SNL
694 // Creation Date :
695 //-----------------------------------------------------------------------------
697 {
698  double * fVec = extData.daeFVectorRawPtr;
699 
700  fVec[li_Pos] += srcCurrent;
701  fVec[li_Neg] += -srcCurrent;
702  fVec[li_Bra] += srcDrop;
703 #ifndef SEPARATE_F_AND_B
704  fVec[li_Bra] -= srcBC;
705 #endif
706 
707  return true;
708 }
709 
710 //-----------------------------------------------------------------------------
711 // Function : Instance::loadDAEBVector
712 //
713 // Purpose : Loads the B-vector contributions for a single
714 // vsrc instance.
715 //
716 // Special Notes :
717 //
718 // Scope : public
719 // Creator : Eric Keiter, SNL
720 // Creation Date :
721 //-----------------------------------------------------------------------------
723 {
724  double * bVec = extData.daeBVectorRawPtr;
725  bVec[li_Bra] += srcBC;
726 
727  return true;
728 }
729 
730 //-----------------------------------------------------------------------------
731 // Function : Instance::loadBVectorsforAC
732 //
733 // Purpose : Loads the B-vector contributions for a single
734 // vsrc instance.
735 //
736 // Special Notes :
737 //
738 // Scope : public
739 // Creator : Ting Mei, SNL
740 // Creation Date : 5/11
741 //-----------------------------------------------------------------------------
742 bool Instance::loadBVectorsforAC(double * bVecReal, double * bVecImag )
743 {
744  if (acSourceData_ != 0)
745  {
746  bool flag = true;
747  acSourceData_->setRealFlag(flag);
748 
751  srcBC = source;
752 
753  if( getDeviceOptions().scale_src != 0.0 )
754  {
755  srcBC *= scale;
756  }
757 
758  bVecReal[li_Bra] += srcBC;
759 
760  flag = false;
761  acSourceData_->setRealFlag(flag);
762 
765  srcBC = source;
766 
767  if( getDeviceOptions().scale_src != 0.0 )
768  {
769  srcBC *= scale;
770  }
771 
772  bVecImag[li_Bra] += srcBC;
773  }
774 
775  return true;
776 }
777 
778 //-----------------------------------------------------------------------------
779 // Function : Instance::loadDAEdFdx ()
780 //
781 // Purpose : Loads the F-vector contributions for a single
782 // vsrc instance.
783 //
784 // Special Notes :
785 //
786 // Scope : public
787 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
788 // Creation Date : 03/05/04
789 //-----------------------------------------------------------------------------
791 {
792  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
793 
794  dFdx[li_Pos][APosEquBraVarOffset] += 1.0;
795  dFdx[li_Neg][ANegEquBraVarOffset] -= 1.0;
796  dFdx[li_Bra][ABraEquPosNodeOffset] += 1.0;
797  dFdx[li_Bra][ABraEquNegNodeOffset] -= 1.0;
798 
799  return true;
800 }
801 
802 // end of new-DAE functions
803 
804 //-----------------------------------------------------------------------------
805 // Function : Instance::getMaxTimeStepSize
806 // Purpose :
807 // Special Notes :
808 // Scope : public
809 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
810 // Creation Date : 07/23/03
811 //-----------------------------------------------------------------------------
813 {
814  double maxStep = 1.0e+100;
815  if (tranSourceData_ != 0)
816  {
817  maxStep = tranSourceData_->getMaxTimeStepSize ();
818  }
819  return maxStep;
820 }
821 
822 //-----------------------------------------------------------------------------
823 // Function : Instance::varTypes
824 // Purpose :
825 // Special Notes :
826 // Scope : public
827 // Creator : Rob Hoekstra, SNL, Parallel Computational Sciences
828 // Creation Date : 02/17/04
829 //-----------------------------------------------------------------------------
830 void Instance::varTypes( std::vector<char> & varTypeVec )
831 {
832  varTypeVec.resize(1);
833  varTypeVec[0] = 'I';
834 }
835 
836 // Class Model
837 
838 //-----------------------------------------------------------------------------
839 // Function : Model::Model
840 // Purpose : copy constructor
841 // Special Notes :
842 // Scope : public
843 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
844 // Creation Date : 04/06/00
845 //-----------------------------------------------------------------------------
847  const Configuration & configuration,
848  const ModelBlock & MB,
849  const FactoryBlock & factory_block)
850  : DeviceModel(MB, configuration.getModelParameters(), factory_block),
851  DC_TRAN (0)
852 {
853 }
854 
855 //-----------------------------------------------------------------------------
856 // Function : Model::~Model
857 // Purpose : destructor
858 // Special Notes :
859 // Scope : public
860 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
861 // Creation Date : 04/06/00
862 //-----------------------------------------------------------------------------
864 {
865  std::vector<Instance*>::iterator iter;
866  std::vector<Instance*>::iterator first = instanceContainer.begin();
867  std::vector<Instance*>::iterator last = instanceContainer.end();
868 
869  for (iter=first; iter!=last; ++iter)
870  {
871  delete (*iter);
872  }
873 }
874 
875 // Additional Declarations
876 
877 //-----------------------------------------------------------------------------
878 // Function : Model::printOutInstances
879 // Purpose : debugging tool.
880 // Special Notes :
881 // Scope : public
882 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
883 // Creation Date : 4/03/00
884 //-----------------------------------------------------------------------------
885 std::ostream &Model::printOutInstances(std::ostream &os) const
886 {
887  std::vector<Instance*>::const_iterator iter;
888  std::vector<Instance*>::const_iterator first = instanceContainer.begin();
889  std::vector<Instance*>::const_iterator last = instanceContainer.end();
890 
891  int i;
892  os << std::endl;
893  os << " name model name Parameters" << std::endl;
894  for (i=0, iter=first; iter!=last; ++iter, ++i)
895  {
896  os << " " << i << ": " << (*iter)->getName() << " ";
897  os << getName();
898  os << std::endl;
899  }
900 
901  os << std::endl;
902 
903  return os;
904 }
905 
906 //-----------------------------------------------------------------------------
907 // Function : Model::forEachInstance
908 // Purpose :
909 // Special Notes :
910 // Scope : public
911 // Creator : David Baur
912 // Creation Date : 2/4/2014
913 //-----------------------------------------------------------------------------
914 /// Apply a device instance "op" to all instances associated with this
915 /// model
916 ///
917 /// @param[in] op Operator to apply to all instances.
918 ///
919 ///
920 void Model::forEachInstance(DeviceInstanceOp &op) const /* override */
921 {
922  for (std::vector<Instance *>::const_iterator it = instanceContainer.begin(); it != instanceContainer.end(); ++it)
923  op(*it);
924 }
925 
926 
927 
928 //-----------------------------------------------------------------------------
929 // Vsrc Master functions:
930 //-----------------------------------------------------------------------------
931 
932 
933 //-----------------------------------------------------------------------------
934 // Function : Master::updateState
935 // Purpose :
936 // Special Notes :
937 // Scope : public
938 // Creator : Eric Keiter, SNL
939 // Creation Date : 11/26/08
940 //-----------------------------------------------------------------------------
941 bool Master::updateState (double * solVec, double * staVec, double * stoVec)
942 {
943  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
944  {
945  Instance & vi = *(*it);
946  // Get the value for the source.
947  SourceData *dataPtr = vi.dcSourceData_; // by default assume the DC value.
948  if ( (getSolverState().HBspecified || getSolverState().tranopFlag || getSolverState().transientFlag) && vi.tranSourceData_ != 0 )
949  {
950  dataPtr = vi.tranSourceData_;
951  }
952 
953  if (dataPtr != 0)
954  {
955  vi.source = dataPtr->returnSource();
956  }
957  else
958  {
959  vi.source = 0.0;
960  }
961 
962  // get the value for v_pos, v_neg, i_bra
963  vi.v_pos = solVec[vi.li_Pos];
964  vi.v_neg = solVec[vi.li_Neg];
965  vi.i_bra = solVec[vi.li_Bra];
966 
967  vi.srcCurrent = vi.i_bra;
968  vi.srcDrop = (vi.v_pos-vi.v_neg);
969  vi.srcBC = vi.source;
970  vi.srcVoltage = vi.srcDrop-vi.srcBC;
971 
972  if( getDeviceOptions().scale_src != 0.0 )
973  {
974  vi.srcCurrent *= vi.scale;
975 
976  // first newton step, generate new scaling
977  if( getSolverState().newtonIter != vi.nlstep )
978  {
980  double new_scale = fabs(vi.i_bra) * vi.scale;
981 
982  vi.scale = Xycemax( new_scale, getDeviceOptions().scale_src );
983  }
984 
985  vi.srcVoltage *= vi.scale;
986  vi.srcDrop *= vi.scale;
987  vi.srcBC *= vi.scale;
988  }
989  }
990 
991  return true;
992 }
993 
994 //-----------------------------------------------------------------------------
995 // Function : Master::loadDAEVectors
996 // Purpose :
997 // Special Notes :
998 // Scope : public
999 // Creator : Eric Keiter, SNL
1000 // Creation Date : 11/26/08
1001 //-----------------------------------------------------------------------------
1002 bool Master::loadDAEVectors (double * solVec, double * fVec, double *qVec, double * bVec, double * storeLeadF, double * storeLeadQ)
1003 {
1004  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
1005  {
1006  Instance & vi = *(*it);
1007  fVec[vi.li_Pos] += vi.srcCurrent;
1008  fVec[vi.li_Neg] += -vi.srcCurrent;
1009  fVec[vi.li_Bra] += vi.srcDrop;
1010 #ifndef SEPARATE_F_AND_B
1011  fVec[vi.li_Bra] -= vi.srcBC;
1012 #else
1013  bVec[vi.li_Bra] += vi.srcBC;
1014 #endif
1015 
1016  }
1017  return true;
1018 }
1019 
1020 //-----------------------------------------------------------------------------
1021 // Function : Master::loadDAEMatrices
1022 // Purpose :
1023 // Special Notes :
1024 // Scope : public
1025 // Creator : Eric Keiter, SNL
1026 // Creation Date : 11/26/08
1027 //-----------------------------------------------------------------------------
1028 bool Master::loadDAEMatrices (N_LAS_Matrix & dFdx, N_LAS_Matrix & dQdx)
1029 {
1030  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
1031  {
1032  Instance & vi = *(*it);
1033 
1034 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
1035  *(vi.fPosEquBraVarPtr) += 1.0;
1036  *(vi.fNegEquBraVarPtr) -= 1.0;
1037  *(vi.fBraEquPosNodePtr) += 1.0;
1038  *(vi.fBraEquNegNodePtr) -= 1.0;
1039 #else
1040  dFdx[vi.li_Pos][vi.APosEquBraVarOffset] += 1.0;
1041  dFdx[vi.li_Neg][vi.ANegEquBraVarOffset] -= 1.0;
1042  dFdx[vi.li_Bra][vi.ABraEquPosNodeOffset] += 1.0;
1043  dFdx[vi.li_Bra][vi.ABraEquNegNodeOffset] -= 1.0;
1044 #endif
1045  }
1046  return true;
1047 }
1048 
1049 Device *
1050 Traits::factory(const Configuration &configuration, const FactoryBlock &factory_block)
1051 {
1052  return new Master(configuration, factory_block, factory_block.solverState_, factory_block.deviceOptions_);
1053 }
1054 
1056 {
1058  .registerDevice("v", 1);
1059 }
1060 
1061 
1062 //-----------------------------------------------------------------------------
1063 // Function : dcVsrcSensitivity::operator
1064 // Purpose : produces df/dp and dq/dp, where p=DCV0.
1065 // Special Notes :
1066 // Scope : public
1067 // Creator : Eric Keiter, SNL
1068 // Creation Date : 7/18/2014
1069 //-----------------------------------------------------------------------------
1071  const ParameterBase &entity,
1072  const std::string & name,
1073  std::vector<double> & dfdp,
1074  std::vector<double> & dqdp,
1075  std::vector<double> & dbdp,
1076  std::vector<int> & Findices,
1077  std::vector<int> & Qindices,
1078  std::vector<int> & Bindices
1079  ) const
1080 {
1081  const ParameterBase * e1 = &entity;
1082  const Instance * in = dynamic_cast<const Instance *> (e1);
1083 
1084 #ifndef SEPARATE_F_AND_B
1085  dfdp.resize(1);
1086  dfdp[0] -= 1.0;
1087  Findices.resize(1);
1088  Findices[0] = in->li_Bra;
1089 #else
1090  dbdp.resize(1);
1091  dbdp[0] += 1.0;
1092  Bindices.resize(1);
1093  Bindices[0] = in->li_Bra;
1094 #endif
1095 
1096 }
1097 
1098 } // namespace Vsrc
1099 } // namespace Device
1100 } // namespace Xyce