2727* gm: [1] M-value of the guide coating
2828* delta1: [m] Optional focal displacement of guide wrt source
2929* delta2: [m] Optional focal displacement of guide wrt sample position
30+ * Nbins: [1] Number of bins on Wavelength / Brilliance transfer monitors
3031*
3132* %L
3233*
3334* %E
3435*******************************************************************************/
35- DEFINE INSTRUMENT BTsimple(lambda=10, dlambda=9.9, maxhd=3, maxvd=3, gw=0.1, gh=0.1, gL=50, gm=6, delta1=0, delta2=0)
36+ DEFINE INSTRUMENT BTsimple(lambda=10, dlambda=9.9, maxhd=3, maxvd=3, gw=0.1, gh=0.1, gL=50, gm=6, delta1=0, delta2=0, int Nbins=101 )
3637
3738DECLARE
3839%{
39- /* Arrays for storing the calculated brilliance transfer */
40- double BT_N[101] ;
41- double BT_p[101] ;
42- double BT_p2[101] ;
40+ /* Arrays for storing the calculated brilliance transfer */
41+ DArray1d BT_N;
42+ DArray1d BT_p;
43+ DArray1d BT_p2;
4344%}
4445
4546USERVARS
4647%{
47- /* Variables for calculation of neutron divergence */
48- double VertDiv;
49- double HorDiv;
48+ /* Variables for calculation of neutron divergence */
49+ double VertDiv;
50+ double HorDiv;
5051%}
5152
5253INITIALIZE
5354%{
55+ BT_N = create_darr1d(Nbins);
56+ BT_p = create_darr1d(Nbins);
57+ BT_p2 = create_darr1d(Nbins);
5458%}
5559
5660TRACE
@@ -84,7 +88,7 @@ EXTEND %{
8488%}
8589
8690/* Measure incoming phase-space */
87- COMPONENT BT_in = L_monitor(xwidth=0.02, yheight=0.02, filename="BT_in.dat",Lmin=lambda-dlambda, Lmax=lambda+dlambda, nL=101 , restore_neutron=1)
91+ COMPONENT BT_in = L_monitor(xwidth=0.02, yheight=0.02, filename="BT_in.dat",Lmin=lambda-dlambda, Lmax=lambda+dlambda, nL=Nbins , restore_neutron=1)
8892WHEN ((VertDiv <= maxvd) && (HorDiv <= maxhd)) AT (0,0,2.0) RELATIVE origin
8993
9094
@@ -121,7 +125,7 @@ EXTEND %{
121125%}
122126
123127/* Measure outgoing phase-space @ sample position */
124- COMPONENT BT_out = L_monitor(xwidth=0.02, yheight=0.02, filename="BT_out.dat",Lmin=lambda-dlambda, Lmax=lambda+dlambda, nL=101 , restore_neutron=1)
128+ COMPONENT BT_out = L_monitor(xwidth=0.02, yheight=0.02, filename="BT_out.dat",Lmin=lambda-dlambda, Lmax=lambda+dlambda, nL=Nbins , restore_neutron=1)
125129WHEN ((VertDiv <= maxvd) && (HorDiv <= maxhd)) AT (0,0,2.0+gL) RELATIVE guide
126130
127131
@@ -138,47 +142,50 @@ AT (0,0,0) RELATIVE PREVIOUS
138142FINALLY
139143%{
140144/* This adds another "monitor" that measures BT_out / BT_in */
141- /* In MPI-mode renormalisation by mpi_node_count needs doing */
142- int j;
143- double* tmpN;
144- double* tmpp1;
145- double* tmpp2;
146- double* tmpd1;
147- double* tmpd2;
148- tmpN =COMP_GETPAR(BT_out,L_N);
149- tmpp1=COMP_GETPAR(BT_in, L_p);
150- tmpp2=COMP_GETPAR(BT_out,L_p);
151- tmpd1=COMP_GETPAR(BT_in, L_p2);
152- tmpd2=COMP_GETPAR(BT_out,L_p2);
153- for (j=0;j<101;j++) {
145+ /* In MPI-mode do the calculation on master only. */
146+ #ifdef USE_MPI
147+ if(mpi_node_rank == mpi_node_root) {
148+ #endif
149+ int j;
150+ double* tmpN;
151+ double* tmpp1;
152+ double* tmpp2;
153+ double* tmpd1;
154+ double* tmpd2;
155+ tmpN =COMP_GETPAR(BT_out,L_N);
156+ tmpp1=COMP_GETPAR(BT_in, L_p);
157+ tmpp2=COMP_GETPAR(BT_out,L_p);
158+ tmpd1=COMP_GETPAR(BT_in, L_p2);
159+ tmpd2=COMP_GETPAR(BT_out,L_p2);
154160
155- BT_N[j]=tmpN[j];
156- if (tmpp1[j] != 0) {
157- BT_p[j]=tmpp2[j]/tmpp1[j];
158- } else {
159- BT_p[j]=0;
161+ for (j=0;j<Nbins;j++) {
162+ BT_N[j]=tmpN[j];
163+ if (tmpp1[j] != 0) {
164+ BT_p[j]=tmpp2[j]/tmpp1[j];
165+ } else {
166+ BT_p[j]=0;
167+ }
168+ if ((tmpp1[j] != 0) && (tmpp2[j] != 0)) {
169+ BT_p2[j]=sqrt((tmpd1[j]/tmpp1[j])*(tmpd1[j]/tmpp1[j]) + (tmpd2[j]/tmpp2[j])*(tmpd2[j]/tmpp2[j]));
170+ } else
171+ BT_p2[j]=0;
160172 }
161- if ((tmpp1[j] != 0) && (tmpp2[j] != 0)) {
162- BT_p2[j]=sqrt((tmpd1[j]/tmpp1[j])*(tmpd1[j]/tmpp1[j]) + (tmpd2[j]/tmpp2[j])*(tmpd2[j]/tmpp2[j]));
163- } else
164- BT_p2[j]=0;
165- #if defined (USE_MPI)
166- BT_p[j] /= mpi_node_count;
167- BT_p2[j] /= mpi_node_count;
168- #endif
169-
173+ #ifdef USE_MPI
170174 }
175+ // Non-master MPI nodes wait here before we write the file to disk
176+ MPI_Barrier(MPI_COMM_WORLD);
177+ #endif
171178 // we can not call the DETECTOR_OUT_1D as it transparently makes use of
172179 // NAME_CURRENT_COMP and POS_A_CURRENT_COMP, which are set to match '_comp'
173180 Rotation Rot;
174181 rot_set_rotation(Rot,0,0,0);
175182 mcdetector_out_1D(
176- "Brilliance transfer",
177- "Wavelength [AA]",
178- "BT",
179- "L", lambda-dlambda, lambda+dlambda, 101 ,
180- &BT_N[0],&BT_p[0],&BT_p2[0],
181- "Brilliance_transfer", "BTransfer", coords_set(0,0,0),Rot,9999);
183+ "Brilliance transfer",
184+ "Wavelength [AA]",
185+ "BT",
186+ "L", lambda-dlambda, lambda+dlambda, Nbins ,
187+ &BT_N[0],&BT_p[0],&BT_p2[0],
188+ "Brilliance_transfer", "BTransfer", coords_set(0,0,0),Rot,9999);
182189
183190%}
184191
0 commit comments