Skip to content

Commit 2ee65e0

Browse files
authored
Merge pull request #2190 from mccode-dev/comp-updates-etc-from-ISIS
Comp updates etc from ISIS
2 parents 3ce16dd + a6a39b0 commit 2ee65e0

File tree

38 files changed

+3504550
-28
lines changed

38 files changed

+3504550
-28
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*******************************************************************************
2+
*
3+
* McStas, neutron ray-tracing package
4+
* Copyright (C) 1997-2008, All rights reserved
5+
* Risoe National Laboratory, Roskilde, Denmark
6+
* Institut Laue Langevin, Grenoble, France
7+
*
8+
* Component: Commodus_I3
9+
*
10+
*
11+
* %I
12+
* Written by: G. Skoro, based on ViewModerator4 from S. Ansell
13+
* Date: July 2022
14+
* Origin: ISIS
15+
*
16+
* ISIS Moderators (Tested with McStas 3.1 (Windows))
17+
*
18+
* %D
19+
* Produces a neutron distribution at the ISIS TS1 or TS2 corresponding moderator front face position.
20+
* The Face argument determines which TS1 or TS2 beamline is to be sampled by using corresponding file.
21+
* Neutrons are created having a range of energies determined by the E0 and E1 arguments.
22+
* Trajectories are produced such that they pass through the moderator face (defined by
23+
* modXsize and modZsize) and a focusing rectangle (defined by xw, yh and dist).
24+
* --- HOW TO USE ---
25+
*
26+
* Example: Commodus_I3(Face="TS1verBase2016_LH8020_newVM-var_South04_Merlin.mcstas", E0 = E_min, E1 = E_max,
27+
* modXsize = 0.12, modZsize = 0.12, xw = 0.094, yh = 0.094, dist = 1.6)
28+
*
29+
* MERLIN simulation; TS1 baseline model.
30+
* In this example, xw and yh are chosen to be identical to the shutter opening dimension.
31+
* dist = 1.6 is the real distance to the shutter front face:
32+
* (This is TimeOffset value (=160 [cm]) from TS1verBase2016_LH8020_newVM-var_South04_Merlin.mcstas file.)
33+
*
34+
* N.B. Absolute normalization: The result of the Mc-Stas simulation will show neutron intensity for beam current of 1 uA.
35+
*
36+
*
37+
* %P
38+
* INPUT PARAMETERS:
39+
*
40+
* Face: [string] TS1 (or TS2) instrument McStas filename
41+
* E0: [meV] Lower edge of energy distribution
42+
* E1: [meV] Upper edge of energy distribution
43+
* modXsize: [m] Moderator width
44+
* modZsize: [m] Moderator height
45+
* xw: [m] Width of focusing rectangle
46+
* yh: [m] Height of focusing rectangle
47+
* dist: [m] Distance from moderator surface to the focusing rectangle
48+
* verbose: [int] Flag to output debugging information
49+
* beamcurrent: [uA] ISIS beam current
50+
*
51+
* %E
52+
*******************************************************************************/
53+
DEFINE COMPONENT Commodus_I3
54+
SETTING PARAMETERS (string Face="TS1_S04_Merlin.mcstas",E0, E1, modPosition=0,
55+
dist=1.7, int verbose=0, beamcurrent=1,
56+
modXsize=0.12,modZsize=0.12,xw=0.094,yh=0.094)
57+
58+
SHARE INHERIT ViewModISIS
59+
60+
DECLARE INHERIT ViewModISIS EXTEND
61+
%{
62+
double xwidth;
63+
double yheight;
64+
double focus_xw;
65+
double focus_yh;
66+
%}
67+
68+
INITIALIZE
69+
%{
70+
xwidth=modXsize;
71+
yheight=modZsize;
72+
focus_xw=xw;
73+
focus_yh=yh;
74+
75+
/* READ IN THE ENERGY FILE */
76+
FILE* Tfile;
77+
78+
Nsim=mcget_ncount(); // Number of points requested.
79+
80+
Tfile=openFile(Face); // Get open file
81+
rtE0=convertEnergy(E0);
82+
rtE1=convertEnergy(E1);
83+
orderEnergy(&rtE0,&rtE1);
84+
85+
readHtable(Tfile,rtE0,rtE1, &TS, modPosition, xwidth, yheight, verbose);
86+
fclose(Tfile);
87+
// Below pragma was needed with PGI 19.x, compilation fails with NVC 20.7
88+
//#pragma acc declare create( TS )
89+
/**********************************************************************/
90+
91+
Tnpts=0;
92+
Ncount=0;
93+
94+
fprintf(stderr,"Face == %s \n",Face);
95+
fprintf(stderr,"Number of Energy Points == %d\n",TS.nEnergy);
96+
if (dist<0)
97+
{
98+
dist=TS.rdumMid;
99+
fprintf(stderr,"Setting distance to moderator surface == %g\n",
100+
dist);
101+
}
102+
/* Do solid angle correction */
103+
angleArea= strArea(TS, focus_xw, focus_yh, dist);
104+
// Below pragma was needed with PGI 19.x, compilation fails with NVC 20.7
105+
//#pragma acc update host( TS )
106+
fprintf(stderr,"Totals:: %g %d %d \n",TS.Total,TS.nEnergy,TS.nTime);
107+
108+
109+
%}
110+
111+
TRACE INHERIT ViewModISIS
112+
113+
MCDISPLAY INHERIT ViewModISIS
114+
115+
END

mcstas-comps/contrib/ISIS_moderator.comp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ double polInterp(double* X,double* Y,int Psize,double Aim)
127127
double out,errOut; /* out put variables */
128128
double *C = malloc(Psize*sizeof(double));
129129
double *D = malloc(Psize*sizeof(double));
130+
if (!C || !D) {
131+
#ifndef OPENACC
132+
fprintf(stderr,"Error in ISIS_moderator: memory allocation failure. Exit!\n");
133+
exit(-1);
134+
#endif
135+
}
130136
double testDiff,diff;
131137

132138
double w,den,ho,hp; /* intermediate variables */
@@ -225,6 +231,10 @@ int cmdnumberD(char *mc,double* num)
225231
(mc[i]=='\t' || mc[i]==' ' || mc[i]==',');i++);
226232
if(i==len || !mc[i]) return 0;
227233
ss=malloc(sizeof(char)*(len+1));
234+
if(!ss) {
235+
fprintf(stderr,"Error in ISIS_moderator: memory allocation failure. Exit!\n");
236+
exit(-1);
237+
}
228238

229239
for(;i<len && mc[i]!='\n' && mc[i]
230240
&& mc[i]!='\t' && mc[i]!=' ' && mc[i]!=',';i++)
@@ -392,10 +402,8 @@ int readHtable(FILE* TFile,const double Einit,const double Eend, Source *TS)
392402
Status Flag::
393403
Ftime=1 :: [time ] Reading Time : Data : Err [Exit on Total]
394404

395-
396-
/*
397405
Double Read File to determine how many bins and
398-
memery size
406+
memory size
399407
*/
400408
if (!TFile) return(0);
401409
Ea=0.0;

mcstas-comps/contrib/Multilayer_Sample.comp

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Date: June 2010
1111
* Origin: ISIS
1212
*
13-
* Multilayer Reflecting sample using matrix Formula.
13+
* Multilayer Reflecting sample using matrix Formula, v2 with 'nrepeats' for repeating multilayer structure.
1414
*
1515
* %D
1616
*
@@ -45,12 +45,13 @@
4545
* target_index: [1] Used in combination with focus_xw and focus_yh to indicate solid angle for incoherent scattering.
4646
* focus_xw: [m] Used in combination with target_index and focus_yh to indicate solid angle for incoherent scattering.
4747
* focus_yh: [m] Used in combination with focus_xw and target_index to indicate solid angle for incoherent scattering.
48+
* nrepeats: [1] Number of repeats of the block of layers appart from the superphase and substrate
4849
*
4950
* %E
5051
*******************************************************************************/
5152

5253
DEFINE COMPONENT Multilayer_Sample
53-
SETTING PARAMETERS (xwidth = 0.2, zlength = 0.2, nlayer=1,
54+
SETTING PARAMETERS (xwidth=0.2, zlength=0.2, int nlayer=1, int nrepeats=1,
5455
vector sldPar={0.0,2.0e-6,0.0e-6},
5556
vector dPar={20.0},
5657
vector sigmaPar={5.0,5.0},
@@ -174,6 +175,7 @@ TRACE
174175
double dbl0,dbl1,tvar;
175176
int arrsize=nlayer+2;
176177
int i;
178+
int j;
177179

178180
gsl_complex rnf,rnf1;
179181
gsl_complex a12t,a22t,cr,c0,ci;
@@ -252,7 +254,9 @@ TRACE
252254
gsl_matrix_complex_set(a1,1,1,cr);
253255

254256
if(nlayer > 0){
255-
for(i=1;i<nlayer+1;i++){
257+
if(nrepeats > 1){
258+
for(j=1;j<nrepeats-1;j++){
259+
for(i=1;i<nlayer;i++){
256260
btm=gsl_complex_mul(gsl_matrix_complex_get(betan,i,0),ci);
257261
btm1=gsl_complex_mul(gsl_complex_mul_real(gsl_matrix_complex_get(betan,i,0),-1.0),ci);
258262
cbtm=gsl_complex_exp(btm);
@@ -284,7 +288,57 @@ TRACE
284288
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,1));
285289
gsl_matrix_complex_set(a3,0,1,gsl_complex_add(tcvar2,tcvar4));
286290

287-
tcvar1=gsl_matrix_complex_get(a1,1,0);
291+
tcvar1=gsl_matrix_complex_get(a1,1,0);
292+
tcvar2=gsl_complex_mul(tcvar1,gsl_matrix_complex_get(a2,0,0));
293+
tcvar3=gsl_matrix_complex_get(a1,1,1);
294+
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,0));
295+
gsl_matrix_complex_set(a3,1,0,gsl_complex_add(tcvar2,tcvar4));
296+
297+
tcvar1=gsl_matrix_complex_get(a1,1,0);
298+
tcvar2=gsl_complex_mul(tcvar1,gsl_matrix_complex_get(a2,0,1));
299+
tcvar3=gsl_matrix_complex_get(a1,1,1);
300+
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,1));
301+
gsl_matrix_complex_set(a3,1,1,gsl_complex_add(tcvar2,tcvar4));
302+
303+
gsl_matrix_complex_set(a1,0,0,gsl_matrix_complex_get(a3,0,0));
304+
gsl_matrix_complex_set(a1,0,1,gsl_matrix_complex_get(a3,0,1));
305+
gsl_matrix_complex_set(a1,1,0,gsl_matrix_complex_get(a3,1,0));
306+
gsl_matrix_complex_set(a1,1,1,gsl_matrix_complex_get(a3,1,1));
307+
}
308+
} // Exit the nrepeats block and do the final repeat with the substrate
309+
for(i=1;i<nlayer+1;i++){
310+
btm=gsl_complex_mul(gsl_matrix_complex_get(betan,i,0),ci);
311+
btm1=gsl_complex_mul(gsl_complex_mul_real(gsl_matrix_complex_get(betan,i,0),-1.0),ci);
312+
cbtm=gsl_complex_exp(btm);
313+
cbtm1=gsl_complex_exp(btm1);
314+
gsl_matrix_complex_set(a2,0,0,cbtm);
315+
tcvar1=gsl_matrix_complex_get(pfn,i,0);
316+
tcvar2=gsl_matrix_complex_get(pfn,i+1,0);
317+
if(GSL_REAL(gsl_complex_add(tcvar1,tcvar2))!=0.0 || GSL_IMAG(gsl_complex_add(tcvar1,tcvar2))!=0.0){
318+
a22t=gsl_complex_div(gsl_complex_sub(tcvar1,tcvar2),gsl_complex_add(tcvar1,tcvar2));
319+
} else {
320+
a22t=c0;
321+
}
322+
tcvar3=gsl_complex_mul(tcvar1,tcvar2);
323+
tcvar4=gsl_complex_mul_real(tcvar3,-1.0*tlc*sigmaPar[i]*sigmaPar[i]);
324+
a22t=gsl_complex_mul(a22t,gsl_complex_exp(tcvar4));
325+
gsl_matrix_complex_set(a2,0,1,gsl_complex_mul(a22t,cbtm));
326+
gsl_matrix_complex_set(a2,1,0,gsl_complex_mul(a22t,cbtm1));
327+
gsl_matrix_complex_set(a2,1,1,cbtm1);
328+
329+
tcvar1=gsl_matrix_complex_get(a1,0,0);
330+
tcvar2=gsl_complex_mul(tcvar1,gsl_matrix_complex_get(a2,0,0));
331+
tcvar3=gsl_matrix_complex_get(a1,0,1);
332+
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,0));
333+
gsl_matrix_complex_set(a3,0,0,gsl_complex_add(tcvar2,tcvar4));
334+
335+
tcvar1=gsl_matrix_complex_get(a1,0,0);
336+
tcvar2=gsl_complex_mul(tcvar1,gsl_matrix_complex_get(a2,0,1));
337+
tcvar3=gsl_matrix_complex_get(a1,0,1);
338+
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,1));
339+
gsl_matrix_complex_set(a3,0,1,gsl_complex_add(tcvar2,tcvar4));
340+
341+
tcvar1=gsl_matrix_complex_get(a1,1,0);
288342
tcvar2=gsl_complex_mul(tcvar1,gsl_matrix_complex_get(a2,0,0));
289343
tcvar3=gsl_matrix_complex_get(a1,1,1);
290344
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,0));
@@ -296,12 +350,63 @@ TRACE
296350
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,1));
297351
gsl_matrix_complex_set(a3,1,1,gsl_complex_add(tcvar2,tcvar4));
298352

353+
gsl_matrix_complex_set(a1,0,0,gsl_matrix_complex_get(a3,0,0));
354+
gsl_matrix_complex_set(a1,0,1,gsl_matrix_complex_get(a3,0,1));
355+
gsl_matrix_complex_set(a1,1,0,gsl_matrix_complex_get(a3,1,0));
356+
gsl_matrix_complex_set(a1,1,1,gsl_matrix_complex_get(a3,1,1));
357+
}
358+
} else { // if we are not repeating then just do the same as the original
359+
for(i=1;i<nlayer+1;i++){
360+
btm=gsl_complex_mul(gsl_matrix_complex_get(betan,i,0),ci);
361+
btm1=gsl_complex_mul(gsl_complex_mul_real(gsl_matrix_complex_get(betan,i,0),-1.0),ci);
362+
cbtm=gsl_complex_exp(btm);
363+
cbtm1=gsl_complex_exp(btm1);
364+
gsl_matrix_complex_set(a2,0,0,cbtm);
365+
tcvar1=gsl_matrix_complex_get(pfn,i,0);
366+
tcvar2=gsl_matrix_complex_get(pfn,i+1,0);
367+
if(GSL_REAL(gsl_complex_add(tcvar1,tcvar2))!=0.0 || GSL_IMAG(gsl_complex_add(tcvar1,tcvar2))!=0.0){
368+
a22t=gsl_complex_div(gsl_complex_sub(tcvar1,tcvar2),gsl_complex_add(tcvar1,tcvar2));
369+
} else {
370+
a22t=c0;
371+
}
372+
tcvar3=gsl_complex_mul(tcvar1,tcvar2);
373+
tcvar4=gsl_complex_mul_real(tcvar3,-1.0*tlc*sigmaPar[i]*sigmaPar[i]);
374+
a22t=gsl_complex_mul(a22t,gsl_complex_exp(tcvar4));
375+
gsl_matrix_complex_set(a2,0,1,gsl_complex_mul(a22t,cbtm));
376+
gsl_matrix_complex_set(a2,1,0,gsl_complex_mul(a22t,cbtm1));
377+
gsl_matrix_complex_set(a2,1,1,cbtm1);
378+
379+
tcvar1=gsl_matrix_complex_get(a1,0,0);
380+
tcvar2=gsl_complex_mul(tcvar1,gsl_matrix_complex_get(a2,0,0));
381+
tcvar3=gsl_matrix_complex_get(a1,0,1);
382+
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,0));
383+
gsl_matrix_complex_set(a3,0,0,gsl_complex_add(tcvar2,tcvar4));
384+
385+
tcvar1=gsl_matrix_complex_get(a1,0,0);
386+
tcvar2=gsl_complex_mul(tcvar1,gsl_matrix_complex_get(a2,0,1));
387+
tcvar3=gsl_matrix_complex_get(a1,0,1);
388+
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,1));
389+
gsl_matrix_complex_set(a3,0,1,gsl_complex_add(tcvar2,tcvar4));
390+
391+
tcvar1=gsl_matrix_complex_get(a1,1,0);
392+
tcvar2=gsl_complex_mul(tcvar1,gsl_matrix_complex_get(a2,0,0));
393+
tcvar3=gsl_matrix_complex_get(a1,1,1);
394+
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,0));
395+
gsl_matrix_complex_set(a3,1,0,gsl_complex_add(tcvar2,tcvar4));
396+
397+
tcvar1=gsl_matrix_complex_get(a1,1,0);
398+
tcvar2=gsl_complex_mul(tcvar1,gsl_matrix_complex_get(a2,0,1));
399+
tcvar3=gsl_matrix_complex_get(a1,1,1);
400+
tcvar4=gsl_complex_mul(tcvar3,gsl_matrix_complex_get(a2,1,1));
401+
gsl_matrix_complex_set(a3,1,1,gsl_complex_add(tcvar2,tcvar4));
402+
299403
gsl_matrix_complex_set(a1,0,0,gsl_matrix_complex_get(a3,0,0));
300404
gsl_matrix_complex_set(a1,0,1,gsl_matrix_complex_get(a3,0,1));
301405
gsl_matrix_complex_set(a1,1,0,gsl_matrix_complex_get(a3,1,0));
302406
gsl_matrix_complex_set(a1,1,1,gsl_matrix_complex_get(a3,1,1));
303407
}
304-
}
408+
}
409+
}
305410
ac1=gsl_matrix_complex_get(a1,1,0);
306411
ac2=gsl_complex_conjugate(ac1);
307412
ac3=gsl_matrix_complex_get(a1,0,0);

0 commit comments

Comments
 (0)