File: Scatter_logger.comp

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (145 lines) | stat: -rw-r--r-- 4,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2002, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Scatter_logger.comp
*
* %I
*
* Written by: Erik B Knudsen, Peter K Willendrup & Esben Klinkby
* Date: January 2013
* Version: $Revision: 1.12 $
* Release: McStas 2.1
* Origin: DTU Physics / DTU Nutech
*
* Logging iteractions of neutrons with components
*
* %D
* Start of the trace-region in which SCATTER events should be logged.
* Whenever a SCATTER occurs in components between this one and its counterpart Scatter_logger_stop the neutron state is logged.
* The log is kept in memory - but only for one single
* numerical neutron at a time, so there should be no real danger of memory overrun.
*
* %P
* Input parameters:
*
* %E
*******************************************************************************/

DEFINE COMPONENT Scatter_logger

SETTING PARAMETERS ()


SHARE
  %{
  struct Generalized_State_t {
    double _x,_y,_z,_vx,_vy,_vz;
    double _p,_t,_sx,_sy,_sz;
    long long int _nid;
    int _comp, _idx;
  };

#define SCATTER_LOG(Size,Store,index,overrun) do { \
    if (index<Size-1){\
      struct Generalized_State_t *bp=&(Store[index]);\
      if( (bp-1)->_p!=p || (bp-1)->_vx!=vx || (bp-1)->_vy!=vy || (bp-1)->_vz!=vz ){\
        Coords ctmp=POS_A_CURRENT_COMP;\
        Coords _r = coords_set(x,y,z);\
        Coords _v = coords_set(vx,vy,vz);\
        Coords _s = coords_set(sx,sy,sz);\
        Coords _rg,_vg,_sg;\
        Rotation _Rt;\
        rot_transpose(ROT_A_CURRENT_COMP,_Rt);\
        _rg=coords_add(rot_apply(_Rt,_r),ctmp);\
        _vg=rot_apply(_Rt,_v);\
        _sg=rot_apply(_Rt,_s);\
        coords_get(_rg,&(bp->_x),&(bp->_y),&(bp->_z));\
        coords_get(_vg,&(bp->_vx),&(bp->_vy),&(bp->_vz));\
        coords_get(_sg,&(bp->_sx),&(bp->_sy),&(bp->_sz));\
        bp->_t=t;\
        bp->_p=p;\
        bp->_nid=mcget_run_num();\
        bp->_comp=INDEX_CURRENT_COMP;\
        bp->_idx=index;\
        index++;\
      }\
    }else if(index==(Size-1) && !overrun){\
      printf("Warning (%s): Scatter_log overrun at %llu - not logging any more events\n",NAME_CURRENT_COMP,mcget_run_num());\
      overrun=1;\
    }\
    do {mcDEBUG_SCATTER(x, y, z, vx, vy, vz, \
			t,sx,sy,sz, p); SCATTERED++;} while(0);\
} while(0)

#define SCATTER0\
    do {mcDEBUG_SCATTER(x, y, z, vx, vy, vz, \
			t,sx,sy,sz, p); SCATTERED++;} while(0)

  const int BOUNCE_LOG_SIZE=1000000;
  struct Generalized_State_t *Bounce_store;

%}

DECLARE
%{
  int bounce_store_index;
  int bounce_store_overrun;
%}

INITIALIZE
%{
  bounce_store_index=0;
  if ( (Bounce_store=malloc(sizeof(*Bounce_store)*BOUNCE_LOG_SIZE))==NULL){
    fprintf(stderr,"Error: Scatter_logger: Cannot allocate memory for %d Generalized_State_ts of size %d bytes\n",BOUNCE_LOG_SIZE,sizeof(*Bounce_store));
    exit(-1);
  }
  Bounce_store[BOUNCE_LOG_SIZE-1]._p=-1;
%}


TRACE
%{
#undef SCATTER
#define SCATTER SCATTER_LOG(BOUNCE_LOG_SIZE, Bounce_store, bounce_store_index,bounce_store_overrun)

#undef mcabsorb
#define mcabsorb scatter_logger_stop
  bounce_store_index=0;/*we are now starting logging so we should start afresh*/
  if (bounce_store_index<BOUNCE_LOG_SIZE){
    struct Generalized_State_t *bp=&(Bounce_store[bounce_store_index]);
    Coords ctmp=POS_A_CURRENT_COMP;
    Coords r = coords_set(x,y,z);
    Coords v = coords_set(vx,vy,vz);
    Coords s = coords_set(sx,sy,sz);

    Coords rg,vg,sg;

    Rotation Rt;
    rot_transpose(ROT_A_CURRENT_COMP,Rt);
    rg=coords_add(rot_apply(Rt,r),ctmp);
    vg=rot_apply(Rt,v);
    sg=rot_apply(Rt,s);
    coords_get(rg,&(bp->_x),&(bp->_y),&(bp->_z));
    coords_get(vg,&(bp->_vx),&(bp->_vy),&(bp->_vz));
    coords_get(sg,&(bp->_sx),&(bp->_sy),&(bp->_sz));
    bp->_t=t;
    bp->_p=p;
    bp->_nid=mcget_run_num();
    bp->_comp=INDEX_CURRENT_COMP;
    bp->_idx=bounce_store_index;
    bounce_store_index++;
  }else if(bounce_store_index==BOUNCE_LOG_SIZE && !bounce_store_overrun){
    printf("Warning (%s): Scatter_log overrun - not logging any more SCATTER events\n",NAME_CURRENT_COMP);
    bounce_store_overrun=1;
  }
%}

FINALLY
%{
%}

END