LifeV
ZeroDimensionalCircuitData.cpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  * @file
29  * @brief File containing a class for 0D model circuit data handling.
30  *
31  * @date 26-09-2011
32  * @author Mahmoud Jafargholi <mahmoud.jafargholi@epfl.ch>
33  *
34  * @contributors Cristiano Malossi <cristiano.malossi@epfl.ch>
35  * @mantainer Cristiano Malossi <cristiano.malossi@epfl.ch>
36  */
37 
38 #include <Epetra_Vector.h>
39 #include <lifev/zero_dimensional/solver/ZeroDimensionalCircuitData.hpp>
40 
41 namespace LifeV
42 {
43 
44 // ===================================================
45 // Methods
46 // ===================================================
47 void ZeroDimensionalElement::showMe ( const Int& /*flag*/)
48 {
49  std::cout << "Id = " << id();
50  std::cout << "\t type = ";
51  std::cout << enum2string ( type() );
52 }
53 
55 {
56  std::string output;
57  switch ( type )
58  {
59  case resistor:
60  {
61  output = "resistor";
62  break;
63  }
64  case capacitor:
65  {
66  output = "capacitor";
67  break;
68  }
69  case inductor:
70  {
71  output = "inductor";
72  break;
73  }
74  case diode:
75  {
76  output = "diode";
77  break;
78  }
79  case voltageSource:
80  {
81  output = "voltageSource";
82  break;
83  }
84  case currentSource:
85  {
86  output = "currentSource";
87  break;
88  }
89  }
90  return ( output );
91 }
92 
93 // ===================================================
94 // Constructors
95 // ===================================================
98 {
99  M_nodeIndex.reserve ( 2 );
100 }
101 
103 {
105  std::cout << "\t Node1= " << nodeIndex ( 0 ) << "\t Node2= " << nodeIndex ( 1 );
106 }
107 
109 {
110  Int Index = M_nodeIndex.at ( 0 );
111  zeroDimensionalNodePtr_Type theNode = Nodes->nodeListAt ( Index );
112 
113  theNode->setElementListIndex ( M_id );
114  Int otherEndIndex = M_nodeIndex.at ( 1 );
115  theNode->setNodeListIndex ( otherEndIndex );
116 
117  Index = M_nodeIndex.at ( 1 );
118  theNode = Nodes->nodeListAt ( Index );
119 
120  theNode->setElementListIndex ( M_id );
121  otherEndIndex = M_nodeIndex.at ( 0 );
122  theNode->setNodeListIndex ( otherEndIndex );
123 }
124 
126 {
127  if ( M_nodeIndex.at ( 0 ) == nodeId )
128  {
129  return -1.0;
130  }
131  else
132  {
133  return 1.0;
134  }
135 }
136 // ===================================================
137 // Constructors
138 // ===================================================
140 {
141  M_type = resistor;
142 }
143 
145 {
147  std::cout << "\t parameter = " << parameter() << std::endl;
148 }
149 
151  matrix_Type& B,
152  vector_Type& C,
153  const zeroDimensionalNodeSPtr_Type& Nodes )
154 {
155  for ( Int i = 0; i < 2; i++ )
156  {
157  const Int& theNodeCounter = ( i ) % 2;
158  const Int& theOtherNodeCounter = ( i + 1 ) % 2;
159  const ZeroDimensionalNode& theNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex[theNodeCounter] ) );
160  const ZeroDimensionalNode& theOtherNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex[theOtherNodeCounter] ) );
161  if ( theNodeTest.type() == unknownNode )
162  {
163  const ZeroDimensionalNodeUnknown& theNode = * ( Nodes->unknownNodeMapAt ( M_nodeIndex[theNodeCounter] ) );
164  const Int& equationRow = theNode.equationRow();
165  B.addToCoefficient ( equationRow,
166  theNode.variableIndex(),
167  -M_parameter );
168  if ( theOtherNodeTest.type() == unknownNode )
169  {
170  const ZeroDimensionalNodeUnknown& theOtherNode = * ( Nodes->unknownNodeMapAt ( M_nodeIndex[theOtherNodeCounter] ) );
171  B.addToCoefficient ( equationRow,
172  theOtherNode.variableIndex(),
173  M_parameter );
174  }
175  else
176  {
177  const ZeroDimensionalNodeKnown& theOtherNode = * ( Nodes->knownNodeMapAt ( M_nodeIndex[theOtherNodeCounter] ) );
178  C[equationRow] += ( M_parameter * theOtherNode.voltage() );
179 
180  }
181  }
182  }
183 }
184 
186 {
187  M_current = ( Nodes.nodeListAt ( M_nodeIndex.at ( 0 ) )->voltage() - Nodes.nodeListAt ( M_nodeIndex.at ( 1 ) )->voltage() ) * M_parameter;
188  M_deltaCurrent = ( Nodes.nodeListAt ( M_nodeIndex.at ( 0 ) )->deltaVoltage() - Nodes.nodeListAt ( M_nodeIndex.at ( 1 ) )->deltaVoltage() )
189  * M_parameter;
190 }
191 // ===================================================
192 // Constructors
193 // ===================================================
196 {
197  M_type = diode;
198 }
199 
201 {
203  std::cout << "\t FB = " << forwardBias() << "\t alpha = " << alpha() << "\t beta = " << beta() << std::endl;
204 }
206 {
207 
208  Real e0 = ( M_beta * exp ( M_alpha * ( - M_forwardBias ) ) );
209  Real e1 = M_alpha * e0;
210  if ( voltage == 0 )
211  {
212  M_parameter = e1;
213  }
214  else
215  {
216  Real current = ( M_beta * exp ( M_alpha * ( voltage - M_forwardBias ) ) ) - e0;
217  M_parameter = abs ( current / voltage );
218  }
219 }
221 {
222  Real deltaVoltage = ( Nodes.nodeListAt ( M_nodeIndex.at ( 0 ) )->voltage() - Nodes.nodeListAt ( M_nodeIndex.at ( 1 ) )->voltage() );
223  calculateEffectiveResistance ( deltaVoltage );
224  M_current = deltaVoltage * M_parameter;
225  M_deltaCurrent = ( Nodes.nodeListAt ( M_nodeIndex.at ( 0 ) )->deltaVoltage() - Nodes.nodeListAt ( M_nodeIndex.at ( 1 ) )->deltaVoltage() )
226  * M_parameter;
227 }
228 
230  matrix_Type& B,
231  vector_Type& C,
232  const zeroDimensionalNodeSPtr_Type& Nodes )
233 {
234 
235  const Int& theNodeCounter = 0;
236  const Int& theOtherNodeCounter = 1;
237  const ZeroDimensionalNode& theNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex[theNodeCounter] ) );
238  const ZeroDimensionalNode& theOtherNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex[theOtherNodeCounter] ) );
239  Real deltaVoltage = theNodeTest.voltage() - theOtherNodeTest.voltage();
240  calculateEffectiveResistance ( deltaVoltage );
242 }
243 
244 // ===================================================
245 // Constructors
246 // ===================================================
248 {
249  M_type = capacitor;
250 }
251 
253 {
255  std::cout << "\t parameter = " << parameter() << std::endl;
256 }
257 
259 {
260  M_current = ( Nodes.nodeListAt ( M_nodeIndex.at ( 0 ) )->deltaVoltage() - Nodes.nodeListAt ( M_nodeIndex.at ( 1 ) )->deltaVoltage() ) * M_parameter;
261 }
262 
264  matrix_Type& /*B*/,
265  vector_Type& C,
266  const zeroDimensionalNodeSPtr_Type& Nodes )
267 {
268  for ( Int i = 0; i < 2; i++ )
269  {
270  const Int& theNodeCounter = ( i ) % 2;
271  const Int& theOtherNodeCounter = ( i + 1 ) % 2;
272  const ZeroDimensionalNode& theNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex[theNodeCounter] ) );
273  const ZeroDimensionalNode& theOtherNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex[theOtherNodeCounter] ) );
274  if ( theNodeTest.type() == unknownNode )
275  {
276  const ZeroDimensionalNodeUnknown& theNode = * ( Nodes->unknownNodeMapAt ( M_nodeIndex[theNodeCounter] ) );
277  const Int& equationRow = theNode.equationRow();
278  A.addToCoefficient ( equationRow,
279  theNode.variableIndex(),
280  -M_parameter );
281  if ( theOtherNodeTest.type() == unknownNode )
282  {
283  const ZeroDimensionalNodeUnknown& theOtherNode = * ( Nodes->unknownNodeMapAt ( M_nodeIndex[theOtherNodeCounter] ) );
284  A.addToCoefficient ( equationRow,
285  theOtherNode.variableIndex(),
286  M_parameter );
287  }
288  else
289  {
290  const ZeroDimensionalNodeKnown& theOtherNode = * ( Nodes->knownNodeMapAt ( M_nodeIndex[theOtherNodeCounter] ) );
291  C[equationRow] += ( M_parameter * theOtherNode.deltaVoltage() );
292  }
293  }
294  }
295 }
296 // ===================================================
297 // Constructors
298 // ===================================================
301 {
302  M_type = inductor;
303 }
304 
306 {
308  if ( flag == 0 )
309  {
310  std::cout << "\t parameter = " << parameter() << std::endl;
311  }
312  else
313  {
314  std::cout << "\t EquationRow= " << M_equationRow << "\t VariableIndex= " << M_variableIndex;
315  }
316 }
317 
319 {
320  M_equationRow = index;
321  M_variableIndex = index;
322 
323 }
325  matrix_Type& B,
326  vector_Type& C,
327  const zeroDimensionalNodeSPtr_Type& Nodes )
328 {
329  for ( Int i = 0; i < 2; i++ )
330  {
331  const Int& theNodeCounter = ( i ) % 2;
332  const ZeroDimensionalNode& theNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex[theNodeCounter] ) );
333  if ( theNodeTest.type() == unknownNode )
334  {
335  const ZeroDimensionalNodeUnknown& theNode = * ( Nodes->unknownNodeMapAt ( M_nodeIndex[theNodeCounter] ) );
336  const Int& equationRow = theNode.equationRow();
337  B.addToCoefficient ( equationRow,
338  M_variableIndex,
339  direction ( theNode.id() ) );
340  }
341  }
342  // Write the equations for an inductor
343  A.addToCoefficient ( M_equationRow,
344  M_variableIndex,
345  1.0 );
346  {
347  Int i = 0;
348  const Int& theNodeCounter = ( i ) % 2;
349  const Int& theOtherNodeCounter = ( i + 1 ) % 2;
350  const ZeroDimensionalNode& theNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex[theNodeCounter] ) );
351  const ZeroDimensionalNode& theOtherNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex[theOtherNodeCounter] ) );
352  if ( theNodeTest.type() == unknownNode )
353  {
354  const ZeroDimensionalNodeUnknown& theNode = * ( Nodes->unknownNodeMapAt ( M_nodeIndex[theNodeCounter] ) );
355  B.addToCoefficient ( M_equationRow,
356  theNode.variableIndex(),
357  -M_parameter );
358  }
359  else
360  {
361  const ZeroDimensionalNodeKnown& theNode = * ( Nodes->knownNodeMapAt ( M_nodeIndex[theNodeCounter] ) );
362  C[M_equationRow] += ( -M_parameter * theNode.voltage() );
363  }
364 
365  if ( theOtherNodeTest.type() == unknownNode )
366  {
367  const ZeroDimensionalNodeUnknown& theOtherNode = * ( Nodes->unknownNodeMapAt ( M_nodeIndex[theOtherNodeCounter] ) );
368  B.addToCoefficient ( M_equationRow,
369  theOtherNode.variableIndex(),
370  M_parameter );
371  }
372  else
373  {
374  const ZeroDimensionalNodeKnown& theOtherNode = * ( Nodes->knownNodeMapAt ( M_nodeIndex[theOtherNodeCounter] ) );
375  C[M_equationRow] += ( +M_parameter * theOtherNode.voltage() );
376  }
377  }
378 
379 }
380 
381 // ==================================================
382 // Constructors
383 // ===================================================
385  M_nodeIndex()
386 
387 {
388 }
389 
391 {
393  std::cout << "\t Node1= " << nodeIndex() << std::endl;
394 }
395 
396 // ===================================================
397 // Constructors
398 // ===================================================
401 {
403 
404 }
405 
407 {
408  zeroDimensionalNodeKnownPtr_Type theNode = Nodes->knownNodeMapAt ( M_nodeIndex );
409  theNode->setElementListIndex ( M_id );
410  Int otherEndIndex = -1;// In fact this element is not connecting this node to a new node, only filling the vector
411  theNode->setNodeListIndex ( otherEndIndex );
412 }
414  const ZeroDimensionalElementS& Elements )
415 {
416  ZeroDimensionalNodeKnown& theNode = *Nodes.knownNodeMapAt ( M_nodeIndex );
417  const vecInt_Type& indexList = theNode.elementListIndex();
418  Real current = 0.0;
419  Int length = indexList.size();
420  for ( Int i = 0; i < length; ++i )
421  {
422  const ZeroDimensionalElement& theElement = *Elements.elementListAt ( indexList.at ( i ) );
423  if ( theElement.id() != M_id )
424  {
425  Real tmp = theElement.current() * theElement.direction ( theNode.id() );
426  current += tmp;
427  }
428  }
429  M_current = current;
430 }
431 // ===================================================
432 // Constructors
433 // ===================================================
435 {
437 }
438 
440 {
441  Int Index = M_nodeIndex;
442  zeroDimensionalNodePtr_Type theNode = Nodes->nodeListAt ( Index );
443  theNode->setElementListIndex ( M_id );
444  Int otherEndIndex = -1;// In fact this element is not connecting this node to a new node, only filling the vector
445  theNode->setNodeListIndex ( otherEndIndex );
446 }
447 
449  matrix_Type& /*B*/,
450  vector_Type& C,
451  const zeroDimensionalNodeSPtr_Type& Nodes )
452 {
453  const ZeroDimensionalNode& theNodeTest = * ( Nodes->nodeListAt ( M_nodeIndex ) );
454  if ( theNodeTest.type() == unknownNode )
455  {
456  const ZeroDimensionalNodeUnknown& theNode = * ( Nodes->unknownNodeMapAt ( M_nodeIndex ) );
457  const Int& equationRow = theNode.equationRow();
458  C[equationRow] += ( -M_current );
459  }
460  else
461  {
462  std::cerr << "Error at ZeroDimensionalElementCurrentSource::buildABC, source connected to voltage";
463  std::exit ( -1 );
464  }
465 }
466 
467 // ===================================================
468 // Constructors
469 // ===================================================
472 {
473 }
474 
475 void ZeroDimensionalNode::showMe ( const Int& flag )
476 {
477  std::cout << "Id = " << id();
478  std::cout << "\t type = ";
479  std::cout << enum2string ( type() ) << "\t";
480  if ( flag == 0 )
481  {
482  for ( UInt i = 0; i < M_elementListIndex.size(); i++ )
483  {
484  std::cout << "{" << M_elementListIndex.at ( i ) << "," << M_nodeListIndex.at ( i ) << "} ";
485  }
486  std::cout << std::endl;
487  }
488 }
489 
490 const std::string ZeroDimensionalNode::enum2string ( const ZeroDimensionalNodeType& type ) const
491 {
492  std::string output;
493  switch ( type )
494  {
495  case knownNode:
496  {
497  output = "knownNode";
498  break;
499  }
500  case unknownNode:
501  {
502  output = "unknownNode";
503  break;
504  }
505 
506  }
507  return ( output );
508 }
509 
511 {
512  Real currentBalance = 0.0;
513  Int length = M_elementListIndex.size();
514  for ( Int i = 0; i < length; ++i )
515  {
516  const ZeroDimensionalElement& theElement = *Elements.elementListAt ( M_elementListIndex.at ( i ) );
517  currentBalance += theElement.current() * theElement.direction ( M_id );
518  }
519  M_currentBalance = currentBalance;
520 }
521 
522 // ===================================================
523 // Constructors
524 // ===================================================
527 {
529 }
531 {
532  M_equationRow = index;
533  M_variableIndex = index;
534 
535 }
536 
537 void ZeroDimensionalNodeUnknown::showMe ( const Int& flag )
538 {
540  if ( flag == 1 )
541  {
542  std::cout << "\t EquationRow= " << M_equationRow << "\t VariableIndex= " << M_variableIndex;
543  }
544 }
545 
546 // ===================================================
547 // Constructors
548 // ===================================================
550 {
551  M_type = knownNode;
552 }
554 {
555  M_element = theElement;
556  M_type = knownNode;
557 }
558 
559 // ===================================================
560 // Constructors
561 // ===================================================
569 {
570 }
571 
572 void ZeroDimensionalElementS::showMe ( const Int& flag )
573 {
574  std::cout << "=============== Show all ZeroDimensional Elements ===========" << std::endl;
575  for ( iterZeroDimensionalElement_Type theElement = M_elementList->begin(); theElement != M_elementList->end(); theElement++ )
576  {
577  ( *theElement )->showMe ( flag );
578  }
579 }
580 
581 // ===================================================
582 // Constructors
583 // ===================================================
588 {
589 }
590 
591 void ZeroDimensionalNodeS::showMe ( const Int& flag )
592 {
593  std::cout << "=============== Show all ZeroDimensional Nodes ===========" << std::endl;
594  for ( iterZeroDimensionalNode_Type theNode = M_nodeList->begin(); theNode != M_nodeList->end(); theNode++ )
595  {
596  ( *theNode )->showMe ( flag );
597  }
598 }
599 
600 // ===================================================
601 // Constructors
602 // ===================================================
605 
606 {
607 }
608 
609 void ZeroDimensionalCircuitData::showMe ( const Int& flag )
610 {
611  M_Elements->showMe ( flag );
612  M_Nodes->showMe ( flag );
613 }
614 
615 // ===================================================
616 // Methods
617 // ===================================================
618 void ZeroDimensionalCircuitData::buildCircuit ( const char* fileName,
619  bcPtr_Type bc )
620 {
621  using namespace std;
622  ifstream infile;
623  string stringline1;
624  string stringline2;
625  string stringline3;
626  string stringtmp;
627  stringstream stringStreamLine1 ( stringstream::in | stringstream::out );
628  stringstream stringStreamLine2 ( stringstream::in | stringstream::out );
629 
630  bool boolElementType[ZERO_DIMENTIONAL_DEFINED_ELEMENTS];
631  Int numberOfNodes = 0;
632  Int numberOfElements = 0;
633  Int numberOfTerminalNodes = 0;
634  Int ID = 0;
635  Int Node1 = 0;
636  Int Node2 = 0;
637  Real parameter1 = 0.0;
638  Real forwardBias = 0.0;
639  Real alpha = 0.0;
640  Real beta = 0.0;
641 
642 
643  std::vector< ZeroDimensionalNodeType > nodesType;
644  vecInt_Type nodesConnectingSource;
645  vecInt_Type terminalNodes;
646 
647  infile.open ( fileName,
648  ifstream::in );
649  if ( infile.is_open() )
650  {
651  // ----------Read the first line----------------------------
652  getline ( infile, stringline1 );
653  stringStreamLine1 << stringline1;
654  stringStreamLine1 >> stringtmp;
655  numberOfElements = std::atoi ( stringtmp.c_str() );
656  stringStreamLine1 >> stringtmp;
657  numberOfNodes = std::atoi ( stringtmp.c_str() );
658  stringStreamLine1 >> stringtmp;
659  numberOfTerminalNodes = std::atoi ( stringtmp.c_str() );
660  getline ( infile, stringline2 );
661  Int nodeId = -1;
662 
663  // ----------Read the second line---------------------------
664  stringStreamLine2 << stringline2;
665  for ( Int i = 0; i < numberOfTerminalNodes; i++ )
666  {
667  stringStreamLine2 >> stringtmp;
668  nodeId = std::atoi ( stringtmp.c_str() );
669  terminalNodes.push_back ( nodeId );
670  }
671  //---------------------------------------------------------
672  for ( Int i = 0; i < numberOfNodes; i++ )
673  {
674  nodesType.push_back ( unknownNode );
675  nodesConnectingSource.push_back ( -1 );
676  }
677  // ----------Read Elements-----------------------------------
678  for ( Int i = 0; i < numberOfElements; i++ )
679  {
680  stringstream stringStreamLine3 ( stringstream::in | stringstream::out );
681  getline ( infile, stringline3 );
682  stringStreamLine3 << stringline3;
683  stringStreamLine3 >> stringtmp;
684  ID = std::atoi ( stringtmp.c_str() );
685  stringStreamLine3 >> stringtmp;
686  boolElementType[0] = stringtmp.compare ( "resistor" );
687  boolElementType[1] = stringtmp.compare ( "capacitor" );
688  boolElementType[2] = stringtmp.compare ( "inductor" );
689  boolElementType[3] = stringtmp.compare ( "voltageSource" );
690  boolElementType[4] = stringtmp.compare ( "currentSource" );
691  boolElementType[5] = stringtmp.compare ( "diode" );
692 
693  stringStreamLine3 >> stringtmp;
694  Node1 = std::atoi ( stringtmp.c_str() );
695 
696  if ( !boolElementType[0] ) //resistor
697  {
698  stringStreamLine3 >> stringtmp;
699  Node2 = std::atoi ( stringtmp.c_str() );
700  stringStreamLine3 >> stringtmp;
701  parameter1 = std::atof ( stringtmp.c_str() );
702  createElementResistor ( ID, Node1, Node2, parameter1 );
703  }
704  if ( !boolElementType[1] ) //capacitor
705  {
706  stringStreamLine3 >> stringtmp;
707  Node2 = std::atoi ( stringtmp.c_str() );
708  stringStreamLine3 >> stringtmp;
709  parameter1 = std::atof ( stringtmp.c_str() );
710  createElementCapacitor ( ID, Node1, Node2, parameter1 );
711  }
712  if ( !boolElementType[2] ) //inductor
713  {
714  stringStreamLine3 >> stringtmp;
715  Node2 = std::atoi ( stringtmp.c_str() );
716  stringStreamLine3 >> stringtmp;
717  parameter1 = std::atof ( stringtmp.c_str() );
718  createElementInductor ( ID, Node1, Node2, parameter1 );
719  }
720  if ( !boolElementType[5] ) //diode
721  {
722  stringStreamLine3 >> stringtmp;
723  Node2 = std::atoi ( stringtmp.c_str() );
724  stringStreamLine3 >> stringtmp;
725  forwardBias = std::atof ( stringtmp.c_str() );
726  stringStreamLine3 >> stringtmp;
727  alpha = std::atof ( stringtmp.c_str() );
728  stringStreamLine3 >> stringtmp;
729  beta = std::atof ( stringtmp.c_str() );
730  createElementDiode ( ID, Node1, Node2, forwardBias, alpha, beta );
731  }
732  }
733  infile.close();
734  }
735  else
736  {
737  cerr << "Error opening circuit file";
738  exit ( -1 );
739  }
740 
741  for ( iterVecInt_Type theNodeId = terminalNodes.begin(); theNodeId != terminalNodes.end(); theNodeId++ )
742  {
743  // create Source Elements -----------------------------------------------
744  switch ( bc->bc ( *theNodeId ).bcType() )
745  {
746  case Voltage://create voltage source
747  nodesConnectingSource.at ( *theNodeId ) = createElementVoltageSource ( *theNodeId );
748  nodesType.at ( *theNodeId ) = knownNode;
749  break;
750  case Current:// current source
751  createElementCurrentSource ( *theNodeId );
752  break;
753  default:
754  break;
755  }
756  }
757  //create Nodes --------------------------------------------------------
758  for ( Int i = 0; i < numberOfNodes; i++ )
759  {
760  if ( nodesType.at ( i ) == knownNode )
761  {
762  //Create known Node and connect the voltage source to the node
763  createKnownNode ( i, M_Elements->voltageSourceMap ( nodesConnectingSource.at ( i ) ) );
764  }
765  else
766  {
768  }
769  }
770  //connect elements to the nodes
771  for ( Int i = 0; i < M_Elements->elementCounter(); i++ )
772  {
773  M_Elements->elementListAt ( i )->connectElement ( M_Nodes );
774  }
775  //set BC to source elements
776  fixBC ( bc );
777 }
778 
780  Int node1,
781  Int node2,
782  Real parameter )
783 {
784  zeroDimensionalElementPassiveResistorPtr_Type theElement ( new ZeroDimensionalElementPassiveResistor() );
785  theElement->setId ( ID );
786  if ( M_Elements->elementCounter() != ID )
787  {
788  std::cerr << "Error: Element Id error at " << ID;
789  exit ( -1 );
790  }
791  theElement->setNodeIndex ( node1 );
792  theElement->setNodeIndex ( node2 );
793  if ( parameter <= 0 )
794  {
795  std::cerr << "Error: Resistance value <=0, ID = " << ID;
796  exit ( -1 );
797  }
798  theElement->setParameter ( 1.0 / parameter );
799  M_Elements->setelementList ( theElement );
800  M_Elements->setResistorList ( theElement );
801 }
803  Int node1,
804  Int node2,
805  Real parameter )
806 {
807  zeroDimensionalElementPassiveCapacitorPtr_Type theElement ( new ZeroDimensionalElementPassiveCapacitor() );
808  theElement->setId ( ID );
809  if ( M_Elements->elementCounter() != ID )
810  {
811  std::cerr << "Error: Element Id error at " << ID;
812  exit ( -1 );
813  }
814  theElement->setNodeIndex ( node1 );
815  theElement->setNodeIndex ( node2 );
816  theElement->setParameter ( parameter );
817  M_Elements->setelementList ( theElement );
818  M_Elements->setCapacitorList ( theElement );
819 }
821  Int node1,
822  Int node2,
823  Real parameter )
824 {
825  zeroDimensionalElementPassiveInductorPtr_Type theElement ( new ZeroDimensionalElementPassiveInductor() );
826  theElement->setId ( ID );
827 
828  if ( M_Elements->elementCounter() != ID )
829  {
830  std::cerr << "Error: Element Id error at " << ID;
831  exit ( -1 );
832  }
833  theElement->setNodeIndex ( node1 );
834  theElement->setNodeIndex ( node2 );
835  theElement->setParameter ( 1.0 / parameter );
836  M_Elements->setelementList ( theElement );
837  M_Elements->setInductorList ( theElement );
838 }
840  Int node1,
841  Int node2,
842  Real forwardBias,
843  Real alpha,
844  Real beta )
845 {
846  zeroDimensionalElementPassiveDiodePtr_Type theElement ( new ZeroDimensionalElementPassiveDiode() );
847  theElement->setId ( ID );
848  if ( M_Elements->elementCounter() != ID )
849  {
850  std::cerr << "Error: Element Id error at " << ID;
851  exit ( -1 );
852  }
853  theElement->setNodeIndex ( node1 );
854  theElement->setNodeIndex ( node2 );
855  theElement->setParameter ( 0 );
856  theElement->setforwardBias ( forwardBias );
857  theElement->setalpha ( alpha );
858  theElement->setbeta ( beta );
859  M_Elements->setelementList ( theElement );
860  M_Elements->setDiodeList ( theElement );
861 }
862 
864 {
865  zeroDimensionalElementVoltageSourcePtr_Type theElement ( new ZeroDimensionalElementVoltageSource() );
866  theElement->setId ( M_Elements->elementCounter() );
867  theElement->setNodeIndex ( node1 );
868  M_Elements->setelementList ( theElement );
869  M_Elements->setVoltageSourceList ( theElement );
870  M_Elements->setVoltageSourceMap ( theElement->id(), theElement );
871  return theElement->id();
872 }
874 {
875  zeroDimensionalElementCurrentSourcePtr_Type theElement ( new ZeroDimensionalElementCurrentSource() );
876  theElement->setId ( M_Elements->elementCounter() );
877  theElement->setNodeIndex ( node1 );
878  M_Elements->setelementList ( theElement );
879  M_Elements->setCurrentSourceList ( theElement );
880 }
881 
883 {
884  zeroDimensionalNodeUnknownPtr_Type theNode ( new ZeroDimensionalNodeUnknown() );
885  theNode->setId ( id );
886  M_Nodes->setnodeList ( theNode );
887  M_Nodes->setunknownNodeList ( theNode );
888  M_Nodes->setunknownNodeMap ( theNode->id(), theNode );
889 }
890 
893 {
894  zeroDimensionalNodeKnownPtr_Type theNode ( new ZeroDimensionalNodeKnown ( theElement ) );
895  theNode->setId ( id );
896  M_Nodes->setnodeList ( theNode );
897  M_Nodes->setknownNodeList ( theNode );
898  M_Nodes->setknownNodeMap ( theNode->id(), theNode );
899 }
900 
902 {
903  zeroDimensionalNodeKnownPtr_Type theNode ( new ZeroDimensionalNodeKnown() );
904  theNode->setId ( id );
905  M_Nodes->setnodeList ( theNode );
906  M_Nodes->setknownNodeList ( theNode );
907  M_Nodes->setknownNodeMap ( theNode->id(), theNode );
908 }
909 void ZeroDimensionalCircuitData::fixBC ( bcPtr_Type bc )
910 {
911  M_bc = bc;
912 
913  // Set BC handler in source elements
914 
915  ptrVecZeroDimensionalElementCurrentSourcePtr_Type currentElementList = M_Elements->currentSourceList();
916  for ( iterZeroDimensionalElementCurrentSource_Type theElement = currentElementList->begin(); theElement != currentElementList->end(); theElement++ )
917  {
918  ( *theElement )->setBC ( bc );
919  }
920  ptrVecZeroDimensionalElementVoltageSourcePtr_Type voltageElementList = M_Elements->voltageSourceList();
921  for ( iterZeroDimensionalElementVoltageSourcePtr_Type theElement = voltageElementList->begin(); theElement != voltageElementList->end(); theElement++ )
922  {
923  ( *theElement )->setBC ( bc );
924  }
925 }
926 
928  const Epetra_Vector* x,
929  const Epetra_Vector* x_dot )
930 {
931  const ptrVecZeroDimensionalNodeUnknownPtr_Type& unknownNodeList = M_Nodes->unknownNodeList();
932 
933  //update unknown Nodes from solution.
934  for ( iterZeroDimensionalNodeUnknown_Type theNode = unknownNodeList ->begin(); theNode != unknownNodeList->end(); theNode++ )
935  {
936  const Int& variableIndex = ( *theNode )->variableIndex();
937  ( *theNode )->setVoltage ( (*x) [variableIndex] );
938  ( *theNode )->setDeltaVoltage ( ( *x_dot ) [variableIndex] );
939  }
940 
941  //unpdate inductor unknown current
942  const ptrVecZeroDimensionalElementPassiveInductorPtr_Type& inductorList = M_Elements->inductorList();
943  for ( iterZeroDimensionalElementPassiveInductor_Type theInductor = inductorList ->begin(); theInductor != inductorList->end(); theInductor++ )
944  {
945  const Int& variableIndex = ( *theInductor )->variableIndex();
946  ( *theInductor )->setCurrent ( ( *x ) [variableIndex] );
947  ( *theInductor )->setDeltaCurrent ( ( *x_dot ) [variableIndex] );
948  }
949 
950  //update BCs by time
951  const ptrVecZeroDimensionalNodeKnownPtr_Type& knownNodeList = M_Nodes->knownNodeList();
952  for ( iterZeroDimensionalNodeKnown_Type theNode = knownNodeList ->begin(); theNode != knownNodeList->end(); theNode++ )
953  {
954  ( *theNode )->setVoltageByTime ( t );
955  ( *theNode )->setDeltaVoltageByTime ( t );
956  }
957  const ptrVecZeroDimensionalElementCurrentSourcePtr_Type& currentElementList = M_Elements->currentSourceList();
958  for ( iterZeroDimensionalElementCurrentSource_Type theElement = currentElementList ->begin(); theElement != currentElementList->end(); theElement++ )
959  {
960  ( *theElement )->setCurrentByTime ( t );
961  }
962 }
964  const Epetra_Vector& y,
965  const Epetra_Vector& yp )
966 {
967  //First invoke the updateCircuitDataFromY method (light update)
968  updateCircuitDataFromY ( t, &y, &yp );
969 
970  //deep update (mainly currents), iterate over all elements
971  const ptrVecZeroDimensionalElementPtr_Type& elementList = M_Elements->elementList();
972  for ( iterZeroDimensionalElement_Type theElement = elementList ->begin(); theElement != elementList->end(); theElement++ )
973  {
974  ( *theElement )->extractSolution ( *M_Nodes );
975  }
976 
977  // Now we can compute voltage source current
978  const ptrVecZeroDimensionalElementVoltageSourcePtr_Type& voltageList = M_Elements->voltageSourceList();
979  for ( iterZeroDimensionalElementVoltageSourcePtr_Type theElement = voltageList ->begin(); theElement != voltageList->end(); theElement++ )
980  {
981  ( *theElement )->calculateCurrent ( *M_Nodes,
982  *M_Elements );
983  }
984 
985  // check the conservation of current at each node.
986  const ptrVecZeroDimensionalNodePtr_Type& nodeList = M_Nodes->nodeList();
987  for ( iterZeroDimensionalNode_Type theNode = nodeList ->begin(); theNode != nodeList->end(); theNode++ )
988  {
989  ( *theNode )->calculateCurrentBalance ( *M_Elements );
990  }
991 
992 }
993 void ZeroDimensionalCircuitData::updateABC ( matrix_Type& A,
994  matrix_Type& B,
995  vector_Type& C )
996 {
997  A.openCrsMatrix();
998  B.openCrsMatrix();
999  A.matrixPtr()->PutScalar ( 0.0 );
1000  B.matrixPtr()->PutScalar ( 0.0 );
1001  C.epetraVector().PutScalar ( 0.0 );
1002 
1003  const ptrVecZeroDimensionalElementPtr_Type& elementList = M_Elements->elementList();
1004 
1005  //iterate over all elements and compute each element's contribution on A,B,C
1006  for ( iterZeroDimensionalElement_Type theElement = elementList ->begin(); theElement != elementList->end(); theElement++ )
1007  {
1008 
1009  ( *theElement )->buildABC ( A, B, C, M_Nodes );
1010  }
1011  A.globalAssemble();
1012  B.globalAssemble();
1013 }
1014 
1015 
1016 OutPutFormat::OutPutFormat ( std::string width,
1017  std::string precision,
1018  std::string whiteSpace,
1019  Int bufferSize )
1020 {
1021  M_width = width;
1022  M_precision = precision;
1023  M_whiteSpace = whiteSpace;
1024  M_buffer = new char[bufferSize];
1025  M_formatDouble = "% " + M_width + "." + M_precision + "f";
1026  M_formatInteger = "% " + M_width + "d";
1027  M_formatString = "% " + M_width + "s";
1028 }
1029 void OutPutFormat::writeDataFormat ( const Real& number,
1030  std::ofstream& stream,
1031  const EndLine& flag )
1032 {
1033  sprintf ( M_buffer,
1034  M_formatDouble.data(),
1035  number );
1036  stream << M_buffer;
1037  switch ( flag )
1038  {
1039  case newLine:
1040  stream << std::endl;
1041  break;
1042  case space:
1043  stream << M_whiteSpace;
1044  break;
1045  case nothing:
1046  break;
1047  default:
1048  std::cerr << "no flag at OutPutFormat::writeDataFormat";
1049  break;
1050  }
1051 }
1052 void OutPutFormat::writeDataFormat ( const Int& number,
1053  std::ofstream& stream,
1054  const EndLine& flag )
1055 {
1056  //format string to align "Node ..." in the output header;
1057  UInt integerWidth (atoi (M_width.c_str() ) ); //integer storing the length of the output values
1058 
1059  std::ostringstream numberToWrite;
1060  numberToWrite << number;
1061 
1062  UInt integerWidthToUse (integerWidth);
1063  integerWidthToUse -= numberToWrite.str().size(); //the new width is obtained subtracting the length of the integer to write in the header (index of the node)
1064 
1065  std::ostringstream nodeStringWidth;
1066  nodeStringWidth << integerWidthToUse;
1067 
1068  std::string formatNodeString ("% " + nodeStringWidth.str() + "s"); //this guarantees the alignment of the header's fields to the output values in the following lines (regardless of the node index's length)
1069 
1070  sprintf ( M_buffer,
1071  formatNodeString.data(),
1072  "Node " );
1073 
1074  stream << M_buffer << number;
1075  switch ( flag )
1076  {
1077  case newLine:
1078  stream << std::endl;
1079  break;
1080  case space:
1081  stream << M_whiteSpace;
1082  break;
1083  case nothing:
1084  break;
1085  default:
1086  std::cerr << "no flag at OutPutFormat::writeDataFormat";
1087  break;
1088  }
1089 }
1090 
1091 void OutPutFormat::writeDataFormat ( const std::string& text,
1092  std::ofstream& stream,
1093  const EndLine& flag )
1094 {
1095  sprintf ( M_buffer,
1096  M_formatString.data(),
1097  text.c_str() );
1098  stream << M_buffer;
1099  switch ( flag )
1100  {
1101  case newLine:
1102  stream << std::endl;
1103  break;
1104  case space:
1105  stream << M_whiteSpace;
1106  break;
1107  case nothing:
1108  break;
1109  default:
1110  std::cerr << "no flag at OutPutFormat::writeDataFormat";
1111  break;
1112  }
1113 }
1114 
1115 void OutPutFormat::writeNewLine ( std::ofstream& stream )
1116 {
1117  stream << std::endl;
1118 }
1120 {
1121  delete[] M_buffer;
1122 }
1123 
1124 } // LifeV namespace
void connectElement(zeroDimensionalNodeSPtr_Type &Nodes)
Connect elements to the nodes.
ZeroDimensionalElementPassiveInductor - Inductor.
ZeroDimensionalNodeKnown(const zeroDimensionalElementVoltageSourcePtr_Type &theElement)
Contructor.
void extractSolution(const ZeroDimensionalNodeS &nodes)
Compute outputs (currents and voltages) from the solution vector after each succesful iteration...
void buildABC(matrix_Type &A, matrix_Type &B, vector_Type &C, const zeroDimensionalNodeSPtr_Type &Nodes)
Contribution of the element of matrix {A} and {B} and vector {C}.
void createElementCapacitor(Int ID, Int node1, Int node2, Real parameter)
virtual const Real & voltage() const
virtual void showMe(const Int &flag=0)
ZerodimentionalElementPassiveCapacitor - Capacitor.
ZeroDimensionalElementPassive - A class for passive elements.
std::shared_ptr< ZeroDimensionalNodeKnown > zeroDimensionalNodeKnownPtr_Type
std::vector< Int > vecInt_Type
std::shared_ptr< vecZeroDimensionalElementCurrentSourcePtr_Type > ptrVecZeroDimensionalElementCurrentSourcePtr_Type
void buildABC(matrix_Type &A, matrix_Type &B, vector_Type &C, const zeroDimensionalNodeSPtr_Type &Nodes)
Contribution of the element of matrix {A} and {B} and vector {C}.
std::shared_ptr< ZeroDimensionalNodeS > zeroDimensionalNodeSPtr_Type
ZeroDimensionalCircuitData - Container of circuit data.
void connectElement(zeroDimensionalNodeSPtr_Type &nodes)
Impleaments the abstarct class for passive elements.
void createKnownNode(const Int &id, const zeroDimensionalElementVoltageSourcePtr_Type &theElement)
void calculateCurrentBalance(const ZeroDimensionalElementS &Elements)
Calculate current balance at node.
ZerodimentionalElementPassiveDiode - Diode.
std::shared_ptr< vecZeroDimensionalNodeKnownPtr_Type > ptrVecZeroDimensionalNodeKnownPtr_Type
std::shared_ptr< ZeroDimensionalElementPassiveDiode > zeroDimensionalElementPassiveDiodePtr_Type
void showMe(const Int &flag=0)
Show some information.
void showMe(const Int &flag=0)
Show some information.
virtual void showMe(const Int &flag=0)
OutPutFormat - Write to output.
ZeroDimensionalElement - The base element class.
const std::string enum2string(const ZeroDimensionalElementType &type)
void extractSolution(const ZeroDimensionalNodeS &nodes)
Compute outputs (currents and voltages) from the solution vector after each succesful iteration...
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
OutPutFormat(std::string width, std::string precision, std::string whiteSpace, Int bufferSize)
Constructor.
void updateABC(matrix_Type &A, matrix_Type &B, vector_Type &C)
create matrix A,B and C.
void updateInverseJacobian(const UInt &iQuadPt)
const ZeroDimensionalElementType & type() const
void writeNewLine(std::ofstream &stream)
const ZeroDimensionalNodeType & type() const
void createElementInductor(Int ID, Int node1, Int node2, Real parameter)
std::shared_ptr< vecZeroDimensionalNodePtr_Type > ptrVecZeroDimensionalNodePtr_Type
void showMe(const Int &flag=0)
Show some information.
void createElementDiode(Int ID, Int node1, Int node2, Real forwardBias, Real alpha, Real beta)
void buildCircuit(const char *fileName, bcPtr_Type bc)
create the circuit.
void showMe(const Int &flag=0)
Show some information.
std::shared_ptr< ZeroDimensionalElementCurrentSource > zeroDimensionalElementCurrentSourcePtr_Type
ZeroDimensionalNodeKnown - This class defines the known node class. A Voltage Source element is conne...
void writeDataFormat(const Real &number, std::ofstream &stream, const EndLine &flag)
ZeroDimensionalElementVoltageSource - Voltage Source.
void calculateCurrent(const ZeroDimensionalNodeS &Nodes, const ZeroDimensionalElementS &Elements)
calculate current passing outward in voltage source.
const vecInt_Type & elementListIndex() const
ZeroDimensionalElementS - Container of elements.
const std::string enum2string(const ZeroDimensionalNodeType &type) const
Real direction(const Int &nodeId) const
This method specifies the convention of current direction in an element.
ZeroDimensionalElementPassiveResistor - Resistor.
virtual void assignVariableIndex(const Int &index)
Set the variable index and equation row index for Inductor.
double Real
Generic real data.
Definition: LifeV.hpp:175
#define ZERO_DIMENTIONAL_DEFINED_ELEMENTS
void showMe(const Int &flag=0)
Show some information.
ZeroDimensionalNodeS - Container of nodes.
void showMe(const Int &flag=0)
Display some information.
void createElementResistor(Int ID, Int node1, Int node2, Real parameter)
std::shared_ptr< vecZeroDimensionalElementPtr_Type > ptrVecZeroDimensionalElementPtr_Type
ZeroDimensionalNode - The base node class.
void connectElement(zeroDimensionalNodeSPtr_Type &Nodes)
Connect elements to the nodes.
void buildABC(matrix_Type &A, matrix_Type &B, vector_Type &C, const zeroDimensionalNodeSPtr_Type &Nodes)
Contribution of the element of matrix {A} and {B} and vector {C}.
std::shared_ptr< vecZeroDimensionalElementVoltageSourcePtr_Type > ptrVecZeroDimensionalElementVoltageSourcePtr_Type
void extractSolution(const ZeroDimensionalNodeS &nodes)
Compute outputs (currents and voltages) from the solution vector after each succesful iteration...
void assignVariableIndex(const Int &index)
assign the index of the unknown voltage.
std::shared_ptr< vecZeroDimensionalNodeUnknownPtr_Type > ptrVecZeroDimensionalNodeUnknownPtr_Type
virtual ~OutPutFormat()
Destructor.
void updateCircuitDataFromY(const Real &t, const Epetra_Vector *y, const Epetra_Vector *yp)
(shallow) update the circuit data from the solution.
virtual Real direction(const Int &nodeId) const =0
This method specifies the convention of current direction in an element.
std::shared_ptr< ZeroDimensionalElementVoltageSource > zeroDimensionalElementVoltageSourcePtr_Type
std::shared_ptr< ZeroDimensionalNodeUnknown > zeroDimensionalNodeUnknownPtr_Type
std::shared_ptr< ZeroDimensionalNode > zeroDimensionalNodePtr_Type
void buildABC(matrix_Type &A, matrix_Type &B, vector_Type &C, const zeroDimensionalNodeSPtr_Type &Nodes)
Contribution of the element of matrix {A} and {B} and vector {C}.
void extractSolutionFromY(const Real &t, const Epetra_Vector &y, const Epetra_Vector &yp)
(deep) update the circuit data from solution.
virtual void showMe(const Int &flag=0)
Display some information.
ZeroDimensionalElementCurrentSource - Current Source.
ZeroDimensionalElementSource - Base class for source elements.
zeroDimensionalElementVoltageSourcePtr_Type M_element
std::shared_ptr< ZeroDimensionalElementPassiveCapacitor > zeroDimensionalElementPassiveCapacitorPtr_Type
std::shared_ptr< ZeroDimensionalElementPassiveInductor > zeroDimensionalElementPassiveInductorPtr_Type
ZeroDimensionalNodeUnknown - This class defines the unknown node class.
std::shared_ptr< ZeroDimensionalElementPassiveResistor > zeroDimensionalElementPassiveResistorPtr_Type
void writeDataFormat(const std::string &text, std::ofstream &stream, const EndLine &flag)
void calculateEffectiveResistance(const Real &voltage)
calculate the effective resistance.
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191
data_type & operator[](const UInt row)
Access operators.
std::shared_ptr< vecZeroDimensionalElementPassiveInductorPtr_Type > ptrVecZeroDimensionalElementPassiveInductorPtr_Type
void buildABC(matrix_Type &A, matrix_Type &B, vector_Type &C, const zeroDimensionalNodeSPtr_Type &Nodes)
Contribution of the element of matrix {A} and {B} and vector {C}.