-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bloodcircuit.cc
290 lines (256 loc) · 9.96 KB
/
Bloodcircuit.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* -*- Mode: C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2017 Universität zu Lübeck [GEYER]
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Regine Geyer <[email protected]>
*/
#include "Bloodcircuit.h"
#include <fstream>
/**
* This function sets the Bloodvessel map of the human body of a female: hight 1.72m, weight 69kg, age 29.
*/
namespace ns3 {
Bloodcircuit::Bloodcircuit (unsigned int numberOfNanobots, unsigned int injectionVessel, Ptr<Orchestrator> printer, std::vector<int> gatewayPositions, std::vector<int> tissue_ID, float vesselthickness)
{
// initialise map with bloodvesselinformation
m_bloodvessels = map<int, Ptr<Bloodvessel>> ();
this->printer = printer;
// loading vasculature via csv
std::ifstream infile{"vasculature.csv"};
if (infile.good ())
{
std::vector<int> numbers;
numbers.resize (valuesPerLine);
std::string buffer;
buffer.reserve (64);
int errorflag = 0;
while (infile.good ())
{
for (auto &&elem : numbers)
{
if (std::getline (infile, buffer, ','))
{
elem = std::stoi (buffer);
}
else
{
elem = 0;
errorflag = 1;
}
}
if (errorflag == 0)
{
AddVesselData (numbers[0], (BloodvesselType) numbers[1],
Vector (numbers[2], numbers[3], numbers[4]),
Vector (numbers[5], numbers[6], numbers[7]));
}
}
cout << "load CSV - done" << endl;
}
else
{ // Old way of loading data
cout << "NO VALID CSV FILE FOUND! " << endl;
cout << "please provide a valid 'vasculatures.csv'" << endl;
}
//Create Bloodcircuit with all Bloodvessels.
//
ConnectBloodvessels ();
//Inject Nanobots here!
//Places x Bots, randomly on Streams at specific vessel injected.
//If you choose other values, the nanobots all get injected at the same coordinates
if (m_bloodvessels.size() > 1){
InjectNanobots (numberOfNanobots, m_bloodvessels[injectionVessel < m_bloodvessels.size() ? injectionVessel : m_bloodvessels.size() -1],gatewayPositions, tissue_ID, vesselthickness);
}
}
//Starts the simulation in a bloodvessel
void
Starter (Ptr<Bloodvessel> vessel)
{
vessel->Start ();
}
int
Bloodcircuit::BeginnSimulation (unsigned int simulationDuration, unsigned int numOfNanobots, unsigned int injectionVessel, std::vector<int> gatewayPositions, std::vector<int> tissue_ID, float vesselthickness)
{
//execution time
clock_t start, finish;
start = clock ();
Ptr<Orchestrator> printNano = new Orchestrator ();
//Create the bloodcircuit
Bloodcircuit circuit (numOfNanobots, injectionVessel, printNano, gatewayPositions, tissue_ID, vesselthickness);
//Get the map of the bloodcircuit
map<int, ns3::Ptr<ns3::Bloodvessel>> circuitMap = circuit.GetBloodcircuit ();
if(circuitMap.size() > 1){
// Schedule and run the Simulation in each bloodvessel
for (unsigned int i = 1; i < circuitMap.size() + 1; i++)
{
Simulator::Schedule (Seconds (0.0), &Starter, circuitMap[i]);
}
//Stop simulation after specific time
Simulator::Stop (Seconds (simulationDuration + 1));
Simulator::Run ();
Simulator::Destroy ();
//output execution time
finish = clock ();
cout << "Dauer: " << simulationDuration << "s " << numOfNanobots << "NB -> "
<< (finish - start) / 1000000 << "s ------------------------" << endl;
cout << "Injected Vessel: " << injectionVessel << endl;
return 0;
} else {
cout << "Not enough vessels for simulation! Please check 'vasculature.csv'" << endl;
return 1;
}
}
Bloodcircuit::~Bloodcircuit ()
{
m_bloodvessels.clear ();
}
map<int, ns3::Ptr<ns3::Bloodvessel>>
Bloodcircuit::GetBloodcircuit ()
{
return m_bloodvessels;
}
Vector
Bloodcircuit::CalcDirectionVectorNorm (Ptr<Bloodvessel> m_bloodvessel)
{
Vector start = m_bloodvessel->GetStartPositionBloodvessel ();
Vector end = m_bloodvessel->GetStopPositionBloodvessel ();
double x = end.x - start.x;
double y = end.y - start.y;
double z = end.z - start.z;
double vectorLength = sqrt (pow (x, 2) + pow (y, 2) + pow (z, 2));
x = x / vectorLength;
y = y / vectorLength;
z = z / vectorLength;
return Vector (x, y, z);
}
void
Bloodcircuit::InjectNanobots (int numberOfNanobots, Ptr<Bloodvessel> bloodvessel,std::vector<int> gatewayPositions, std::vector<int> tissue_ID, float vesselthickness)
{
int nanobotGroupSize = 10;
Ptr<UniformRandomVariable> distribute_randomly =
bloodvessel->getRandomObjectBetween (0, bloodvessel->GetNumberOfStreams ());
Vector m_coordinateStart = bloodvessel->GetStartPositionBloodvessel ();
int intervall =
(numberOfNanobots >= nanobotGroupSize) //IF if equal or more than 10 Nanobots are injected,
? div (numberOfNanobots, nanobotGroupSize)
.quot // THEN Divide number of Nanobots into 10 equaly sized groups + remainder
: numberOfNanobots; // ELSE put them on beginning of the bloodvessel in one point.
//Calculate the normalized direction vector of the start vessel.
Vector m_direction = CalcDirectionVectorNorm (bloodvessel);
//Set direction intervall as 1/10 of the normalized direction vector.
Vector directionIntervall =
Vector (m_direction.x / nanobotGroupSize, m_direction.y / nanobotGroupSize,
m_direction.z / nanobotGroupSize);
unsigned int group = 0; // Group 0 to 9
//create channel for transmission
Ptr<BVSChannel> channel = CreateObject<BVSChannel> (vesselthickness);
for (int i = 0; i <= (int) gatewayPositions.size(); i++)
{
Ptr<GatewayNetDevice> gdev = CreateObject<GatewayNetDevice> (gatewayPositions[i]);
channel->AddGateway(gdev);
}
//Distribute Nanobots in 10 groups over the beginning of the start vessel.
for (int i = 1; i <= numberOfNanobots; ++i)
{
group = (i - 1) / intervall;
Ptr<Nanobot> temp_nb = CreateObject<Nanobot> ();
temp_nb->SetNanobotID (i);
temp_nb->SetGatewayPositions(gatewayPositions);
temp_nb->Settissue_ID(tissue_ID);
temp_nb->InstallNanoNetDevice(channel);
//Get random stream number.
int dr = floor (distribute_randomly->GetValue ());
temp_nb->SetShouldChange (false);
temp_nb->SetPosition (Vector (m_coordinateStart.x + (directionIntervall.x * group),
m_coordinateStart.y + (directionIntervall.y * group),
m_coordinateStart.z + (directionIntervall.z * group)));
//Set position with random stream dr.
bloodvessel->AddNanobotToStream (dr, temp_nb);
}
//Print Nanobots in csv-file.
bloodvessel->OrchestratorOfVessel ();
}
double
Bloodcircuit::GetSpeedClassOfBloodVesselType (BloodvesselType type)
{
if (type == ARTERY)
{
return 10.0;
}
else if (type == VEIN)
{
return 3.7;
}
else // if (type == ORGAN)
{
return 1.0;
}
}
void
Bloodcircuit::AddVesselData (int id, BloodvesselType type, Vector start, Vector stop)
{
Ptr<Bloodvessel> vessel = CreateObject<Bloodvessel> ();
vessel->SetBloodvesselID (id);
vessel->SetBloodvesselType (type);
vessel->SetStartPositionBloodvessel (start);
vessel->SetStopPositionBloodvessel (stop);
vessel->SetVesselWidth (0.25); // 0.25
vessel->SetPrinter (printer);
// Init Bloodvessel: Calculate length and angle & velocity.
vessel->InitBloodstreamLengthAngleAndVelocity (GetSpeedClassOfBloodVesselType (type));
m_bloodvessels[id] = vessel;
cout << "New Vessel(" + to_string (id) + "," + to_string (type) + "," + to_string (start.x) +
"," + to_string (start.y) + "," + to_string (start.z) + "," + to_string (stop.x) +
"," + to_string (stop.y) + "," + to_string (stop.z) + ")"
<< endl;
}
void
Bloodcircuit::InitialiseBloodvessels (int vesseldata[][8], unsigned int count)
{
for (unsigned int i = 0; i < count; i++)
{
AddVesselData (vesseldata[i][0], (BloodvesselType) vesseldata[i][1],
Vector (vesseldata[i][2], vesseldata[i][3], vesseldata[i][4]),
Vector (vesseldata[i][5], vesseldata[i][6], vesseldata[i][7]));
}
}
void
Bloodcircuit::ConnectBloodvessels ()
{
unsigned int count = m_bloodvessels.size();
//Set Connections between bloodvessels if they have the same start/end coordinates
for (unsigned int i = 1; i < count + 1; i++)
{
unsigned int counter = 0;
Vector end = m_bloodvessels[i]->GetStopPositionBloodvessel ();
for (unsigned int j = 1; j < count + 1; j++)
{
Vector start = m_bloodvessels[j]->GetStartPositionBloodvessel ();
if (end.x == start.x && end.y == start.y && end.z == start.z)
{
counter++;
if (counter == 1)
{
m_bloodvessels[i]->SetNextBloodvessel1 (m_bloodvessels[j]);
}
else
{
m_bloodvessels[i]->SetNextBloodvessel2 (m_bloodvessels[j]);
}
}
}
}
}
} // namespace ns3