NEURON
report_event.cpp
Go to the documentation of this file.
1 /*
2 # =============================================================================
3 # Copyright (c) 2016 - 2021 Blue Brain Project/EPFL
4 #
5 # See top-level LICENSE file for details.
6 # =============================================================================
7 */
8 
9 #include "report_event.hpp"
14 #ifdef ENABLE_SONATA_REPORTS
15 #include "bbp/sonata/reports.h"
16 #endif // ENABLE_SONATA_REPORTS
17 
18 namespace coreneuron {
19 
20 #ifdef ENABLE_SONATA_REPORTS
21 ReportEvent::ReportEvent(double dt,
22  double tstart,
23  const VarsToReport& filtered_gids,
24  const char* name,
25  double report_dt,
27  : dt(dt)
28  , tstart(tstart)
29  , report_path(name)
30  , report_dt(report_dt)
31  , report_type(type)
32  , vars_to_report(filtered_gids) {
33  nrn_assert(filtered_gids.size());
34  step = tstart / dt;
35  reporting_period = static_cast<int>(report_dt / dt);
36  gids_to_report.reserve(filtered_gids.size());
37  for (const auto& gid: filtered_gids) {
38  gids_to_report.push_back(gid.first);
39  }
40  std::sort(gids_to_report.begin(), gids_to_report.end());
41 }
42 
43 void ReportEvent::summation_alu(NrnThread* nt) {
44  auto& summation_report = nt->summation_report_handler_->summation_reports_[report_path];
45  // Add currents of all variables in each segment
46  for (const auto& kv: summation_report.currents_) {
47  double sum = 0.0;
48  int segment_id = kv.first;
49  for (const auto& value: kv.second) {
50  double current_value = *value.first;
51  int scale = value.second;
52  sum += current_value * scale;
53  }
54  summation_report.summation_[segment_id] = sum;
55  }
56  // Add all currents in the soma
57  // Only when type summation and soma target
58  if (!summation_report.gid_segments_.empty()) {
59  for (const auto& kv: summation_report.gid_segments_) {
60  double sum_soma = 0.0;
61  int gid = kv.first;
62  for (const auto& segment_id: kv.second) {
63  sum_soma += summation_report.summation_[segment_id];
64  }
65  *(vars_to_report[gid].front().var_value) = sum_soma;
66  }
67  }
68 }
69 
70 void ReportEvent::lfp_calc(NrnThread* nt) {
71  auto* mapinfo = static_cast<NrnThreadMappingInfo*>(nt->mapping);
72  double* fast_imem_rhs = nt->nrn_fast_imem->nrn_sav_rhs;
73  auto& summation_report = nt->summation_report_handler_->summation_reports_[report_path];
74  for (const auto& kv: vars_to_report) {
75  int gid = kv.first;
76  const auto& to_report = kv.second;
77  const auto& cell_mapping = mapinfo->get_cell_mapping(gid);
78  int num_electrodes = cell_mapping->num_electrodes();
79  std::vector<double> lfp_values(num_electrodes, 0.0);
80  for (const auto& kv: cell_mapping->lfp_factors) {
81  int segment_id = kv.first;
82  const auto& factors = kv.second;
83  int electrode_id = 0;
84  for (const auto& factor: factors) {
85  double iclamp = 0.0;
86  for (const auto& value: summation_report.currents_[segment_id]) {
87  double current_value = *value.first;
88  int scale = value.second;
89  iclamp += current_value * scale;
90  }
91  lfp_values[electrode_id] += (fast_imem_rhs[segment_id] + iclamp) * factor;
92  electrode_id++;
93  }
94  }
95  for (int i = 0; i < to_report.size(); i++) {
96  *(to_report[i].var_value) = lfp_values[i];
97  }
98  }
99 }
100 
101 /** on deliver, call ReportingLib and setup next event */
102 void ReportEvent::deliver(double t, NetCvode* nc, NrnThread* nt) {
103 /* libsonata is not thread safe */
104 #pragma omp critical
105  {
106  // Sum currents and calculate lfp only on reporting steps
107  if (step > 0 && (static_cast<int>(step) % reporting_period) == 0) {
108  if (report_type == ReportType::SummationReport) {
109  summation_alu(nt);
110  } else if (report_type == ReportType::LFPReport) {
111  lfp_calc(nt);
112  }
113  }
114  // each thread needs to know its own step
115 #ifdef ENABLE_SONATA_REPORTS
116  sonata_record_node_data(step,
117  gids_to_report.size(),
118  gids_to_report.data(),
119  report_path.data());
120 #endif
121  send(t + dt, nc, nt);
122  step++;
123  }
124 }
125 
126 bool ReportEvent::require_checkpoint() {
127  return false;
128 }
129 #endif
130 
131 } // Namespace coreneuron
#define i
Definition: md1redef.h:19
step
Definition: extdef.h:7
const char * name
Definition: init.cpp:16
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
@ SummationReport
Definition: nrnreport.hpp:79
#define nrn_assert(x)
assert()-like macro, independent of NDEBUG status
Definition: nrn_assert.h:33
NrnMappingInfo mapinfo
mapping information
short type
Definition: cabvars.h:10
static uint32_t value
Definition: scoprand.cpp:25
CellMapping * get_cell_mapping(int gid)
get cell mapping information for given gid if exist otherwise return NULL.
Represent main neuron object computed by single thread.
Definition: multicore.h:58