/******************************************************************************* * * McStas, the neutron ray-tracing package * Maintained by Kristian Nielsen and Kim Lefmann, * Copyright 1997-2000 Risoe National Laboratory, Roskilde, Denmark * * %I * Written by: Kristian Nielsen * Date: June 6, 2000 * Version: $Revision: 1.1 $ * Origin: McStas release * * Read neutron state parameters from VITESS neutron file. * * %D * Source-like component reading neutron state parameters from a * VITESS neutron file. Used to interface McStas components or * simulations into VITESS. * * %P * INPUT PARAMETERS * * input: Filename of neutron file to read. Default is * standard input [string] * bufsize: Size of neutron input buffer [records] * repeat_count: Number of times to repeat each neutron read [1] * * OUTPUT PARAMETERS * * finished: Set to 1 when the last neutron has been read [int] * * %E *******************************************************************************/ DEFINE COMPONENT Vitess_input DEFINITION PARAMETERS (input = 0, repeat_count = 1) SETTING PARAMETERS (bufsize = 10000) OUTPUT PARAMETERS (file, buf, size, pos, rep, finished) STATE PARAMETERS (x,y,z,vx,vy,vz,t,s1,s2,p) DECLARE %{ #include "vitess-lib.h" FILE *file; /* Neutron input file handle */ Neutron *buf; /* Neutron input buffer */ int size; /* Number of neutrons currently in buffer */ int pos; /* Current position in buffer */ int rep; /* Neutron repeat count */ int finished; /* Set to 1 when last neutron read */ %} INITIALIZE %{ /* Open neutron input file. */ if(input) file = fopen(input, "rb"); else file = stdin; if(!file) { fprintf(stderr, "Vitess_input: Error: Cannot open input file.\n"); exit(1); } /* Allocate neutron input buffer. */ buf = calloc(bufsize, sizeof(Neutron)); if(!buf) { fprintf(stderr, "Vitess_input: Error: Cannot allocate neutron buffer.\n"); exit(1); } /* Initialize buffer. */ size = 0; pos = 0; rep = 0; finished = 0; %} TRACE %{ double v; /* Neutron velocity */ if(pos >= size) { /* Buffer is empty. */ size = fread(buf, sizeof(Neutron), bufsize, file); if(size <= 0) { if(ferror(file)) fprintf(stderr, "Vitess_input: Error during read of neutron file.\n"); if(feof(file) || ferror(file)) finished = 1; /* End of file or error reached */ } else { pos = 0; /* Reposition at start of buffer */ } } /* When no more neutron records are available in the neutron file, any remaining iterations are skipped by immediately ABSORB'ing the neutron. */ if(finished) ABSORB; vitess2mcstas(buf[pos], &x, &y, &z, &vx, &vy, &vz, &t, &p); /* Repeat the same neutron state parameters the required number of times. */ ++rep; if(rep >= repeat_count) { rep = 0; ++pos; } %} FINALLY %{ if(buf) free(buf); if(input && file) fclose(file); %} MCDISPLAY %{ /* Invisible component. */ %} END