@@ -2024,6 +2024,7 @@ TRACE
20242024 printf(" TO VOLUME %d \n",current_volume);
20252025 #endif
20262026
2027+ if (previous_volume != current_volume) { // With masks, it can happen that the ray takes an extra iteration within the same volume, can skip surface / refraction
20272028 double n1, n2;
20282029 int perform_refraction;
20292030
@@ -2050,7 +2051,7 @@ TRACE
20502051 struct surface_stack_struct *entering_stack;
20512052
20522053 #ifdef Union_trace_verbal_setting
2053- printf(" Creating leaving surface stack %d \n",current_volume );
2054+ printf(" Creating leaving surface stack %d \n",previous_volume );
20542055 #endif
20552056
20562057 if (previous_volume == 0) {
@@ -2090,7 +2091,7 @@ TRACE
20902091 int n_total = n_leaving + n_entering;
20912092
20922093 #ifdef Union_trace_verbal_setting
2093- printf(" Combining into one surface stack %d \n",current_volume );
2094+ printf(" Combining into one surface stack \n");
20942095 #endif
20952096
20962097 // Make combined list, leaving bottom to top followed by entering top to bottom
@@ -2127,7 +2128,7 @@ TRACE
21272128 surface_wavevector_before[2] = surface_wavevector[2];
21282129
21292130 #ifdef Union_trace_verbal_setting
2130- printf(" Entering surface stack loop %d \n",current_volume );
2131+ printf(" Entering surface stack loop \n");
21312132 #endif
21322133
21332134 while (1) {
@@ -2245,23 +2246,57 @@ TRACE
22452246
22462247 if (previous_volume == 0) n1 = 1.0;
22472248 else {
2248- if (Volumes[previous_volume]->p_physics->has_refraction_info == 0) perform_refraction = 0;
2249- n1 = sqrt(1.0-(lambda*lambda*Volumes[previous_volume]->p_physics->refraction_rho*Volumes[previous_volume]->p_physics->refraction_bc/PI));
2249+ if (Volumes[previous_volume]->p_physics->has_refraction_info == 0 && Volumes[previous_volume]->p_physics->is_vacuum == 0) {
2250+ perform_refraction = 0;
2251+ } else {
2252+
2253+ if (Volumes[previous_volume]->p_physics->is_vacuum == 1) {
2254+ n1 = 1.0;
2255+ } else {
2256+ n1 = sqrt(1.0-(lambda*lambda*Volumes[previous_volume]->p_physics->refraction_rho*Volumes[previous_volume]->p_physics->refraction_bc/PI));
2257+ }
22502258
2251- // If the intersection is found with this volume, it is leaving, and the normal needs to be flipped
2252- if (previous_volume == min_volume) {
2259+ // If the intersection is found with this volume, it is leaving, and the normal needs to be flipped
2260+ if (previous_volume == min_volume) {
22532261 nx *= -1;
22542262 ny *= -1;
22552263 nz *= -1;
2256- }
2264+
2265+ }
2266+ }
22572267 }
22582268
22592269 if (current_volume == 0) n2 = 1.0;
22602270 else {
2261- if (Volumes[current_volume]->p_physics->has_refraction_info == 0) perform_refraction = 0;
2262- n2 = sqrt(1.0-(lambda*lambda*Volumes[current_volume]->p_physics->refraction_rho*Volumes[current_volume]->p_physics->refraction_bc/PI));
2271+ if (Volumes[current_volume]->p_physics->has_refraction_info == 0 && Volumes[current_volume]->p_physics->is_vacuum == 0) {
2272+ perform_refraction = 0;
2273+ } else {
2274+
2275+ if (Volumes[current_volume]->p_physics->is_vacuum == 1) {
2276+ n2 = 1.0;
2277+ } else {
2278+ n2 = sqrt(1.0-(lambda*lambda*Volumes[current_volume]->p_physics->refraction_rho*Volumes[current_volume]->p_physics->refraction_bc/PI));
2279+ }
2280+ }
22632281 }
22642282
2283+ // Check if the two materials are the same, no need to check for refraction / reflection.
2284+ if (perform_refraction == 1 && previous_volume != 0 && current_volume != 0) {
2285+ if (strcmp(Volumes[previous_volume]->p_physics->name, Volumes[current_volume]->p_physics->name) == 0 ) {
2286+ perform_refraction = 0;
2287+ }
2288+ }
2289+
2290+ if (previous_volume == 0 && current_volume == 0) {
2291+ perform_refraction = 0; // Vacuum to vacuum
2292+ } else if (previous_volume == 0) {
2293+ if (Volumes[current_volume]->p_physics->is_vacuum == 1) perform_refraction = 0; // Vacuum to vacuum
2294+ } else if (current_volume == 0) {
2295+ if (Volumes[previous_volume]->p_physics->is_vacuum == 1) perform_refraction = 0; // Vacuum to vacuum
2296+ } else {
2297+ if (Volumes[previous_volume]->p_physics->is_vacuum == 1 && Volumes[current_volume]->p_physics->is_vacuum == 1) perform_refraction = 0; // Vacuum to vacuum
2298+ }
2299+
22652300 #ifdef Union_trace_verbal_setting
22662301 if (perform_refraction == 1)
22672302 printf("ready to calculate refraction, current_volume = %d, n1=%lf, n2=%lf \n", current_volume, n1, n2);
@@ -2294,7 +2329,6 @@ TRACE
22942329
22952330 use_fresnel = 1;
22962331
2297-
22982332 /* Reflectivity (see component Guide). */
22992333 //StdReflecFunc(q_normal, par, &reflectivity);
23002334
@@ -2358,6 +2392,10 @@ TRACE
23582392 coords_get(V_reflect, &vx, &vy, &vz);
23592393
23602394 current_volume = previous_volume; // Reflected, stays in current volume
2395+
2396+ #ifdef Union_trace_verbal_setting
2397+ printf(" Refraction system : ray reflected, (branch 1) going back to volume %d\n", previous_volume);
2398+ #endif
23612399
23622400 // Update velocity in all ways used in master
23632401 v[0] = vx; v[1] = vy; v[2] = vz;
@@ -2397,6 +2435,9 @@ TRACE
23972435
23982436 coords_get(V_refract, &vx, &vy, &vz);
23992437
2438+ #ifdef Union_trace_verbal_setting
2439+ printf(" Refraction system : ray refracted, continues to to volume %d\n", current_volume);
2440+ #endif
24002441
24012442
24022443 // Update velocity in all ways used in master
@@ -2438,8 +2479,11 @@ TRACE
24382479 if (0 < reflectivity && reflectivity < 1) p *= reflectivity; // should be R0
24392480 coords_get(V_reflect, &vx, &vy, &vz);
24402481
2441-
24422482 current_volume = previous_volume; // Reflected, stays in current volume
2483+
2484+ #ifdef Union_trace_verbal_setting
2485+ printf(" Refraction system : ray reflected (branch 3), going back to volume %d\n", previous_volume);
2486+ #endif
24432487
24442488 // Update velocity in all ways used in master
24452489 v[0] = vx; v[1] = vy; v[2] = vz;
@@ -2480,13 +2524,16 @@ TRACE
24802524 done = 1; // Exit volumes allow the ray to escape the component
24812525 ray_sucseeded = 1; // Allows the ray to leave loop
24822526 }
2527+
24832528
24842529 #ifdef Union_trace_verbal_setting
24852530 printf("After refraction system \n");
24862531 printf("r = (%f,%f,%f) v = (%f,%f,%f) \n", x, y, z, vx, vy, vz);
24872532 printf("CURRENT_VOLUME = %d \n", current_volume);
24882533 #endif
2489- }
2534+
2535+ } // End of if (previous_volume != current_volume)
2536+ } // End of scattering or propagation if
24902537
24912538 } else { // Here because a shortest time is not found
24922539 if (current_volume == 0) {
0 commit comments