Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
N_DEV_Synapse.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-2011 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_Synapse.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.41.2.2 $
40 //
41 // Revision Date : $Date: 2014/03/06 23:33:43 $
42 //
43 // Current Owner : $Author: tvrusso $
44 //----------------------------------------------------------------------------
45 
46 #include <Xyce_config.h>
47 
48 
49 // ---------- Standard Includes ----------
50 
51 // ---------- Xyce Includes ----------
52 //
53 #include <N_DEV_Const.h>
54 #include <N_DEV_DeviceOptions.h>
55 #include <N_DEV_DeviceMaster.h>
56 #include <N_DEV_ExternData.h>
57 #include <N_DEV_MatrixLoadData.h>
58 #include <N_DEV_SolverState.h>
59 #include <N_DEV_Synapse.h>
60 #include <N_DEV_Message.h>
61 #include <N_ERH_ErrorMgr.h>
62 
63 #include <N_LAS_Matrix.h>
64 #include <N_LAS_Vector.h>
65 
66 namespace Xyce {
67 namespace Device {
68 
69 namespace Synapse {
70 
71 
73 {
74  // Set up configuration constants:
75 }
76 
78 {
79  // Default values are taken from Destexhe 98, values for AMPA receptor
80  p.addPar ("GMAX", 1.0E-9, &Synapse::Model::gMax)
81  .setUnit(U_OHMM1)
82  .setDescription("Maximal Synaptic Conductance");
83 
84  p.addPar ("EREV", 0.0, &Synapse::Model::eRev)
85  .setUnit(U_VOLT)
86  .setDescription("Reversal Potential");
87 
88  p.addPar ("ALPHA", 1.1E-6, &Synapse::Model::alpha)
89  .setUnit(U_SECM1)
90  .setDescription("Forward rate constant for receptor opening");
91 
92  p.addPar ("BETA", 190.0, &Synapse::Model::beta)
93  .setUnit(U_SECM1)
94  .setDescription("Backward rate constant for receptor opening");
95 
96  p.addPar ("VP", 0.002, &Synapse::Model::vP)
97  .setUnit(U_VOLT)
98  .setDescription("Presynaptic voltage at which neurotransmitter concentration is half-maximal");
99 
100  // KP should NOT be 0
101  p.addPar ("KP", 0.005, &Synapse::Model::kP)
102  .setUnit(U_VOLT)
103  .setDescription("Steepness parameter for neurotransmitter concentration");
104 
105  p.addPar ("TMAX", 0.001, &Synapse::Model::tMax)
106  .setDescription("Maximal neurotransmitter concentration");
107 
108  // NOTE: not including concentration units - TMAX should be in moles/liter, and
109  // alpha has concentration in the denominator; they cancel out
110 }
111 
112 
113 
114 std::vector< std::vector<int> > Instance::jacStamp;
115 
116 // Class Instance
117 //-----------------------------------------------------------------------------
118 // Function : Instance::processParams
119 // Purpose :
120 // Special Notes :
121 // Scope : public
122 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
123 // Creation Date : 6/03/02
124 //-----------------------------------------------------------------------------
126 {
127  // now set the temperature related stuff.
128  //updateTemperature(temp);
129 
130  return true;
131 }
132 
133 //-----------------------------------------------------------------------------
134 // Function : Instance::Instance
135 // Purpose : instance block constructor
136 // Special Notes :
137 // Scope : public
138 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
139 // Creation Date : 3/16/00
140 //-----------------------------------------------------------------------------
142  const Configuration & configuration,
143  const InstanceBlock & IB,
144  Model & Riter,
145  const FactoryBlock & factory_block)
146 
147  : DeviceInstance(IB, configuration.getInstanceParameters(), factory_block),
148  model_(Riter),
149  li_Prev(-1),
150  li_Post(-1),
151  APostEquPostNodeOffset(-1),
152  APostEquRNodeOffset(-1),
153  AREquPostNodeOffset(-1),
154  AREquRNodeOffset(-1),
155  f_PostEquPostNodePtr(0),
156  f_PostEquRNodePtr(0),
157  f_REquPostNodePtr(0),
158  f_REquRNodePtr(0)
159 {
160  numIntVars = 1;
161  numExtVars = 2;
162  numStateVars = 0;
163 
164  if( jacStamp.empty() )
165  {
166  // Vpre, Vpost, r
167  jacStamp.resize(3);
168  jacStamp[0].resize(0);
169  jacStamp[1].resize(2);
170  jacStamp[2].resize(2);
171  jacStamp[1][0] = 1; // Vpost PostPostVar
172  jacStamp[1][1] = 2; // r PostRVar
173  jacStamp[2][0] = 0; // Vpre RVpre
174  jacStamp[2][1] = 2; // r RR
175  }
176 
177 
178  // Set params to constant default values:
179  setDefaultParams ();
180 
181  // Set params according to instance line and constant defaults from metadata:
182  setParams (IB.params);
183 
184  // Set any non-constant parameter defaults:
185 
186  // Calculate any parameters specified as expressions:
188 
189  // calculate dependent (ie computed) params and check for errors:
190  processParams ();
191 }
192 
193 //-----------------------------------------------------------------------------
194 // Function : Instance::~Instance
195 // Purpose : destructor
196 // Special Notes :
197 // Scope : public
198 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
199 // Creation Date : 3/16/00
200 //-----------------------------------------------------------------------------
202 {
203 }
204 
205 //-----------------------------------------------------------------------------
206 // Function : Instance::registerLIDs
207 // Purpose :
208 // Special Notes :
209 // Scope : public
210 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
211 // Creation Date : 6/12/02
212 //-----------------------------------------------------------------------------
213 void Instance::registerLIDs( const std::vector<int> & intLIDVecRef,
214  const std::vector<int> & extLIDVecRef )
215 {
216  AssertLIDs(intLIDVecRef.size() == numIntVars);
217  AssertLIDs(extLIDVecRef.size() == numExtVars);
218 
219 #ifdef Xyce_DEBUG_DEVICE
220  if (getDeviceOptions().debugLevel > 0)
221  {
222  Xyce::dout() << std::endl << section_divider << std::endl;
223  Xyce::dout() << " SynapseInstance::registerLIDs" << std::endl;
224  Xyce::dout() << " name = " << getName() << std::endl;
225  }
226 #endif
227 
228  // copy over the global ID lists.
229  intLIDVec = intLIDVecRef;
230  extLIDVec = extLIDVecRef;
231 
232  li_Prev = extLIDVec[0];
233  li_Post = extLIDVec[1];
234 
235 #ifdef Xyce_DEBUG_DEVICE
236  if (getDeviceOptions().debugLevel > 0 )
237  {
238  Xyce::dout() << " li_Prev = " << li_Prev << std::endl;
239  Xyce::dout() << " li_Post = " << li_Post << std::endl;
240  }
241 #endif
242 
243  li_rVar = intLIDVec[0];
244 
245 #ifdef Xyce_DEBUG_DEVICE
246  if (getDeviceOptions().debugLevel > 0 )
247  {
248  Xyce::dout() << section_divider << std::endl;
249  }
250 #endif
251 }
252 
253 //-----------------------------------------------------------------------------
254 // Function : Instance::getIntNameMap
255 // Purpose :
256 // Special Notes :
257 // Scope : public
258 // Creator : Christina Warrender, SNL, Cognitive Modeling
259 // Creation Date : 10/17/11
260 //-----------------------------------------------------------------------------
261 std::map<int,std::string> & Instance::getIntNameMap ()
262 {
263  // set up the internal name map, if it hasn't been already.
264  if (intNameMap.empty ())
265  {
266  std::string tmpstr;
267 
268  tmpstr = getName() + "_r";
269  spiceInternalName (tmpstr);
270  intNameMap[ li_rVar ] = tmpstr;
271  }
272  return intNameMap;
273 }
274 
275 
276 
277 //-----------------------------------------------------------------------------
278 // Function : Instance::registerStateLIDs
279 // Purpose : Note that the Synapse does not have any state vars.
280 // Special Notes :
281 // Scope : public
282 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
283 // Creation Date : 06/12/02
284 //-----------------------------------------------------------------------------
285 void Instance::registerStateLIDs(const std::vector<int> & staLIDVecRef )
286 {
287  AssertLIDs(staLIDVecRef.size() == numStateVars);
288 }
289 
290 //-----------------------------------------------------------------------------
291 // Function : Instance::jacobianStamp
292 // Purpose :
293 // Special Notes :
294 // Scope : public
295 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
296 // Creation Date : 08/20/01
297 //-----------------------------------------------------------------------------
298 const std::vector< std::vector<int> > & Instance::jacobianStamp() const
299 {
300  return jacStamp;
301 }
302 
303 //-----------------------------------------------------------------------------
304 // Function : Instance::registerJacLIDs
305 // Purpose :
306 // Special Notes :
307 // Scope : public
308 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
309 // Creation Date : 08/27/01
310 //-----------------------------------------------------------------------------
311 void Instance::registerJacLIDs( const std::vector< std::vector<int> > & jacLIDVec )
312 {
313  DeviceInstance::registerJacLIDs( jacLIDVec );
314 
315  APostEquPostNodeOffset = jacLIDVec[1][0];
316  APostEquRNodeOffset = jacLIDVec[1][1];
317  AREquPostNodeOffset = jacLIDVec[2][0];
318  AREquRNodeOffset = jacLIDVec[2][1];
319 }
320 
321 //-----------------------------------------------------------------------------
322 // Function : Instance::setupPointers
323 // Purpose :
324 // Special Notes :
325 // Scope : public
326 // Creator : Eric Keiter, SNL
327 // Creation Date : 11/30/08
328 //-----------------------------------------------------------------------------
330 {
331 #ifndef Xyce_NONPOINTER_MATRIX_LOAD
332  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
337 #endif
338 }
339 
340 //-----------------------------------------------------------------------------
341 // Function : Instance::updateIntermediateVars
342 // Purpose : update intermediate variables for one diode instance
343 // Special Notes :
344 // Scope : public
345 // Creator : Eric R. Keiter, Dept. 9233.
346 // Creation Date : 3/05/04
347 //-----------------------------------------------------------------------------
349 {
350  bool bsuccess = true;
351 
352  N_LAS_Vector * solVecPtr = extData.nextSolVectorPtr;
353 
354  double vPre = (*solVecPtr)[li_Prev];
355  double vPost = (*solVecPtr)[li_Post];
356  double rVar = (*solVecPtr)[li_rVar];
357 
358  // Load RHS vector element for the positive circuit node KCL equ.
359  {
360  Sacado::Fad::SFad<double,2> vPostVar( 2, 0, vPost );
361  Sacado::Fad::SFad<double,2> rVarS( 2, 1, rVar);
362 
363  // parameters
364  Sacado::Fad::SFad<double,2> gMaxVar( model_.gMax );
365  Sacado::Fad::SFad<double,2> eRevVar( model_.eRev );
366 
367  Sacado::Fad::SFad<double,2> resultFad;
368  resultFad = PostCurrentEqu( vPostVar, rVarS, gMaxVar, eRevVar );
369  ipost = resultFad.val();
370  didVpost = resultFad.dx(0);
371  didr = resultFad.dx(1);
372  }
373  {
374  Sacado::Fad::SFad<double,2> vPreVar( 2, 0, vPre );
375  Sacado::Fad::SFad<double,2> rVarS( 2, 1, rVar);
376 
377  // parameters
378  Sacado::Fad::SFad<double,2> alphaVar( model_.alpha );
379  Sacado::Fad::SFad<double,2> betaVar( model_.beta );
380  Sacado::Fad::SFad<double,2> tMaxVar( model_.tMax );
381  Sacado::Fad::SFad<double,2> vPVar( model_.vP );
382  Sacado::Fad::SFad<double,2> kPVar( model_.kP );
383 
384  Sacado::Fad::SFad<double,2> resultFad;
385  resultFad = rEquF( vPreVar, rVarS, alphaVar, betaVar, tMaxVar, vPVar, kPVar);
386  rFval = resultFad.val();
387  drFdVpre = resultFad.dx(0);
388  drFdr = resultFad.dx(1);
389  }
390  return bsuccess;
391 }
392 
393 //-----------------------------------------------------------------------------
394 // Function : Instance::updatePrimaryState
395 // Purpose :
396 // Special Notes :
397 // Scope : public
398 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
399 // Creation Date : 01/29/01
400 //-----------------------------------------------------------------------------
402 {
403  bool bsuccess = updateIntermediateVars();
404  return bsuccess;
405 }
406 
407 //-----------------------------------------------------------------------------
408 // Function : Instance::updateSecondaryState
409 // Purpose :
410 // Special Notes :
411 // Scope : public
412 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
413 // Creation Date : 01/29/01
414 //-----------------------------------------------------------------------------
416 {
417  return true;
418 }
419 
420 //-----------------------------------------------------------------------------
421 // Function : Instance::loadDAEQVector
422 //
423 // Purpose : Loads the Q-vector contributions for a single
424 // synapse
425 //
426 // Special Notes : The "Q" vector is part of a standard DAE formalism in
427 // which the system of equations is represented as:
428 //
429 // f(x) = dQ(x)/dt + F(x) - B(t) = 0
430 //
431 // Scope : public
432 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
433 // Creation Date : 8/31/2010
434 //-----------------------------------------------------------------------------
436 {
437  N_LAS_Vector * qVecPtr = extData.daeQVectorPtr;
438  N_LAS_Vector * solVecPtr = extData.nextSolVectorPtr;
439  double rVar = (*solVecPtr)[li_rVar];
440  (*qVecPtr)[li_rVar] -= rVar;
441 
442  return true;
443 }
444 //-----------------------------------------------------------------------------
445 // Function : Instance::loadDAEFVector
446 //
447 // Purpose : Loads the F-vector contributions for a single
448 // Synapse instance.
449 // Special Notes :
450 // Scope : public
451 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
452 // Creation Date : 01/24/03
453 //-----------------------------------------------------------------------------
455 {
456  N_LAS_Vector * fVecPtr = extData.daeFVectorPtr;
457  (*fVecPtr)[li_Prev] += 0.0;
458  (*fVecPtr)[li_Post] += ipost;
459  (*fVecPtr)[li_rVar] += rFval;
460  return true;
461 }
462 
463 //-----------------------------------------------------------------------------
464 // Function : Instance::loadDAEdQdx
465 //
466 // Purpose : Loads the dQdx-matrix contributions for a single
467 // synapse instance.
468 //
469 // Special Notes : The "Q" vector is part of a standard DAE formalism in
470 // which the system of equations is represented as:
471 //
472 // f(x) = dQ(x)/dt + F(x) - B(t) = 0
473 //
474 // Scope : public
475 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
476 // Creation Date : 03/05/04
477 //-----------------------------------------------------------------------------
479 {
480  N_LAS_Matrix & dQdx = *(extData.dQdxMatrixPtr);
481  dQdx[li_rVar][AREquRNodeOffset] += -1.0;
482 
483  return true;
484 }
485 
486 //-----------------------------------------------------------------------------
487 // Function : Instance::loadDAEdFdx ()
488 // Purpose : Loads the F-vector contributions for a single
489 // Synapse instance.
490 // Special Notes :
491 // Scope : public
492 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
493 // Creation Date : 03/05/04
494 //-----------------------------------------------------------------------------
496 {
497  N_LAS_Matrix & dFdx = *(extData.dFdxMatrixPtr);
499  dFdx[li_Post][APostEquRNodeOffset] += didr;
501  dFdx[li_rVar][AREquRNodeOffset] += drFdr;
502 
503  return true;
504 }
505 
506 //-----------------------------------------------------------------------------
507 // Function : Instance::updateTemperature
508 // Purpose :
509 // Special Notes :
510 // Scope : public
511 // Creator : Tom Russo, Component Information and Models
512 // Creation Date : 02/27/01
513 //-----------------------------------------------------------------------------
514 bool Instance::updateTemperature ( const double & temp_tmp)
515 {
516  bool bsuccess = true;
517  return bsuccess;
518 }
519 
520 //-----------------------------------------------------------------------------
521 // Function : Model::processParams
522 // Purpose :
523 // Special Notes :
524 // Scope : public
525 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
526 // Creation Date : 6/03/02
527 //-----------------------------------------------------------------------------
529 {
530  if (kP == 0.0)
531  kP = 0.001;
532 
533  return true;
534 }
535 
536 //----------------------------------------------------------------------------
537 // Function : Model::processInstanceParams
538 // Purpose :
539 // Special Notes :
540 // Scope : public
541 // Creator : Dave Shirely, PSSI
542 // Creation Date : 03/23/06
543 //----------------------------------------------------------------------------
545 {
546 
547  std::vector<Instance*>::iterator iter;
548  std::vector<Instance*>::iterator first = instanceContainer.begin();
549  std::vector<Instance*>::iterator last = instanceContainer.end();
550 
551  for (iter=first; iter!=last; ++iter)
552  {
553  (*iter)->processParams();
554  }
555 
556  return true;
557 }
558 
559 //-----------------------------------------------------------------------------
560 // Function : Model::Model
561 // Purpose : model block constructor
562 // Special Notes :
563 // Scope : public
564 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
565 // Creation Date : 5/16/00
566 //-----------------------------------------------------------------------------
568  const Configuration & configuration,
569  const ModelBlock & MB,
570  const FactoryBlock & factory_block)
571  : DeviceModel(MB, configuration.getModelParameters(), factory_block),
572  gMax(0.0),
573  eRev(0.0),
574  alpha(0.0),
575  beta(0.0),
576  vP(0.0),
577  kP(0.0),
578  tMax(0.0)
579 {
580 
581  // Set params to constant default values:
582  setDefaultParams ();
583 
584  // Set params according to .model line and constant defaults from metadata:
585  setModParams (MB.params);
586 
587  // Set any non-constant parameter defaults:
588 
589  // Calculate any parameters specified as expressions:
591 
592  // calculate dependent (ie computed) params and check for errors:
593  processParams();
594 }
595 
596 //-----------------------------------------------------------------------------
597 // Function : Model::~Model
598 // Purpose : destructor
599 // Special Notes :
600 // Scope : public
601 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
602 // Creation Date : 3/16/00
603 //-----------------------------------------------------------------------------
605 {
606  std::vector<Instance*>::iterator iter;
607  std::vector<Instance*>::iterator first = instanceContainer.begin();
608  std::vector<Instance*>::iterator last = instanceContainer.end();
609 
610  for (iter=first; iter!=last; ++iter)
611  {
612  delete (*iter);
613  }
614 }
615 
616 //-----------------------------------------------------------------------------
617 // Function : Model::printOutInstances
618 // Purpose : debugging tool.
619 // Special Notes :
620 // Scope : public
621 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
622 // Creation Date : 4/03/00
623 //-----------------------------------------------------------------------------
624 std::ostream &Model::printOutInstances(std::ostream &os) const
625 {
626  std::vector<Instance*>::const_iterator iter;
627  std::vector<Instance*>::const_iterator first = instanceContainer.begin();
628  std::vector<Instance*>::const_iterator last = instanceContainer.end();
629 
630  int i,isize;
631  isize = instanceContainer.size();
632  os << std::endl;
633  os << "Number of Synapse Instances: " << isize << std::endl;
634  os << " name model name Parameters" << std::endl;
635  for (i=0, iter=first; iter!=last; ++iter, ++i)
636  {
637  os << " " << i << ": " << (*iter)->getName() << "\t";
638  os << getName();
639  os << std::endl;
640  }
641 
642  os << std::endl;
643  return os;
644 }
645 
646 //-----------------------------------------------------------------------------
647 // Function : Model::forEachInstance
648 // Purpose :
649 // Special Notes :
650 // Scope : public
651 // Creator : David Baur
652 // Creation Date : 2/4/2014
653 //-----------------------------------------------------------------------------
654 /// Apply a device instance "op" to all instances associated with this
655 /// model
656 ///
657 /// @param[in] op Operator to apply to all instances.
658 ///
659 ///
660 void Model::forEachInstance(DeviceInstanceOp &op) const /* override */
661 {
662  for (std::vector<Instance *>::const_iterator it = instanceContainer.begin(); it != instanceContainer.end(); ++it)
663  op(*it);
664 }
665 
666 
667 // Synapse Master functions:
668 
669 //-----------------------------------------------------------------------------
670 // Function : Master::updateState
671 // Purpose :
672 // Special Notes :
673 // Scope : public
674 // Creator : Eric Keiter, SNL
675 // Creation Date : 11/26/08
676 //-----------------------------------------------------------------------------
677 bool Master::updateState (double * solVec, double * staVec, double * stoVec)
678 {
679  for (InstanceVector::const_iterator it = getInstanceBegin(); it != getInstanceEnd(); ++it)
680  {
681  (*it)->updateIntermediateVars();
682  }
683 
684  return true;
685 }
686 
687 //-----------------------------------------------------------------------------
688 // Function : Master::updateSecondaryState
689 // Purpose :
690 // Special Notes :
691 // Scope : public
692 // Creator : Eric Keiter, SNL
693 // Creation Date : 11/26/08
694 //-----------------------------------------------------------------------------
695 bool Master::updateSecondaryState ( double * staDerivVec, double * stoVec )
696 {
697  return true;
698 }
699 
700 Device *Traits::factory(const Configuration &configuration, const FactoryBlock &factory_block)
701 {
702 
703  return new Master(configuration, factory_block, factory_block.solverState_, factory_block.deviceOptions_);
704 }
705 
707 {
709  .registerDevice("synapse", 1)
710  .registerModelType("synapse", 1);
711 }
712 
713 } // namespace Synapse
714 } // namespace Device
715 } // namespace Xyce