Skip to content

Commit

Permalink
Merge pull request #1811 from McStasMcXtrace/post-3.5.16-minor-fixes
Browse files Browse the repository at this point in the history
Post 3.5.16 minor Union fixes +  NeXus x wide MPI
  • Loading branch information
willend authored Dec 20, 2024
2 parents aa3ba7b + f30e837 commit 8a72403
Show file tree
Hide file tree
Showing 6 changed files with 705 additions and 13 deletions.
5 changes: 3 additions & 2 deletions common/lib/share/mccode-r.c
Original file line number Diff line number Diff line change
Expand Up @@ -2037,8 +2037,9 @@ int mcdetector_out_data_nexus(NXhandle f, MCDETECTOR detector)
mcdetector_out_array_nexus(f, "ncount", detector.p0, detector);
} else if (strcasestr(detector.format, "pixels")) {
mcdetector_out_array_nexus( f, "pixels", detector.p1, detector);
} else
} else {
mcdetector_out_array_nexus( f, "events", detector.p1, detector);
}
NXclosegroup(f);
NXopengroup(f, data_name, "NXdata");
NXgetgroupID(nxhandle, &pLink);
Expand Down Expand Up @@ -2089,7 +2090,6 @@ MCDETECTOR mcdetector_out_list_slaves(MCDETECTOR detector)
|| mc_MPI_Send(detector.p1, mnp[0]*mnp[1]*mnp[2], MPI_DOUBLE, mpi_node_root) != MPI_SUCCESS)
fprintf(stderr, "Warning: proc %i to master: MPI_Send p1 list error: mnp=%i (mcdetector_out_list_slaves)\n", mpi_node_rank, abs(mnp[0]*mnp[1]*mnp[2]));
/* slaves are done: sent mnp and p1 */
return (detector);
} /* end slaves */

/* MPI master: receive data from slaves sequentially: 2 MPI_Recv calls */
Expand Down Expand Up @@ -2122,6 +2122,7 @@ MCDETECTOR mcdetector_out_list_slaves(MCDETECTOR detector)
printf("\n** Done ** \n");
);
}
// Common return statement for slaves / master alike
return(detector);
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion common/lib/share/mccode-r.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ void destroy_darr3d(DArray3d a);
}

#ifndef MPI_REDUCE_BLOCKSIZE
#define MPI_REDUCE_BLOCKSIZE 1000
#define MPI_REDUCE_BLOCKSIZE 100000
#endif

int mc_MPI_Sum(double* buf, long count);
Expand Down
1 change: 1 addition & 0 deletions mcstas-comps/share/union-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ union abs_logger_data_union{
struct a_1D_time_to_lambda_abs_storage_struct *p_1D_time_to_lambda_abs_storage;
struct a_event_abs_storage_struct *p_event_abs_storage;
struct a_1D_event_abs_storage_struct *p_1D_event_abs_storage;
struct a_nD_abs_storage_struct *p_nD_abs_storage;
// Additional logger storage structs to be addedd
};

Expand Down
131 changes: 121 additions & 10 deletions mcstas-comps/union/Union_abs_logger_event.comp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
DEFINE COMPONENT Union_abs_logger_event

SETTING PARAMETERS(string target_geometry="NULL", string filename="NULL",
xwidth=0, xbins=0, yheight=0, ybins=0, zdepth=0, zbins=0,
order_total=-1, order_volume=-1, logger_conditional_extend_index=-1, string init="init")


Expand Down Expand Up @@ -120,19 +121,40 @@ struct a_event_abs_storage_struct {
int order;
int order_in_this_volume;
int order_process_in_this_volume;

double stored_xwidth;
double stored_yheight;
double stored_zdepth;

int stored_xbins;
int stored_ybins;
int stored_zbins;

Coords position;
Rotation rotation;
Rotation t_rotation;
};

double round_to_nearest_bin(double value, double w, int N) {
// Calculate the width of each bin
double bin_width = w / N;

// Calculate the index of the nearest bin
int bin_index = round((value + w / 2) / bin_width);

// Calculate the center of the nearest bin
double bin_center = (bin_index * bin_width) - (w / 2) + (bin_width / 2);

return bin_center;
}

// record_to_temp
// Would be nice if x y z, k_new and k_old were all coords
void record_to_temp_abs_event(Coords *position, double *k, double p, double time, int scattered_in_this_volume, int total_number_of_scattering_events, struct abs_logger_struct *abs_logger, struct abs_logger_with_data_struct *abs_logger_with_data_array) {

struct a_event_abs_storage_struct *storage;
storage = abs_logger->data_union.p_event_abs_storage;

int add_point = 1;

if (storage->order != -1) {
Expand All @@ -141,19 +163,59 @@ void record_to_temp_abs_event(Coords *position, double *k, double p, double time
else
add_point = 0;
}

if (storage->order_in_this_volume != -1) {
if (storage->order_in_this_volume == scattered_in_this_volume)
add_point = 1;
else
add_point = 0;
}

if (storage->stored_xwidth != 0) {
if (position->x < 0.5*storage->stored_xwidth && position->x > -0.5*storage->stored_xwidth)
add_point = 1;
else
add_point = 0;
}

if (storage->stored_yheight != 0) {
if (position->y < 0.5*storage->stored_yheight && position->y > -0.5*storage->stored_yheight)
add_point = 1;
else
add_point = 0;
}

if (storage->stored_zdepth != 0) {
if (position->z < 0.5*storage->stored_zdepth && position->z > -0.5*storage->stored_xwidth)
add_point = 1;
else
add_point = 0;
}

if (add_point == 1) {

int i;
double given_x_pos, given_y_pos, given_z_pos;
double used_x_pos, used_y_pos, used_z_pos;
coords_get(*position, &given_x_pos, &given_y_pos, &given_z_pos);

if (storage->stored_xbins != 0) {
used_x_pos = round_to_nearest_bin(given_x_pos, storage->stored_xwidth, storage->stored_xbins);
} else {
used_x_pos = given_x_pos;
}

if (storage->stored_ybins != 0) {
used_y_pos = round_to_nearest_bin(given_y_pos, storage->stored_yheight, storage->stored_ybins);
} else {
used_y_pos = given_y_pos;
}

if (storage->stored_zbins != 0) {
used_z_pos = round_to_nearest_bin(given_z_pos, storage->stored_zdepth, storage->stored_zbins);
} else {
used_z_pos = given_z_pos;
}

double given_x_vel, given_y_vel, given_z_vel;
given_x_vel = k[0]*K2V;
Expand All @@ -162,9 +224,9 @@ void record_to_temp_abs_event(Coords *position, double *k, double p, double time

if (storage->temp_abs_event_data.num_elements < storage->temp_abs_event_data.allocated_elements) {

storage->temp_abs_event_data.elements[storage->temp_abs_event_data.num_elements].x_pos = given_x_pos;
storage->temp_abs_event_data.elements[storage->temp_abs_event_data.num_elements].y_pos = given_y_pos;
storage->temp_abs_event_data.elements[storage->temp_abs_event_data.num_elements].z_pos = given_z_pos;
storage->temp_abs_event_data.elements[storage->temp_abs_event_data.num_elements].x_pos = used_x_pos;
storage->temp_abs_event_data.elements[storage->temp_abs_event_data.num_elements].y_pos = used_y_pos;
storage->temp_abs_event_data.elements[storage->temp_abs_event_data.num_elements].z_pos = used_z_pos;
storage->temp_abs_event_data.elements[storage->temp_abs_event_data.num_elements].x_vel = given_x_vel;
storage->temp_abs_event_data.elements[storage->temp_abs_event_data.num_elements].y_vel = given_y_vel;
storage->temp_abs_event_data.elements[storage->temp_abs_event_data.num_elements].z_vel = given_z_vel;
Expand Down Expand Up @@ -270,22 +332,62 @@ void record_to_perm_abs_event(Coords *position, double *k, double p, double time
add_point = 0;
}

if (storage->stored_xwidth != 0) {
if (position->x < 0.5*storage->stored_xwidth && position->x > -0.5*storage->stored_xwidth)
add_point = 1;
else
add_point = 0;
}

if (storage->stored_yheight != 0) {
if (position->y < 0.5*storage->stored_yheight && position->y > -0.5*storage->stored_yheight)
add_point = 1;
else
add_point = 0;
}

if (storage->stored_zdepth != 0) {
if (position->z < 0.5*storage->stored_zdepth && position->z > -0.5*storage->stored_xwidth)
add_point = 1;
else
add_point = 0;
}

if (add_point == 1) {
//printf("storage was set \n");

double given_x_pos, given_y_pos, given_z_pos;
double used_x_pos, used_y_pos, used_z_pos;
coords_get(*position, &given_x_pos, &given_y_pos, &given_z_pos);


if (storage->stored_xbins != 0) {
used_x_pos = round_to_nearest_bin(given_x_pos, storage->stored_xwidth, storage->stored_xbins);
} else {
used_x_pos = given_x_pos;
}

if (storage->stored_ybins != 0) {
used_y_pos = round_to_nearest_bin(given_y_pos, storage->stored_yheight, storage->stored_ybins);
} else {
used_y_pos = given_y_pos;
}

if (storage->stored_zbins != 0) {
used_z_pos = round_to_nearest_bin(given_z_pos, storage->stored_zdepth, storage->stored_zbins);
} else {
used_z_pos = given_z_pos;
}

double given_x_vel, given_y_vel, given_z_vel;
given_x_vel = k[0]*K2V;
given_y_vel = k[1]*K2V;
given_z_vel = k[2]*K2V;

_class_particle _localparticle;

_localparticle.x = given_x_pos;
_localparticle.y = given_y_pos;
_localparticle.z = given_z_pos;
_localparticle.x = used_x_pos;
_localparticle.y = used_y_pos;
_localparticle.z = used_z_pos;

_localparticle.vx = given_x_vel;
_localparticle.vy = given_y_vel;
Expand Down Expand Up @@ -478,8 +580,17 @@ INITIALIZE
this_abs_storage.order = order_total;
this_abs_storage.order_in_this_volume = order_volume;

this_abs_storage.stored_xwidth = xwidth;
this_abs_storage.stored_xbins = (int) xbins;

this_abs_storage.stored_yheight = yheight;
this_abs_storage.stored_ybins = (int) ybins;

this_abs_storage.stored_zdepth = zdepth;
this_abs_storage.stored_zbins = (int) zbins;

this_abs_storage.temp_abs_event_data.num_elements=0;

this_abs_storage.temp_abs_event_data.allocated_elements = 10;
this_abs_storage.temp_abs_event_data.elements = malloc(this_abs_storage.temp_abs_event_data.allocated_elements*sizeof(struct temp_abs_event_data_element_struct));

Expand Down
Loading

0 comments on commit 8a72403

Please sign in to comment.