/******************************************************************************* * * 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 * * Write neutron state parameters to VITESS neutron file. * * %D * Detector-like component writing neutron state parameters to a * VITESS neutron file. Used to interface McStas components or * simulations into VITESS. * * Note that when standard output is used, as is the default, no * monitors or other components that produce terminal output must be * used, or the neutron output from this component will become * currupted. * * %P * INPUT PARAMETERS * * outname: Filename of neutron file to write. Default is standard * output [string] * bufsize: Size of neutron output buffer [records] * progress: If not zero, output dots as progress indicator [flag] * * %E *******************************************************************************/ DEFINE COMPONENT Vitess_output DEFINITION PARAMETERS (outname = 0) SETTING PARAMETERS (bufsize = 10000, progress = 0) OUTPUT PARAMETERS (file, buf, pos) STATE PARAMETERS (x,y,z,vx,vy,vz,t,s1,s2,p) DECLARE %{ #include "vitess-lib.h" FILE *file; /* Neutron output file handle */ Neutron *buf; /* Neutron output buffer */ int pos; /* Current position in buffer */ %} INITIALIZE %{ /* Open neutron output file. */ if(outname) { file = fopen(outname, "wb"); } else { file = stdout; } if(!file) { fprintf(stderr, "Vitess_output: Error: Cannot open output file.\n"); exit(1); } /* Allocate neutron output buffer. */ buf = calloc(bufsize, sizeof(Neutron)); if(!buf) { fprintf(stderr, "Vitess_output: Error: Cannot allocate neutron buffer.\n"); exit(1); } /* Initialize buffer. */ pos = 0; %} TRACE %{ int count; double v; /* Neutron velocity */ /* Flush output buffer if full. */ if(pos >= bufsize) { count = fwrite(buf, sizeof(Neutron), bufsize, file); if(progress) { fputc('.', stderr); /* Output progress indicator. */ fflush(stderr); } if(count != bufsize) { fprintf(stderr, "Vitess_output: Error during write of neutron file.\n"); exit(1); } else { pos = 0; /* Reposition at start of buffer */ } } buf[pos] = mcstas2vitess(x, y, z, vx, vy, vz, t, p); ++pos; %} FINALLY %{ int count; /* Flush output buffer if necessary. */ if(pos > 0) { count = fwrite(buf, sizeof(Neutron), pos, file); if(count != pos) { fprintf(stderr, "Vitess_output: Error during write of neutron file.\n"); exit(1); } } if(progress) fprintf(stderr, ".\n"); /* Output final progress indicator. */ if(buf) free(buf); if(outname && file) fclose(file); %} MCDISPLAY %{ /* Invisible component. */ %} END