Skip to content

Commit

Permalink
Merge pull request #1823 from McStasMcXtrace/willend-sync-monitor_nD
Browse files Browse the repository at this point in the history
Sync variants of Monitor_nD acc/noacc McStas, McXtrace
  • Loading branch information
willend authored Jan 24, 2025
2 parents bb8fbb6 + 3cf9fb1 commit 5c25e29
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
34 changes: 14 additions & 20 deletions mcstas-comps/share/monitor_nd_noacc-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void Monitor_nd_noaccInit(Monitornd_noaccDefines_type *DEFS,
Vars->Coord_NumberNoPixel=0; /* same but without counting PixelID */

Vars->Buffer_Block = MONND_BUFSIZ; /* Buffer size for list or auto limits */
Vars->Neutron_Counter = -1; /* event counter, simulation total counts is mcget_ncount() */
Vars->Neutron_Counter = 0; /* event counter, simulation total counts is mcget_ncount() */
Vars->Buffer_Counter = 0; /* index in Buffer size (for realloc) */
Vars->Buffer_Size = 0;
Vars->He3_pressure = 0;
Expand Down Expand Up @@ -1000,6 +1000,9 @@ int Monitor_nd_noaccTrace(Monitornd_noaccDefines_type *DEFS, Monitornd_noaccVari
long While_Buffer=0;
char Set_Vars_Coord_Type = DEFS->COORD_NONE;

long long ParticleCount;
ParticleCount=Vars->Neutron_Counter++;

/* the logic below depends mainly on:
Flag_List: 1=store 1 buffer, 2=list all, 3=re-use buffer
Flag_Auto_Limits: 0 (no auto limits/list), 1 (store events into Buffer), 2 (re-emit store events)
Expand Down Expand Up @@ -1042,18 +1045,17 @@ int Monitor_nd_noaccTrace(Monitornd_noaccDefines_type *DEFS, Monitornd_noaccVari
Vars->Buffer_Block = Vars->Buffer_Size;
Vars->Buffer_Counter = 0;
Vars->Neutron_Counter = 0;

}
else
{
Vars->Mon2D_Buffer = (double *)realloc(Vars->Mon2D_Buffer, (Vars->Coord_Number+1)*(Vars->Neutron_Counter+Vars->Buffer_Block)*sizeof(double));
Vars->Mon2D_Buffer = (double *)realloc(Vars->Mon2D_Buffer, (Vars->Coord_Number+1)*(ParticleCount+Vars->Buffer_Block)*sizeof(double));
if (Vars->Mon2D_Buffer == NULL)
{ printf("Monitor_nD: %s cannot reallocate Vars->Mon2D_Buffer[%li] (%li). Skipping.\n", Vars->compcurname, i, (long int)(Vars->Neutron_Counter+Vars->Buffer_Block)*sizeof(double)); Vars->Flag_List = 1; }
else { Vars->Buffer_Counter = 0; Vars->Buffer_Size = Vars->Neutron_Counter+Vars->Buffer_Block; }
{ printf("Monitor_nD: %s cannot reallocate Vars->Mon2D_Buffer[%li] (%li). Skipping.\n", Vars->compcurname, i, (long int)(ParticleCount+Vars->Buffer_Block)*sizeof(double)); Vars->Flag_List = 1; }
else { Vars->Buffer_Counter = 0; Vars->Buffer_Size = ParticleCount+Vars->Buffer_Block; }
}
} /* end if Buffer realloc */
#endif

char outsidebounds=0;
while (!While_End)
{ /* we generate Coord[] and Coord_index[] from Buffer (auto limits) or passing neutron */
Expand Down Expand Up @@ -1254,7 +1256,7 @@ int Monitor_nd_noaccTrace(Monitornd_noaccDefines_type *DEFS, Monitornd_noaccVari
else
if (Set_Vars_Coord_Type == DEFS->COORD_LAMBDA) { XY = sqrt(_particle->vx*_particle->vx+_particle->vy*_particle->vy+_particle->vz*_particle->vz); XY *= V2K; if (XY != 0) XY = 2*PI/XY; }
else
if (Set_Vars_Coord_Type == DEFS->COORD_NCOUNT) XY = Vars->Neutron_Counter;
if (Set_Vars_Coord_Type == DEFS->COORD_NCOUNT) XY = _particle->_uid;
else
if (Set_Vars_Coord_Type == DEFS->COORD_ANGLE)
{ XY = sqrt(_particle->vx*_particle->vx+_particle->vy*_particle->vy);
Expand Down Expand Up @@ -1381,13 +1383,9 @@ int Monitor_nd_noaccTrace(Monitornd_noaccDefines_type *DEFS, Monitornd_noaccVari
{ /* now store Coord into Buffer (no index needed) if necessary (list or auto limits) */
if ((Vars->Buffer_Counter < Vars->Buffer_Block) && ((Vars->Flag_List) || (Vars->Flag_Auto_Limits == 1)))
{
#pragma acc atomic
Vars->Neutron_Counter = Vars->Neutron_Counter + 1;
for (i = 0; i <= Vars->Coord_Number; i++)
{
// This is is where the list is appended. How to make this "atomic"?
#pragma acc atomic write
Vars->Mon2D_Buffer[i + Vars->Neutron_Counter*(Vars->Coord_Number+1)] = Coord[i];
Vars->Mon2D_Buffer[i + ParticleCount*(Vars->Coord_Number+1)] = Coord[i];
}
#pragma acc atomic
Vars->Buffer_Counter = Vars->Buffer_Counter + 1;
Expand Down Expand Up @@ -1994,13 +1992,11 @@ void Monitor_nd_noaccMcDisplay(Monitornd_noaccDefines_type *DEFS,
}
if (Vars->Flag_mantid) {
/* First define the base pixel type */
#ifndef OPENACC
double dt, dy;
dt = (Vars->Coord_Max[1]-Vars->Coord_Min[1])/Vars->Coord_Bin[1];
dy = (Vars->Coord_Max[2]-Vars->Coord_Min[2])/Vars->Coord_Bin[2];
printf("MANTID_BANANA_DET: %g, %g, %g, %g, %g, %li, %li, %g\n", radius,
Vars->Coord_Min[1],Vars->Coord_Max[1], Vars->Coord_Min[2],Vars->Coord_Max[2], Vars->Coord_Bin[1], Vars->Coord_Bin[2], Vars->Coord_Min[4]);
#endif
printf("MANTID_BANANA_DET: %g, %g, %g, %g, %g, %li, %li, %llu\n", radius,
Vars->Coord_Min[1],Vars->Coord_Max[1], Vars->Coord_Min[2],Vars->Coord_Max[2], Vars->Coord_Bin[1], Vars->Coord_Bin[2], (long long unsigned)Vars->Coord_Min[4]);
}
}
/* disk (circle) */
Expand All @@ -2022,14 +2018,12 @@ void Monitor_nd_noaccMcDisplay(Monitornd_noaccDefines_type *DEFS,
(double)xmin, (double)ymin, 0.0);

if (Vars->Flag_mantid) {
#ifndef OPENACC
/* First define the base pixel type */
double dx, dy;
dx = (Vars->Coord_Max[1]-Vars->Coord_Min[1])/Vars->Coord_Bin[1];
dy = (Vars->Coord_Max[2]-Vars->Coord_Min[2])/Vars->Coord_Bin[2];
printf("MANTID_RECTANGULAR_DET: %g, %g, %g, %g, %li, %li, %g\n",
Vars->Coord_Min[1],Vars->Coord_Max[1], Vars->Coord_Min[2],Vars->Coord_Max[2], Vars->Coord_Bin[1], Vars->Coord_Bin[2], Vars->Coord_Min[4]);
#endif
printf("MANTID_RECTANGULAR_DET: %g, %g, %g, %g, %li, %li, %llu\n",
Vars->Coord_Min[1],Vars->Coord_Max[1], Vars->Coord_Min[2],Vars->Coord_Max[2], Vars->Coord_Bin[1], Vars->Coord_Bin[2], (long long unsigned)Vars->Coord_Min[4]);
}
}
/* full cylinder/banana */
Expand Down
2 changes: 0 additions & 2 deletions mcstas-comps/share/monitor_nd_noacc-lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@
char compcurname[128];
Coords compcurpos;
Rotation compcurrot;
} MonitornD_Variables_type;

} Monitornd_noaccVariables_type;

/* monitor_nd_noacc-lib function prototypes */
Expand Down
26 changes: 21 additions & 5 deletions mcxtrace-comps/share/monitor_nd-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ void Monitor_nD_Init(MonitornD_Defines_type *DEFS,
}
/* no Mon2D allocated for
* (Vars->Coord_Number != 2) && !Vars->Flag_Multiple && Vars->Flag_List */

Vars->psum = 0;
Vars->p2sum = 0;
Vars->Nsum = 0;
Expand Down Expand Up @@ -985,7 +986,7 @@ int Monitor_nD_Trace(MonitornD_Defines_type *DEFS, MonitornD_Variables_type *Var
else
if (Set_Vars_Coord_Type == DEFS->COORD_LAMBDA) { if (k!=0) XY = 2*M_PI/k; }
else
if (Set_Vars_Coord_Type == DEFS->COORD_NCOUNT) XY = ParticleCount;
if (Set_Vars_Coord_Type == DEFS->COORD_NCOUNT) XY = _particle->_uid;
else
if (Set_Vars_Coord_Type == DEFS->COORD_ANGLE)
{ XY = sqrt(_particle->kx*_particle->kx+_particle->ky*_particle->ky);
Expand Down Expand Up @@ -1147,6 +1148,7 @@ int Monitor_nD_Trace(MonitornD_Defines_type *DEFS, MonitornD_Variables_type *Var
/* ========================================================================= */
/* Monitor_nD_Save: this routine is used to save data files */
/* ========================================================================= */

MCDETECTOR Monitor_nD_Save(MonitornD_Defines_type *DEFS, MonitornD_Variables_type *Vars)
{
char *fname;
Expand Down Expand Up @@ -1339,7 +1341,7 @@ MCDETECTOR Monitor_nD_Save(MonitornD_Defines_type *DEFS, MonitornD_Variables_typ
if (strchr(Vars->Mon_File,'.') == NULL)
{ strcat(fname, "."); strcat(fname, Vars->Coord_Var[i]); }
}
if (Vars->Flag_Verbose) printf("Monitor_nD: %s write monitor file %s List (%lix%li).\n", Vars->compcurname, fname,Vars->Photon_Counter,Vars->Coord_Number);
if (Vars->Flag_Verbose) printf("Monitor_nD: %s write monitor file %s List (%lix%li).\n", Vars->compcurname, fname,(long int)Vars->Photon_Counter,Vars->Coord_Number);

/* handle the type of list output */
strcpy(label, Vars->Monitor_Label);
Expand Down Expand Up @@ -1510,7 +1512,18 @@ MCDETECTOR Monitor_nD_Save(MonitornD_Defines_type *DEFS, MonitornD_Variables_typ
if (Vars->Coord_Bin[1]*Vars->Coord_Bin[2] > 1
&& Vars->Flag_signal == DEFS->COORD_P)
strcat(label, " per bin");

if (Vars->Flag_List) {
detector = mcdetector_out_2D_list(
label,
Vars->Coord_Label[1],
Vars->Coord_Label[2],
min1d, max1d,
min2d, max2d,
Vars->Coord_Bin[1],
Vars->Coord_Bin[2],
p0m,p1m,p2m,
fname, Vars->compcurname, Vars->compcurpos, Vars->compcurrot,Vars->option);
} else {
detector = mcdetector_out_2D(
label,
Vars->Coord_Label[1],
Expand All @@ -1532,6 +1545,7 @@ MCDETECTOR Monitor_nD_Save(MonitornD_Defines_type *DEFS, MonitornD_Variables_typ
}
}
free(fname);
}
return(detector);
} /* end Monitor_nD_Save */

Expand Down Expand Up @@ -1658,8 +1672,10 @@ void Monitor_nD_McDisplay(MonitornD_Defines_type *DEFS,
int issphere;
issphere = (abs(Vars->Flag_Shape) == DEFS->SHAPE_SPHERE);
width = (hdiv_max-hdiv_min)/NH;
if (!issphere) NV=1; /* cylinder has vertical axis */
else height= (vdiv_max-vdiv_min)/NV;
if (!issphere) {
NV=1; /* cylinder has vertical axis */
}
height= (vdiv_max-vdiv_min)/NV;

/* check width and height of elements (sphere) to make sure the nb
of plates remains limited */
Expand Down

0 comments on commit 5c25e29

Please sign in to comment.