BioDynaMo  v1.05.120-25dc9790
exporter.cc
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #include "core/exporter.h"
16 
17 #include <array>
18 #include <cmath>
19 #include <fstream>
20 #include <iostream>
21 #include <sstream>
22 
23 #include "core/agent/cell.h"
24 #include "core/param/param.h"
25 #include "core/resource_manager.h"
26 #include "core/simulation.h"
27 #include "core/util/log.h"
28 
29 namespace bdm {
30 
31 Exporter::~Exporter() = default;
32 
33 void BasicExporter::ExportIteration(std::string filename, uint64_t iteration) {
34  std::ofstream outfile;
35  outfile.open(filename);
37  rm->ForEachAgent([&](Agent* agent) {
38  auto curr_pos = agent->GetPosition();
39  outfile << "[" << curr_pos[0] << "," << curr_pos[1] << "," << curr_pos[2]
40  << "]" << std::endl;
41  });
42 
43  outfile.close();
44 }
45 
46 void BasicExporter::ExportSummary(std::string filename,
47  uint64_t num_iterations) {}
48 
49 // -----------------------------------------------------------------------------
50 void MatlabExporter::ExportIteration(std::string filename, uint64_t iteration) {
51  std::ofstream outfile;
52  outfile.open(filename);
53 
55  auto num_cells = rm->GetNumAgents();
56  outfile << "CellPos = zeros(" << num_cells << "," << 3 << ");" << std::endl;
57 
58  uint64_t i = 0;
59  rm->ForEachAgent([&](Agent* agent) {
60  auto curr_pos = agent->GetPosition();
61  outfile << "CellPos(" << i++ + 1 << ",1:3) = [" << curr_pos[0] << ","
62  << curr_pos[1] << "," << curr_pos[2] << "];" << std::endl;
63  });
64 
65  outfile.close();
66 }
67 
68 void MatlabExporter::ExportSummary(std::string filename,
69  uint64_t num_iterations) {}
70 
71 // -----------------------------------------------------------------------------
72 void NeuroMLExporter::ExportIteration(std::string filename,
73  uint64_t iteration) {
74  std::ofstream outfile;
75  outfile.open(filename);
76 
77  std::string space1 = " ";
78  std::string space2 = " ";
79  std::string space3 = " ";
80  std::string space4 = " ";
81 
82  outfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
83  outfile << "<neuroml xmlns = \"http://morphml.org/neuroml/schema\""
84  << std::endl;
85  outfile << space1
86  << "xmlns:xsi = \"http://www.w3.org/2001/XMLSchema-instance\" "
87  << std::endl;
88  outfile << space1 << "xmlns:meta = \"http://morphml.org/metadata/schema\" "
89  << std::endl;
90  outfile << space1
91  << "xsi:schemaLocation=\"http://morphml.org/neuroml/"
92  "schema NeuroML_Level3_v1.7.1.xsd\" "
93  << std::endl;
94  outfile << space1 << "lengthUnits=\"micrometer\" " << std::endl;
95 
96  outfile << space1 << "<cells>" << std::endl;
97 
100  outfile << space2 << "<cell name=\"exz_lif\"> " << std::endl;
101  outfile << space3 << "<meta:properties>" << std::endl;
102  outfile << space4 << "<meta:property tag=\"PointNeuronModel\" value=\"yes\"/>"
103  << std::endl;
104  outfile << space4 << "<meta:property tag=\"Class\" value=\"CbLifNeuron\"/>"
105  << std::endl;
106  outfile << space4 << "<meta:property tag=\"Cm\" value=\"5e-10\"/>"
107  << std::endl;
108  outfile << space4 << "<meta:property tag=\"Rm\" value=\"1e8\"/>"
109  << std::endl;
110  outfile << space4 << "<meta:property tag=\"Vthresh\" value=\"-50e-3\"/>"
111  << std::endl;
112  outfile << space4 << "<meta:property tag=\"Vresting\" value=\"-60e-3\"/>"
113  << std::endl;
114  outfile << space4 << "<meta:property tag=\"Vreset\" value=\"-60e-3\"/>"
115  << std::endl;
116  outfile << space4 << "<meta:property tag=\"Trefract\" value=\"5e-3\"/>"
117  << std::endl;
118  outfile << space4 << "<meta:property tag=\"Vinit\" value=\"-60e-3\"/>"
119  << std::endl;
120  outfile << space3 << "</meta:properties>" << std::endl;
121  outfile << space2 << "</cell>" << std::endl;
122 
123  outfile << space2 << "<cell name=\"inh_lif\"> " << std::endl;
124  outfile << space3 << "<meta:properties>" << std::endl;
125  outfile << space4 << "<meta:property tag=\"PointNeuronModel\" value=\"yes\"/>"
126  << std::endl;
127  outfile << space4 << "<meta:property tag=\"Class\" value=\"CbLifNeuron\"/>"
128  << std::endl;
129  outfile << space4 << "<meta:property tag=\"Cm\" value=\"2e-10\"/>"
130  << std::endl;
131  outfile << space4 << "<meta:property tag=\"Rm\" value=\"1e8\"/>"
132  << std::endl;
133  outfile << space4 << "<meta:property tag=\"Vthresh\" value=\"-50e-3\"/>"
134  << std::endl;
135  outfile << space4 << "<meta:property tag=\"Vresting\" value=\"-60e-3\"/>"
136  << std::endl;
137  outfile << space4 << "<meta:property tag=\"Vreset\" value=\"-60e-3\"/>"
138  << std::endl;
139  outfile << space4 << "<meta:property tag=\"Trefract\" value=\"5e-3\"/>"
140  << std::endl;
141  outfile << space4 << "<meta:property tag=\"Vinit\" value=\"-60e-3\"/>"
142  << std::endl;
143  outfile << space3 << "</meta:properties>" << std::endl;
144  outfile << space2 << "</cell>" << std::endl;
145  outfile << space1 << "</cells>" << std::endl;
146 
149  // auto* rm = Simulation::GetActive()->GetResourceManager();
150  // rm->ForEachAgent([&](Agent* agent) { });
151 
152  outfile << std::endl;
153  outfile << "</neuroml>" << std::endl;
154 
155  outfile.close();
156 
157  Log::Info("Exporter", "Created NeuroML file");
158 }
159 
160 void NeuroMLExporter::ExportSummary(std::string filename,
161  uint64_t num_iterations) {}
162 
163 // -----------------------------------------------------------------------------
164 void ParaviewExporter::ExportIteration(std::string filename,
165  uint64_t iteration) {
167  auto num_cells = rm->GetNumAgents();
168  size_t index = 0;
169  std::ofstream vtu(filename + "-" + std::to_string(iteration) + ".vtu");
170  auto float_size = sizeof(real_t) * 8;
171 
172  vtu << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" "
173  "byte_order=\"LittleEndian\">"
174  << std::endl;
175  vtu << " <UnstructuredGrid>" << std::endl;
176  vtu << " <Piece NumberOfPoints=\"" << num_cells << "\" NumberOfCells=\""
177  << num_cells << "\">" << std::endl;
178  vtu << " <Points>" << std::endl;
179  vtu << " <DataArray type=\"Float" << float_size
180  << "\" NumberOfComponents=\"3\" "
181  "format=\"ascii\">"
182  << std::endl;
183  rm->ForEachAgent([&](Agent* agent) {
184  auto& coord = agent->GetPosition();
185  vtu << ' ' << coord[0] << ' ' << coord[1] << ' ' << coord[2] << std::flush;
186  });
187  vtu << std::endl;
188  vtu << " </DataArray>" << std::endl;
189  vtu << " </Points>" << std::endl;
190  vtu << " <PointData>" << std::endl;
191  vtu << " <DataArray type=\"Float" << float_size
192  << "\" Name=\"Cell_ID\" "
193  "NumberOfComponents=\"1\" format=\"ascii\">"
194  << std::endl;
195  index = 0;
196  for (size_t i = 0; i < num_cells; i++) {
197  vtu << ' ' << index++ << std::flush;
198  }
199  vtu << std::endl;
200  vtu << " </DataArray>" << std::endl;
201  vtu << " <DataArray type=\"Float" << float_size
202  << "\" Name=\"Adherence\" "
203  "NumberOfComponents=\"1\" format=\"ascii\">"
204  << std::endl;
205 
206  rm->ForEachAgent([&](Agent* agent) {
207  if (auto* cell = dynamic_cast<Cell*>(agent)) {
208  auto adhr = cell->GetAdherence();
209  vtu << ' ' << adhr << std::flush;
210  }
211  });
212 
213  vtu << std::endl;
214  vtu << " </DataArray>" << std::endl;
215  vtu << " <DataArray type=\"Float" << float_size
216  << "\" Name=\"Diameter\" "
217  "NumberOfComponents=\"1\" format=\"ascii\">"
218  << std::endl;
219  rm->ForEachAgent([&](Agent* agent) {
220  auto diam = agent->GetDiameter();
221  vtu << ' ' << diam << std::flush;
222  });
223 
224  vtu << std::endl;
225  vtu << " </DataArray>" << std::endl;
226  vtu << " <DataArray type=\"Float" << float_size
227  << "\" Name=\"Mass\" "
228  "NumberOfComponents=\"1\" format=\"ascii\">"
229  << std::endl;
230 
231  rm->ForEachAgent([&](Agent* agent) {
232  if (auto* cell = dynamic_cast<Cell*>(agent)) {
233  auto mass = cell->GetMass();
234  vtu << ' ' << mass << std::flush;
235  }
236  });
237 
238  vtu << std::endl;
239  vtu << " </DataArray>" << std::endl;
240  vtu << " <DataArray type=\"Float" << float_size
241  << "\" Name=\"Volume\" "
242  "NumberOfComponents=\"1\" format=\"ascii\">"
243  << std::endl;
244 
245  rm->ForEachAgent([&](Agent* agent) {
246  if (auto* cell = dynamic_cast<Cell*>(agent)) {
247  auto v = cell->GetVolume();
248  vtu << ' ' << v << std::flush;
249  }
250  });
251 
252  vtu << std::endl;
253  vtu << " </DataArray>" << std::endl;
254  vtu << " <DataArray type=\"Float" << float_size
255  << "\" Name=\"TractionForce\" "
256  "NumberOfComponents=\"3\" format=\"ascii\">"
257  << std::endl;
258 
259  rm->ForEachAgent([&](Agent* agent) {
260  if (auto* cell = dynamic_cast<Cell*>(agent)) {
261  auto& tracf = cell->GetTractorForce();
262  vtu << ' ' << tracf[0] << ' ' << tracf[1] << ' ' << tracf[2]
263  << std::flush;
264  }
265  });
266 
267  vtu << std::endl;
268  vtu << " </DataArray>" << std::endl;
269  vtu << " </PointData>" << std::endl;
270 
271  vtu << " <Cells>" << std::endl;
272  vtu << " <DataArray type=\"Int32\" Name=\"connectivity\" "
273  "format=\"ascii\">"
274  << std::endl;
275  index = 0;
276  for (size_t i = 0; i < num_cells; i++) {
277  vtu << ' ' << index++ << std::flush;
278  }
279  vtu << std::endl;
280  vtu << " </DataArray>" << std::endl;
281  vtu << " <DataArray type=\"Int32\" Name=\"offsets\" "
282  "format=\"ascii\">"
283  << std::endl;
284  for (size_t i = 0; i < num_cells; i++) {
285  vtu << ' ' << 1 << std::flush;
286  }
287  vtu << std::endl;
288  vtu << " </DataArray>" << std::endl;
289  vtu << " <DataArray type=\"Int32\" Name=\"types\" "
290  "format=\"ascii\">"
291  << std::endl;
292  for (size_t i = 0; i < num_cells; i++) {
293  vtu << ' ' << 1 << std::flush;
294  }
295  vtu << std::endl;
296  vtu << " </DataArray>" << std::endl;
297  vtu << " </Cells>" << std::endl;
298  vtu << " </Piece>" << std::endl;
299  vtu << " </UnstructuredGrid>" << std::endl;
300  vtu << "</VTKFile>" << std::endl;
301 }
302 
303 void ParaviewExporter::ExportSummary(std::string filename,
304  uint64_t num_iterations) {
305  std::ofstream pvd(filename + ".pvd");
306  auto* param = Simulation::GetActive()->GetParam();
307 
308  pvd << "<?xml version=\"1.0\"?>" << std::endl;
309  pvd << "<VTKFile type=\"Collection\" version=\"0.1\" "
310  "byte_order=\"LittleEndian\">"
311  << std::endl;
312  pvd << "<Collection>" << std::endl;
314  for (uint64_t i = 0; i < num_iterations; i++) {
315  pvd << "<DataSet timestep=\"" << (i * param->simulation_time_step)
316  << "\" group=\"\" part=\"0\" file=\"" << filename << '-' << i
317  << ".vtu\">";
318  pvd << std::endl;
320  }
321  pvd << "</Collection>" << std::endl;
322  pvd << "</VTKFile>" << std::endl;
323 }
324 
325 // -----------------------------------------------------------------------------
326 std::unique_ptr<Exporter> ExporterFactory::GenerateExporter(ExporterType type) {
327  switch (type) {
328  case kBasic:
329  return std::unique_ptr<Exporter>(new BasicExporter);
330  case kMatlab:
331  return std::unique_ptr<Exporter>(new MatlabExporter);
332  case kNeuroML:
333  return std::unique_ptr<Exporter>(new NeuroMLExporter);
334  case kParaview:
335  return std::unique_ptr<Exporter>(new ParaviewExporter);
336  default:
337  throw std::invalid_argument("export format not recognized");
338  }
339 }
340 
341 } // namespace bdm
bdm::ExporterType
ExporterType
Definition: exporter.h:69
bdm::BasicExporter::ExportSummary
void ExportSummary(std::string filename, uint64_t num_iterations) override
Definition: exporter.cc:46
bdm::ResourceManager::GetNumAgents
size_t GetNumAgents(int numa_node=-1) const
Definition: resource_manager.h:256
bdm
Definition: agent.cc:39
bdm::BasicExporter
Definition: exporter.h:38
bdm::kBasic
@ kBasic
Definition: exporter.h:69
bdm::kNeuroML
@ kNeuroML
Definition: exporter.h:69
bdm::Exporter::~Exporter
virtual ~Exporter()
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::NeuroMLExporter::ExportIteration
void ExportIteration(std::string filename, uint64_t iteration) override
Definition: exporter.cc:72
bdm::ExporterFactory::GenerateExporter
static std::unique_ptr< Exporter > GenerateExporter(ExporterType type)
Definition: exporter.cc:326
bdm::MatlabExporter::ExportSummary
void ExportSummary(std::string filename, uint64_t num_iterations) override
Definition: exporter.cc:68
bdm::Agent::GetDiameter
virtual real_t GetDiameter() const =0
exporter.h
bdm::ResourceManager::ForEachAgent
virtual void ForEachAgent(const std::function< void(Agent *)> &function, Functor< bool, Agent * > *filter=nullptr)
Definition: resource_manager.h:280
bdm::BasicExporter::ExportIteration
void ExportIteration(std::string filename, uint64_t iteration) override
Definition: exporter.cc:33
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::kMatlab
@ kMatlab
Definition: exporter.h:69
bdm::MatlabExporter
Definition: exporter.h:45
bdm::MatlabExporter::ExportIteration
void ExportIteration(std::string filename, uint64_t iteration) override
Definition: exporter.cc:50
bdm::Log::Info
static void Info(const std::string &location, const Args &... parts)
Prints information message.
Definition: log.h:55
bdm::Simulation::GetResourceManager
ResourceManager * GetResourceManager()
Returns the ResourceManager instance.
Definition: simulation.cc:244
bdm::ParaviewExporter
Definition: exporter.h:59
bdm::NeuroMLExporter::ExportSummary
void ExportSummary(std::string filename, uint64_t num_iterations) override
Definition: exporter.cc:160
bdm::Cell
Definition: cell.h:40
log.h
bdm::NeuroMLExporter
Definition: exporter.h:52
simulation.h
bdm::Simulation::GetParam
const Param * GetParam() const
Returns the simulation parameters.
Definition: simulation.cc:254
bdm::ParaviewExporter::ExportIteration
void ExportIteration(std::string filename, uint64_t iteration) override
Definition: exporter.cc:164
bdm::Agent::GetPosition
virtual const Real3 & GetPosition() const =0
resource_manager.h
param.h
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
bdm::kParaview
@ kParaview
Definition: exporter.h:69
cell.h
bdm::ParaviewExporter::ExportSummary
void ExportSummary(std::string filename, uint64_t num_iterations) override
Definition: exporter.cc:303