@@ -471,8 +471,7 @@ struct scattering_process_struct *p_scattering_array;
471471
472472// refraction related
473473int has_refraction_info ;
474- double refraction_rho ;
475- double refraction_bc ;
474+ double refraction_scattering_length_density ; // [AA^-2]
476475double refraction_Qc ;
477476};
478477
@@ -2646,11 +2645,7 @@ int sample_box_intersect_advanced(double *t, double *nx, double *ny, double *nz,
26462645 Coords normal_vector_rotated ;
26472646 Coords normal_vector ;
26482647
2649-
2650- // Rotate back to master coordinate system
2651- normal_vector_rotated = coords_set (nx [0 ], nx [1 ], nx [2 ]);
2652- normal_vector = rot_apply (geometry -> rotation_matrix , normal_vector_rotated );
2653-
2648+ // Sort solution according to intersection time and rotate normal vectors to master coordinate system
26542649 switch (* num_solutions ) {
26552650 case 2 :
26562651 if (t [0 ] > t [1 ]) {
@@ -2668,7 +2663,7 @@ int sample_box_intersect_advanced(double *t, double *nx, double *ny, double *nz,
26682663 ny [0 ] = temp ;
26692664
26702665 temp = nz [1 ];
2671- nz [1 ] = nx [0 ];
2666+ nz [1 ] = nz [0 ];
26722667 nz [0 ] = temp ;
26732668
26742669 // Switch surface_index
@@ -4127,7 +4122,7 @@ int sample_cylinder_intersect(double *t, double *nx, double *ny, double *nz, int
41274122 double radius = geometry -> geometry_parameters .p_cylinder_storage -> cyl_radius ;
41284123 double height = geometry -> geometry_parameters .p_cylinder_storage -> height ;
41294124
4130- // Declare variables for the function
4125+ // Declare position variables for the local coordinate system
41314126 double x_new ,y_new ,z_new ;
41324127
41334128 // Coordinate transformation
@@ -4137,69 +4132,77 @@ int sample_cylinder_intersect(double *t, double *nx, double *ny, double *nz, int
41374132
41384133 Coords coordinates = coords_set (x_new ,y_new ,z_new );
41394134 Coords rotated_coordinates ;
4140- // printf("Cords coordinates = (%f,%f,%f)\n",coordinates.x,coordinates.y,coordinates.z);
4141-
4142- // debug
4143- // Rotation rotation_matrix_debug[3][3];
4144- // rot_set_rotation(rotation_matrix_debug,-1.0*geometry->rotation.x,-1.0*geometry->rotation.y,-1.0*geometry->rotation.z);
4145- // rot_transpose(geometry->rotation_matrix,rotation_matrix_debug);
41464135
41474136 // Rotate the position of the neutron around the center of the cylinder
41484137 rotated_coordinates = rot_apply (geometry -> transpose_rotation_matrix ,coordinates );
4149- // rotated_coordinates = rot_apply(rotation_matrix_debug,coordinates);
4150- // printf("Cords rotated_coordinates = (%f,%f,%f)\n",rotated_coordinates.x,rotated_coordinates.y,rotated_coordinates.z);
41514138
41524139 Coords velocity = coords_set (v [0 ],v [1 ],v [2 ]);
41534140 Coords rotated_velocity ;
4154- // printf("Cords velocity = (%f,%f,%f)\n",velocity.x,velocity.y,velocity.z);
41554141
41564142 // Rotate the position of the neutron around the center of the cylinder
41574143 rotated_velocity = rot_apply (geometry -> transpose_rotation_matrix ,velocity );
4158- // rotated_velocity = rot_apply(rotation_matrix_debug,velocity);
4159- // printf("Cords rotated_velocity = (%f,%f,%f)\n",rotated_velocity.x,rotated_velocity.y,rotated_velocity.z);
4160-
4161-
4162-
4163- // Cases where the velocity is parallel with the cylinder axis have given problems, and is checked for explicitly
4164- if (sqrt (rotated_velocity .x * rotated_velocity .x + rotated_velocity .z * rotated_velocity .z )/fabs (rotated_velocity .y ) < 0.00001 ) {
4165- // The velocity is parallel with the cylinder axis. Either there is two solutions
4166- if (sqrt (rotated_coordinates .x * rotated_coordinates .x + rotated_coordinates .z * rotated_coordinates .z ) > radius ) {
4167- * num_solutions = 0 ;
4168- return 0 ;
4169- } else {
4170- * num_solutions = 2 ;
4171- t [0 ] = (0.5 * height - rotated_coordinates .y )/rotated_velocity .y ;
4172- t [1 ] = (-0.5 * height - rotated_coordinates .y )/rotated_velocity .y ;
4173- return 1 ;
4174- }
4175- }
41764144
4177- int output ;
4178- // Run McStas built in sphere intersect funtion (sphere centered around origin)
4179- if ((output = cylinder_intersect (& t [0 ],& t [1 ],
4180- rotated_coordinates .x ,rotated_coordinates .y ,rotated_coordinates .z ,
4181- rotated_velocity .x ,rotated_velocity .y ,rotated_velocity .z ,radius ,height )) == 0 ) {
4182- * num_solutions = 0 ;t [0 ]= -1 ;t [1 ]= -1 ;
4183- }
4184- else if (t [1 ] != 0 ) * num_solutions = 2 ;
4185- else {* num_solutions = 1 ; t [1 ]= -1 ;}
4145+ int output ;
4146+ // Cases where the velocity is parallel with the cylinder axis have given problems, and is checked for explicitly
4147+ if (sqrt (rotated_velocity .x * rotated_velocity .x + rotated_velocity .z * rotated_velocity .z )/fabs (rotated_velocity .y ) < 0.00001 ) {
4148+ // The velocity is parallel with the cylinder axis. Either there are no solutions or two solutions
4149+ if (sqrt (rotated_coordinates .x * rotated_coordinates .x + rotated_coordinates .z * rotated_coordinates .z ) > radius ) {
4150+ * num_solutions = 0 ;
4151+ return 0 ;
4152+ } else {
4153+ * num_solutions = 2 ;
4154+ t [0 ] = (0.5 * height - rotated_coordinates .y )/rotated_velocity .y ;
4155+ surface_index [0 ] = 1 ; // index indicating top
4156+
4157+ t [1 ] = (-0.5 * height - rotated_coordinates .y )/rotated_velocity .y ;
4158+ surface_index [1 ] = 2 ; // index indicating bottom
4159+
4160+ // sort solutions
4161+ if (t [0 ] > t [1 ]) {
4162+ double d_temp ;
4163+
4164+ d_temp = t [0 ];
4165+ t [0 ] = t [1 ];
4166+ t [1 ] = d_temp ;
4167+
4168+ int i_temp ;
4169+
4170+ i_temp = surface_index [0 ];
4171+ surface_index [0 ] = surface_index [1 ];
4172+ surface_index [1 ] = i_temp ;
4173+ }
4174+ }
4175+ } else {
4176+ // velocity not parallel to cylinder axis, call standard mcstas cylinder intersect
4177+
4178+ // Run McStas built in sphere intersect funtion (sphere centered around origin)
4179+ if ((output = cylinder_intersect (& t [0 ],& t [1 ],
4180+ rotated_coordinates .x ,rotated_coordinates .y ,rotated_coordinates .z ,
4181+ rotated_velocity .x ,rotated_velocity .y ,rotated_velocity .z ,radius ,height )) == 0 ) {
4182+ * num_solutions = 0 ;t [0 ]= -1 ;t [1 ]= -1 ;
4183+ }
4184+ else if (t [1 ] != 0 ) * num_solutions = 2 ;
4185+ else {* num_solutions = 1 ; t [1 ]= -1 ;}
41864186
4187- // decode output value
4188- // Check the bitmask for entry and exit
4189- if (* num_solutions > 0 ) {
4190- int entry_index = 0 ;
4191- if (output & 2 ) entry_index = 1 ; // Entry intersects top cap
4192- if (output & 4 ) entry_index = 2 ; // Entry intersects bottom cap
4193- surface_index [0 ] = entry_index ;
4194- }
4187+ // decode output value
4188+ // Check the bitmask for entry and exit
4189+ if (* num_solutions > 0 ) {
4190+ int entry_index = 0 ;
4191+ if (output & 2 ) entry_index = 1 ; // Entry intersects top cap
4192+ if (output & 4 ) entry_index = 2 ; // Entry intersects bottom cap
4193+ surface_index [0 ] = entry_index ;
4194+ }
41954195
4196- if (* num_solutions > 1 ) {
4197- int exit_index = 0 ;
4198- if (output & 8 ) exit_index = 1 ; // Exit intersects top cap
4199- if (output & 16 ) exit_index = 2 ; // Exit intersects bottom cap
4200- surface_index [1 ] = exit_index ;
4196+ if (* num_solutions > 1 ) {
4197+ int exit_index = 0 ;
4198+ if (output & 8 ) exit_index = 1 ; // Exit intersects top cap
4199+ if (output & 16 ) exit_index = 2 ; // Exit intersects bottom cap
4200+ surface_index [1 ] = exit_index ;
4201+ }
42014202 }
4203+
42024204
4205+ // Calculate normal vectors from surface index and cylinder geometry
42034206 int index ;
42044207 double x , y , z , dt ;
42054208 Coords normal_vector_rotated ;
@@ -4238,7 +4241,7 @@ int sample_cylinder_intersect(double *t, double *nx, double *ny, double *nz, int
42384241};
42394242
42404243int r_within_cylinder (Coords pos ,struct geometry_struct * geometry ) {
4241- // Unpack parameters
4244+ // Unpack parameters
42424245 double radius = geometry -> geometry_parameters .p_cylinder_storage -> cyl_radius ;
42434246 double height = geometry -> geometry_parameters .p_cylinder_storage -> height ;
42444247
0 commit comments