EGS Brachy
An egs++ user code for rapid brachytherapy calculations
Loading...
Searching...
No Matches
/Users/marc/Developer/EGSnrc/HEN_HOUSE/user_codes/egs_brachy/egs_brachy/muen.h
Go to the documentation of this file.
1/*
2################################################################################
3#
4# egs_brachy muen.h
5# Copyright (C) 2016 Rowan Thomson, Dave Rogers, Randle Taylor, and Marc
6# Chamberland
7#
8# This file is part of egs_brachy
9#
10# egs_brachy is free software: you can redistribute it and/or modify it
11# under the terms of the GNU Affero General Public License as published
12# by the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# egs_brachy is distributed in the hope that it will be useful, but
16# WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# Affero General Public License for more details:
19# <http://www.gnu.org/licenses/>.
20#
21################################################################################
22#
23# When egs_brachy is used for publications, please cite our paper:
24# M. J. P. Chamberland, R. E. P. Taylor, D. W. O. Rogers, and R. M. Thomson,
25# egs brachy: a versatile and fast Monte Carlo code for brachytherapy,
26# Phys. Med. Biol. 61, 8214-8231 (2016).
27#
28################################################################################
29#
30# Author: Randle Taylor, 2016
31#
32# Contributors: Marc Chamberland
33# Dave Rogers
34# Rowan Thomson
35#
36################################################################################
37*/
38
44#include <algorithm>
45#include <fstream>
46#include <string>
47#include <istream>
48#include <iostream>
49#include <sstream>
50#include <map>
51#include <cstdlib>
52#include <vector>
53
54#include "egs_functions.h"
55#include "egs_input.h"
56#include "egs_interpolator.h"
57
58
59#ifndef MUENDATA_
60#define MUENDATA_
61
62namespace muen {
63
64using namespace std;
65
66
68std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
69 std::stringstream ss(s);
70 std::string item;
71 while (std::getline(ss, item, delim)) {
72 elems.push_back(item);
73 }
74 return elems;
75}
76
77
79std::vector<std::string> split(const std::string &s, char delim) {
80 std::vector<std::string> elems;
81 split(s, delim, elems);
82 return elems;
83}
84
86static inline std::string &ltrim(std::string &s) {
87 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {return !std::isspace(ch);}));
88 return s;
89}
90
92static inline std::string &rtrim(std::string &s) {
93 s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {return !std::isspace(ch);}).base(), s.end());
94 return s;
95}
96
98static inline std::string &trim(std::string &s) {
99 return ltrim(rtrim(s));
100}
101
102
104typedef pair<double, double> MuenAtET;
105
107typedef map<string, vector<MuenAtET> > MuenMapT;
108
132
135 static const int NSKIP = 3;
141 MuenMapT splitFileByMed(ifstream &in) {
142
143 MuenMapT med_lines;
144
145 string cur_med_name, dummy;
146
147 while (in) {
148
149 string line;
150 getline(in, line);
151 if (trim(line).size() == 0) {
152 continue;
153 }
154
155 bool new_med = line.find(MUEN_START) != string::npos;
156
157 if (new_med) {
158 /* new medium found. Update the current medium name
159 * and move file pointer to start of data */
160 cur_med_name = trim(split(line, '=')[1]);
161 for (int i=0; i < NSKIP; i++) {
162 getline(in, dummy);
163 if (trim(dummy).size() == 0) {
164 i--;
165 }
166 }
167 } else {
168
169 istringstream iss(line);
170
171 double energy, muen;
172 iss >> energy;
173 iss >> dummy; // skip the MeV unit
174 iss >> muen;
175
176 if (iss.fail() && in) {
177 egsWarning("MuenDataParser:: Invalid muen data file format. Last line was:\n%s\n ", line.c_str());
178 med_lines.clear();
179 return med_lines;
180 } else {
181 med_lines[cur_med_name].push_back(MuenAtET(energy, muen));
182 }
183 }
184 }
185
186 return med_lines;
187
188 }
189
190
191public:
192
193 static const string MUEN_START;
206
207 int setMuenFile(string filename) {
208
209 ifstream muen_data(filename.c_str());
210
211 if (!muen_data) {
212 return 1;
213 }
214
215 med_data = splitFileByMed(muen_data);
216 muen_data.close();
217 return 0;
218
219 };
220
228 EGS_Interpolator *getMuenInterpolator(string med_name) {
229
230 size_t ndat = med_data[med_name].size();
231 if (ndat < 2) {
232 return 0;
233 }
234 EGS_Float emin = med_data[med_name][0].first;
235 EGS_Float emax= med_data[med_name][ndat-1].first;
236
237 EGS_Float *fmuen = new EGS_Float[ndat];
238 for (size_t i=0; i < ndat; i++) {
239 fmuen[i] = med_data[med_name][i].second;
240 }
241
242 EGS_Interpolator *muen = new EGS_Interpolator(ndat, log(emin), log(emax), fmuen);
243 delete [] fmuen;
244
245 return muen;
246
247 }
248};
249
250
251const string MuenDataParser::MUEN_START = "Muen values for medium MEDIUM =";
252
253}
254
255#endif
class for parsing muen data from a file.
Definition muen.h:131
int setMuenFile(string filename)
Definition muen.h:207
MuenMapT splitFileByMed(ifstream &in)
does the actual parsing of data from the muen file
Definition muen.h:141
static const int NSKIP
Definition muen.h:135
EGS_Interpolator * getMuenInterpolator(string med_name)
Create a new EGS_Interpolator of muen data for the requested medium and return pointer to it....
Definition muen.h:228
static const string MUEN_START
Definition muen.h:193
MuenDataParser()
construct class by parsing the data file. Note the actual EGS_Interpolator classes are only created w...
Definition muen.h:205
MuenMapT med_data
Definition muen.h:133
Definition muen.h:62
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
Split a string on input delimeter.
Definition muen.h:68
map< string, vector< MuenAtET > > MuenMapT
Map from medium name to vector of (e, muen(e)) data for that medium.
Definition muen.h:107
pair< double, double > MuenAtET
pair of form (energy, muen(energy))
Definition muen.h:104