Skip to content

Commit 0e290c2

Browse files
authored
Merge pull request #1995 from mccode-dev/res-sample-save-cov-mtrx
Sync Res_monitor <-> TOFRes_monitor
2 parents e0c3024 + 2c02a70 commit 0e290c2

2 files changed

Lines changed: 77 additions & 4 deletions

File tree

mcstas-comps/monitors/Res_monitor.comp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* scattering events in the sample along with their intensities in the
2222
* detector. The output file may be analyzed with the mcresplot front-end.
2323
*
24-
* Example: Res_monitor(filename="Output.res", res_sample_comp=RSample, xmin=-0.1, xmax=0.1, ymin=-0.1, ymax=0.1)
24+
* Example: Res_monitor(filename="Output.res", res_sample_comp="RSample", xmin=-0.1, xmax=0.1, ymin=-0.1, ymax=0.1)
2525
*
2626
* Setting the monitor geometry.
2727
* The optional parameter 'options' may be set as a string with the
@@ -43,7 +43,7 @@
4343
* zmin: [m] Lower z bound of detector opening
4444
* zmax: [m] Upper z bound of detector opening
4545
* filename: [string] Name of output file. If unset, use automatic name
46-
* res_sample_comp: [no quotes] Name of Res_sample component in the instrument definition
46+
* res_sample_comp: [WITH quotes] Name of Res_sample component in the instrument definition
4747
* bufsize: [1] Number of events to store. Use 0 to store all
4848
*
4949
* OPTIONAL PARAMETERS (derived from Monitor_nD)

mcstas-comps/monitors/TOFRes_monitor.comp

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ DECLARE%{
102102
char res_rx_var[20];
103103
char res_ry_var[20];
104104
char res_rz_var[20];
105+
/* Arrays for storing resolution matrix */
106+
DArray2d Covar_p;
107+
DArray2d Covar_p2;
108+
DArray2d Covar_N;
109+
DArray2d Res_p;
110+
DArray2d Res_p2;
111+
DArray2d Res_N;
112+
int num_cores;
105113
%}
106114

107115

@@ -110,7 +118,7 @@ INITIALIZE %{
110118
/* Use instance name for monitor output if no input was given */
111119
if (!strcmp(filename, "\0"))
112120
sprintf(filename, "%s", NAME_CURRENT_COMP);
113-
121+
114122
num_events = 0;
115123
reso_events = 0;
116124
reso_probabilities = 0;
@@ -175,6 +183,20 @@ INITIALIZE %{
175183
sprintf(res_rx_var, "res_rx_%i", index);
176184
sprintf(res_ry_var, "res_ry_%i", index);
177185
sprintf(res_rz_var, "res_rz_%i", index);
186+
if(live_calc) {
187+
/* Allocate arrays to save cov and res matrix */
188+
Covar_p = create_darr2d(4, 4);
189+
Covar_p2 = create_darr2d(4, 4);
190+
Covar_N = create_darr2d(4, 4);
191+
Res_p = create_darr2d(4, 4);
192+
Res_p2 = create_darr2d(4, 4);
193+
Res_N = create_darr2d(4, 4);
194+
#ifdef USE_MPI
195+
num_cores=mpi_node_count;
196+
#else
197+
num_cores=1;
198+
#endif
199+
}
178200
%}
179201

180202

@@ -302,12 +324,24 @@ TRACE %{
302324

303325

304326
SAVE %{
327+
int dbg_save_events = 0;
328+
305329
/* save results, but do not free pointers */
306330
Monitor_nD_Save(&DEFS, &Vars);
307331

308332
/* live calculation */
309333
if(live_calc && num_events > 0) {
310-
/*tl2_save_events(reso_events, reso_probabilities, "reso_events.dat", num_events);*/
334+
if(dbg_save_events) {
335+
/* save individual neutron events */
336+
#ifndef USE_MPI
337+
const char* event_filename = "reso_events.dat";
338+
#else
339+
char event_filename[256];
340+
sprintf(event_filename, "reso_events_%d.dat", mpi_node_rank);
341+
#endif
342+
343+
tl2_save_events(reso_events, reso_probabilities, event_filename, num_events);
344+
}
311345

312346
double cov[4*4], res[4*4];
313347
if(tl2_reso(reso_events, reso_probabilities, cov, res, num_events)) {
@@ -319,6 +353,39 @@ SAVE %{
319353
else {
320354
printf("Error: Resolution matrix could not be calculated.");
321355
}
356+
int i,j;
357+
for(i=0;i<4;i++) {
358+
for(j=0;j<4;j++) {
359+
/* "Events" */
360+
Covar_N[i][j]=1;
361+
Res_N[i][j]=1;
362+
/* Covariance/resolution matrixces (potentiall pr. MPI node) */
363+
Covar_p[i][j]=cov[4*i+j]/num_cores;
364+
Res_p[i][j]=res[4*i+j]/num_cores;
365+
/* "errors" */
366+
Covar_p2[i][j]=(cov[4*i+j]/num_cores)*(cov[4*i+j]/num_cores);
367+
Res_p2[i][j]=(res[4*i+j]/num_cores)*(res[4*i+j]/num_cores);
368+
}
369+
}
370+
/* Nasty call to DETECTOR_OUT_2D to store covariance matrix */
371+
char covar_fname[1024];
372+
char res_fname[1024];
373+
sprintf(covar_fname,"%s_%s", _comp->_name, "covar");
374+
sprintf(res_fname,"%s_%s", _comp->_name, "resol");
375+
DETECTOR_OUT_2D("Covariance",
376+
"Columns",
377+
"Rows",
378+
0.0, 4.0, 0.0, 4.0,
379+
4, 4,
380+
&Covar_N[0][0],&Covar_p[0][0],&Covar_p2[0][0],
381+
covar_fname);
382+
DETECTOR_OUT_2D("Resolution",
383+
"Columns",
384+
"Rows",
385+
0.0, 4.0, 0.0, 4.0,
386+
4, 4,
387+
&Res_N[0][0],&Res_p[0][0],&Res_p2[0][0],
388+
res_fname);
322389
}
323390
%}
324391

@@ -331,6 +398,12 @@ FINALLY %{
331398
free(reso_events);
332399
free(reso_probabilities);
333400
}
401+
destroy_darr2d(Covar_p);
402+
destroy_darr2d(Covar_p2);
403+
destroy_darr2d(Covar_N);
404+
destroy_darr2d(Res_p);
405+
destroy_darr2d(Res_p2);
406+
destroy_darr2d(Res_N);
334407
%}
335408

336409

0 commit comments

Comments
 (0)