Branch data Line data Source code
1 : : /**
2 : : * \deprecarted
3 : : * \file generate_scripts.cxx \brief Generating scripts implementations
4 : : *
5 : : * This source file implements the different methods to generate FullMonte TCL scripts in the src_placer class
6 : : *
7 : : * @author: Abed Yassine
8 : : * @date: April 4th, 2017
9 : : *
10 : : * // TODO Deprecate this
11 : : */
12 : :
13 : : #include "src_placer.h"
14 : :
15 : : /* This function generates a tcl script to run FullMonte with the input source positions given as tetrahedra
16 : : * The script generates a path tracing of the light inside the mesh
17 : : */
18 : 0 : void src_placer::make_tcl_script_traces(string out_file_name, string mesh_path, const vector<tetrahedron* > & sources)
19 : : //const vector<Point*> & sources)
20 : : {
21 [ # # ]: 0 : fprintf(stderr, "\033[1;%dmCreating a TCL FullMonte Path Tracing Script: %s\033[0m\n", 33, out_file_name.c_str());
22 : :
23 [ # # ]: 0 : if (sources.size() == 0)
24 : : {
25 [ # # ]: 0 : fprintf(stderr, "\033[1;%dmNo sources provided! Exiting... \033[0m\n", 31);
26 : 0 : exit(-1);
27 : : }
28 : :
29 : : // Creating output file
30 [ # # ]: 0 : stringstream out_file_ss;
31 [ # # ][ # # ]: 0 : out_file_ss << "TCL_scripts/" << out_file_name;
32 [ # # ][ # # ]: 0 : FILE* outfile = fopen(out_file_ss.str().c_str(), "w");
33 : :
34 : : // Importing VTK TCK Package
35 [ # # ]: 0 : fprintf(outfile, "##### Import VTK TCL Package \n");
36 [ # # ]: 0 : fprintf(outfile, "package require vtk \n \n");
37 : :
38 : : // Close the default TCL/TK window
39 [ # # ]: 0 : fprintf(outfile, "##### Close the default tcl/tk window\n");
40 [ # # ]: 0 : fprintf(outfile, "wm withdraw .\n\n");
41 : :
42 : : // Load required packages
43 [ # # ]: 0 : fprintf(outfile, "##### Loading the required packages\n\n");
44 : :
45 [ # # ]: 0 : fprintf(outfile, "##### TIMOS format reader (for Digimouse) with TCL bindings\n");
46 [ # # ]: 0 : fprintf(outfile, "load libFullMonteTIMOSTCL.so\n\n");
47 : :
48 [ # # ]: 0 : fprintf(outfile, "##### Geometry model with TCL bindings\n");
49 [ # # ]: 0 : fprintf(outfile, "load libFullMonteGeometryTCL.so\n\n");
50 : :
51 [ # # ]: 0 : fprintf(outfile, "##### Software kernels with TCL bindings\n");
52 [ # # ]: 0 : fprintf(outfile, "load libFullMonteSWKernelTCL.so\n");
53 [ # # ]: 0 : fprintf(outfile, "load libFullMonteKernelsTCL.so\n\n");
54 : :
55 [ # # ]: 0 : fprintf(outfile, "##### Data output manipulation\n");
56 [ # # ]: 0 : fprintf(outfile, "load libFullMonteDataTCL.so\n");
57 [ # # ]: 0 : fprintf(outfile, "load libFullMonteQueriesTCL.so\n\n");
58 : :
59 [ # # ]: 0 : fprintf(outfile, "##### VTK interface\n");
60 [ # # ]: 0 : fprintf(outfile, "load libvtkFullMonteTCL-6.3.so\n");
61 [ # # ]: 0 : fprintf(outfile, "load libFullMonteVTKFileTCL.so\n\n");
62 : :
63 [ # # ]: 0 : fprintf(outfile, "puts \"loaded libs\" \n\n");
64 : :
65 : : // File parameters
66 [ # # ]: 0 : fprintf(outfile, "##### Basic parameters: file name \n\n");
67 : :
68 [ # # ]: 0 : fprintf(outfile, "#default file prefix\n");
69 [ # # ]: 0 : fprintf(outfile, "set pfx \"%s\"\n\n", mesh_path.c_str());
70 : :
71 [ # # ]: 0 : fprintf(outfile, "##### override with 1st cmdline arg\n");
72 [ # # ]: 0 : fprintf(outfile, "if { $argc >= 1 } { set pfx [lindex $argv 0] }\n\n");
73 : :
74 : : // Setting optical properties and mesh files
75 [ # # ]: 0 : fprintf(outfile, "set optfn \"$pfx.opt\"\n");
76 [ # # ]: 0 : fprintf(outfile, "set meshfn \"$pfx.mesh\"\n");
77 [ # # ]: 0 : fprintf(outfile, "set legendfn \"$pfx.legend\"\n");
78 [ # # ]: 0 : fprintf(outfile, "set ofn \"fluence.out\"\n\n");
79 : :
80 : : // Reading problem definitions in TIMOS format
81 [ # # ]: 0 : fprintf(outfile, "###### Read problem definition in TIMOS format\n");
82 [ # # ]: 0 : fprintf(outfile, "TIMOSAntlrParser R\n\n");
83 : :
84 [ # # ]: 0 : fprintf(outfile, "R setMeshFileName $meshfn\n");
85 [ # # ]: 0 : fprintf(outfile, "R setOpticalFileName $optfn\n\n");
86 : : // fprintf(outfile, "R setLegendFileName $legendfn\n\n");
87 : :
88 [ # # ]: 0 : fprintf(outfile, "set mesh [R mesh]\n");
89 [ # # ]: 0 : fprintf(outfile, "set opt [R materials_simple]\n\n");
90 : :
91 : :
92 : : // Configuring sources
93 [ # # ]: 0 : fprintf(outfile, "##### Configuring Sources\n");
94 : : // The sources are volume elements
95 : : // int tetra_idx;
96 [ # # ]: 0 : for (unsigned int i = 0; i < sources.size(); i++)
97 : : {
98 : : // tetra_idx = sources[i]->get_id();
99 : :
100 [ # # ]: 0 : stringstream source_name;
101 [ # # ][ # # ]: 0 : source_name << "V" << i;
102 : :
103 [ # # ]: 0 : SP_Point centroid = sources[i]->compute_centroid_of();
104 : : // sources are generated with unit weighy
105 : : //fprintf(outfile, "Volume %s 1.0 %d\n", source_name.str().c_str(), tetra_idx);
106 [ # # ][ # # ]: 0 : fprintf(outfile, "Ball %s\n", source_name.str().c_str());
107 [ # # ]: 0 : fprintf(outfile, "%s centre \"%g %g %g\"\n", source_name.str().c_str(),
108 [ # # ][ # # ]: 0 : centroid.get_xcoord(), centroid.get_ycoord(), centroid.get_zcoord());
[ # # # # ]
109 [ # # ][ # # ]: 0 : fprintf(outfile, "%s radius 0.5\n", source_name.str().c_str());
110 : : }
111 [ # # ]: 0 : fprintf(outfile, "\n");
112 : :
113 : : // if number of sources is more than 1, we have to create a composite source
114 [ # # ]: 0 : if (sources.size() > 1)
115 : : {
116 [ # # ]: 0 : fprintf(outfile, "Composite C");
117 [ # # ]: 0 : for (unsigned int i = 0; i < sources.size(); i++)
118 : : {
119 [ # # ]: 0 : stringstream source_name;
120 [ # # ][ # # ]: 0 : source_name << "V" << i;
121 : :
122 [ # # ][ # # ]: 0 : fprintf(outfile, "C add %s\n", source_name.str().c_str());
123 : : }
124 [ # # ]: 0 : fprintf(outfile, "\n");
125 : : }
126 : :
127 : : // Configuring simulation kernel
128 [ # # ]: 0 : fprintf(outfile, "##### Create and configure simulation kernel with surface scoring\n");
129 : :
130 [ # # ]: 0 : fprintf(outfile, "TetraTraceKernel k $mesh\n\n");
131 : :
132 : 0 : string source_name;
133 [ # # ]: 0 : if (sources.size() == 1)
134 : : {
135 [ # # ]: 0 : source_name = "V0";
136 : : }
137 : : else
138 : : {
139 [ # # ]: 0 : source_name = "C";
140 : : }
141 : :
142 [ # # ]: 0 : fprintf(outfile, "k source %s\n # the source to launch from \n", source_name.c_str());
143 [ # # ]: 0 : fprintf(outfile, "k energy 1\n # total energy\n"); // Candidate sources are all of unit power
144 [ # # ]: 0 : fprintf(outfile, "k materials $opt\n # materials\n");
145 : : fprintf(outfile, "k setUnitsToMM\n # units for mesh dimensions "
146 [ # # ]: 0 : "& optical properties (must match each other)\n");
147 : :
148 : : // Monte carlo kernel properties
149 [ # # ]: 0 : fprintf(outfile, "\n##### Monte Carlo kernel properties\n");
150 [ # # ]: 0 : fprintf(outfile, "k roulettePrWin 0.1\n # probability of roulette win\n");
151 : : fprintf(outfile, "k rouletteWMin 1e-5\n # minimum weight "
152 [ # # ]: 0 : "\"wmin\" before roulette takes effect\n");
153 : : fprintf(outfile, "k maxSteps 10000\n # maximum"
154 [ # # ]: 0 : " number of steps to trace a packet\n");
155 : : fprintf(outfile, "k maxHits 100\n # maximum number"
156 [ # # ]: 0 : "of boundaries a single step can take\n");
157 : : fprintf(outfile, "k packetCount 1000000\n"
158 : : " # number of packets to simulate"
159 [ # # ]: 0 : " (more -> better quality, longer run)\n");
160 : : fprintf(outfile, "k threadCount 8\n # number of threads "
161 [ # # ]: 0 : "(set to number of cores, or 2x number of cores if hyperthreading\n\n");
162 : :
163 : :
164 : : // Defining progress timer call back function for use during simulation run
165 [ # # ]: 0 : fprintf(outfile, "##### Define progress timer callback function for use during simulation run\n");
166 : :
167 : : fprintf(outfile, "\nproc progresstimer {} {\n"
168 : : " # loop while not finished\n"
169 : : " while { ![k done] } {\n"
170 : : " # display %% completed to 2 decimal places\n"
171 : : " puts -nonewline [format \"\\rProgress %%6.2f%%%%\" [expr 100.0*[k progressFraction]]]\n"
172 : : " flush stdout\n\n"
173 : :
174 : : " # refresh interval: 200ms\n"
175 : : " after 200\n"
176 : : " }\n"
177 : : " puts [format \"\\rProgress %%6.2f%%%%\" 100.0]\n"
178 [ # # ]: 0 : "}\n\n");
179 : :
180 : : // Running the kernel
181 [ # # ]: 0 : fprintf(outfile, "##### Run it\n\n ##### Run Kernel, display progress timer, and awaut finish\n");
182 [ # # ]: 0 : fprintf(outfile, " k startAsync\n progresstimer\n k finishAsync\n\n");
183 : :
184 [ # # ]: 0 : stringstream vtk_file_name;
185 [ # # ][ # # ]: 0 : vtk_file_name << out_file_name.substr(0, out_file_name.length()-4) << "_traces.vtk";
[ # # ]
186 : 0 : fprintf(outfile, "for { set i 0 } { $i < [k getResultCount] } { incr i } { puts \""
187 : : "[[k getResultByIndex $i] typeString]\" }\n\n"
188 : : "set paths [k getResultByTypeString \"PacketPositionTraceSet\"]\n\n"
189 : :
190 : : "puts \"paths=$paths\"\n"
191 : : // "puts \"Returned a path with [$paths nTraces] traces and a total"
192 : : // " of [$paths nPoints] points\"\n\n"
193 : :
194 : : "vtkFullMontePacketPositionTraceSetToPolyData O\n"
195 : : " O source $paths\n"
196 : : " O update\n\n"
197 : :
198 : : "vtkPolyDataWriter W\n"
199 : : " W SetInputData [O getPolyData]\n"
200 : : " W SetFileName \"%s\"\n"
201 : : " W Update\n"
202 : : " W Delete\n"
203 [ # # # # ]: 0 : "exit", vtk_file_name.str().c_str());
204 : :
205 : : // closing the file
206 [ # # ]: 0 : fclose(outfile);
207 : 0 : }
208 : :
209 : : /* This function generates a tcl script to run FullMonte with the input source positions given as tetrahedra
210 : : * The script outputs the amount of energy absorbed inside each tetrahedron
211 : : */
212 : 0 : void src_placer::make_tcl_script_absorption_multiple_sources(string out_file_name,
213 : : string mesh_path, const vector<tetrahedron* > & sources,
214 : : const vector<float>& source_powers_to_run,
215 : : bool read_mesh_from_vtk) //const vector<Point*> & sources)
216 : : {
217 [ # # ]: 0 : fprintf(stderr, "\033[1;%dm\nCreating a TCL FullMonte Absorption Script: %s\033[0m\n", 33, out_file_name.c_str());
218 : :
219 [ # # ]: 0 : if (sources.size() == 0)
220 : : {
221 [ # # ]: 0 : fprintf(stderr, "\033[1;%dmNo sources provided! Exiting... \033[0m\n", 31);
222 : 0 : exit(-1);
223 : : }
224 : :
225 : : // Creating output file
226 [ # # ]: 0 : stringstream out_file_ss;
227 [ # # ][ # # ]: 0 : out_file_ss << "TCL_scripts/" << out_file_name;
228 [ # # ][ # # ]: 0 : FILE* outfile = fopen(out_file_ss.str().c_str(), "w");
229 : :
230 : : // Importing VTK TCK Package
231 [ # # ]: 0 : fprintf(outfile, "##### Import VTK TCL Package \n");
232 [ # # ]: 0 : fprintf(outfile, "package require vtk \n \n");
233 : :
234 : : // Close the default TCL/TK window
235 [ # # ]: 0 : fprintf(outfile, "##### Close the default tcl/tk window\n");
236 [ # # ]: 0 : fprintf(outfile, "wm withdraw .\n\n");
237 : :
238 : : // Load required packages
239 [ # # ]: 0 : fprintf(outfile, "##### Loading the required packages\n\n");
240 : :
241 [ # # ]: 0 : fprintf(outfile, "##### TIMOS format reader (for Digimouse) with TCL bindings\n");
242 [ # # ]: 0 : fprintf(outfile, "load libFullMonteTIMOSTCL.so\n\n");
243 : :
244 [ # # ]: 0 : fprintf(outfile, "##### Geometry model with TCL bindings\n");
245 [ # # ]: 0 : fprintf(outfile, "load libFullMonteGeometryTCL.so\n\n");
246 : :
247 [ # # ]: 0 : fprintf(outfile, "##### Software kernels with TCL bindings\n");
248 [ # # ]: 0 : fprintf(outfile, "load libFullMonteSWKernelTCL.so\n");
249 [ # # ]: 0 : fprintf(outfile, "load libFullMonteKernelsTCL.so\n\n");
250 : :
251 [ # # ]: 0 : fprintf(outfile, "##### Data output manipulation\n");
252 [ # # ]: 0 : fprintf(outfile, "load libFullMonteDataTCL.so\n");
253 [ # # ]: 0 : fprintf(outfile, "load libFullMonteQueriesTCL.so\n\n");
254 : :
255 [ # # ]: 0 : fprintf(outfile, "##### VTK interface\n");
256 [ # # ]: 0 : fprintf(outfile, "load libvtkFullMonteTCL-6.3.so\n");
257 [ # # ]: 0 : fprintf(outfile, "load libFullMonteVTKFileTCL.so\n\n");
258 : :
259 [ # # ]: 0 : fprintf(outfile, "puts \"loaded libs\" \n\n");
260 : :
261 : : // File parameters
262 [ # # ]: 0 : fprintf(outfile, "##### Basic parameters: file name \n\n");
263 : :
264 [ # # ]: 0 : fprintf(outfile, "#default file prefix\n");
265 [ # # ]: 0 : fprintf(outfile, "set pfx \"%s\"\n\n", mesh_path.c_str());
266 : :
267 [ # # ]: 0 : fprintf(outfile, "##### override with 1st cmdline arg\n");
268 [ # # ]: 0 : fprintf(outfile, "if { $argc >= 1 } { set pfx [lindex $argv 0] }\n\n");
269 : :
270 : : // Setting optical properties and mesh files
271 [ # # ]: 0 : fprintf(outfile, "set optfn \"$pfx.opt\"\n");
272 [ # # ]: 0 : if (read_mesh_from_vtk)
273 [ # # ]: 0 : fprintf(outfile, "set meshfn \"$pfx.vtk\"\n");
274 : : else
275 [ # # ]: 0 : fprintf(outfile, "set meshfn \"$pfx.mesh\"\n");
276 [ # # ]: 0 : fprintf(outfile, "set legendfn \"$pfx.legend\"\n");
277 [ # # ]: 0 : fprintf(outfile, "set ofn \"fluence.out\"\n\n");
278 : :
279 : :
280 : : // Reading problem definitions in TIMOS or VTK format
281 [ # # ]: 0 : if (read_mesh_from_vtk)
282 : : {
283 [ # # ]: 0 : fprintf(outfile, "#### Read Problem Mesh in VTK fromat\n");
284 : : fprintf(outfile, "VTKLegacyReader VTKR\n"
285 : : " VTKR setFileName $meshfn\n"
286 : : " VTKR addZeroPoint 1\n"
287 [ # # ]: 0 : " VTKR addZeroCell 1\n");
288 : :
289 : : fprintf(outfile, "set MB [VTKR mesh]\n"
290 [ # # ]: 0 : "set mesh [TetraMesh foo $MB]\n");
291 : : }
292 [ # # ]: 0 : fprintf(outfile, "###### Read problem definition in TIMOS format\n");
293 [ # # ]: 0 : fprintf(outfile, "TIMOSAntlrParser R\n\n");
294 : :
295 [ # # ]: 0 : if (!read_mesh_from_vtk)
296 [ # # ]: 0 : fprintf(outfile, "R setMeshFileName $meshfn\n");
297 [ # # ]: 0 : fprintf(outfile, "R setOpticalFileName $optfn\n\n");
298 : : // fprintf(outfile, "R setLegendFileName $legendfn\n\n");
299 : :
300 [ # # ]: 0 : if (!read_mesh_from_vtk)
301 [ # # ]: 0 : fprintf(outfile, "set mesh [R mesh]\n");
302 [ # # ]: 0 : fprintf(outfile, "set opt [R materials_simple]\n\n");
303 : :
304 : :
305 : : // Configuring sources
306 [ # # ]: 0 : fprintf(outfile, "##### Configuring Sources\n");
307 : : // The sources are volume elements
308 : : // int tetra_idx;
309 : : assert(sources.size() == source_powers_to_run.size());
310 [ # # ]: 0 : for (unsigned int i = 0; i < sources.size(); i++)
311 : : {
312 : : // tetra_idx = sources[i]->get_id();
313 : :
314 [ # # ]: 0 : stringstream source_name;
315 [ # # ][ # # ]: 0 : source_name << "V" << i;
316 : :
317 [ # # ]: 0 : SP_Point centroid = sources[i]->compute_centroid_of();
318 : : // sources are generated with unit weight
319 : : // fprintf(outfile, "Volume %s %f %d\n", source_name.str().c_str(), source_powers_to_run[i], tetra_idx);
320 : : // fprintf(outfile, "Volume %s %d\n", source_name.str().c_str(), tetra_idx);
321 : :
322 : : // Making point sources
323 [ # # ][ # # ]: 0 : fprintf(outfile, "Point %s\n", source_name.str().c_str());
324 [ # # ][ # # ]: 0 : fprintf(outfile, "%s position \"%g %g %g\"\n", source_name.str().c_str(), centroid.get_xcoord(),
325 [ # # ]: 0 : centroid.get_ycoord(), centroid.get_zcoord());
[ # # # # ]
326 : :
327 : :
328 : :
329 : : // fprintf(outfile, "Ball %s\n", source_name.str().c_str());
330 : : // fprintf(outfile, "%s centre \"%g %g %g\"\n", source_name.str().c_str(),
331 : : // centroid->get_xcoord(), centroid->get_ycoord(), centroid->get_zcoord());
332 : : // fprintf(outfile, "%s radius 0.5\n", source_name.str().c_str());
333 : : }
334 [ # # ]: 0 : fprintf(outfile, "\n");
335 : :
336 : : // if number of sources is more than 1, we have to create a composite source
337 [ # # ]: 0 : if (sources.size() > 1)
338 : : {
339 [ # # ]: 0 : fprintf(outfile, "Composite C\n");
340 [ # # ]: 0 : for (unsigned int i = 0; i < sources.size(); i++)
341 : : {
342 [ # # ]: 0 : stringstream source_name;
343 [ # # ][ # # ]: 0 : source_name << "V" << i;
344 : :
345 [ # # ][ # # ]: 0 : fprintf(outfile, "C add %f %s\n", source_powers_to_run[i], source_name.str().c_str());
346 : : }
347 [ # # ]: 0 : fprintf(outfile, "\n");
348 : : }
349 : :
350 : : // Configuring simulation kernel
351 [ # # ]: 0 : fprintf(outfile, "##### Create and configure simulation kernel with surface scoring\n");
352 : :
353 [ # # ]: 0 : fprintf(outfile, "TetraSVKernel k $mesh\n\n");
354 : :
355 : 0 : string source_name;
356 [ # # ]: 0 : if (sources.size() == 1)
357 : : {
358 [ # # ]: 0 : source_name = "V0";
359 : : }
360 : : else
361 : : {
362 [ # # ]: 0 : source_name = "C";
363 : : }
364 : :
365 [ # # ]: 0 : fprintf(outfile, "k source %s\n # the source to launch from \n", source_name.c_str());
366 [ # # ]: 0 : fprintf(outfile, "k energy 1\n # total energy\n"); // Candidate sources are all of unit power
367 [ # # ]: 0 : fprintf(outfile, "k materials $opt\n # materials\n");
368 : : fprintf(outfile, "k setUnitsToMM\n # units for mesh dimensions "
369 [ # # ]: 0 : "& optical properties (must match each other)\n");
370 : :
371 : : // Monte carlo kernel properties
372 [ # # ]: 0 : fprintf(outfile, "\n##### Monte Carlo kernel properties\n");
373 [ # # ]: 0 : fprintf(outfile, "k roulettePrWin 0.1\n # probability of roulette win\n");
374 : : fprintf(outfile, "k rouletteWMin 1e-5\n # minimum weight "
375 [ # # ]: 0 : "\"wmin\" before roulette takes effect\n");
376 : : fprintf(outfile, "k maxSteps 10000\n # maximum"
377 [ # # ]: 0 : " number of steps to trace a packet\n");
378 : : fprintf(outfile, "k maxHits 100\n # maximum number"
379 [ # # ]: 0 : "of boundaries a single step can take\n");
380 : : fprintf(outfile, "k packetCount 1000000\n"
381 : : " # number of packets to simulate"
382 [ # # ]: 0 : " (more -> better quality, longer run)\n");
383 : : fprintf(outfile, "k threadCount 8\n # number of threads "
384 [ # # ]: 0 : "(set to number of cores, or 2x number of cores if hyperthreading\n\n");
385 : :
386 : :
387 : : // Defining progress timer call back function for use during simulation run
388 [ # # ]: 0 : fprintf(outfile, "##### Define progress timer callback function for use during simulation run\n");
389 : :
390 : : fprintf(outfile, "\nproc progresstimer {} {\n"
391 : : " # loop while not finished\n"
392 : : " while { ![k done] } {\n"
393 : : " # display %% completed to 2 decimal places\n"
394 : : " puts -nonewline [format \"\\rProgress %%6.2f%%%%\" [expr 100.0*[k progressFraction]]]\n"
395 : : " flush stdout\n\n"
396 : :
397 : : " # refresh interval: 200ms\n"
398 : : " after 200\n"
399 : : " }\n"
400 : : " puts [format \"\\rProgress %%6.2f%%%%\" 100.0]\n"
401 [ # # ]: 0 : "}\n\n");
402 : :
403 : : // Setting up internal fluence counting
404 [ # # ]: 0 : stringstream vtk_v_file_name;
405 [ # # ][ # # ]: 0 : vtk_v_file_name << out_file_name.substr(0, out_file_name.length()-4) << "_volume.vtk";
[ # # ]
406 : 0 : fprintf(outfile, "##### Set up internal fluence counting\n"
407 : : "TriFilterRegionBounds TF\n"
408 : : " TF mesh $mesh\n"
409 : : " TF bidirectional 1\n\n"
410 : :
411 : : "##### designate regions whose boundaries should be monitored\n"
412 : : " TF includeRegion 4 1\n"
413 : : " TF includeRegion 3 1\n\n"
414 : :
415 : : "vtkFullMonteArrayAdaptor vtkPhiV\n\n"
416 : :
417 : : "TetraFilterByRegion MF\n"
418 : : " MF mesh $mesh\n"
419 : : " MF include 4 1\n\n"
420 : :
421 : : "$mesh setFacesForFluenceCounting TF\n\n"
422 : : "vtkFullMonteTetraMeshWrapper VTKM\n"
423 : : " VTKM mesh $mesh\n\n"
424 : :
425 : : "##### Writer pipeline for volume field data (regions & vol fluence)\n"
426 : : "vtkFieldData volumeFieldData\n"
427 : : " volumeFieldData AddArray [VTKM regions]\n\n"
428 : :
429 : : "vtkDataObject volumeDataObject\n"
430 : : " volumeDataObject SetFieldData volumeFieldData\n\n"
431 : :
432 : : "vtkMergeDataObjectFilter mergeVolume\n"
433 : : " mergeVolume SetDataObjectInputData volumeDataObject\n"
434 : : " mergeVolume SetInputData [VTKM blankMesh]\n"
435 : : " mergeVolume SetOutputFieldToCellDataField\n\n"
436 : :
437 : : "vtkUnstructuredGridWriter VW\n"
438 : : " VW SetInputConnection [mergeVolume GetOutputPort]\n"
439 : : " VW SetFileName \"%s\"\n\n"
440 : :
441 : : "EnergyToFluence EVF\n"
442 : : " EVF mesh $mesh\n"
443 [ # # # # ]: 0 : " EVF materials $opt\n\n", vtk_v_file_name.str().c_str());
444 : :
445 : :
446 : : /*fprintf(outfile, "## Writer pipeline for surface field data (organ-surface fluence)\n"
447 : : "vtkFullMonteSpatialMapWrapperFU vtkPhi\n\n"
448 : :
449 : : "set surfaceFluenceArray [vtkPhi array]\n"
450 : : " $surfaceFluenceArray SetName \"Surface Fluence (au)\"\n\n"
451 : :
452 : : "vtkFieldData surfaceFieldData\n"
453 : : " surfaceFieldData AddArray $surfaceFluenceArray\n\n"
454 : :
455 : : "puts \"surfaceFieldData size: [surfaceFieldData GetNumberOfTuples]\"\n"
456 : : "puts \"number of faces: [[VTKM faces] GetNumberOfCells]\"\n\n"
457 : :
458 : : "vtkDataObject surfaceData\n"
459 : : " surfaceData SetFieldData surfaceFieldData\n\n"
460 : :
461 : : "vtkMergeDataObjectFilter mergeFluence\n"
462 : : " mergeFluence SetDataObjectInputData surfaceData\n"
463 : : " mergeFluence SetInputData [VTKM faces]\n"
464 : : " mergeFluence SetOutputFieldToCellDataField\n\n"
465 : :
466 : : "vtkFullMonteFilterTovtkIdList surfaceTriIDs\n"
467 : : " surfaceTriIDs mesh $mesh\n"
468 : : " surfaceTriIDs filter [TF self]\n\n"
469 : :
470 : :
471 : : "vtkExtractCells extractSurface\n"
472 : : " extractSurface SetInputConnection [mergeFluence GetOutputPort]\n"
473 : : " extractSurface SetCellList [surfaceTriIDs idList]\n\n"
474 : :
475 : : "vtkGeometryFilter geom\n"
476 : : " geom SetInputConnection [extractSurface GetOutputPort]\n\n"
477 : :
478 : : "vtkPolyDataWriter VTKW\n"
479 : : " VTKW SetInputConnection [geom GetOutputPort]\n\n"
480 : :
481 : : "DoseSurfaceHistogramGenerator DSHG\n"
482 : : " DSHG mesh $mesh\n"
483 : : " DSHG filter TF\n\n"
484 : :
485 : : "DoseVolumeHistogramGenerator DVHG\n"
486 : : " DVHG mesh $mesh\n"
487 : : " DVHG filter MF\n\n"
488 : :
489 : : "BidirectionalFluence BF\n\n"
490 : :
491 : : "FluenceConverter FC\n"
492 : : " FC mesh $mesh\n"
493 : : " FC materials $opt\n\n");
494 : : // */
495 : :
496 : :
497 : : // Running the kernel
498 [ # # ]: 0 : fprintf(outfile, "##### Run it\n\n ##### Run Kernel, display progress timer, and awaut finish\n");
499 [ # # ]: 0 : fprintf(outfile, " k startAsync\n progresstimer\n k finishAsync\n\n");
500 : :
501 : :
502 : : // Gathering data
503 [ # # ]: 0 : stringstream vtk_file_name;
504 [ # # ][ # # ]: 0 : vtk_file_name << "TCL_scripts/" << out_file_name.substr(0, out_file_name.length()-4) << "_volume1.vtk";
[ # # ][ # # ]
505 : :
506 : : /*
507 : : fprintf(outfile, "BF source [k getInternalSurfaceFluenceMap]\n\n"
508 : :
509 : : "set Emap [FC convertToEnergyDensity [k getVolumeAbsorbedEnergyMap]]\n\n"
510 : :
511 : : "set phiV [FC convertToFluence [k getVolumeAbsorbedEnergyMap]]\n\n"
512 : :
513 : : "vtkFullMonteArrayAdaptor EmapAdaptor\n"
514 : : " EmapAdaptor source $Emap\n\n"
515 : :
516 : : "vtkFullMonteArrayAdaptor PhiAdaptor\n"
517 : : " PhiAdaptor source [k getVolumeFluenceMap]\n\n"
518 : :
519 : : "puts \"Emap = $Emap\"\n\n"
520 : :
521 : : "set phi [BF result]\n\n"
522 : :
523 : : "vtkPhi source $phi\n"
524 : : "vtkPhi update\n\n"
525 : :
526 : :
527 : : "DSHG fluence $phi\n"
528 : : "set dsh [DSHG result]\n\n"
529 : :
530 : : "puts \"DSH generated\"\n\n"
531 : :
532 : : "volumeFieldData AddArray [EmapAdaptor result]\n"
533 : : "volumeFieldData AddArray [PhiAdaptor result]\n\n"
534 : :
535 : : "$dsh print\n\n"
536 : :
537 : : "DVHG fluence $phiV\n"
538 : : "set dvh [DVHG result]\n\n"
539 : :
540 : : "puts \"DVH generated\"\n\n"
541 : :
542 : : "$dvh print\n\n"
543 : :
544 : : "VTKW SetFileName \"%s\"\n"
545 : : "VTKW Update\n"
546 : :
547 : : "VW Update", vtk_file_name.str().c_str());
548 : : // */
549 : :
550 : 0 : fprintf(outfile, "EVF source [k getResultByIndex 2]\n"
551 : : "EVF update\n\n"
552 : :
553 : : "vtkPhiV source [EVF result]\n"
554 : : "volumeFieldData AddArray [vtkPhiV array]\n"
555 : : " [vtkPhiV array] SetName \"Fluence\"\n\n"
556 : :
557 : : "VW SetFileName \"%s\"\n"
558 : : "VW Update\n"
559 [ # # # # ]: 0 : "exit", vtk_file_name.str().c_str());
560 : :
561 : : // closing the file
562 [ # # ]: 0 : fclose(outfile);
563 [ # # ]: 0 : fprintf(stderr, "\033[1;%dmScript Generated!\033[0m\n\n", 36);
564 : 0 : }
565 : :
566 : : /* This function generates a tcl script to run FullMonte with the input source positions given as tetrahedra
567 : : * (one source at a time)
568 : : * The script outputs the amount of energy absorbed inside each tetrahedron
569 : : */
570 : 0 : void src_placer::make_tcl_script_absorption(string out_file_name,
571 : : string mesh_path, const vector<tetrahedron* > & sources,
572 : : bool read_mesh_from_vtk) //const vector<Point*> & sources)
573 : : {
574 [ # # ]: 0 : fprintf(stderr, "\033[1;%dm\nCreating a TCL FullMonte Absorption Script: %s\033[0m\n", 33, out_file_name.c_str());
575 : :
576 [ # # ]: 0 : if (sources.size() == 0)
577 : : {
578 [ # # ]: 0 : fprintf(stderr, "\033[1;%dmNo sources provided! Exiting... \033[0m\n", 31);
579 : 0 : exit(-1);
580 : : }
581 : :
582 : : // Creating output file
583 [ # # ]: 0 : stringstream out_file_ss;
584 [ # # ][ # # ]: 0 : out_file_ss << "TCL_scripts/" << out_file_name;
585 [ # # ][ # # ]: 0 : FILE* outfile = fopen(out_file_ss.str().c_str(), "w");
586 : :
587 : : // Importing VTK TCK Package
588 [ # # ]: 0 : fprintf(outfile, "##### Import VTK TCL Package \n");
589 [ # # ]: 0 : fprintf(outfile, "package require vtk \n \n");
590 : :
591 : : // Close the default TCL/TK window
592 [ # # ]: 0 : fprintf(outfile, "##### Close the default tcl/tk window\n");
593 [ # # ]: 0 : fprintf(outfile, "wm withdraw .\n\n");
594 : :
595 : : // Load required packages
596 [ # # ]: 0 : fprintf(outfile, "##### Loading the required packages\n\n");
597 : :
598 [ # # ]: 0 : fprintf(outfile, "##### TIMOS format reader (for Digimouse) with TCL bindings\n");
599 [ # # ]: 0 : fprintf(outfile, "load libFullMonteTIMOSTCL.so\n\n");
600 : :
601 [ # # ]: 0 : fprintf(outfile, "##### Geometry model with TCL bindings\n");
602 [ # # ]: 0 : fprintf(outfile, "load libFullMonteGeometryTCL.so\n\n");
603 : :
604 [ # # ]: 0 : fprintf(outfile, "##### Software kernels with TCL bindings\n");
605 [ # # ]: 0 : fprintf(outfile, "load libFullMonteSWKernelTCL.so\n");
606 [ # # ]: 0 : fprintf(outfile, "load libFullMonteKernelsTCL.so\n\n");
607 : :
608 [ # # ]: 0 : fprintf(outfile, "##### Data output manipulation\n");
609 [ # # ]: 0 : fprintf(outfile, "load libFullMonteDataTCL.so\n");
610 [ # # ]: 0 : fprintf(outfile, "load libFullMonteQueriesTCL.so\n\n");
611 : :
612 [ # # ]: 0 : fprintf(outfile, "##### VTK interface\n");
613 [ # # ]: 0 : fprintf(outfile, "load libvtkFullMonteTCL-6.3.so\n");
614 [ # # ]: 0 : fprintf(outfile, "load libFullMonteVTKFileTCL.so\n\n");
615 : :
616 [ # # ]: 0 : fprintf(outfile, "puts \"loaded libs\" \n\n");
617 : :
618 : : // File parameters
619 [ # # ]: 0 : fprintf(outfile, "##### Basic parameters: file name \n\n");
620 : :
621 [ # # ]: 0 : fprintf(outfile, "#default file prefix\n");
622 [ # # ]: 0 : fprintf(outfile, "set pfx \"%s\"\n\n", mesh_path.c_str());
623 : :
624 [ # # ]: 0 : fprintf(outfile, "##### override with 1st cmdline arg\n");
625 [ # # ]: 0 : fprintf(outfile, "if { $argc >= 1 } { set pfx [lindex $argv 0] }\n\n");
626 : :
627 : : // Setting optical properties and mesh files
628 [ # # ]: 0 : fprintf(outfile, "set optfn \"$pfx.opt\"\n");
629 [ # # ]: 0 : if (read_mesh_from_vtk)
630 [ # # ]: 0 : fprintf(outfile, "set meshfn \"$pfx.vtk\"\n");
631 : : else
632 [ # # ]: 0 : fprintf(outfile, "set meshfn \"$pfx.mesh\"\n");
633 [ # # ]: 0 : fprintf(outfile, "set legendfn \"$pfx.legend\"\n");
634 [ # # ]: 0 : fprintf(outfile, "set ofn \"fluence.out\"\n\n");
635 : :
636 : :
637 : : // Reading problem definitions in TIMOS or VTK format
638 [ # # ]: 0 : if (read_mesh_from_vtk)
639 : : {
640 [ # # ]: 0 : fprintf(outfile, "#### Read Problem Mesh in VTK fromat\n");
641 : : fprintf(outfile, "VTKLegacyReader VTKR\n"
642 : : " VTKR setFileName $meshfn\n"
643 : : " VTKR addZeroPoint 1\n"
644 [ # # ]: 0 : " VTKR addZeroCell 1\n");
645 : :
646 : : fprintf(outfile, "set MB [VTKR mesh]\n"
647 [ # # ]: 0 : "set mesh [TetraMesh foo $MB]\n");
648 : : }
649 [ # # ]: 0 : fprintf(outfile, "###### Read problem definition in TIMOS format\n");
650 [ # # ]: 0 : fprintf(outfile, "TIMOSAntlrParser R\n\n");
651 : :
652 [ # # ]: 0 : if (!read_mesh_from_vtk)
653 [ # # ]: 0 : fprintf(outfile, "R setMeshFileName $meshfn\n");
654 [ # # ]: 0 : fprintf(outfile, "R setOpticalFileName $optfn\n\n");
655 : : // fprintf(outfile, "R setLegendFileName $legendfn\n\n");
656 : :
657 [ # # ]: 0 : if (!read_mesh_from_vtk)
658 [ # # ]: 0 : fprintf(outfile, "set mesh [R mesh]\n");
659 [ # # ]: 0 : fprintf(outfile, "set opt [R materials_simple]\n\n");
660 : :
661 : :
662 : : // Configuring simulation kernel
663 [ # # ]: 0 : fprintf(outfile, "##### Create and configure simulation kernel with surface scoring\n");
664 : :
665 [ # # ]: 0 : fprintf(outfile, "TetraSVKernel k $mesh\n\n");
666 [ # # ]: 0 : fprintf(outfile, "k energy 1\n # total energy\n"); // Candidate sources are all of unit power
667 [ # # ]: 0 : fprintf(outfile, "k materials $opt\n # materials\n");
668 : : fprintf(outfile, "k setUnitsToMM\n # units for mesh dimensions "
669 [ # # ]: 0 : "& optical properties (must match each other)\n");
670 : :
671 : : // Monte carlo kernel properties
672 [ # # ]: 0 : fprintf(outfile, "\n##### Monte Carlo kernel properties\n");
673 [ # # ]: 0 : fprintf(outfile, "k roulettePrWin 0.1\n # probability of roulette win\n");
674 : : fprintf(outfile, "k rouletteWMin 1e-5\n # minimum weight "
675 [ # # ]: 0 : "\"wmin\" before roulette takes effect\n");
676 : : fprintf(outfile, "k maxSteps 10000\n # maximum"
677 [ # # ]: 0 : " number of steps to trace a packet\n");
678 : : fprintf(outfile, "k maxHits 100\n # maximum number"
679 [ # # ]: 0 : "of boundaries a single step can take\n");
680 : : fprintf(outfile, "k packetCount 1000000\n"
681 : : " # number of packets to simulate"
682 [ # # ]: 0 : " (more -> better quality, longer run)\n");
683 : : fprintf(outfile, "k threadCount 8\n # number of threads "
684 [ # # ]: 0 : "(set to number of cores, or 2x number of cores if hyperthreading\n\n");
685 : :
686 : :
687 : : // Defining progress timer call back function for use during simulation run
688 [ # # ]: 0 : fprintf(outfile, "##### Define progress timer callback function for use during simulation run\n");
689 : :
690 : : fprintf(outfile, "\nproc progresstimer {} {\n"
691 : : " # loop while not finished\n"
692 : : " while { ![k done] } {\n"
693 : : " # display %% completed to 2 decimal places\n"
694 : : " puts -nonewline [format \"\\rProgress %%6.2f%%%%\" [expr 100.0*[k progressFraction]]]\n"
695 : : " flush stdout\n\n"
696 : :
697 : : " # refresh interval: 200ms\n"
698 : : " after 200\n"
699 : : " }\n"
700 : : " puts [format \"\\rProgress %%6.2f%%%%\" 100.0]\n"
701 [ # # ]: 0 : "}\n\n");
702 : :
703 : : // Setting up internal fluence counting
704 [ # # ]: 0 : stringstream vtk_v_file_name;
705 [ # # ][ # # ]: 0 : vtk_v_file_name << out_file_name.substr(0, out_file_name.length()-4) << "_volume.vtk";
[ # # ]
706 : 0 : fprintf(outfile, "##### Set up internal fluence counting\n"
707 : : "TriFilterRegionBounds TF\n"
708 : : " TF mesh $mesh\n"
709 : : " TF bidirectional 1\n\n"
710 : :
711 : : "##### designate regions whose boundaries should be monitored\n"
712 : : " TF includeRegion 4 1\n"
713 : : " TF includeRegion 3 1\n\n"
714 : :
715 : : "vtkFullMonteArrayAdaptor vtkPhiV\n\n"
716 : :
717 : : "TetraFilterByRegion MF\n"
718 : : " MF mesh $mesh\n"
719 : : " MF include 4 1\n\n"
720 : :
721 : : "$mesh setFacesForFluenceCounting TF\n\n"
722 : : "vtkFullMonteTetraMeshWrapper VTKM\n"
723 : : " VTKM mesh $mesh\n\n"
724 : :
725 : : "##### Writer pipeline for volume field data (regions & vol fluence)\n"
726 : : "vtkFieldData volumeFieldData\n"
727 : : " volumeFieldData AddArray [VTKM regions]\n\n"
728 : :
729 : : "vtkDataObject volumeDataObject\n"
730 : : " volumeDataObject SetFieldData volumeFieldData\n\n"
731 : :
732 : : "vtkMergeDataObjectFilter mergeVolume\n"
733 : : " mergeVolume SetDataObjectInputData volumeDataObject\n"
734 : : " mergeVolume SetInputData [VTKM blankMesh]\n"
735 : : " mergeVolume SetOutputFieldToCellDataField\n\n"
736 : :
737 : : "vtkUnstructuredGridWriter VW\n"
738 : : " VW SetInputConnection [mergeVolume GetOutputPort]\n"
739 : : " VW SetFileName \"%s\"\n\n"
740 : :
741 : : "EnergyToFluence EVF\n"
742 : : " EVF mesh $mesh\n"
743 [ # # # # ]: 0 : " EVF materials $opt\n\n", vtk_v_file_name.str().c_str());
744 : :
745 : : // Looping over all the sources
746 [ # # ]: 0 : fprintf(outfile, "##### Initializing array of tetrahedral sources\n");
747 : : int tetra_idx;
748 [ # # ]: 0 : for(unsigned int i = 0; i < sources.size(); i++)
749 : : {
750 [ # # ]: 0 : tetra_idx = sources[i]->get_id();
751 : :
752 [ # # ]: 0 : fprintf(outfile, "set sources_array(%d) %d\n", i, tetra_idx);
753 : : }
754 [ # # ]: 0 : fprintf(outfile, "\n");
755 : :
756 : :
757 : : // TCL commands to loop over the sources
758 [ # # ]: 0 : fprintf(outfile, "##### Looping over the sources array\n");
759 [ # # ]: 0 : fprintf(outfile, "for {set index 0} { $index < [array size sources_array] } { incr index} {\n puts $index\n");
760 : :
761 : : // Configuring sources
762 [ # # ]: 0 : fprintf(outfile, " ##### Configuring Sources\n");
763 [ # # ]: 0 : fprintf(outfile, " Volume V0 $sources_array($index)\n\n");
764 : : // The sources are volume elements with unit power
765 : :
766 [ # # ]: 0 : fprintf(outfile, " k source V0\n # the source to launch from \n");
767 : :
768 : : // Running the kernel
769 [ # # ]: 0 : fprintf(outfile, " ##### Run it\n\n ##### Run Kernel, display progress timer, and awaut finish\n");
770 [ # # ]: 0 : fprintf(outfile, " k startAsync\n progresstimer\n k finishAsync\n\n");
771 : :
772 : : // Gathering data
773 [ # # ]: 0 : stringstream vtk_file_name;
774 [ # # ][ # # ]: 0 : vtk_file_name << "TCL_scripts/" << out_file_name.substr(0, out_file_name.length()-4) << "_volume.vtk";
[ # # ][ # # ]
775 : :
776 : : fprintf(outfile, " EVF source [k getResultByIndex 2]\n"
777 : : " EVF update\n\n"
778 : :
779 : : " vtkPhiV source [EVF result]\n"
780 : : " volumeFieldData AddArray [vtkPhiV array]\n"
781 [ # # ]: 0 : " [vtkPhiV array] SetName \"Fluence$sources_array($index)\"\n");
782 [ # # ]: 0 : fprintf(outfile, "}\n\n");
783 : :
784 : 0 : fprintf(outfile, "VW SetFileName \"%s\"\n"
785 : : "VW Update\n"
786 [ # # # # ]: 0 : "exit", vtk_file_name.str().c_str());
787 : :
788 : : // closing the file
789 [ # # ]: 0 : fclose(outfile);
790 [ # # ]: 0 : fprintf(stderr, "\033[1;%dmScript Generated!\033[0m\n\n", 36);
791 [ + - ][ + - ]: 84 : }
792 : :
|