CoreNEURON
lfp.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <vector>
5 
7 #include "coreneuron/nrnconf.h"
9 
10 namespace coreneuron {
11 
12 namespace lfputils {
13 
14 using Point3D = std::array<double, 3>;
15 using Point3Ds = std::vector<Point3D>;
16 using DoublePtr = double*;
17 
18 inline double dot(const Point3D& p1, const Point3D& p2) {
19  return p1[0] * p2[0] + p1[1] * p2[1] + p1[2] * p2[2];
20 }
21 
22 inline double norm(const Point3D& p1) {
23  return std::sqrt(dot(p1, p1));
24 }
25 
26 inline Point3D barycenter(const Point3D& p1, const Point3D& p2) {
27  return {0.5 * (p1[0] + p2[0]), 0.5 * (p1[1] + p2[1]), 0.5 * (p1[2] + p2[2])};
28 }
29 
30 inline Point3D paxpy(const Point3D& p1, const double alpha, const Point3D& p2) {
31  return {p1[0] + alpha * p2[0], p1[1] + alpha * p2[1], p1[2] + alpha * p2[2]};
32 }
33 
34 /**
35  *
36  * \param e_pos electrode position
37  * \param seg_pos segment position
38  * \param radius segment radius
39  * \param double conductivity factor 1/([4 pi] * [conductivity])
40  * \return Resistance of the medium from the segment to the electrode.
41  */
42 inline double point_source_lfp_factor(const Point3D& e_pos,
43  const Point3D& seg_pos,
44  const double radius,
45  const double f) {
46  nrn_assert(radius >= 0.0);
47  Point3D es = paxpy(e_pos, -1.0, seg_pos);
48  return f / std::max(norm(es), radius);
49 }
50 
51 /**
52  *
53  * \param e_pos electrode position
54  * \param seg_pos segment position
55  * \param radius segment radius
56  * \param f conductivity factor 1/([4 pi] * [conductivity])
57  * \return Resistance of the medium from the segment to the electrode.
58  */
59 double line_source_lfp_factor(const Point3D& e_pos,
60  const Point3D& seg_0,
61  const Point3D& seg_1,
62  const double radius,
63  const double f);
64 } // namespace lfputils
65 
67 
68 /**
69  * \brief LFPCalculator allows calculation of LFP given membrane currents.
70  */
71 template <LFPCalculatorType Ty, typename SegmentIdTy = int>
72 struct LFPCalculator {
73  /**
74  * LFP Calculator constructor
75  * \param seg_start all segments start owned by the proc
76  * \param seg_end all segments end owned by the proc
77  * \param radius fence around the segment. Ensures electrode cannot be
78  * arbitrarily close to the segment
79  * \param electrodes positions of the electrodes
80  * \param extra_cellular_conductivity conductivity of the extra-cellular
81  * medium
82  */
83  LFPCalculator(const lfputils::Point3Ds& seg_start,
84  const lfputils::Point3Ds& seg_end,
85  const std::vector<double>& radius,
86  const std::vector<SegmentIdTy>& segment_ids,
87  const lfputils::Point3Ds& electrodes,
88  double extra_cellular_conductivity);
89 
90  template <typename Vector>
91  void lfp(const Vector& membrane_current);
92 
93  const std::vector<double>& lfp_values() const noexcept {
94  return lfp_values_;
95  }
96 
97  private:
98  inline double getFactor(const lfputils::Point3D& e_pos,
99  const lfputils::Point3D& seg_0,
100  const lfputils::Point3D& seg_1,
101  const double radius,
102  const double f) const;
103  std::vector<double> lfp_values_;
104  std::vector<std::vector<double>> m;
105  const std::vector<SegmentIdTy>& segment_ids_;
106 };
107 
108 template <>
110  const lfputils::Point3D& seg_0,
111  const lfputils::Point3D& seg_1,
112  const double radius,
113  const double f) const {
114  return lfputils::line_source_lfp_factor(e_pos, seg_0, seg_1, radius, f);
115 }
116 
117 template <>
119  const lfputils::Point3D& seg_0,
120  const lfputils::Point3D& seg_1,
121  const double radius,
122  const double f) const {
123  return lfputils::point_source_lfp_factor(e_pos, lfputils::barycenter(seg_0, seg_1), radius, f);
124 }
125 
126 extern template LFPCalculator<LineSource>::LFPCalculator(const lfputils::Point3Ds& seg_start,
127  const lfputils::Point3Ds& seg_end,
128  const std::vector<double>& radius,
129  const std::vector<int>& segment_ids,
130  const lfputils::Point3Ds& electrodes,
131  double extra_cellular_conductivity);
132 extern template LFPCalculator<PointSource>::LFPCalculator(const lfputils::Point3Ds& seg_start,
133  const lfputils::Point3Ds& seg_end,
134  const std::vector<double>& radius,
135  const std::vector<int>& segment_ids,
136  const lfputils::Point3Ds& electrodes,
137  double extra_cellular_conductivity);
138 extern template void LFPCalculator<LineSource>::lfp(const lfputils::DoublePtr& membrane_current);
139 extern template void LFPCalculator<PointSource>::lfp(const lfputils::DoublePtr& membrane_current);
140 extern template void LFPCalculator<LineSource>::lfp(const std::vector<double>& membrane_current);
141 extern template void LFPCalculator<PointSource>::lfp(const std::vector<double>& membrane_current);
142 } // namespace coreneuron
coreneuron::LFPCalculator::segment_ids_
const std::vector< SegmentIdTy > & segment_ids_
Definition: lfp.hpp:105
coreneuron::lfputils::Point3Ds
std::vector< Point3D > Point3Ds
Definition: lfp.hpp:15
coreneuron::LFPCalculator::getFactor
double getFactor(const lfputils::Point3D &e_pos, const lfputils::Point3D &seg_0, const lfputils::Point3D &seg_1, const double radius, const double f) const
coreneuron::lfputils::DoublePtr
double * DoublePtr
Definition: lfp.hpp:16
coreneuron::LFPCalculator::lfp
void lfp(const Vector &membrane_current)
Definition: lfp.cpp:102
coreneuron::lfputils::dot
double dot(const Point3D &p1, const Point3D &p2)
Definition: lfp.hpp:18
coreneuron
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
Definition: corenrn_parameters.cpp:12
coreneuron::lfputils::norm
double norm(const Point3D &p1)
Definition: lfp.hpp:22
coreneuron::lfputils::barycenter
Point3D barycenter(const Point3D &p1, const Point3D &p2)
Definition: lfp.hpp:26
coreneuron::LFPCalculator::lfp_values
const std::vector< double > & lfp_values() const noexcept
Definition: lfp.hpp:93
coreneuron::lfputils::point_source_lfp_factor
double point_source_lfp_factor(const Point3D &e_pos, const Point3D &seg_pos, const double radius, const double f)
Definition: lfp.hpp:42
coreneuron::lfputils::line_source_lfp_factor
double line_source_lfp_factor(const Point3D &e_pos, const Point3D &seg_0, const Point3D &seg_1, const double radius, const double f)
Definition: lfp.cpp:12
nrnconf.h
coreneuron::LFPCalculator::lfp_values_
std::vector< double > lfp_values_
Definition: lfp.hpp:103
coreneuron::lfputils::Point3D
std::array< double, 3 > Point3D
Definition: lfp.hpp:14
coreneuron::LFPCalculator
LFPCalculator allows calculation of LFP given membrane currents.
Definition: lfp.hpp:72
coreneuron::lfputils::paxpy
Point3D paxpy(const Point3D &p1, const double alpha, const Point3D &p2)
Definition: lfp.hpp:30
coreneuron::LFPCalculatorType
LFPCalculatorType
Definition: lfp.hpp:66
coreneuron::LFPCalculator::LFPCalculator
LFPCalculator(const lfputils::Point3Ds &seg_start, const lfputils::Point3Ds &seg_end, const std::vector< double > &radius, const std::vector< SegmentIdTy > &segment_ids, const lfputils::Point3Ds &electrodes, double extra_cellular_conductivity)
LFP Calculator constructor.
Definition: lfp.cpp:75
coreneuron::PointSource
@ PointSource
Definition: lfp.hpp:66
nrn_assert
#define nrn_assert(x)
assert()-like macro, independent of NDEBUG status
Definition: nrn_assert.h:33
coreneuron::LineSource
@ LineSource
Definition: lfp.hpp:66
nrn_assert.h
nrnmpi.h
coreneuron::LFPCalculator::m
std::vector< std::vector< double > > m
Definition: lfp.hpp:104