mmc_mesh.h
Go to the documentation of this file.
1 /***************************************************************************/
32 /***************************************************************************/
38 #ifndef _MMC_MESH_UNIT_H
39 #define _MMC_MESH_UNIT_H
40 
41 #include <stdio.h>
42 #include <math.h>
43 #include "mmc_utils.h"
44 
45 #ifdef MMC_USE_SSE
46  #include <smmintrin.h>
47 #endif
48 
49 #if !defined(USE_OPENCL) && !defined(__NVCC__)
50 
51  #ifdef MMC_SFMT
52  #include "mmc_rand_sfmt.c"
53  #elif defined MMC_XORSHIFT
54  #include "mmc_rand_xorshift128p.c"
55  #else
56  #include "mmc_rand_posix.c"
57  #endif
58 
59 #elif defined(__NVCC__)
60  #include "mmc_rand_xorshift128p.h"
61 #else
62  #include "mmc_rand_xorshift128p.c"
63 #endif
64 
65 #define MMC_UNDEFINED (3.40282347e+38F)
66 #define ID_UNDEFINED 0x7FFFFFFF
67 
68 #define R_RAND_MAX (1.f/RAND_MAX)
69 #define TWO_PI (M_PI*2.0)
70 #define EPS 1e-6f
71 #define LOG_MT_MAX 22.1807097779182f
72 #define R_MIN_MUS 1e9f
73 #define R_C0 3.335640951981520e-12f //1/C0 in s/mm
74 #define DELTA_MUA 1e-4f
75 #define VERY_BIG 1e30f
76 #define MIN(a,b) ((a)<(b)?(a):(b))
77 #define MAX(a,b) ((a)>(b)?(a):(b))
78 
79 #define MESH_ERROR(a) mesh_error((a),__FILE__,__LINE__)
80 
81 /***************************************************************************/
90 typedef struct MMC_mesh {
91  int nn;
92  int ne;
93  int nf;
94  int prop;
95  int elemlen;
97  int* elem;
98  int* elem2;
99  float* edgeroi;
100  float* faceroi;
101  float* noderoi;
102  int* srcelem;
104  int* detelem;
106  int* type;
107  int* facenb;
109  float* atte;
110  double* weight;
111  double* dref;
112  float* evol;
113  float* nvol;
114  float4 nmin;
115  float4 nmax;
116 } tetmesh;
117 
118 /***************************************************************************/
128 typedef struct MMC_raytracer {
130  char method;
134 } raytracer;
135 #ifdef __cplusplus
136 extern "C" {
137 #endif
138 void mesh_init(tetmesh* mesh);
139 void mesh_init_from_cfg(tetmesh* mesh, mcconfig* cfg);
140 void mesh_loadnode(tetmesh* mesh, mcconfig* cfg);
141 void mesh_loadelem(tetmesh* mesh, mcconfig* cfg);
142 void mesh_loadfaceneighbor(tetmesh* mesh, mcconfig* cfg);
143 void mesh_loadmedia(tetmesh* mesh, mcconfig* cfg);
144 void mesh_loadelemvol(tetmesh* mesh, mcconfig* cfg);
145 void mesh_loadseedfile(tetmesh* mesh, mcconfig* cfg);
146 
147 void mesh_clear(tetmesh* mesh);
148 float mesh_normalize(tetmesh* mesh, mcconfig* cfg, float Eabsorb, float Etotal, int pair);
149 void mesh_build(tetmesh* mesh);
150 void mesh_error(const char* msg, const char* file, const int linenum);
151 void mesh_filenames(const char* format, char* foutput, mcconfig* cfg);
152 void mesh_saveweight(tetmesh* mesh, mcconfig* cfg, int isref);
153 void mesh_savedetphoton(float* ppath, void* seeds, int count, int seedbyte, mcconfig* cfg);
154 void mesh_getdetimage(float* detmap, float* ppath, int count, mcconfig* cfg, tetmesh* mesh);
155 void mesh_savedetimage(float* detmap, mcconfig* cfg);
156 float mesh_getdetweight(int photonid, int colcount, float* ppath, mcconfig* cfg);
157 void mesh_srcdetelem(tetmesh* mesh, mcconfig* cfg);
158 void mesh_createdualmesh(tetmesh* mesh, mcconfig* cfg);
159 void mesh_loadroi(tetmesh* mesh, mcconfig* cfg);
160 double mesh_getreff_approx(double n_in, double n_out);
161 double mesh_getreff(double n_in, double n_out);
162 
163 void tracer_init(raytracer* tracer, tetmesh* mesh, char methodid);
164 void tracer_build(raytracer* tracer);
165 void tracer_prep(raytracer* tracer, mcconfig* cfg);
166 void tracer_clear(raytracer* tracer);
167 
168 float mc_next_scatter(float g, float3* dir, RandType* ran, RandType* ran0, mcconfig* cfg, float* pmom);
169 #ifdef MCX_CONTAINER
170 #ifdef __cplusplus
171 extern "C"
172 #endif
173 int mcx_throw_exception(const int id, const char* msg, const char* filename, const int linenum);
174 #endif
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 static inline void vec_add(float3* a, float3* b, float3* res) {
181  res->x = a->x + b->x;
182  res->y = a->y + b->y;
183  res->z = a->z + b->z;
184 }
185 
186 static inline void vec_diff(float3* a, float3* b, float3* res) {
187  res->x = b->x - a->x;
188  res->y = b->y - a->y;
189  res->z = b->z - a->z;
190 }
191 
192 static inline void vec_mult(float3* a, float sa, float3* res) {
193  res->x = sa * a->x;
194  res->y = sa * a->y;
195  res->z = sa * a->z;
196 }
197 
198 static inline void vec_mult_add(float3* a, float3* b, float sa, float sb, float3* res) {
199  res->x = sb * b->x + sa * a->x;
200  res->y = sb * b->y + sa * a->y;
201  res->z = sb * b->z + sa * a->z;
202 }
203 
204 static inline void vec_cross(float3* a, float3* b, float3* res) {
205  res->x = a->y * b->z - a->z * b->y;
206  res->y = a->z * b->x - a->x * b->z;
207  res->z = a->x * b->y - a->y * b->x;
208 }
209 
210 static inline void mmc_sincosf(float x, float* sine, float* cosine) {
211 #if defined(__GNUC__) && defined(__linux__) && !defined(__clang__)
212  __builtin_sincosf(x, sine, cosine);
213 #else
214  *sine = sinf(x);
215  *cosine = cosf(x);
216 #endif
217 }
218 
219 //#ifndef MMC_USE_SSE
220 static inline float vec_dot(float3* a, float3* b) {
221  return a->x * b->x + a->y * b->y + a->z * b->z;
222 }/*
223 #else
224 
225 #ifndef __SSE4_1__
226 static inline float vec_dot(float3 *a,float3 *b){
227  float dot;
228  __m128 na,nb,res;
229  na=_mm_load_ps(&a->x);
230  nb=_mm_load_ps(&b->x);
231  res=_mm_mul_ps(na,nb);
232  res=_mm_hadd_ps(res,res);
233  res=_mm_hadd_ps(res,res);
234  _mm_store_ss(&dot,res);
235  return dot;
236 }
237 #else
238 static inline float vec_dot(float3 *a,float3 *b){
239  float dot;
240  __m128 na,nb,res;
241  na=_mm_load_ps(&a->x);
242  nb=_mm_load_ps(&b->x);
243  res=_mm_dp_ps(na,nb,0x7f);
244  _mm_store_ss(&dot,res);
245  return dot;
246 }
247 #endif
248 #endif
249 */
250 
251 static inline float pinner(float3* Pd, float3* Pm, float3* Ad, float3* Am) {
252  return vec_dot(Pd, Am) + vec_dot(Pm, Ad);
253 }
254 
255 
256 static inline float dist2(float3* p0, float3* p1) {
257  return (p1->x - p0->x) * (p1->x - p0->x) + (p1->y - p0->y) * (p1->y - p0->y) + (p1->z - p0->z) * (p1->z - p0->z);
258 }
259 
260 static inline float dist(float3* p0, float3* p1) {
261  return sqrtf(dist2(p0, p1));
262 }
263 
264 static inline float dist2d2(float* p0, float* p1) {
265  return (p1[0] - p0[0]) * (p1[0] - p0[0]) + (p1[1] - p0[1]) * (p1[1] - p0[1]);
266 }
267 
268 static inline float mmc_rsqrtf(float a) {
269 #ifdef MMC_USE_SSE
270  return _mm_cvtss_f32( _mm_rsqrt_ss( _mm_set_ss( a ) ) );
271 #else
272  return 1.f / sqrtf(a);
273 #endif
274 }
275 
276 #endif
This structure defines the problem settings (domain, filenames, session)
Definition: mmc_utils.h:184
void mesh_loadelemvol(tetmesh *mesh, mcconfig *cfg)
Load tet element volume file and initialize the related mesh properties.
Definition: mmc_mesh.c:517
double mesh_getreff(double n_in, double n_out)
Compute the effective reflection coefficient Reff.
Definition: mmc_mesh.c:1616
void mesh_loadseedfile(tetmesh *mesh, mcconfig *cfg)
Load previously saved photon seeds from an .mch file for replay.
Definition: mmc_mesh.c:604
void mesh_saveweight(tetmesh *mesh, mcconfig *cfg, int isref)
Save the fluence output to a file.
Definition: mmc_mesh.c:1237
int * elem2
Definition: mmc_mesh.h:98
float3 * d
Definition: mmc_mesh.h:131
float mc_next_scatter(float g, float3 *dir, RandType *ran, RandType *ran0, mcconfig *cfg, float *pmom)
Performing one scattering event of the photon.
Definition: mmc_mesh.c:1155
float * edgeroi
Definition: mmc_mesh.h:99
float3 * n
Definition: mmc_mesh.h:133
float4 nmax
Definition: mmc_mesh.h:115
int * type
Definition: mmc_mesh.h:106
double * weight
Definition: mmc_mesh.h:110
void tracer_clear(raytracer *tracer)
Clear the ray-tracing data structure.
Definition: mmc_mesh.c:1123
char method
Definition: mmc_mesh.h:130
void mesh_error(const char *msg, const char *file, const int linenum)
Error-handling in mesh operations.
Definition: mmc_mesh.c:182
int detelemlen
Definition: mmc_mesh.h:105
void mesh_loadnode(tetmesh *mesh, mcconfig *cfg)
Load node file and initialize the related mesh properties.
Definition: mmc_mesh.c:301
tetmesh * mesh
Definition: mmc_mesh.h:129
float mesh_getdetweight(int photonid, int colcount, float *ppath, mcconfig *cfg)
Recompute the detected photon weight from the partial-pathlengths.
Definition: mmc_mesh.c:1464
void mesh_init(tetmesh *mesh)
Initializing the mesh data structure with default values.
Definition: mmc_mesh.c:111
float3 * m
Definition: mmc_mesh.h:132
Basic FEM mesh data structrure.
Definition: mmc_mesh.h:90
This structure defines the optical properties for each medium.
Definition: mmc_utils.h:129
float * evol
Definition: mmc_mesh.h:112
Definition of program options and problem domain configurations.
int * srcelem
Definition: mmc_mesh.h:102
int ne
Definition: mmc_mesh.h:92
void mesh_loadmedia(tetmesh *mesh, mcconfig *cfg)
Load optical property file and initialize the related mesh properties.
Definition: mmc_mesh.c:339
void mesh_filenames(const char *format, char *foutput, mcconfig *cfg)
Construct a full mesh file name using cfg session and root path.
Definition: mmc_mesh.c:199
void mesh_loadelem(tetmesh *mesh, mcconfig *cfg)
Load element file and initialize the related mesh properties.
Definition: mmc_mesh.c:455
int * elem
Definition: mmc_mesh.h:97
float * nvol
Definition: mmc_mesh.h:113
void mesh_getdetimage(float *detmap, float *ppath, int count, mcconfig *cfg, tetmesh *mesh)
Save binned detected photon data over an area-detector as time-resolved 2D images.
Definition: mmc_mesh.c:1372
int * detelem
Definition: mmc_mesh.h:104
float3 * node
Definition: mmc_mesh.h:96
int srcelemlen
Definition: mmc_mesh.h:103
int nn
Definition: mmc_mesh.h:91
An interface to use the SFMT-19937 random number generator.
float * noderoi
Definition: mmc_mesh.h:101
float mesh_normalize(tetmesh *mesh, mcconfig *cfg, float Eabsorb, float Etotal, int pair)
Function to normalize the fluence and remove influence from photon number and volume.
Definition: mmc_mesh.c:1498
void tracer_prep(raytracer *tracer, mcconfig *cfg)
Preparing for the ray-tracing calculations.
Definition: mmc_mesh.c:818
double * dref
Definition: mmc_mesh.h:111
medium * med
Definition: mmc_mesh.h:108
int * facenb
Definition: mmc_mesh.h:107
int elemlen
Definition: mmc_mesh.h:95
float * faceroi
Definition: mmc_mesh.h:100
void mesh_srcdetelem(tetmesh *mesh, mcconfig *cfg)
Identify wide-field source and detector-related elements (type=-1 for source, type=-2 for det) ...
Definition: mmc_mesh.c:252
int prop
Definition: mmc_mesh.h:94
void mesh_init_from_cfg(tetmesh *mesh, mcconfig *cfg)
Loading user-specified mesh data.
Definition: mmc_mesh.c:153
void mesh_loadfaceneighbor(tetmesh *mesh, mcconfig *cfg)
Load face-neightbor element list and initialize the related mesh properties.
Definition: mmc_mesh.c:562
void mesh_loadroi(tetmesh *mesh, mcconfig *cfg)
Load edge/node/face roi for implicit MMC.
Definition: mmc_mesh.c:403
int nf
Definition: mmc_mesh.h:93
void mesh_savedetphoton(float *ppath, void *seeds, int count, int seedbyte, mcconfig *cfg)
Save detected photon data into an .mch history file.
Definition: mmc_mesh.c:1305
void mesh_savedetimage(float *detmap, mcconfig *cfg)
Save binned detected photon data over an area-detector.
Definition: mmc_mesh.c:1431
float4 nmin
Definition: mmc_mesh.h:114
floating-point triplet {x,y,z}
Definition: vector_types.h:73
void tracer_build(raytracer *tracer)
Pre-computed ray-tracing related data.
Definition: mmc_mesh.c:995
Ray-tracer data structrure for pre-computed data.
Definition: mmc_mesh.h:128
void tracer_init(raytracer *tracer, tetmesh *mesh, char methodid)
Initialize a data structure storing all pre-computed ray-tracing related data.
Definition: mmc_mesh.c:799
double mesh_getreff_approx(double n_in, double n_out)
Compute the effective reflection coefficient Reff using approximated formula.
Definition: mmc_mesh.c:1604
void mesh_clear(tetmesh *mesh)
Clearing the mesh data structure.
Definition: mmc_mesh.c:698
float * atte
Definition: mmc_mesh.h:109