File: Virtual_mcnp_ss_output.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 (192 lines) | stat: -rw-r--r-- 6,161 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2012, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Virtual_mcnp_ss_output.comp
* %I
* Written by: <a href="mailto:esbe@dtu.dk">Esben Klinkby</a> and <a href="mailto:pkwi@dtu.dk">Peter Willendrup</a>
* Date: January, 2012
* Origin: <a href="http://www.risoe.dtu.dk/">DTU</a>
*
* This component uses a Source Surface type file of recorded neutrons from the
* reactor monte carlo code MCNP as a source of particles.
*
* %D
* This component draws neutron events from a Source Surface file created using the
* MCNP Monte Carlo code and converts them to make them suitable for a McStas simulation
*
* Note that axes orientation may be different between MCNP and McStas!
* Note also that the conversion of between McStas and MCNP units and parameters
* is done automatically by this component - but the user must ensure that
* geometry description matches between the two Monte Carlo codes.
*
* The verbose mode is highly recommended as it displays lots of useful informations.
*
* This interface uses the MCNP Source Surface Read/Write format (SSW/SSR).
* Infomation transfer from(to) SSW files proceeds via a set of Fortran modules
* and subroutines collected in "subs.f"
* For succesful compilation, it is required that these subroutines are compiled
* and linked to the instrument file:
*
*  mcstas dummy.instr  -> generates dummy.c
*  gfortran -c subs.f  -> generates subs.f
*  gcc -o runme.out dummy.c subs.o -lm -lgfortran  -> generates runme.out
*
* Note that this requires a fortran compiler (here gfortran) and gcc.
*
* %BUGS
* None known bugs so far. But surely this will change...
* Caveat: when writing the header for the output wssa file, the number of histories and tracks are assumed to be that of the
*         input MNCP run. This is generally not the case, due to losses in the McStas simulation step.
*         In case of losses any subsequel MCNP run based on the McStas output, can be confused by inconsistency between
*         header and file content. To resolve, either ensure that NPS is lower than the actual number of events in the McStas output.
*         Or hardcode values of nhis & ntrk in either subs.f or Virtual_mcnp_ss_output.comp.
*
* EXAMPLE of usage:
*     first a MCNP simulation is run
*     and at a relevant surface, a Source Surface Write card is given (e.g. for MCNP surface #1, add the line:
*     SSW 1
*     to the end of the input file.
*     By this a ".w" file is produced, which then serves as input to the McStas simulation.
* Present version: rename *.w to rssa, and make sure to put it in the dir from which McStas is run (or use symbolic links)
*
* %P
* INPUT PARAMETERS
* file: [str]   Name of the MCNP SSW neutron input file. Not active: Name assumed to be 'rssa'
* verbose: [1]  Toggles debugging info on/off
*
* %L
* <a href="http://mcnp-green.lanl.gov/index.html">MCNP</a>
* %L
* MCNP -- A General Monte Carlo N-Particle Transport Code, Version 5, Volume II: User's Guide, p177
*
* %E
*
*******************************************************************************/

DEFINE COMPONENT Virtual_mcnp_ss_output

SETTING PARAMETERS (string file="rssa", verbose=1)

DEPENDENCY "-L@MCCODE_LIB@/libs/neutronics/ -lneutronics -lgfortran"
NOACC

SHARE
%{
#ifndef MCNP_INPUT_DEFS
#define MCNP_INPUT_DEFS
/* number of neutron events to read for file pre-analysis */

#include <sys/stat.h>

#endif

%}

DECLARE
%{
  FILE *hfile;                    /* Neutron input file handle */

  double to_mcstas[8];
  double from_mcstas[8];
  unsigned int counterout;
  unsigned int ntrk;
  unsigned int nhis;
%}

INITIALIZE
%{

  char   exit_flag=0;  /* set to 1 if end of simulation */
  long   filesize  =0;

  counterout = 0;

  struct stat stfile;
/* Open neutron input file. */
/* (If empty filename given, present warning in case of verbose, perform nothing) */

  if (strcmp(file,"rssa")) {
    printf("MCNP_input: %s: Given filename is %s, please rename to 'rssa' and rerun!\n", NAME_CURRENT_COMP, file);
  } else {
    if (file) {
      stat(file,&stfile);
      filesize = stfile.st_size;
      hfile = fopen(file, "r");
    }
    if(!hfile)
      {
	fprintf(stderr, "MCNP_input: %s: Error: Cannot open input file %s.\n", NAME_CURRENT_COMP, file);
      } else if (verbose)
	printf("MCNP_input: %s: opening MCNP SSW file '%s'\n", NAME_CURRENT_COMP, file);

    // put hardcoded nhis and ntrk values here
    //    ntrk=10;
    //    nhis=10;
    ntrk=10;
    nhis=10;  // Number of histories in MCNP SSW file
    writeheader_(&ntrk,&nhis);

    if (verbose) printf("Number of histories in MCNP SSW file: %d \n ", nhis);
    if (verbose) printf("WARNING: #histories & #tracks are copied from orginal MCNP run \n");
    if (verbose) printf("WARNING: Generally there is a loss of events in the McStas step, \n");
    if (verbose) printf("WARNING: causing ntrk and nhis to be wrong in subsequent MCNP running. \n");
    if (verbose) printf("WARNING: MCNP cannot handle this (crashes). \n");
    if (verbose) printf("WARNING: To resolve, reduce ntrk & nhis by hardcoding numbers in either: \n");
    if (verbose) printf("WARNING: Virtual_mcnp_ss_output.comp or subs.f \n");
    if (verbose) printf("WARNING: sorry for the inconvience. \n");
    if (verbose) printf("Analysing MCNP file: %s \n", file);

  } /* non-empty filename */

%}

TRACE
%{
  char exit_flag=0;  /* set to 1 if end of simulation */
  int  result_read=1;


  while(1) {
    from_mcstas[0]=x;
    from_mcstas[1]=y;
    from_mcstas[2]=z;
    from_mcstas[3]=vx;
    from_mcstas[4]=vy;
    from_mcstas[5]=vz;
    from_mcstas[6]=p;
    from_mcstas[7]=t;

    writeneutron_(&from_mcstas);

    if ( counterout >= (nhis-1) ) {
      result_read=EOF;
      exit_flag = 1;
    }
    counterout++;

  if (exit_flag) {
    if (verbose) printf("  Finishing simulation\n");
    mcset_ncount(mcget_run_num()); ABSORB;
  }
  SCATTER;
  break;
 /* end while */
  }

%}

FINALLY
%{
  closefiles_();
%}

MCDISPLAY
%{

%}

END