/* Created by Language version: 7.7.0 */
/* NOT VECTORIZED */
#define NRN_VECTORIZED 0
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "scoplib_ansi.h"
#undef PI
#define nil 0
#include "md1redef.h"
#include "section.h"
#include "nrniv_mf.h"
#include "md2redef.h"
 
#if METHOD3
extern int _method3;
#endif

#if !NRNGPU
#undef exp
#define exp hoc_Exp
extern double hoc_Exp(double);
#endif
 
#define nrn_init _nrn_init__xtrau
#define _nrn_initial _nrn_initial__xtrau
#define nrn_cur _nrn_cur__xtrau
#define _nrn_current _nrn_current__xtrau
#define nrn_jacob _nrn_jacob__xtrau
#define nrn_state _nrn_state__xtrau
#define _net_receive _net_receive__xtrau 
 
#define _threadargscomma_ /**/
#define _threadargsprotocomma_ /**/
#define _threadargs_ /**/
#define _threadargsproto_ /**/
 	/*SUPPRESS 761*/
	/*SUPPRESS 762*/
	/*SUPPRESS 763*/
	/*SUPPRESS 765*/
	 extern double *getarg();
 static double *_p; static Datum *_ppvar;
 
#define t nrn_threads->_t
#define dt nrn_threads->_dt
#define rx _p[0]
#define x _p[1]
#define y _p[2]
#define z _p[3]
#define d _p[4]
#define er _p[5]
#define im	*_ppvar[0]._pval
#define _p_im	_ppvar[0]._pval
#define ex	*_ppvar[1]._pval
#define _p_ex	_ppvar[1]._pval
#define area	*_ppvar[2]._pval
 
#if MAC
#if !defined(v)
#define v _mlhv
#endif
#if !defined(h)
#define h _mlhh
#endif
#endif
 
#if defined(__cplusplus)
extern "C" {
#endif
 static int hoc_nrnpointerindex =  0;
 /* external NEURON variables */
 /* declaration of user functions */
 static int _mechtype;
extern void _nrn_cacheloop_reg(int, int);
extern void hoc_register_prop_size(int, int, int);
extern void hoc_register_limits(int, HocParmLimits*);
extern void hoc_register_units(int, HocParmUnits*);
extern void nrn_promote(Prop*, int, int);
extern Memb_func* memb_func;
 
#define NMODL_TEXT 1
#if NMODL_TEXT
static const char* nmodl_file_text;
static const char* nmodl_filename;
extern void hoc_reg_nmodl_text(int, const char*);
extern void hoc_reg_nmodl_filename(int, const char*);
#endif

 extern void _nrn_setdata_reg(int, void(*)(Prop*));
 static void _setdata(Prop* _prop) {
 _p = _prop->param; _ppvar = _prop->dparam;
 }
 static void _hoc_setdata() {
 Prop *_prop, *hoc_getdata_range(int);
 _prop = hoc_getdata_range(_mechtype);
   _setdata(_prop);
 hoc_retpushx(1.);
}
 /* connect user functions to hoc names */
 static VoidFunc hoc_intfunc[] = {
 "setdata_xtrau", _hoc_setdata,
 0, 0
};
 /* declare global and static user variables */
#define E E_xtrau
 double E = 0;
 /* some parameters have upper and lower limits */
 static HocParmLimits _hoc_parm_limits[] = {
 0,0,0
};
 static HocParmUnits _hoc_parm_units[] = {
 "E_xtrau", "volt/m",
 "rx_xtrau", "megohm",
 "x_xtrau", "1",
 "y_xtrau", "1",
 "z_xtrau", "1",
 "d_xtrau", "micron",
 "er_xtrau", "microvolts",
 "im_xtrau", "milliamp/cm2",
 "ex_xtrau", "millivolts",
 0,0
};
 static double v = 0;
 /* connect global user variables to hoc */
 static DoubScal hoc_scdoub[] = {
 "E_xtrau", &E_xtrau,
 0,0
};
 static DoubVec hoc_vdoub[] = {
 0,0,0
};
 static double _sav_indep;
 static void _ba1() , _ba2() ;
 static void nrn_alloc(Prop*);
static void  nrn_init(_NrnThread*, _Memb_list*, int);
static void nrn_state(_NrnThread*, _Memb_list*, int);
 /* connect range variables in _p that hoc is supposed to know about */
 static const char *_mechanism[] = {
 "7.7.0",
"xtrau",
 "rx_xtrau",
 "x_xtrau",
 "y_xtrau",
 "z_xtrau",
 0,
 "d_xtrau",
 "er_xtrau",
 0,
 0,
 "im_xtrau",
 "ex_xtrau",
 0};
 extern Node* nrn_alloc_node_;
 
extern Prop* need_memb(Symbol*);

static void nrn_alloc(Prop* _prop) {
	Prop *prop_ion;
	double *_p; Datum *_ppvar;
 	_p = nrn_prop_data_alloc(_mechtype, 6, _prop);
 	/*initialize range parameters*/
 	rx = 1;
 	x = 0;
 	y = 0;
 	z = 0;
 	_prop->param = _p;
 	_prop->param_size = 6;
 	_ppvar = nrn_prop_datum_alloc(_mechtype, 3, _prop);
 	_prop->dparam = _ppvar;
 	/*connect ionic variables to this model*/
 	_ppvar[2]._pval = &nrn_alloc_node_->_area; /* diam */
 
}
 static void _initlists();
 extern Symbol* hoc_lookup(const char*);
extern void _nrn_thread_reg(int, int, void(*)(Datum*));
extern void _nrn_thread_table_reg(int, void(*)(double*, Datum*, Datum*, _NrnThread*, int));
extern void hoc_register_tolerance(int, HocStateTolerance*, Symbol***);
extern void _cvode_abstol( Symbol**, double*, int);

 void _xtrau_reg() {
	int _vectorized = 0;
  _initlists();
 	register_mech(_mechanism, nrn_alloc,(void*)0, (void*)0, (void*)0, nrn_init, hoc_nrnpointerindex, 0);
 _mechtype = nrn_get_mechtype(_mechanism[1]);
     _nrn_setdata_reg(_mechtype, _setdata);
 #if NMODL_TEXT
  hoc_reg_nmodl_text(_mechtype, nmodl_file_text);
  hoc_reg_nmodl_filename(_mechtype, nmodl_filename);
#endif
  hoc_register_prop_size(_mechtype, 6, 3);
  hoc_register_dparam_semantics(_mechtype, 0, "pointer");
  hoc_register_dparam_semantics(_mechtype, 1, "pointer");
  hoc_register_dparam_semantics(_mechtype, 2, "area");
 	hoc_reg_ba(_mechtype, _ba1, 11);
 	hoc_reg_ba(_mechtype, _ba2, 22);
 	hoc_register_var(hoc_scdoub, hoc_vdoub, hoc_intfunc);
 	ivoc_help("help ?1 xtrau D:/NEURON models/DCN LuthmanEtAl2011 Stimulation Ef/mod_files/xtrau.mod\n");
 hoc_register_limits(_mechtype, _hoc_parm_limits);
 hoc_register_units(_mechtype, _hoc_parm_units);
 }
static int _reset;
static char *modelname = "";

static int error;
static int _ninits = 0;
static int _match_recurse=1;
static void _modl_cleanup(){ _match_recurse=1;}
 /* BEFORE BREAKPOINT */
 static void _ba1(Node*_nd, double* _pp, Datum* _ppd, Datum* _thread, _NrnThread* _nt)  {
    _p = _pp; _ppvar = _ppd;
  v = NODEV(_nd);
 ex = - E * d * ( 1e-3 ) ;
   }
 /* AFTER SOLVE */
 static void _ba2(Node*_nd, double* _pp, Datum* _ppd, Datum* _thread, _NrnThread* _nt)  {
    _p = _pp; _ppvar = _ppd;
  v = NODEV(_nd);
 er = ( 10.0 ) * rx * im * area ;
   }

static void initmodel() {
  int _i; double _save;_ninits++;
{
 {
   ex = - E * d * ( 1e-3 ) ;
   er = ( 10.0 ) * rx * im * area ;
   }

}
}

static void nrn_init(_NrnThread* _nt, _Memb_list* _ml, int _type){
Node *_nd; double _v; int* _ni; int _iml, _cntml;
#if CACHEVEC
    _ni = _ml->_nodeindices;
#endif
_cntml = _ml->_nodecount;
for (_iml = 0; _iml < _cntml; ++_iml) {
 _p = _ml->_data[_iml]; _ppvar = _ml->_pdata[_iml];
#if CACHEVEC
  if (use_cachevec) {
    _v = VEC_V(_ni[_iml]);
  }else
#endif
  {
    _nd = _ml->_nodelist[_iml];
    _v = NODEV(_nd);
  }
 v = _v;
 initmodel();
}}

static double _nrn_current(double _v){double _current=0.;v=_v;{
} return _current;
}

static void nrn_state(_NrnThread* _nt, _Memb_list* _ml, int _type){
Node *_nd; double _v = 0.0; int* _ni; int _iml, _cntml;
#if CACHEVEC
    _ni = _ml->_nodeindices;
#endif
_cntml = _ml->_nodecount;
for (_iml = 0; _iml < _cntml; ++_iml) {
 _p = _ml->_data[_iml]; _ppvar = _ml->_pdata[_iml];
 _nd = _ml->_nodelist[_iml];
#if CACHEVEC
  if (use_cachevec) {
    _v = VEC_V(_ni[_iml]);
  }else
#endif
  {
    _nd = _ml->_nodelist[_iml];
    _v = NODEV(_nd);
  }
 v=_v;
{
}}

}

static void terminal(){}

static void _initlists() {
 int _i; static int _first = 1;
  if (!_first) return;
_first = 0;
}

#if NMODL_TEXT
static const char* nmodl_filename = "xtrau.mod";
static const char* nmodl_file_text = 
  ": xtrau.mod, based on xtra.mod,v 1.3 2009/02/24 00:52:07\n"
  ": the \"u\" stands for \"uniform\" as in \"uniform field\"\n"
  ": because this modification was made to accommodate a\n"
  ": uniform extracellular field,\n"
  ": as would occur between two parallel plates\n"
  "\n"
  "COMMENT\n"
  "This mechanism is intended to be used in conjunction \n"
  "with the extracellular mechanism.  Pointers specified \n"
  "at the hoc level must be used to connect the \n"
  "extracellular mechanism's e_extracellular and i_membrane \n"
  "to this mechanism's ex and im, respectively.\n"
  "\n"
  "xtrau does four useful things:\n"
  "\n"
  "1. Serves as a target for Vector.play() to facilitate \n"
  "extracellular stimulation.  Assumes that one has initialized \n"
  "a Vector to hold the time sequence of the extracellular field\n"
  "strength E.\n"
  "\n"
  "This Vector is to be played into the GLOBAL variable E \n"
  "(GLOBAL so only one Vector.play() needs to be executed), \n"
  "which is multiplied by the RANGE variable d (\"distance \n"
  "between the local node and the zero potential plane\") \n"
  "and then by -1.\n"
  "\n"
  "That product, called ex in this mechanism, is the extracellular \n"
  "potential at the local node, i.e. is used to drive local \n"
  "e_extracellular.\n"
  "\n"
  "2. Reports the contribution of local i_membrane to the \n"
  "total signal that would be picked up by an extracellular \n"
  "recording electrode.  This is computed as the product of \n"
  "rx (transfer resistance between the local node and the \n"
  "monopolar recording electrode), i_membrane (called im in \n"
  "this mechanism), and the surface area of the local segment, \n"
  "and is reported as er.  The total extracellularly recorded \n"
  "potential is the sum of all er_xtrau over all segments in \n"
  "all sections, and is to be computed at the hoc level, \n"
  "e.g. with code like\n"
  "\n"
  "func fieldrec() { local sum\n"
  "  sum = 0\n"
  "  forall {\n"
  "    if (ismembrane(\"xtrau\")) {\n"
  "      for (x,0) sum += er_xtrau(x)\n"
  "    }\n"
  "  }\n"
  "  return sum\n"
  "}\n"
  "\n"
  "Bipolar recording, i.e. recording the difference in potential \n"
  "between two extracellular electrodes, can be achieved with no \n"
  "change to either this NMODL code or fieldrec(); the values of \n"
  "rx will reflect the difference between the potentials at the \n"
  "recording electrodes caused by the local membrane current, so \n"
  "some rx will be negative and others positive.\n"
  "\n"
  "3. Enables local storage of xyz coordinates interpolated from \n"
  "the pt3d data.  These coordinates are used by hoc code that \n"
  "computes the transfer resistance that couples the membrane \n"
  "to extracellular stimulating and recording electrodes.\n"
  "\n"
  "4. Enables local storage of the distance d of internal \n"
  "nodes from the zero potential plane.\n"
  "\n"
  "Prior to NEURON 5.5, the SOLVE statement in the BREAKPOINT block \n"
  "used METHOD cvode_t so that the adaptive integrators wouldn't miss \n"
  "the stimulus.  Otherwise, the BREAKPOINT block would have been called \n"
  "_after_ the integration step, rather than from within cvodes/ida, \n"
  "causing this mechanism to fail to deliver a stimulus current \n"
  "when the adaptive integrator is used.\n"
  "\n"
  "With NEURON 5.5 and later, this mechanism abandons the BREAKPOINT \n"
  "block and uses the two new blocks BEFORE BREAKPOINT and  \n"
  "AFTER BREAKPOINT, like this--\n"
  "\n"
  "BEFORE BREAKPOINT { : before each cy' = f(y,t) setup\n"
  "  ex = is*rx*(1e6)\n"
  "}\n"
  "AFTER SOLVE { : after each solution step\n"
  "  er = (10)*rx*im*area\n"
  "}\n"
  "\n"
  "This ensures that the stimulus potential is computed prior to the \n"
  "solution step, and that the recorded potential is computed after.\n"
  "ENDCOMMENT\n"
  "\n"
  "NEURON {\n"
  "	SUFFIX xtrau\n"
  "	RANGE rx, er, d\n"
  "	RANGE x, y, z\n"
  "	GLOBAL E\n"
  "	POINTER im, ex\n"
  "}\n"
  "\n"
  "PARAMETER {\n"
  "	: default transfer resistance between recording electrode and node\n"
  "	rx = 1 (megohm) : mV/nA\n"
  "	x = 0 (1) : spatial coords\n"
  "	y = 0 (1)\n"
  "	z = 0 (1)\n"
  "}\n"
  "\n"
  "ASSIGNED {\n"
  "	v (millivolts)\n"
  ":	is (milliamp)\n"
  "	E (volt/m) : field strength\n"
  "	d (micron) : distance of node from zero potential plane\n"
  "	ex (millivolts)\n"
  "	im (milliamp/cm2)\n"
  "	er (microvolts)\n"
  "	area (micron2)\n"
  "}\n"
  "\n"
  "INITIAL {\n"
  ":	ex = is*rx*(1e6)\n"
  "	ex = -E*d*(1e-3)\n"
  "	er = (10)*rx*im*area\n"
  ": this demonstrates that area is known\n"
  ": UNITSOFF\n"
  ": printf(\"area = %f\\n\", area)\n"
  ": UNITSON\n"
  "}\n"
  "\n"
  ": Use BREAKPOINT for NEURON 5.4 and earlier\n"
  ": BREAKPOINT {\n"
  ":	SOLVE f METHOD cvode_t\n"
  ": }\n"
  ":\n"
  ": PROCEDURE f() {\n"
  ":	: 1 mA * 1 megohm is 1000 volts\n"
  ":	: but ex is in mV\n"
  ":	ex = E*d*(1e-3)\n"
  ":	er = (10)*rx*im*area\n"
  ": }\n"
  "\n"
  ": With NEURON 5.5 and later, abandon the BREAKPOINT block and PROCEDURE f(),\n"
  ": and instead use BEFORE BREAKPOINT and AFTER BREAKPOINT\n"
  "\n"
  "BEFORE BREAKPOINT { : before each cy' = f(y,t) setup\n"
  ":  ex = is*rx*(1e6)\n"
  "  ex = -E*d*(1e-3)\n"
  "}\n"
  "AFTER SOLVE { : after each solution step\n"
  "  er = (10)*rx*im*area\n"
  "}\n"
  ;
#endif