Skip to content

Commit 52e9def

Browse files
authored
Merge pull request #1993 from mccode-dev/res-sample-save-cov-mtrx
Add infrastructure to save calculated resolution/covariance matrices …
2 parents f20c696 + 2b88aab commit 52e9def

1 file changed

Lines changed: 62 additions & 1 deletion

File tree

mcstas-comps/monitors/Res_monitor.comp

Lines changed: 62 additions & 1 deletion
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

@@ -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

@@ -319,7 +341,7 @@ SAVE %{
319341
#endif
320342

321343
tl2_save_events(reso_events, reso_probabilities, event_filename, num_events);
322-
}
344+
}
323345

324346
double cov[4*4], res[4*4];
325347
if(tl2_reso(reso_events, reso_probabilities, cov, res, num_events)) {
@@ -331,6 +353,39 @@ SAVE %{
331353
else {
332354
printf("Error: Resolution matrix could not be calculated.");
333355
}
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);
334389
}
335390
%}
336391

@@ -343,6 +398,12 @@ FINALLY %{
343398
free(reso_events);
344399
free(reso_probabilities);
345400
}
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);
346407
%}
347408

348409

0 commit comments

Comments
 (0)