diff --git a/mcstas-comps/misc/Annulus.comp b/mcstas-comps/misc/Annulus.comp index d13bc2a49..646163d6f 100644 --- a/mcstas-comps/misc/Annulus.comp +++ b/mcstas-comps/misc/Annulus.comp @@ -62,7 +62,7 @@ TRACE %{ MCDISPLAY %{ - mcdis_annulus(0,0,0,outer_radius, inner_radius, 0,1,0); + mcdis_annulus (0, 0, 0, outer_radius, inner_radius, 0, 1, 0); %} END diff --git a/mcstas-comps/misc/Circle.comp b/mcstas-comps/misc/Circle.comp index f62d8cc6a..b57a058fb 100644 --- a/mcstas-comps/misc/Circle.comp +++ b/mcstas-comps/misc/Circle.comp @@ -60,7 +60,7 @@ TRACE %{ MCDISPLAY %{ - mcdis_new_circle(0,0,0,radius, 0,1,0); + mcdis_new_circle (0, 0, 0, radius, 0, 1, 0); %} END diff --git a/mcstas-comps/misc/Cone.comp b/mcstas-comps/misc/Cone.comp index 12b581adb..177cc724d 100644 --- a/mcstas-comps/misc/Cone.comp +++ b/mcstas-comps/misc/Cone.comp @@ -57,7 +57,7 @@ TRACE %{ MCDISPLAY %{ - mcdis_cone(0,0,0,radius,yheight, 0,1,0); + mcdis_cone (0, 0, 0, radius, yheight, 0, 1, 0); %} END diff --git a/mcstas-comps/misc/Disc.comp b/mcstas-comps/misc/Disc.comp index 021ea2da9..36e61b544 100644 --- a/mcstas-comps/misc/Disc.comp +++ b/mcstas-comps/misc/Disc.comp @@ -63,7 +63,7 @@ TRACE %{ MCDISPLAY %{ - mcdis_disc(0,0,0,radius, nx,ny,nz); + mcdis_disc (0, 0, 0, radius, nx, ny, nz); %} END diff --git a/mcstas-comps/misc/File.comp b/mcstas-comps/misc/File.comp index 66eed12e0..5fe87bc8b 100644 --- a/mcstas-comps/misc/File.comp +++ b/mcstas-comps/misc/File.comp @@ -36,88 +36,90 @@ SHARE %} DECLARE %{ -char * key; -char * name; + char* key; + char* name; %} INITIALIZE %{ -// Sort-out the key that we are searching for -if (metadatakey == NULL || metadatakey[0] == '\0'){ - key = malloc((strlen(NAME_CURRENT_COMP) + 1) * sizeof(char)); - strcpy(key, NAME_CURRENT_COMP); -} else { - key = malloc((strlen(metadatakey) + 1) * sizeof(char)); - strcpy(key, metadatakey); -} -int matches = metadata_table_defined(num_metadata, metadata_table, key); -if (matches != 1) { - // 0 would mean _no_ matches; maybe they metadata name without the component was given? - if (matches == 0 && strcmp(key, NAME_CURRENT_COMP)) { - free(key); - key = malloc((strlen(NAME_CURRENT_COMP) + strlen(metadatakey) + 2) * sizeof(char)); - sprintf(key, "%s:%s", NAME_CURRENT_COMP, metadatakey); - matches = metadata_table_defined(num_metadata, metadata_table, key); + // Sort-out the key that we are searching for + if (metadatakey == NULL || metadatakey[0] == '\0') { + key = malloc ((strlen (NAME_CURRENT_COMP) + 1) * sizeof (char)); + strcpy (key, NAME_CURRENT_COMP); + } else { + key = malloc ((strlen (metadatakey) + 1) * sizeof (char)); + strcpy (key, metadatakey); } - // there's a bug in metadata_table_defined that returns num_metadata if key == NULL - // But since key _isn't_ null, any other result _should_ mean key == A_COMPONENT_NAME - // _and_ that component defines _multiple_ METADATA entries. - else { - printf("%s: There are %d METADATA entries that match %s; please select only one of:\n", NAME_CURRENT_COMP, matches, key); - metadata_table_print_component_keys(num_metadata, metadata_table, key); - exit(1); + int matches = metadata_table_defined (num_metadata, metadata_table, key); + if (matches != 1) { + // 0 would mean _no_ matches; maybe they metadata name without the component was given? + if (matches == 0 && strcmp (key, NAME_CURRENT_COMP)) { + free (key); + key = malloc ((strlen (NAME_CURRENT_COMP) + strlen (metadatakey) + 2) * sizeof (char)); + sprintf (key, "%s:%s", NAME_CURRENT_COMP, metadatakey); + matches = metadata_table_defined (num_metadata, metadata_table, key); + } + // there's a bug in metadata_table_defined that returns num_metadata if key == NULL + // But since key _isn't_ null, any other result _should_ mean key == A_COMPONENT_NAME + // _and_ that component defines _multiple_ METADATA entries. + else { + printf ("%s: There are %d METADATA entries that match %s; please select only one of:\n", NAME_CURRENT_COMP, matches, key); + metadata_table_print_component_keys (num_metadata, metadata_table, key); + exit (1); + } + } + if (metadata_table_defined (num_metadata, metadata_table, key) != 1) { + fprintf (stderr, "%s: No unique metadata defined with key %s\n", NAME_CURRENT_COMP, key); + exit (1); + } + + char* name_part = metadata_table_key_literal (key); + if (name_part == NULL) { + #if defined(__MCCODE_VERSION__) && __MCCODE_VERSION__ >= 305000L + // We already restrict that only one key matches. + name_part = metadata_table_name (num_metadata, metadata_table, key); + // is key NAME_CURRENT_COMP or something provided by the user? We don't know + // so we _must_ cycle the key allocation to make this work. + char* new_key = malloc ((strlen (key) + strlen (name_part) + 2) * sizeof (char)); + sprintf (new_key, "%s:%s", key, name_part); + free (key); + key = new_key; + #else + printf ("Can not update the key automatically prior to McCode v3.5.0, please fix %s usage or upgrade\n", NAME_CURRENT_COMP); + exit (1); + #endif } -} -if (metadata_table_defined(num_metadata, metadata_table, key) != 1) { - fprintf(stderr, "%s: No unique metadata defined with key %s\n", NAME_CURRENT_COMP, key); - exit(1); -} - -char * name_part = metadata_table_key_literal(key); -if (name_part == NULL) { -#if defined(__MCCODE_VERSION__) && __MCCODE_VERSION__ >= 305000L - // We already restrict that only one key matches. - name_part = metadata_table_name(num_metadata, metadata_table, key); - // is key NAME_CURRENT_COMP or something provided by the user? We don't know - // so we _must_ cycle the key allocation to make this work. - char * new_key = malloc((strlen(key) + strlen(name_part) + 2) * sizeof(char)); - sprintf(new_key, "%s:%s", key, name_part); - free(key); - key = new_key; -#else - printf("Can not update the key automatically prior to McCode v3.5.0, please fix %s usage or upgrade\n", NAME_CURRENT_COMP); - exit(1); -#endif -} -if (filename == NULL || filename[0] == '\0'){ - char * comp_part = metadata_table_key_component(key); - name = malloc((strlen(comp_part) + strlen(name_part) + 2) * sizeof(char)); - sprintf(name, "%s_%s", comp_part, name_part); - free(comp_part); -} else{ - name = malloc((strlen(filename) + 1) * sizeof(char)); - strcpy(name, filename); -} - -free(name_part); + if (filename == NULL || filename[0] == '\0') { + char* comp_part = metadata_table_key_component (key); + name = malloc ((strlen (comp_part) + strlen (name_part) + 2) * sizeof (char)); + sprintf (name, "%s_%s", comp_part, name_part); + free (comp_part); + } else { + name = malloc ((strlen (filename) + 1) * sizeof (char)); + strcpy (name, filename); + } + + free (name_part); -// Read the file contents and write them to file now. -FILE * file_ptr = fopen(name, "w"); -fprintf(file_ptr, "%s", metadata_table_literal(num_metadata, metadata_table, key)); -fclose(file_ptr); + // Read the file contents and write them to file now. + FILE* file_ptr = fopen (name, "w"); + fprintf (file_ptr, "%s", metadata_table_literal (num_metadata, metadata_table, key)); + fclose (file_ptr); %} TRACE %{ -// Do nothing + // Do nothing %} FINALLY %{ -// (Optionally) Remove the file now that the runtime is done -if (!keep && !remove(name)) { - fprintf(stderr, "%s: Could not remove file %s\n", NAME_CURRENT_COMP, name); -} -if (key) free(key); -if (name) free(name); + // (Optionally) Remove the file now that the runtime is done + if (!keep && !remove (name)) { + fprintf (stderr, "%s: Could not remove file %s\n", NAME_CURRENT_COMP, name); + } + if (key) + free (key); + if (name) + free (name); %} MCDISPLAY %{ diff --git a/mcstas-comps/misc/Legacy_circle.comp b/mcstas-comps/misc/Legacy_circle.comp index 8946282e5..5c978008c 100644 --- a/mcstas-comps/misc/Legacy_circle.comp +++ b/mcstas-comps/misc/Legacy_circle.comp @@ -82,7 +82,7 @@ TRACE MCDISPLAY %{ - circle("xy",0,0,0,radius); + circle ("xy", 0, 0, 0, radius); %} END diff --git a/mcstas-comps/misc/MCPL_input.comp b/mcstas-comps/misc/MCPL_input.comp index e8c425e7b..a99c3d50f 100644 --- a/mcstas-comps/misc/MCPL_input.comp +++ b/mcstas-comps/misc/MCPL_input.comp @@ -48,196 +48,195 @@ DEPENDENCY "@MCPLFLAGS@" SHARE %{ -#include -#include -#ifdef _MSC_EXTENSIONS -#include -#define sleep Sleep -#endif - -int mcplinput_file_exist(char *fn) -{ - struct stat buffer; - return (stat (fn, &buffer) == 0); -} + #include + #include + #ifdef _MSC_EXTENSIONS + #include + #define sleep Sleep + #endif + + int + mcplinput_file_exist (char* fn) { + struct stat buffer; + return (stat (fn, &buffer) == 0); + } %} DECLARE %{ -mcpl_file_t inputfile; -long long nparticles; -long long read_neutrons; -long long used_neutrons; -double weight_scale; -char * resolved_filename; -int repeat_cnt; -int repeat_tot; -int repeating; -int ismpislave; -int mpi_cnt; -DArray1d X; -DArray1d Y; -DArray1d Z; -DArray1d VX; -DArray1d VY; -DArray1d VZ; -DArray1d SX; -DArray1d SY; -DArray1d SZ; -DArray1d E; -DArray1d T; -DArray1d P; + mcpl_file_t inputfile; + long long nparticles; + long long read_neutrons; + long long used_neutrons; + double weight_scale; + char* resolved_filename; + int repeat_cnt; + int repeat_tot; + int repeating; + int ismpislave; + int mpi_cnt; + DArray1d X; + DArray1d Y; + DArray1d Z; + DArray1d VX; + DArray1d VY; + DArray1d VZ; + DArray1d SX; + DArray1d SY; + DArray1d SZ; + DArray1d E; + DArray1d T; + DArray1d P; %} INITIALIZE %{ - { - if ( !filename || !filename[0] ) { - fprintf(stderr,"ERROR(%s): Requires filename parameter.\n", - NAME_CURRENT_COMP); - exit(-1); - } - char * fn_mcpl = mcpl_name_helper( filename, 'M' ); - char * fn_mcplgz = mcpl_name_helper( filename, 'G' ); - if ( mcplinput_file_exist(fn_mcpl) ) { - if ( mcplinput_file_exist(fn_mcplgz) ) { - fprintf(stderr,"ERROR(%s): Can not resolve input file unambiguously" - " since both %s and %s exist.\n",NAME_CURRENT_COMP, - fn_mcpl, fn_mcplgz); - exit(-1); - } - resolved_filename = fn_mcpl; - free(fn_mcplgz); - } else { - resolved_filename = fn_mcplgz; - free(fn_mcpl); - } + { + if (!filename || !filename[0]) { + fprintf (stderr, "ERROR(%s): Requires filename parameter.\n", NAME_CURRENT_COMP); + exit (-1); } - - char line[256]; - long long ncount=mcget_ncount(); - - if(Emax0.0) ) { - fprintf(stderr, "ERROR: Input MCPL file has invalid initial_ray_count" - " (%g). Unable to determine weight scale.\n", mcpl_ray_count ); - exit(1); + char* fn_mcpl = mcpl_name_helper (filename, 'M'); + char* fn_mcplgz = mcpl_name_helper (filename, 'G'); + if (mcplinput_file_exist (fn_mcpl)) { + if (mcplinput_file_exist (fn_mcplgz)) { + fprintf (stderr, + "ERROR(%s): Can not resolve input file unambiguously" + " since both %s and %s exist.\n", + NAME_CURRENT_COMP, fn_mcpl, fn_mcplgz); + exit (-1); + } + resolved_filename = fn_mcpl; + free (fn_mcplgz); } else { - weight_scale = 1.0 / mcpl_ray_count; + resolved_filename = fn_mcplgz; + free (fn_mcpl); } + } + char line[256]; + long long ncount = mcget_ncount (); - if ( !(nparticles=mcpl_hdr_nparticles(inputfile)) ) { - fprintf(stderr,"Warning(%s): MCPL-file reports no present particles. Foolishly trying to go on.\n",NAME_CURRENT_COMP); - #ifndef OPENACC - nparticles=ncount; - #endif - }else{ - printf("Message(%s): MCPL file (%s) produced with %s.\n",NAME_CURRENT_COMP,resolved_filename,mcpl_hdr_srcname(inputfile)); - printf("Message(%s): MCPL file (%s) contains %lu particles.\n",NAME_CURRENT_COMP,resolved_filename,(long unsigned)nparticles); - } - mcset_ncount(nparticles); + if (Emax < Emin) { + fprintf (stderr, "Warning(%s): Nonsensical energy interval: E=[%g,%g]. Aborting.\n", NAME_CURRENT_COMP, Emin, Emax); + exit (-1); + } - if(repeat_count>1 && v_smear==0 && pos_smear==0 && dir_smear==0) { - fprintf(stdout, "\n\n WARNING: You have requested a repeat_count=%i but have left all *_smear parameters at 0!\n --> This is not allowed! Resetting to repeat_count=1!\n",repeat_count); - fprintf(stderr, "\n\n WARNING: You have requested a repeat_count=%i but have left all *_smear parameters at 0!\n --> This is not allowed! Resetting to repeat_count=1!\n",repeat_count); - repeat_count=1; - sleep(10); - } + /* No need to check if the file opens correctly since mcpl will + * abort internally if it cannot open the file.*/ + inputfile = mcpl_open_file (resolved_filename); + double mcpl_ray_count = mcpl_hdr_stat_sum (inputfile, "initial_ray_count"); + if (mcpl_ray_count == -2.0) { + // legacy format without ray count: + weight_scale = 1.0; + } else if (!(mcpl_ray_count > 0.0)) { + fprintf (stderr, + "ERROR: Input MCPL file has invalid initial_ray_count" + " (%g). Unable to determine weight scale.\n", + mcpl_ray_count); + exit (1); + } else { + weight_scale = 1.0 / mcpl_ray_count; + } - if(repeat_count==0) repeat_count=1; - repeat_cnt = repeat_count; - mpi_cnt=1; - ismpislave=0; -#if defined (USE_MPI) - repeat_cnt = ceil(1.0*repeat_cnt/mpi_node_count); - mpi_cnt=mpi_node_count; - ismpislave = mpi_node_rank; -#endif - MPI_MASTER( - fprintf(stdout, "\n\n Warning: You are using MCPL_input with a repeat_count of %lu:\n - Minimum neutron count requested is %lu x %lu <= %lu", - (long unsigned)repeat_count,(long unsigned)nparticles, - (long unsigned)repeat_count,(long unsigned)repeat_cnt*(long unsigned)nparticles); - ); - - char tmpstr[CHAR_BUF_LENGTH]; -#if defined (USE_MPI) - sprintf(tmpstr, " x %i MPI nodes = %lu neutrons total\n", - mpi_node_count,(long unsigned)mpi_node_count*(long unsigned)repeat_cnt*(long unsigned)nparticles); -#else - sprintf(tmpstr, " neutrons total\n\n"); -#endif - - MPI_MASTER( - fprintf(stdout, "%s",tmpstr); - ); - read_neutrons=0; - used_neutrons=0; - - MPI_MASTER( - if (verbose==1) { - printf("MCPL_input verbose mode - outputting data on the 10 first read neutrons in MCPL units:\n"); - } - ); - repeating = 0; -#ifdef OPENACC - preload=1; - printf("OpenACC, preload implicit:\n"); -#endif - if (preload) { - printf("Preload requested, loading MCPLfile in INITIALIZE\n"); - X = create_darr1d(nparticles); - Y = create_darr1d(nparticles); - Z = create_darr1d(nparticles); - VX = create_darr1d(nparticles); - VY = create_darr1d(nparticles); - VZ = create_darr1d(nparticles); - SX = create_darr1d(nparticles); - SY = create_darr1d(nparticles); - SZ = create_darr1d(nparticles); - T = create_darr1d(nparticles); - P = create_darr1d(nparticles); - E = create_darr1d(nparticles); - printf("Initiating file read...\n"); - int loop; - for (loop=0; loop < nparticles ; loop++) { - const mcpl_particle_t *particle; - particle=mcpl_read(inputfile); + if (!(nparticles = mcpl_hdr_nparticles (inputfile))) { + fprintf (stderr, "Warning(%s): MCPL-file reports no present particles. Foolishly trying to go on.\n", NAME_CURRENT_COMP); + #ifndef OPENACC + nparticles = ncount; + #endif + } else { + printf ("Message(%s): MCPL file (%s) produced with %s.\n", NAME_CURRENT_COMP, resolved_filename, mcpl_hdr_srcname (inputfile)); + printf ("Message(%s): MCPL file (%s) contains %lu particles.\n", NAME_CURRENT_COMP, resolved_filename, (long unsigned)nparticles); + } + mcset_ncount (nparticles); + + if (repeat_count > 1 && v_smear == 0 && pos_smear == 0 && dir_smear == 0) { + fprintf ( + stdout, + "\n\n WARNING: You have requested a repeat_count=%i but have left all *_smear parameters at 0!\n --> This is not allowed! Resetting to repeat_count=1!\n", + repeat_count); + fprintf ( + stderr, + "\n\n WARNING: You have requested a repeat_count=%i but have left all *_smear parameters at 0!\n --> This is not allowed! Resetting to repeat_count=1!\n", + repeat_count); + repeat_count = 1; + sleep (10); + } + + if (repeat_count == 0) + repeat_count = 1; + repeat_cnt = repeat_count; + mpi_cnt = 1; + ismpislave = 0; + #if defined (USE_MPI) + repeat_cnt = ceil (1.0 * repeat_cnt / mpi_node_count); + mpi_cnt = mpi_node_count; + ismpislave = mpi_node_rank; + #endif + MPI_MASTER (fprintf (stdout, "\n\n Warning: You are using MCPL_input with a repeat_count of %lu:\n - Minimum neutron count requested is %lu x %lu <= %lu", + (long unsigned)repeat_count, (long unsigned)nparticles, (long unsigned)repeat_count, + (long unsigned)repeat_cnt*(long unsigned)nparticles);); + + char tmpstr[CHAR_BUF_LENGTH]; + #if defined (USE_MPI) + sprintf (tmpstr, " x %i MPI nodes = %lu neutrons total\n", mpi_node_count, (long unsigned)mpi_node_count*(long unsigned)repeat_cnt*(long unsigned)nparticles); + #else + sprintf (tmpstr, " neutrons total\n\n"); + #endif + + MPI_MASTER (fprintf (stdout, "%s", tmpstr);); + read_neutrons = 0; + used_neutrons = 0; + + MPI_MASTER (if (verbose == 1) { printf ("MCPL_input verbose mode - outputting data on the 10 first read neutrons in MCPL units:\n"); }); + repeating = 0; + #ifdef OPENACC + preload = 1; + printf ("OpenACC, preload implicit:\n"); + #endif + if (preload) { + printf ("Preload requested, loading MCPLfile in INITIALIZE\n"); + X = create_darr1d (nparticles); + Y = create_darr1d (nparticles); + Z = create_darr1d (nparticles); + VX = create_darr1d (nparticles); + VY = create_darr1d (nparticles); + VZ = create_darr1d (nparticles); + SX = create_darr1d (nparticles); + SY = create_darr1d (nparticles); + SZ = create_darr1d (nparticles); + T = create_darr1d (nparticles); + P = create_darr1d (nparticles); + E = create_darr1d (nparticles); + printf ("Initiating file read...\n"); + int loop; + for (loop = 0; loop < nparticles; loop++) { + const mcpl_particle_t* particle; + particle = mcpl_read (inputfile); if (particle) { - if (particle->pdgcode==2112) { + if (particle->pdgcode == 2112) { /* check energy range*/ - if ( particle->ekin>Emin*1e-9 && particle->ekinekin > Emin * 1e-9 && particle->ekin < Emax * 1e-9) { /* Particle energy in range */ /*positions are in cm*/ - X[used_neutrons]=particle->position[0]; - Y[used_neutrons]=particle->position[1]; - Z[used_neutrons]=particle->position[2]; - - if(polarisationuse){ - SX[used_neutrons]=(double)particle->polarisation[0]; - SY[used_neutrons]=(double)particle->polarisation[1]; - SZ[used_neutrons]=(double)particle->polarisation[2]; + X[used_neutrons] = particle->position[0]; + Y[used_neutrons] = particle->position[1]; + Z[used_neutrons] = particle->position[2]; + + if (polarisationuse) { + SX[used_neutrons] = (double)particle->polarisation[0]; + SY[used_neutrons] = (double)particle->polarisation[1]; + SZ[used_neutrons] = (double)particle->polarisation[2]; } - double d0=particle->direction[0]; - double d1=particle->direction[1]; - double d2=particle->direction[2]; + double d0 = particle->direction[0]; + double d1 = particle->direction[1]; + double d2 = particle->direction[2]; - VX[used_neutrons]=d0; - VY[used_neutrons]=d1; - VZ[used_neutrons]=d2; + VX[used_neutrons] = d0; + VY[used_neutrons] = d1; + VZ[used_neutrons] = d2; /*time in ms:*/ T[used_neutrons] = particle->time; @@ -249,20 +248,19 @@ INITIALIZE read_neutrons++; } } - } - printf("Done reading MCPL file, found %ld neutrons\n",(long unsigned)read_neutrons); - mcpl_close_file(inputfile); - fprintf(stdout, "\n\n Warning: You are using MCPL_input with a repeat_count of %lu:\n - Neutron count requested is %lu x %lu <= %lu", - (long unsigned)repeat_count,(long unsigned)read_neutrons, - (long unsigned)repeat_count,(long unsigned)repeat_cnt*(long unsigned)read_neutrons); - fprintf(stdout, " neutrons total\n\n"); - } - repeat_tot=repeat_cnt*mpi_cnt; - if (preload) { - mcset_ncount(repeat_tot*used_neutrons); - } else { - mcset_ncount(repeat_tot*nparticles); } + printf ("Done reading MCPL file, found %ld neutrons\n", (long unsigned)read_neutrons); + mcpl_close_file (inputfile); + fprintf (stdout, "\n\n Warning: You are using MCPL_input with a repeat_count of %lu:\n - Neutron count requested is %lu x %lu <= %lu", + (long unsigned)repeat_count, (long unsigned)read_neutrons, (long unsigned)repeat_count, (long unsigned)repeat_cnt * (long unsigned)read_neutrons); + fprintf (stdout, " neutrons total\n\n"); + } + repeat_tot = repeat_cnt * mpi_cnt; + if (preload) { + mcset_ncount (repeat_tot * used_neutrons); + } else { + mcset_ncount (repeat_tot * nparticles); + } %} TRACE @@ -270,132 +268,134 @@ TRACE double nrm; long long ncount; -#ifndef OPENACC - const mcpl_particle_t *particle;// = (mcpl_particle_t *) calloc(sizeof(mcpl_particle_t),1); - if(!preload) { - particle = mcpl_read(inputfile); + #ifndef OPENACC + const mcpl_particle_t* particle; // = (mcpl_particle_t *) calloc(sizeof(mcpl_particle_t),1); + if (!preload) { + particle = mcpl_read (inputfile); - ncount=mcget_ncount(); + ncount = mcget_ncount (); if (!particle) { - if(repeat_cnt>1) { - /* Trigger rewind of the file and ABSORB to get the first neutron "again" */ - repeating++; - mcpl_rewind(inputfile); - particle = mcpl_read(inputfile); - - MPI_MASTER( - printf("MCPL inputfile %s rewound %i time(s)\n",resolved_filename,repeating); - ); + if (repeat_cnt > 1) { + /* Trigger rewind of the file and ABSORB to get the first neutron "again" */ + repeating++; + mcpl_rewind (inputfile); + particle = mcpl_read (inputfile); + + MPI_MASTER (printf ("MCPL inputfile %s rewound %i time(s)\n", resolved_filename, repeating);); } else - ABSORB; + ABSORB; } - if (particle->pdgcode!=2112) { - /*Either no particle read, particle is not a neutron, or it has invalid energy - terminate to trigger next ray*/ - ABSORB; + if (particle->pdgcode != 2112) { + /*Either no particle read, particle is not a neutron, or it has invalid energy - terminate to trigger next ray*/ + ABSORB; } read_neutrons++; /* check energy range*/ - if ( particle->ekinekin>Emax*1e-9 ) { - /*Particle energy out of range - terminate to trigger next ray*/ - ABSORB; + if (particle->ekin < Emin * 1e-9 || particle->ekin > Emax * 1e-9) { + /*Particle energy out of range - terminate to trigger next ray*/ + ABSORB; } used_neutrons++; } -#endif - ncount = mcget_ncount(); - //fprintf(stdout,"Trace ncount is %ld on %i\n",ncount,ismpislave); - unsigned long long i=(_particle->_uid); + #endif + ncount = mcget_ncount (); + // fprintf(stdout,"Trace ncount is %ld on %i\n",ncount,ismpislave); + unsigned long long i = (_particle->_uid); if (preload) { - if (i>=used_neutrons) { - repeating=1; + if (i >= used_neutrons) { + repeating = 1; i = i % used_neutrons; } } if (!preload) { /*positions are in cm*/ #ifndef OPENACC - x=particle->position[0]/100; - y=particle->position[1]/100; - z=particle->position[2]/100; + x = particle->position[0] / 100; + y = particle->position[1] / 100; + z = particle->position[2] / 100; #endif } else { - x=X[i]/100; - y=Y[i]/100; - z=Z[i]/100; + x = X[i] / 100; + y = Y[i] / 100; + z = Z[i] / 100; } if (ismpislave || repeating) { - double tmpx,tmpy,tmpz; + double tmpx, tmpy, tmpz; // Position-MC: - randvec_target_circle(&tmpx, &tmpy, &tmpz, NULL, 0, 0, 1, 0); - NORM(tmpx,tmpy,tmpz); - tmpx *= pos_smear*rand01(); tmpy *= pos_smear*rand01(); tmpz *= pos_smear*rand01(); - x+=tmpx; y+=tmpy; z+=tmpz; + randvec_target_circle (&tmpx, &tmpy, &tmpz, NULL, 0, 0, 1, 0); + NORM (tmpx, tmpy, tmpz); + tmpx *= pos_smear * rand01 (); + tmpy *= pos_smear * rand01 (); + tmpz *= pos_smear * rand01 (); + x += tmpx; + y += tmpy; + z += tmpz; } - if(polarisationuse){ - if(!preload) { -#ifndef OPENACC - sx=particle->polarisation[0]; - sy=particle->polarisation[1]; - sz=particle->polarisation[2]; -#endif + if (polarisationuse) { + if (!preload) { + #ifndef OPENACC + sx = particle->polarisation[0]; + sy = particle->polarisation[1]; + sz = particle->polarisation[2]; + #endif } else { - sx=SX[i]; - sy=SY[i]; - sz=SZ[i]; + sx = SX[i]; + sy = SY[i]; + sz = SZ[i]; } } else { - sx=sy=sz=0; + sx = sy = sz = 0; } /*ekin is in MeV, in McStas meV*/ if (!preload) { -#ifndef OPENACC - nrm = particle->ekin *1e9/VS2E; -#endif + #ifndef OPENACC + nrm = particle->ekin * 1e9 / VS2E; + #endif } else { - nrm = E[i] *1e9/VS2E; + nrm = E[i] * 1e9 / VS2E; } - nrm = sqrt(nrm); + nrm = sqrt (nrm); if (ismpislave || repeating) { - nrm *= (1+v_smear*randpm1()); + nrm *= (1 + v_smear * randpm1 ()); } - double d0,d1,d2; + double d0, d1, d2; if (!preload) { -#ifndef OPENACC - d0=particle->direction[0]; - d1=particle->direction[1]; - d2=particle->direction[2]; -#endif + #ifndef OPENACC + d0 = particle->direction[0]; + d1 = particle->direction[1]; + d2 = particle->direction[2]; + #endif } else { - d0=VX[i]; - d1=VY[i]; - d2=VZ[i]; + d0 = VX[i]; + d1 = VY[i]; + d2 = VZ[i]; } if (ismpislave || repeating) { // Direction-MC: - double tmpx,tmpy,tmpz; + double tmpx, tmpy, tmpz; // Position-MC: - randvec_target_circle(&d0, &d1, &d2, NULL, d0, d1, d2, sin(dir_smear*DEG2RAD)); - NORM(d0,d1,d2); + randvec_target_circle (&d0, &d1, &d2, NULL, d0, d1, d2, sin (dir_smear * DEG2RAD)); + NORM (d0, d1, d2); } - vx=d0*nrm; - vy=d1*nrm; - vz=d2*nrm; + vx = d0 * nrm; + vy = d1 * nrm; + vz = d2 * nrm; if (!preload) { #ifndef OPENACC /*time in ms:*/ - t=particle->time*1e-3; + t = particle->time * 1e-3; /*weight in unspecified units:*/ - p=particle->weight * weight_scale; + p = particle->weight * weight_scale; #endif } else { - t=T[i]*1e-3; - p=P[i]; + t = T[i] * 1e-3; + p = P[i]; } /* Correct for repetition, by repeat_count and/or MPI */ p /= (repeat_tot); @@ -404,53 +404,55 @@ TRACE SAVE %{ -#ifndef OPENACC + #ifndef OPENACC if (!preload) { - mcpl_close_file(inputfile); + mcpl_close_file (inputfile); } -#endif + #endif %} FINALLY %{ long long ncount; - ncount=mcget_ncount(); + ncount = mcget_ncount (); - if (used_neutrons!=read_neutrons){ - fprintf(stdout,"Message(%s): You have used %lu of %lu neutrons available in the MCPL file.\n",NAME_CURRENT_COMP, - (long unsigned)used_neutrons,(long unsigned)read_neutrons); + if (used_neutrons != read_neutrons) { + fprintf (stdout, "Message(%s): You have used %lu of %lu neutrons available in the MCPL file.\n", NAME_CURRENT_COMP, (long unsigned)used_neutrons, + (long unsigned)read_neutrons); } - if (ncount != used_neutrons){ - fprintf(stderr,"Warning (%s): You requested %lu neutrons from a file which contains %lu particles in general, of which only %lu are neutrons (within the wanted energy interval).\n" - "Please examine the recorded intensities carefully.\n", - NAME_CURRENT_COMP,(long unsigned)ncount,(long unsigned)nparticles,(long unsigned)used_neutrons); + if (ncount != used_neutrons) { + fprintf (stderr, + "Warning (%s): You requested %lu neutrons from a file which contains %lu particles in general, of which only %lu are neutrons (within the wanted " + "energy interval).\n" + "Please examine the recorded intensities carefully.\n", + NAME_CURRENT_COMP, (long unsigned)ncount, (long unsigned)nparticles, (long unsigned)used_neutrons); } - destroy_darr1d(X); - destroy_darr1d(Y); - destroy_darr1d(Z); - destroy_darr1d(VX); - destroy_darr1d(VY); - destroy_darr1d(VZ); - destroy_darr1d(SX); - destroy_darr1d(SY); - destroy_darr1d(SZ); - destroy_darr1d(T); - destroy_darr1d(P); - - free(resolved_filename); + destroy_darr1d (X); + destroy_darr1d (Y); + destroy_darr1d (Z); + destroy_darr1d (VX); + destroy_darr1d (VY); + destroy_darr1d (VZ); + destroy_darr1d (SX); + destroy_darr1d (SY); + destroy_darr1d (SZ); + destroy_darr1d (T); + destroy_darr1d (P); + + free (resolved_filename); %} MCDISPLAY %{ - multiline(5, 0.2,0.2,0.0, -0.2,0.2,0.0, -0.2,-0.2,0.0, 0.2,-0.2,0.0, 0.2,0.2,0.0); + multiline (5, 0.2, 0.2, 0.0, -0.2, 0.2, 0.0, -0.2, -0.2, 0.0, 0.2, -0.2, 0.0, 0.2, 0.2, 0.0); /*M*/ - multiline(5,-0.085,-0.085,0.0, -0.085,0.085,0.0, -0.045,-0.085,0.0, -0.005,0.085,0.0, -0.005,-0.085,0.0); + multiline (5, -0.085, -0.085, 0.0, -0.085, 0.085, 0.0, -0.045, -0.085, 0.0, -0.005, 0.085, 0.0, -0.005, -0.085, 0.0); /*I*/ - line(0.045,-0.085,0, 0.045, 0.085,0); - line(0.005, 0.085,0, 0.085, 0.085,0); - line(0.005,-0.085,0, 0.085,-0.085,0); + line (0.045, -0.085, 0, 0.045, 0.085, 0); + line (0.005, 0.085, 0, 0.085, 0.085, 0); + line (0.005, -0.085, 0, 0.085, -0.085, 0); %} END diff --git a/mcstas-comps/misc/MCPL_input_once.comp b/mcstas-comps/misc/MCPL_input_once.comp index 35cb6c293..00c16a402 100644 --- a/mcstas-comps/misc/MCPL_input_once.comp +++ b/mcstas-comps/misc/MCPL_input_once.comp @@ -43,159 +43,162 @@ DEPENDENCY "@MCPLFLAGS@" SHARE %{ -#include -#include + #include + #include -typedef struct { - // Prefixed names to avoid the _particle accessor macros - double _x, _y, _z; - double _vx, _vy, _vz; - double _sx, _sy, _sz; - // insert [int, void*, int, double, double, double, unsigned long] - // here in order to have matching memory layout with - // _struct_particle, e.g., _class_particle - double _t, _p; -} mcpl_input_once_particle_t; + typedef struct { + // Prefixed names to avoid the _particle accessor macros + double _x, _y, _z; + double _vx, _vy, _vz; + double _sx, _sy, _sz; + // insert [int, void*, int, double, double, double, unsigned long] + // here in order to have matching memory layout with + // _struct_particle, e.g., _class_particle + double _t, _p; + } mcpl_input_once_particle_t; -void mcpl_input_once_translator(const int use_polarisation, double weight_scale, const mcpl_particle_t * input, mcpl_input_once_particle_t * output){ - // position in mm -> m - output->_x = input->position[0] / 100; - output->_y = input->position[1] / 100; - output->_z = input->position[2] / 100; - // ekin in MeV -> meV; then to velocity - double nrm = sqrt(input->ekin * 1e9 / VS2E); - output->_vx = input->direction[0] * nrm; - output->_vy = input->direction[1] * nrm; - output->_vz = input->direction[2] * nrm; - // polarization is a direction, and we might ignore it - output->_sx = (use_polarisation) ? input->polarisation[0] : 0; - output->_sy = (use_polarisation) ? input->polarisation[1] : 0; - output->_sz = (use_polarisation) ? input->polarisation[2] : 0; - // time in msec -> sec - output->_t = input->time * 1e-3; - // probability, unitless - output->_p = input->weight * weight_scale; -} + void + mcpl_input_once_translator (const int use_polarisation, double weight_scale, const mcpl_particle_t* input, mcpl_input_once_particle_t* output) { + // position in mm -> m + output->_x = input->position[0] / 100; + output->_y = input->position[1] / 100; + output->_z = input->position[2] / 100; + // ekin in MeV -> meV; then to velocity + double nrm = sqrt (input->ekin * 1e9 / VS2E); + output->_vx = input->direction[0] * nrm; + output->_vy = input->direction[1] * nrm; + output->_vz = input->direction[2] * nrm; + // polarization is a direction, and we might ignore it + output->_sx = (use_polarisation) ? input->polarisation[0] : 0; + output->_sy = (use_polarisation) ? input->polarisation[1] : 0; + output->_sz = (use_polarisation) ? input->polarisation[2] : 0; + // time in msec -> sec + output->_t = input->time * 1e-3; + // probability, unitless + output->_p = input->weight * weight_scale; + } -int mcplinputonce_file_exist(char *fn) -{ - struct stat buffer; - return (stat (fn, &buffer) == 0); -} + int + mcplinputonce_file_exist (char* fn) { + struct stat buffer; + return (stat (fn, &buffer) == 0); + } %} DECLARE %{ -mcpl_file_t inputfile; -uint64_t nparticles; -uint64_t read_neutrons; -uint64_t used_neutrons; -uint64_t emitted_neutrons; -uint64_t current_index; -uint64_t maximum_index; -uint64_t first_particle; -int times_replayed; -mcpl_input_once_particle_t * particles; -double weight_scale; -char * resolved_filename; + mcpl_file_t inputfile; + uint64_t nparticles; + uint64_t read_neutrons; + uint64_t used_neutrons; + uint64_t emitted_neutrons; + uint64_t current_index; + uint64_t maximum_index; + uint64_t first_particle; + int times_replayed; + mcpl_input_once_particle_t* particles; + double weight_scale; + char* resolved_filename; %} INITIALIZE %{ { - if ( !filename || !filename[0] ) { - fprintf(stderr,"ERROR(%s): Requires filename parameter.\n", - NAME_CURRENT_COMP); - exit(-1); + if (!filename || !filename[0]) { + fprintf (stderr, "ERROR(%s): Requires filename parameter.\n", NAME_CURRENT_COMP); + exit (-1); } - char * fn_mcpl = mcpl_name_helper( filename, 'M' ); - char * fn_mcplgz = mcpl_name_helper( filename, 'G' ); - if ( mcplinputonce_file_exist(fn_mcpl) ) { - if ( mcplinputonce_file_exist(fn_mcplgz) ) { - fprintf(stderr,"ERROR(%s): Can not resolve input file unambiguously" - " since both %s and %s exist.\n",NAME_CURRENT_COMP, - fn_mcpl, fn_mcplgz); - exit(-1); + char* fn_mcpl = mcpl_name_helper (filename, 'M'); + char* fn_mcplgz = mcpl_name_helper (filename, 'G'); + if (mcplinputonce_file_exist (fn_mcpl)) { + if (mcplinputonce_file_exist (fn_mcplgz)) { + fprintf (stderr, + "ERROR(%s): Can not resolve input file unambiguously" + " since both %s and %s exist.\n", + NAME_CURRENT_COMP, fn_mcpl, fn_mcplgz); + exit (-1); } resolved_filename = fn_mcpl; - free(fn_mcplgz); + free (fn_mcplgz); } else { resolved_filename = fn_mcplgz; - free(fn_mcpl); + free (fn_mcpl); } } uint64_t particles_per_node; uint64_t last_particle; - if(Emax0.0) ) { - fprintf(stderr, "ERROR: Input MCPL file has invalid initial_ray_count" - " (%g). Unable to determine weight scale.\n", mcpl_ray_count ); - exit(1); + } else if (!(mcpl_ray_count > 0.0)) { + fprintf (stderr, + "ERROR: Input MCPL file has invalid initial_ray_count" + " (%g). Unable to determine weight scale.\n", + mcpl_ray_count); + exit (1); } else { weight_scale = 1.0 / mcpl_ray_count; } - if (!(nparticles = mcpl_hdr_nparticles(inputfile))) { - fprintf(stderr, "Error(%s): MCPL-file reports no present particles. Aborting.\n", NAME_CURRENT_COMP); - exit(-1); + if (!(nparticles = mcpl_hdr_nparticles (inputfile))) { + fprintf (stderr, "Error(%s): MCPL-file reports no present particles. Aborting.\n", NAME_CURRENT_COMP); + exit (-1); } else { - MPI_MASTER( - printf("Message(%s): MCPL file (%s) produced with %s.\n", NAME_CURRENT_COMP, resolved_filename, mcpl_hdr_srcname(inputfile)); - printf("Message(%s): MCPL file (%s) contains %lu particles.\n", NAME_CURRENT_COMP, resolved_filename, (long unsigned)nparticles); - ); + MPI_MASTER (printf ("Message(%s): MCPL file (%s) produced with %s.\n", NAME_CURRENT_COMP, resolved_filename, mcpl_hdr_srcname (inputfile)); + printf ("Message(%s): MCPL file (%s) contains %lu particles.\n", NAME_CURRENT_COMP, resolved_filename, (long unsigned)nparticles);); } first_particle = 0; last_particle = nparticles; -#if defined (USE_MPI) + #if defined (USE_MPI) // divy up the available particles between nodes particles_per_node = last_particle / mpi_node_count; // ensuring at least 1 particle per node (e.g., protecting against division by negative node count) - if (particles_per_node < 1) particles_per_node = 1; + if (particles_per_node < 1) + particles_per_node = 1; // each node has first index given by how many particles each should do first_particle = particles_per_node * mpi_node_rank; // the last worker keeps 'nparticles' as its last particle index, to ensure the full range is covered - if (mpi_node_rank != mpi_node_count - 1) last_particle = first_particle + particles_per_node; -#endif - read_neutrons=0; - used_neutrons=0; -#ifdef OPENACC - preload=1; - printf("OpenACC, preload implicit:\n"); -#endif + if (mpi_node_rank != mpi_node_count - 1) + last_particle = first_particle + particles_per_node; + #endif + read_neutrons = 0; + used_neutrons = 0; + #ifdef OPENACC + preload = 1; + printf ("OpenACC, preload implicit:\n"); + #endif // Move this node's pointer into the file to its first particle: // in preparation for pre-loading or first pass through TRACE - mcpl_seek(inputfile, first_particle); + mcpl_seek (inputfile, first_particle); // Index will track how many particles this component has accessed current_index = 0; // Which we want to check against how large it can grow, to know if we've exhausted the available particles maximum_index = last_particle - first_particle; particles = NULL; if (preload) { - printf("Preload requested, loading MCPLfile particles (%lu, %lu) in INITIALIZE\n", (long unsigned)first_particle, (long unsigned)last_particle); - particles = (mcpl_input_once_particle_t *) calloc(last_particle - first_particle, sizeof(mcpl_input_once_particle_t)); - for (uint64_t loop=first_particle; loop < last_particle ; loop++) { - const mcpl_particle_t *particle; - particle=mcpl_read(inputfile); - if (particle && particle->pdgcode==2112) { - if (particle->ekin>Emin*1e-9 && particle->ekinpdgcode == 2112) { + if (particle->ekin > Emin * 1e-9 && particle->ekin < Emax * 1e-9) { + mcpl_input_once_translator (polarisationuse, weight_scale, particle, particles + used_neutrons++); } read_neutrons++; } } // keep track of how many particles are available to us in the `particles` array maximum_index = used_neutrons; - printf("Done reading MCPL file (%lu, %lu), found %lu neutrons (and kept %lu)\n", - (long unsigned)first_particle, (long unsigned)last_particle, (long unsigned)read_neutrons, (long unsigned)used_neutrons); - mcpl_close_file(inputfile); + printf ("Done reading MCPL file (%lu, %lu), found %lu neutrons (and kept %lu)\n", (long unsigned)first_particle, (long unsigned)last_particle, + (long unsigned)read_neutrons, (long unsigned)used_neutrons); + mcpl_close_file (inputfile); } // keep track of how many times we had to replay the same MCPL input times_replayed = 0; @@ -203,62 +206,64 @@ INITIALIZE emitted_neutrons = 0; // Determine the total number of read (or to be read) neutrons uint64_t total_index = maximum_index; -#if defined (USE_MPI) + #if defined (USE_MPI) // add up the read neutrons (or to be read neutrons) from each node - MPI_Reduce(&maximum_index, &total_index, 1, MPI_UINT64_T, MPI_SUM, 0, MPI_COMM_WORLD); + MPI_Reduce (&maximum_index, &total_index, 1, MPI_UINT64_T, MPI_SUM, 0, MPI_COMM_WORLD); // tell all nodes the value, so they can set ncount for themselves - MPI_Bcast(&total_index, 1, MPI_UINT64_T, 0, MPI_COMM_WORLD); -#endif - mcset_ncount(total_index); // will be divided by mpi_node_count before starting the raytrace + MPI_Bcast (&total_index, 1, MPI_UINT64_T, 0, MPI_COMM_WORLD); + #endif + mcset_ncount (total_index); // will be divided by mpi_node_count before starting the raytrace %} TRACE %{ - mcpl_input_once_particle_t * ptr = NULL; + mcpl_input_once_particle_t* ptr = NULL; - if (current_index >= maximum_index){ + if (current_index >= maximum_index) { // go back to the start of this worker's particles, rather than accessing out-of-range data #pragma acc atomic write current_index = 0; -#ifndef OPENACC - if (!preload){ + #ifndef OPENACC + if (!preload) { // mcpl_seek is not available under openACC, and not used anyway - mcpl_seek(inputfile, first_particle); + mcpl_seek (inputfile, first_particle); } -#endif + #endif times_replayed++; } -#ifndef OPENACC + #ifndef OPENACC // preload can only ever be false if OPENACC is not define (in which case it is the likely code path) - if (!preload){ - ptr = (mcpl_input_once_particle_t *) calloc(1, sizeof(mcpl_input_once_particle_t)); - const mcpl_particle_t *particle;// = (mcpl_particle_t *) calloc(sizeof(mcpl_particle_t),1); - particle = mcpl_read(inputfile); + if (!preload) { + ptr = (mcpl_input_once_particle_t*)calloc (1, sizeof (mcpl_input_once_particle_t)); + const mcpl_particle_t* particle; // = (mcpl_particle_t *) calloc(sizeof(mcpl_particle_t),1); + particle = mcpl_read (inputfile); current_index++; // track how many particles have been accessed, to ensure we don't exceed our slice of the file if (!particle || particle->pdgcode != 2112) { - ABSORB; + ABSORB; } // keep track of how many neutrons have been read; but only on the first replay - if (!times_replayed) read_neutrons++; - if ( particle->ekinekin>Emax*1e-9 ) { - ABSORB; + if (!times_replayed) + read_neutrons++; + if (particle->ekin < Emin * 1e-9 || particle->ekin > Emax * 1e-9) { + ABSORB; } - mcpl_input_once_translator(polarisationuse, weight_scale, particle, ptr); + mcpl_input_once_translator (polarisationuse, weight_scale, particle, ptr); // And how many of the read neutrons actually get used - if (!times_replayed) used_neutrons++; + if (!times_replayed) + used_neutrons++; } else { -#endif + #endif ptr = particles + (_particle->_uid % maximum_index); -#ifdef OPENACC + #ifdef OPENACC #pragma acc atomic current_index++; // track how many particles have been accessed, to ensure we don't exceed our slice of the file -#else + #else } -#endif + #endif // copy from the component particle struct to the particle ray struct: if (ptr == NULL) { - fprintf(stderr, "ERROR (%s): component particle struct pointer not set! Crash before out-of-bounds memory access.\n", NAME_CURRENT_COMP); - exit(-1); + fprintf (stderr, "ERROR (%s): component particle struct pointer not set! Crash before out-of-bounds memory access.\n", NAME_CURRENT_COMP); + exit (-1); } #pragma acc atomic emitted_neutrons++; @@ -276,33 +281,34 @@ TRACE p = ptr->_p; // done with the mcpl_input_once_particle_t pointer -- free it if not pointing into the particles list - if (!preload && ptr != NULL) free(ptr); + if (!preload && ptr != NULL) + free (ptr); - if (always_smear || times_replayed){ + if (always_smear || times_replayed) { // fuzz the input if (pos_smear) { double tmpx, tmpy, tmpz; - randvec_target_circle(&tmpx, &tmpy, &tmpz, NULL, 0, 0, 1, 0); - NORM(tmpx, tmpy, tmpz); - x += tmpx * pos_smear * rand01(); - y += tmpy * pos_smear * rand01(); - z += tmpz * pos_smear * rand01(); + randvec_target_circle (&tmpx, &tmpy, &tmpz, NULL, 0, 0, 1, 0); + NORM (tmpx, tmpy, tmpz); + x += tmpx * pos_smear * rand01 (); + y += tmpy * pos_smear * rand01 (); + z += tmpz * pos_smear * rand01 (); } if (v_smear) { double fraction; - fraction = 1.0 + v_smear * randpm1(); + fraction = 1.0 + v_smear * randpm1 (); vx *= fraction; vy *= fraction; vz *= fraction; } if (dir_smear) { double vv, dx, dy, dz; - vv = sqrt(vx * vx + vy * vy + vz * vz); + vv = sqrt (vx * vx + vy * vy + vz * vz); dx = vx / vv; dy = vy / vv; dz = vz / vv; - randvec_target_circle(&dx, &dy, &dz, NULL, dx, dy, dz, sin(dir_smear * DEG2RAD)); - NORM(dx, dy, dz); + randvec_target_circle (&dx, &dy, &dz, NULL, dx, dy, dz, sin (dir_smear * DEG2RAD)); + NORM (dx, dy, dz); vx = dx * vv; vy = dy * vv; vz = dz * vv; @@ -314,64 +320,65 @@ TRACE SAVE %{ #ifndef OPENACC - if (!preload) mcpl_close_file(inputfile); + if (!preload) + mcpl_close_file (inputfile); #endif - if (particles != NULL) free(particles); + if (particles != NULL) + free (particles); %} FINALLY %{ -if (times_replayed && v_smear == 0 && pos_smear == 0 && dir_smear == 0){ - printf("Warning (%s): Forced to replay particle list %d time(s) without smearing\n", NAME_CURRENT_COMP, times_replayed); -} + if (times_replayed && v_smear == 0 && pos_smear == 0 && dir_smear == 0) { + printf ("Warning (%s): Forced to replay particle list %d time(s) without smearing\n", NAME_CURRENT_COMP, times_replayed); + } char mpi_nodes_message[256]; mpi_nodes_message[0] = '\0'; - uint64_t requested_neutrons = (uint64_t) mcget_ncount(); + uint64_t requested_neutrons = (uint64_t)mcget_ncount (); -#if defined (USE_MPI) + #if defined (USE_MPI) uint64_t accumulated[4], distributed[4]; distributed[0] = used_neutrons; distributed[1] = read_neutrons; distributed[2] = emitted_neutrons; distributed[3] = requested_neutrons; - MPI_Reduce(&distributed, &accumulated, 4, MPI_UINT64_T, MPI_SUM, 0, MPI_COMM_WORLD); -if (mpi_node_rank == 0){ - used_neutrons = accumulated[0]; - read_neutrons = accumulated[1]; - emitted_neutrons = accumulated[2]; - requested_neutrons = accumulated[3]; - sprintf(mpi_nodes_message, "he %d MPI node copies of t", mpi_node_count); -#endif - if (used_neutrons!=read_neutrons){ - fprintf(stdout,"Message(%s): You have used %llu of %llu neutrons available in the MCPL file.\n", NAME_CURRENT_COMP, used_neutrons, read_neutrons); - } - if (requested_neutrons != used_neutrons){ - char bad_particle_message[512]; - bad_particle_message[0] = '\0'; - if (used_neutrons != nparticles){ - sprintf(bad_particle_message, " (of which %llu are neutrons within the requested energy interval)", used_neutrons); + MPI_Reduce (&distributed, &accumulated, 4, MPI_UINT64_T, MPI_SUM, 0, MPI_COMM_WORLD); + if (mpi_node_rank == 0) { + used_neutrons = accumulated[0]; + read_neutrons = accumulated[1]; + emitted_neutrons = accumulated[2]; + requested_neutrons = accumulated[3]; + sprintf (mpi_nodes_message, "he %d MPI node copies of t", mpi_node_count); + #endif + if (used_neutrons != read_neutrons) { + fprintf (stdout, "Message(%s): You have used %llu of %llu neutrons available in the MCPL file.\n", NAME_CURRENT_COMP, used_neutrons, read_neutrons); } - fprintf(stderr, - "Warning (%s): You requested %llu neutrons from a file which contains %llu particles in general%s." - " T%shis component emitted %llu neutrons in total. Please examine the recorded intensities carefully.\n", - NAME_CURRENT_COMP, requested_neutrons, nparticles, bad_particle_message, mpi_nodes_message, emitted_neutrons - ); + if (requested_neutrons != used_neutrons) { + char bad_particle_message[512]; + bad_particle_message[0] = '\0'; + if (used_neutrons != nparticles) { + sprintf (bad_particle_message, " (of which %llu are neutrons within the requested energy interval)", used_neutrons); + } + fprintf (stderr, + "Warning (%s): You requested %llu neutrons from a file which contains %llu particles in general%s." + " T%shis component emitted %llu neutrons in total. Please examine the recorded intensities carefully.\n", + NAME_CURRENT_COMP, requested_neutrons, nparticles, bad_particle_message, mpi_nodes_message, emitted_neutrons); + } + #if defined (USE_MPI) } -#if defined (USE_MPI) -} -#endif - free(resolved_filename); + #endif + free (resolved_filename); %} MCDISPLAY %{ - multiline(5, 0.2,0.2,0.0, -0.2,0.2,0.0, -0.2,-0.2,0.0, 0.2,-0.2,0.0, 0.2,0.2,0.0); + multiline (5, 0.2, 0.2, 0.0, -0.2, 0.2, 0.0, -0.2, -0.2, 0.0, 0.2, -0.2, 0.0, 0.2, 0.2, 0.0); /*M*/ - multiline(5,-0.085,-0.085,0.0, -0.085,0.085,0.0, -0.045,-0.085,0.0, -0.005,0.085,0.0, -0.005,-0.085,0.0); + multiline (5, -0.085, -0.085, 0.0, -0.085, 0.085, 0.0, -0.045, -0.085, 0.0, -0.005, 0.085, 0.0, -0.005, -0.085, 0.0); /*I*/ - line(0.045,-0.085,0, 0.045, 0.085,0); - line(0.005, 0.085,0, 0.085, 0.085,0); - line(0.005,-0.085,0, 0.085,-0.085,0); + line (0.045, -0.085, 0, 0.045, 0.085, 0); + line (0.005, 0.085, 0, 0.085, 0.085, 0); + line (0.005, -0.085, 0, 0.085, -0.085, 0); %} END diff --git a/mcstas-comps/misc/MCPL_output.comp b/mcstas-comps/misc/MCPL_output.comp index 889170ad1..048f64537 100644 --- a/mcstas-comps/misc/MCPL_output.comp +++ b/mcstas-comps/misc/MCPL_output.comp @@ -59,24 +59,24 @@ DEPENDENCY "@MCPLFLAGS@" SHARE %{ -#include -#include -int mcpl_file_exist (char *filename) -{ - struct stat buffer; - return (stat (filename, &buffer) == 0); -} + #include + #include + int + mcpl_file_exist (char* filename) { + struct stat buffer; + return (stat (filename, &buffer) == 0); + } %} DECLARE %{ mcpl_outfile_t outputfile; - mcpl_particle_t *particle; + mcpl_particle_t* particle; int userflagenabled; - char * outfilename; // "/some/where/myfile.mcpl.gz" + char* outfilename; // "/some/where/myfile.mcpl.gz" double weight_scale; - //OPENACC variables: + // OPENACC variables: //(Note that cogen does not respect ifdefs here, so keep them also in non-acc mode) DArray1d X; DArray1d Y; @@ -96,318 +96,305 @@ DECLARE INITIALIZE %{ { - char * tmp = mcfull_file(filename[0] ? filename : NAME_CURRENT_COMP,NULL); - outfilename = mcpl_name_helper( tmp, 'G' ); - free(tmp); + char* tmp = mcfull_file (filename[0] ? filename : NAME_CURRENT_COMP, NULL); + outfilename = mcpl_name_helper (tmp, 'G'); + free (tmp); } -#if defined (USE_MPI) + #if defined (USE_MPI) unsigned long iproc = mpi_node_rank; unsigned long nproc = mpi_node_count; -#else + #else unsigned long iproc = 0; unsigned long nproc = 1; -#endif + #endif - outputfile = mcpl_create_outfile_mpi( outfilename, iproc, nproc ); + outputfile = mcpl_create_outfile_mpi (outfilename, iproc, nproc); - mcpl_enable_universal_pdgcode(outputfile,2112);/*all particles are neutrons*/ + mcpl_enable_universal_pdgcode (outputfile, 2112); /*all particles are neutrons*/ char line[256]; - snprintf(line,sizeof(line), - "%s %s %s", MCCODE_NAME, MCCODE_VERSION, instrument_name ); - mcpl_hdr_set_srcname(outputfile,line); - snprintf( line,sizeof(line), - "Output by COMPONENT: %s",NAME_CURRENT_COMP ); - mcpl_hdr_add_comment(outputfile,line); + snprintf (line, sizeof (line), "%s %s %s", MCCODE_NAME, MCCODE_VERSION, instrument_name); + mcpl_hdr_set_srcname (outputfile, line); + snprintf (line, sizeof (line), "Output by COMPONENT: %s", NAME_CURRENT_COMP); + mcpl_hdr_add_comment (outputfile, line); /*also add the instrument file and the command line as blobs*/ - if ( mcpl_file_exist(instrument_source) ) { + if (mcpl_file_exist (instrument_source)) { uint64_t instrsrc_buflen; char* instrsrc_buf = NULL; - mcpl_read_file_to_buffer( instrument_source, - 100*1024*1024,//100mb, must be less than uint32 max - 1,//text, - &instrsrc_buflen, &instrsrc_buf ); - mcpl_hdr_add_data( outputfile, "mccode_instr_file", - (uint32_t)instrsrc_buflen, - instrsrc_buf); - free(instrsrc_buf); + mcpl_read_file_to_buffer (instrument_source, + 100 * 1024 * 1024, // 100mb, must be less than uint32 max + 1, // text, + &instrsrc_buflen, &instrsrc_buf); + mcpl_hdr_add_data (outputfile, "mccode_instr_file", (uint32_t)instrsrc_buflen, instrsrc_buf); + free (instrsrc_buf); } else { - fprintf(stderr,"\nWarning (%s): Source instrument file (%s)" - " not found, hence not embedded.\n", - NAME_CURRENT_COMP, instrument_source); + fprintf (stderr, + "\nWarning (%s): Source instrument file (%s)" + " not found, hence not embedded.\n", + NAME_CURRENT_COMP, instrument_source); } { char clr[2048]; - char *clrp = clr; + char* clrp = clr; clrp = clr; - clrp += snprintf(clrp,sizeof(clr),"%s",instrument_exe); + clrp += snprintf (clrp, sizeof (clr), "%s", instrument_exe); char Parameters[CHAR_BUF_LENGTH]; - for ( int ii=0; ii_uid;// % GPU_INNERLOOP; - if (cap < ceil(buffermax)) { - X[cap]=x; - Y[cap]=y; - Z[cap]=z; - VX[cap]=vx; - VY[cap]=vy; - VZ[cap]=vz; - SX[cap]=sx; - SY[cap]=sy; - SZ[cap]=sz; - T[cap]=t; - P[cap]=p; - if(userflagenabled) { + if (cap < ceil (buffermax)) { + X[cap] = x; + Y[cap] = y; + Z[cap] = z; + VX[cap] = vx; + VY[cap] = vy; + VZ[cap] = vz; + SX[cap] = sx; + SY[cap] = sy; + SZ[cap] = sz; + T[cap] = t; + P[cap] = p; + if (userflagenabled) { int fail; - double uvar = particle_getvar(_particle,userflag,&fail); - if(fail) - uvar=0; + double uvar = particle_getvar (_particle, userflag, &fail); + if (fail) + uvar = 0; U[cap] = uvar; } SCATTER; } -#else + #else double nrm; /*positions are in cm*/ - particle->position[0]=x*100; - particle->position[1]=y*100; - particle->position[2]=z*100; - - if(polarisationuse){ - particle->polarisation[0]=sx; - particle->polarisation[1]=sy; - particle->polarisation[2]=sz; + particle->position[0] = x * 100; + particle->position[1] = y * 100; + particle->position[2] = z * 100; + + if (polarisationuse) { + particle->polarisation[0] = sx; + particle->polarisation[1] = sy; + particle->polarisation[2] = sz; } - nrm =sqrt(vx*vx + vy*vy + vz*vz); + nrm = sqrt (vx * vx + vy * vy + vz * vz); /*ekin is in MeV, in McStas meV*/ - particle->ekin = VS2E*nrm*nrm/1e9; - particle->direction[0] = vx/nrm; - particle->direction[1] = vy/nrm; - particle->direction[2] = vz/nrm; + particle->ekin = VS2E * nrm * nrm / 1e9; + particle->direction[0] = vx / nrm; + particle->direction[1] = vy / nrm; + particle->direction[2] = vz / nrm; /*time in ms:*/ - particle->time = t*1e3; + particle->time = t * 1e3; /*weight in unspecified units:*/ particle->weight = p * weight_scale; - if( userflagenabled ){ - //TODO: Reconsider this passing of uint32_t flags through a double + if (userflagenabled) { + // TODO: Reconsider this passing of uint32_t flags through a double int fail; - double uvar = particle_getvar(_particle,userflag,&fail); - if(fail) - uvar=0; - particle->userflags = (uint32_t) uvar; + double uvar = particle_getvar (_particle, userflag, &fail); + if (fail) + uvar = 0; + particle->userflags = (uint32_t)uvar; } - MPI_MASTER( - if (verbose==3 && mcrun_num<10) { - printf("id=%lld\tpdg=2112\tekin=%g MeV\tx=%g cm\ty=%g cm\tz=%g cm\tux=%g\tuy=%g\tuz=%g\tt=%g ms\tweight=%g\tpolx=%g\tpoly=%g\tpolz=%g\n", - mcrun_num, particle->ekin, particle->position[0], particle->position[1], particle->position[2], - particle->direction[0], particle->direction[1], particle->direction[2], particle->time, particle->weight, - particle->polarisation[0], particle->polarisation[1], particle->polarisation[2]); - } - ); + MPI_MASTER (if (verbose == 3 && mcrun_num < 10) { + printf ("id=%lld\tpdg=2112\tekin=%g MeV\tx=%g cm\ty=%g cm\tz=%g cm\tux=%g\tuy=%g\tuz=%g\tt=%g ms\tweight=%g\tpolx=%g\tpoly=%g\tpolz=%g\n", mcrun_num, + particle->ekin, particle->position[0], particle->position[1], particle->position[2], particle->direction[0], particle->direction[1], + particle->direction[2], particle->time, particle->weight, particle->polarisation[0], particle->polarisation[1], particle->polarisation[2]); + }); - mcpl_add_particle(outputfile,particle); + mcpl_add_particle (outputfile, particle); SCATTER; -#endif + #endif %} SAVE %{ -#ifdef OPENACC + #ifdef OPENACC double nrm; unsigned long long i; - if (captured > ceil(buffermax)) { - fprintf(stderr,"MCPL_output captured %g particles which is more than the buffersize (%g)!\n",(double)captured,buffermax); + if (captured > ceil (buffermax)) { + fprintf (stderr, "MCPL_output captured %g particles which is more than the buffersize (%g)!\n", (double)captured, buffermax); } mcpl_particle_t Particle; - for (i=0;i0) { + for (i = 0; i < captured; i++) { + if (P[i] > 0) { /*positions are in cm*/ - Particle.position[0]=X[i]*100; - Particle.position[1]=Y[i]*100; - Particle.position[2]=Z[i]*100; - - if(polarisationuse){ - Particle.polarisation[0]=SX[i]; - Particle.polarisation[1]=SY[i]; - Particle.polarisation[2]=SZ[i]; + Particle.position[0] = X[i] * 100; + Particle.position[1] = Y[i] * 100; + Particle.position[2] = Z[i] * 100; + + if (polarisationuse) { + Particle.polarisation[0] = SX[i]; + Particle.polarisation[1] = SY[i]; + Particle.polarisation[2] = SZ[i]; } - nrm =sqrt(VX[i]*VX[i] + VY[i]*VY[i] + VZ[i]*VZ[i]); + nrm = sqrt (VX[i] * VX[i] + VY[i] * VY[i] + VZ[i] * VZ[i]); /*ekin is in MeV, in McStas meV*/ - Particle.ekin = VS2E*nrm*nrm/1e9; - Particle.direction[0] = VX[i]/nrm; - Particle.direction[1] = VY[i]/nrm; - Particle.direction[2] = VZ[i]/nrm; + Particle.ekin = VS2E * nrm * nrm / 1e9; + Particle.direction[0] = VX[i] / nrm; + Particle.direction[1] = VY[i] / nrm; + Particle.direction[2] = VZ[i] / nrm; /*time in ms:*/ - Particle.time = T[i]*1e3; + Particle.time = T[i] * 1e3; /*weight in unspecified units:*/ Particle.weight = P[i] * weight_scale; - if( userflagenabled ) - Particle.userflags = (uint32_t) U[i]; - - if (verbose==3 && mcrun_num<10) { - printf("id=%ld\tpdg=2112\tekin=%g MeV\tx=%g cm\ty=%g cm\tz=%g cm\tux=%g\tuy=%g\tuz=%g\tt=%g ms\tweight=%g\tpolx=%g\tpoly=%g\tpolz=%g\n", - mcrun_num, Particle.ekin, Particle.position[0], Particle.position[1], Particle.position[2], - Particle.direction[0], Particle.direction[1], Particle.direction[2], Particle.time, Particle.weight, - Particle.polarisation[0], Particle.polarisation[1], Particle.polarisation[2]); + if (userflagenabled) + Particle.userflags = (uint32_t)U[i]; + + if (verbose == 3 && mcrun_num < 10) { + printf ("id=%ld\tpdg=2112\tekin=%g MeV\tx=%g cm\ty=%g cm\tz=%g cm\tux=%g\tuy=%g\tuz=%g\tt=%g ms\tweight=%g\tpolx=%g\tpoly=%g\tpolz=%g\n", mcrun_num, + Particle.ekin, Particle.position[0], Particle.position[1], Particle.position[2], Particle.direction[0], Particle.direction[1], + Particle.direction[2], Particle.time, Particle.weight, Particle.polarisation[0], Particle.polarisation[1], Particle.polarisation[2]); } - mcpl_add_particle(outputfile,&Particle); + mcpl_add_particle (outputfile, &Particle); } } -#endif + #endif %} FINALLY %{ - if ( weight_mode != 2 ) - mcpl_hdr_add_stat_sum( outputfile, "initial_ray_count", - (double)mcget_ncount() ); - mcpl_closeandgzip_outfile(outputfile); - -#ifdef USE_MPI - MPI_Barrier(MPI_COMM_WORLD); -#endif -#if defined (USE_MPI) + if (weight_mode != 2) + mcpl_hdr_add_stat_sum (outputfile, "initial_ray_count", (double)mcget_ncount ()); + mcpl_closeandgzip_outfile (outputfile); + + #ifdef USE_MPI + MPI_Barrier (MPI_COMM_WORLD); + #endif + #if defined (USE_MPI) unsigned long iproc = mpi_node_rank; unsigned long nproc = mpi_node_count; -#else + #else unsigned long iproc = 0; unsigned long nproc = 1; -#endif - - if ( iproc == 0 ) - mcpl_merge_outfiles_mpi( outfilename, nproc ); - if(verbose) { - MPI_MASTER( - printf("\n\nMCPL output summary from %s\n",outfilename); - mcpl_dump(outfilename, 0, 0, 10); - ); + #endif + + if (iproc == 0) + mcpl_merge_outfiles_mpi (outfilename, nproc); + if (verbose) { + MPI_MASTER (printf ("\n\nMCPL output summary from %s\n", outfilename); mcpl_dump (outfilename, 0, 0, 10);); } -#ifdef OPENACC - destroy_darr1d(X); - destroy_darr1d(Y); - destroy_darr1d(Z); - destroy_darr1d(VX); - destroy_darr1d(VY); - destroy_darr1d(VZ); - destroy_darr1d(SX); - destroy_darr1d(SY); - destroy_darr1d(SZ); - destroy_darr1d(T); - destroy_darr1d(P); -#endif - free(outfilename); + #ifdef OPENACC + destroy_darr1d (X); + destroy_darr1d (Y); + destroy_darr1d (Z); + destroy_darr1d (VX); + destroy_darr1d (VY); + destroy_darr1d (VZ); + destroy_darr1d (SX); + destroy_darr1d (SY); + destroy_darr1d (SZ); + destroy_darr1d (T); + destroy_darr1d (P); + #endif + free (outfilename); %} MCDISPLAY %{ - double t,dt; + double t, dt; int i; - multiline(5, 0.2,0.2,0.0, -0.2,0.2,0.0, -0.2,-0.2,0.0, 0.2,-0.2,0.0, 0.2,0.2,0.0); + multiline (5, 0.2, 0.2, 0.0, -0.2, 0.2, 0.0, -0.2, -0.2, 0.0, 0.2, -0.2, 0.0, 0.2, 0.2, 0.0); /*M*/ - multiline(5,-0.085,-0.085,0.0, -0.085,0.085,0.0, -0.045,-0.085,0.0, -0.005,0.085,0.0, -0.005,-0.085,0.0); + multiline (5, -0.085, -0.085, 0.0, -0.085, 0.085, 0.0, -0.045, -0.085, 0.0, -0.005, 0.085, 0.0, -0.005, -0.085, 0.0); /*O*/ - dt=2*M_PI/32; - t=0; - for (i=0;i<32;i++){ - line(0.04*cos(t)+0.045,0.08*sin(t),0, 0.04*cos(t+dt)+0.045,0.08*sin(t+dt),0); - t+=dt; + dt = 2 * M_PI / 32; + t = 0; + for (i = 0; i < 32; i++) { + line (0.04 * cos (t) + 0.045, 0.08 * sin (t), 0, 0.04 * cos (t + dt) + 0.045, 0.08 * sin (t + dt), 0); + t += dt; } %} diff --git a/mcstas-comps/misc/MCPL_output_noacc.comp b/mcstas-comps/misc/MCPL_output_noacc.comp index caa082445..8669cf139 100644 --- a/mcstas-comps/misc/MCPL_output_noacc.comp +++ b/mcstas-comps/misc/MCPL_output_noacc.comp @@ -60,132 +60,127 @@ NOACC SHARE %{ -#include -#include -int mcpl_file_exist (char *filename) -{ - struct stat buffer; - return (stat (filename, &buffer) == 0); -} + #include + #include + int + mcpl_file_exist (char* filename) { + struct stat buffer; + return (stat (filename, &buffer) == 0); + } %} DECLARE %{ mcpl_outfile_t outputfile; - mcpl_particle_t *particle; + mcpl_particle_t* particle; int userflagenabled; - char * outfilename; // "/some/where/myfile.mcpl.gz" + char* outfilename; // "/some/where/myfile.mcpl.gz" double weight_scale; %} INITIALIZE %{ { - char * tmp = mcfull_file(filename[0] ? filename : NAME_CURRENT_COMP,NULL); - outfilename = mcpl_name_helper( tmp, 'G' ); - free(tmp); + char* tmp = mcfull_file (filename[0] ? filename : NAME_CURRENT_COMP, NULL); + outfilename = mcpl_name_helper (tmp, 'G'); + free (tmp); } -#if defined (USE_MPI) + #if defined (USE_MPI) unsigned long iproc = mpi_node_rank; unsigned long nproc = mpi_node_count; -#else + #else unsigned long iproc = 0; unsigned long nproc = 1; -#endif + #endif - outputfile = mcpl_create_outfile_mpi( outfilename, iproc, nproc ); + outputfile = mcpl_create_outfile_mpi (outfilename, iproc, nproc); - mcpl_enable_universal_pdgcode(outputfile,2112);/*all particles are neutrons*/ + mcpl_enable_universal_pdgcode (outputfile, 2112); /*all particles are neutrons*/ char line[256]; - snprintf(line,sizeof(line), - "%s %s %s", MCCODE_NAME, MCCODE_VERSION, instrument_name ); - mcpl_hdr_set_srcname(outputfile,line); - snprintf( line,sizeof(line), - "Output by COMPONENT: %s",NAME_CURRENT_COMP ); - mcpl_hdr_add_comment(outputfile,line); + snprintf (line, sizeof (line), "%s %s %s", MCCODE_NAME, MCCODE_VERSION, instrument_name); + mcpl_hdr_set_srcname (outputfile, line); + snprintf (line, sizeof (line), "Output by COMPONENT: %s", NAME_CURRENT_COMP); + mcpl_hdr_add_comment (outputfile, line); /*also add the instrument file and the command line as blobs*/ - if ( mcpl_file_exist(instrument_source) ) { + if (mcpl_file_exist (instrument_source)) { uint64_t instrsrc_buflen; char* instrsrc_buf = NULL; - mcpl_read_file_to_buffer( instrument_source, - 100*1024*1024,//100mb, must be less than uint32 max - 1,//text, - &instrsrc_buflen, &instrsrc_buf ); - mcpl_hdr_add_data( outputfile, "mccode_instr_file", - (uint32_t)instrsrc_buflen, - instrsrc_buf); - free(instrsrc_buf); + mcpl_read_file_to_buffer (instrument_source, + 100 * 1024 * 1024, // 100mb, must be less than uint32 max + 1, // text, + &instrsrc_buflen, &instrsrc_buf); + mcpl_hdr_add_data (outputfile, "mccode_instr_file", (uint32_t)instrsrc_buflen, instrsrc_buf); + free (instrsrc_buf); } else { - fprintf(stderr,"\nWarning (%s): Source instrument file (%s)" - " not found, hence not embedded.\n", - NAME_CURRENT_COMP, instrument_source); + fprintf (stderr, + "\nWarning (%s): Source instrument file (%s)" + " not found, hence not embedded.\n", + NAME_CURRENT_COMP, instrument_source); } { char clr[2048]; - char *clrp = clr; + char* clrp = clr; clrp = clr; - clrp += snprintf(clrp,sizeof(clr),"%s",instrument_exe); + clrp += snprintf (clrp, sizeof (clr), "%s", instrument_exe); char Parameters[CHAR_BUF_LENGTH]; - for ( int ii=0; iiposition[0]=x*100; - particle->position[1]=y*100; - particle->position[2]=z*100; + particle->position[0] = x * 100; + particle->position[1] = y * 100; + particle->position[2] = z * 100; - if(polarisationuse){ - particle->polarisation[0]=sx; - particle->polarisation[1]=sy; - particle->polarisation[2]=sz; + if (polarisationuse) { + particle->polarisation[0] = sx; + particle->polarisation[1] = sy; + particle->polarisation[2] = sz; } - nrm =sqrt(vx*vx + vy*vy + vz*vz); + nrm = sqrt (vx * vx + vy * vy + vz * vz); /*ekin is in MeV, in McStas meV*/ - particle->ekin = VS2E*nrm*nrm/1e9; - particle->direction[0] = vx/nrm; - particle->direction[1] = vy/nrm; - particle->direction[2] = vz/nrm; + particle->ekin = VS2E * nrm * nrm / 1e9; + particle->direction[0] = vx / nrm; + particle->direction[1] = vy / nrm; + particle->direction[2] = vz / nrm; /*time in ms:*/ - particle->time = t*1e3; + particle->time = t * 1e3; /*weight in unspecified units:*/ particle->weight = p * weight_scale; - if( userflagenabled ){ - //TODO: Reconsider this passing of uint32_t flags through a double + if (userflagenabled) { + // TODO: Reconsider this passing of uint32_t flags through a double int fail; - double uvar = particle_getvar(_particle,userflag,&fail); - if(fail) - uvar=0; - particle->userflags = (uint32_t) uvar; + double uvar = particle_getvar (_particle, userflag, &fail); + if (fail) + uvar = 0; + particle->userflags = (uint32_t)uvar; } - MPI_MASTER( - if (verbose==3 && mcrun_num<10) { - printf("id=%lld\tpdg=2112\tekin=%g MeV\tx=%g cm\ty=%g cm\tz=%g cm\tux=%g\tuy=%g\tuz=%g\tt=%g ms\tweight=%g\tpolx=%g\tpoly=%g\tpolz=%g\n", - mcrun_num, particle->ekin, particle->position[0], particle->position[1], particle->position[2], - particle->direction[0], particle->direction[1], particle->direction[2], particle->time, particle->weight, - particle->polarisation[0], particle->polarisation[1], particle->polarisation[2]); - } - ); + MPI_MASTER (if (verbose == 3 && mcrun_num < 10) { + printf ("id=%lld\tpdg=2112\tekin=%g MeV\tx=%g cm\ty=%g cm\tz=%g cm\tux=%g\tuy=%g\tuz=%g\tt=%g ms\tweight=%g\tpolx=%g\tpoly=%g\tpolz=%g\n", mcrun_num, + particle->ekin, particle->position[0], particle->position[1], particle->position[2], particle->direction[0], particle->direction[1], + particle->direction[2], particle->time, particle->weight, particle->polarisation[0], particle->polarisation[1], particle->polarisation[2]); + }); - mcpl_add_particle(outputfile,particle); + mcpl_add_particle (outputfile, particle); SCATTER; %} @@ -242,46 +234,42 @@ SAVE FINALLY %{ - if ( weight_mode != 2 ) - mcpl_hdr_add_stat_sum( outputfile, "initial_ray_count", - (double)mcget_ncount() ); - mcpl_closeandgzip_outfile(outputfile); + if (weight_mode != 2) + mcpl_hdr_add_stat_sum (outputfile, "initial_ray_count", (double)mcget_ncount ()); + mcpl_closeandgzip_outfile (outputfile); -#ifdef USE_MPI - MPI_Barrier(MPI_COMM_WORLD); -#endif -#if defined (USE_MPI) + #ifdef USE_MPI + MPI_Barrier (MPI_COMM_WORLD); + #endif + #if defined (USE_MPI) unsigned long iproc = mpi_node_rank; unsigned long nproc = mpi_node_count; -#else + #else unsigned long iproc = 0; unsigned long nproc = 1; -#endif + #endif - if ( iproc == 0 ) - mcpl_merge_outfiles_mpi( outfilename, nproc ); - if(verbose) { - MPI_MASTER( - printf("\n\nMCPL output summary from %s\n",outfilename); - mcpl_dump(outfilename, 0, 0, 10); - ); + if (iproc == 0) + mcpl_merge_outfiles_mpi (outfilename, nproc); + if (verbose) { + MPI_MASTER (printf ("\n\nMCPL output summary from %s\n", outfilename); mcpl_dump (outfilename, 0, 0, 10);); } - free(outfilename); + free (outfilename); %} MCDISPLAY %{ - double t,dt; + double t, dt; int i; - multiline(5, 0.2,0.2,0.0, -0.2,0.2,0.0, -0.2,-0.2,0.0, 0.2,-0.2,0.0, 0.2,0.2,0.0); + multiline (5, 0.2, 0.2, 0.0, -0.2, 0.2, 0.0, -0.2, -0.2, 0.0, 0.2, -0.2, 0.0, 0.2, 0.2, 0.0); /*M*/ - multiline(5,-0.085,-0.085,0.0, -0.085,0.085,0.0, -0.045,-0.085,0.0, -0.005,0.085,0.0, -0.005,-0.085,0.0); + multiline (5, -0.085, -0.085, 0.0, -0.085, 0.085, 0.0, -0.045, -0.085, 0.0, -0.005, 0.085, 0.0, -0.005, -0.085, 0.0); /*O*/ - dt=2*M_PI/32; - t=0; - for (i=0;i<32;i++){ - line(0.04*cos(t)+0.045,0.08*sin(t),0, 0.04*cos(t+dt)+0.045,0.08*sin(t+dt),0); - t+=dt; + dt = 2 * M_PI / 32; + t = 0; + for (i = 0; i < 32; i++) { + line (0.04 * cos (t) + 0.045, 0.08 * sin (t), 0, 0.04 * cos (t + dt) + 0.045, 0.08 * sin (t + dt), 0); + t += dt; } %} diff --git a/mcstas-comps/misc/Progress_bar.comp b/mcstas-comps/misc/Progress_bar.comp index a27a18982..c8dd6bc71 100644 --- a/mcstas-comps/misc/Progress_bar.comp +++ b/mcstas-comps/misc/Progress_bar.comp @@ -41,125 +41,109 @@ SETTING PARAMETERS (string profile="NULL", percent=10,flag_save=0,minutes=0) /* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */ DECLARE %{ -#ifndef PROGRESS_BAR -#define PROGRESS_BAR -#else -#error Only one Progress_bar component may be used in an instrument definition. -#endif + #ifndef PROGRESS_BAR + #define PROGRESS_BAR + #else + #error Only one Progress_bar component may be used in an instrument definition. + #endif -double IntermediateCnts; -time_t StartTime; -time_t EndTime; -time_t CurrentTime; -char infostring[64]; + double IntermediateCnts; + time_t StartTime; + time_t EndTime; + time_t CurrentTime; + char infostring[64]; %} INITIALIZE %{ -IntermediateCnts=0; -StartTime=0; -EndTime=0; -CurrentTime=0; - -fprintf(stdout, "[%s] Initialize\n", instrument_name); - if (percent*mcget_ncount()/100 < 1e5) { - percent=1e5*100.0/mcget_ncount(); + IntermediateCnts = 0; + StartTime = 0; + EndTime = 0; + CurrentTime = 0; + + fprintf (stdout, "[%s] Initialize\n", instrument_name); + if (percent * mcget_ncount () / 100 < 1e5) { + percent = 1e5 * 100.0 / mcget_ncount (); } #ifdef OPENACC - time(&StartTime); + time (&StartTime); #endif -#ifdef USE_MPI - sprintf(infostring, "(%i MPI processes) ", mpi_node_count); -#else - sprintf(infostring, "(single process) "); -#endif - + #ifdef USE_MPI + sprintf (infostring, "(%i MPI processes) ", mpi_node_count); + #else + sprintf (infostring, "(single process) "); + #endif %} TRACE %{ -#ifndef OPENACC + #ifndef OPENACC double ncount; - ncount = mcget_run_num(); + ncount = mcget_run_num (); if (!StartTime) { - time(&StartTime); /* compute starting time */ + time (&StartTime); /* compute starting time */ IntermediateCnts = 1e3; } time_t NowTime; - time(&NowTime); + time (&NowTime); /* compute initial estimate of computation duration */ if (!EndTime && ncount >= IntermediateCnts) { CurrentTime = NowTime; - if (difftime(NowTime,StartTime) > 10 && ncount) { /* wait 10 sec before writing ETA */ - EndTime = StartTime + (time_t)(difftime(NowTime,StartTime) - *(double)mcget_ncount()/ncount); + if (difftime (NowTime, StartTime) > 10 && ncount) { /* wait 10 sec before writing ETA */ + EndTime = StartTime + (time_t)(difftime (NowTime, StartTime) * (double)mcget_ncount () / ncount); IntermediateCnts = 0; - MPI_MASTER( - fprintf(stdout, "\nTrace ETA "); - fprintf(stdout, "%s", infostring); - if (difftime(EndTime,StartTime) < 60.0) - fprintf(stdout, "%g [s] ", difftime(EndTime,StartTime)); - else if (difftime(EndTime,StartTime) > 3600.0) - fprintf(stdout, "%g [h] ", difftime(EndTime,StartTime)/3600.0); - else - fprintf(stdout, "%g [min] ", difftime(EndTime,StartTime)/60.0); - fprintf(stdout, "\n"); - ); - } else IntermediateCnts += 1e3; - fflush(stdout); + MPI_MASTER (fprintf (stdout, "\nTrace ETA "); fprintf (stdout, "%s", infostring); + if (difftime (EndTime, StartTime) < 60.0) fprintf (stdout, "%g [s] ", difftime (EndTime, StartTime)); + else if (difftime (EndTime, StartTime) > 3600.0) fprintf (stdout, "%g [h] ", difftime (EndTime, StartTime) / 3600.0); + else fprintf (stdout, "%g [min] ", difftime (EndTime, StartTime) / 60.0); fprintf (stdout, "\n");); + } else + IntermediateCnts += 1e3; + fflush (stdout); } /* display percentage when percent or minutes have reached step */ - if (EndTime && mcget_ncount() && - ( (minutes && difftime(NowTime,CurrentTime) > minutes*60) - || (percent && !minutes && ncount >= IntermediateCnts)) ) - { - MPI_MASTER( - fprintf(stdout, "%llu %%\n", (unsigned long long)(ncount*100.0/mcget_ncount())); fflush(stdout); - ); + if (EndTime && mcget_ncount () && ((minutes && difftime (NowTime, CurrentTime) > minutes * 60) || (percent && !minutes && ncount >= IntermediateCnts))) { + MPI_MASTER (fprintf (stdout, "%llu %%\n", (unsigned long long)(ncount * 100.0 / mcget_ncount ())); fflush (stdout);); CurrentTime = NowTime; - IntermediateCnts = ncount + percent*mcget_ncount()/100; + IntermediateCnts = ncount + percent * mcget_ncount () / 100; /* check that next intermediate ncount check is a multiple of the desired percentage */ - IntermediateCnts = floor(IntermediateCnts*100/percent/mcget_ncount())*percent*mcget_ncount()/100; + IntermediateCnts = floor (IntermediateCnts * 100 / percent / mcget_ncount ()) * percent * mcget_ncount () / 100; /* raise flag to indicate that we did something */ SCATTER; - if (flag_save) save(NULL); + if (flag_save) + save (NULL); } -#endif + #endif %} SAVE %{ - MPI_MASTER(fprintf(stdout, "\nSave [%s]\n", instrument_name);); - if (profile && strlen(profile) && strcmp(profile,"NULL") && strcmp(profile,"0")) { + MPI_MASTER (fprintf (stdout, "\nSave [%s]\n", instrument_name);); + if (profile && strlen (profile) && strcmp (profile, "NULL") && strcmp (profile, "0")) { char filename[256]; - if (!strlen(profile) || !strcmp(profile,"NULL") || !strcmp(profile,"0")) strcpy(filename, instrument_name); - else strcpy(filename, profile); - DETECTOR_OUT_1D( - "Intensity profiler", - "Component index [1]", - "Intensity", - "prof", 1, mcNUMCOMP, mcNUMCOMP-1, - &(instrument->counter_N[1]),&(instrument->counter_P[1]),&(instrument->counter_P2[1]), - filename); - + if (!strlen (profile) || !strcmp (profile, "NULL") || !strcmp (profile, "0")) + strcpy (filename, instrument_name); + else + strcpy (filename, profile); + DETECTOR_OUT_1D ("Intensity profiler", "Component index [1]", "Intensity", "prof", 1, mcNUMCOMP, mcNUMCOMP - 1, &(instrument->counter_N[1]), + &(instrument->counter_P[1]), &(instrument->counter_P2[1]), filename); } %} FINALLY %{ time_t NowTime; - time(&NowTime); - fprintf(stdout, "\nFinally [%s: %s]. Time: ", instrument_name, dirname ? dirname : "."); - if (difftime(NowTime,StartTime) < 60.0) - fprintf(stdout, "%g [s] ", difftime(NowTime,StartTime)); - else if (difftime(NowTime,StartTime) > 3600.0) - fprintf(stdout, "%g [h] ", difftime(NowTime,StartTime)/3600.0); + time (&NowTime); + fprintf (stdout, "\nFinally [%s: %s]. Time: ", instrument_name, dirname ? dirname : "."); + if (difftime (NowTime, StartTime) < 60.0) + fprintf (stdout, "%g [s] ", difftime (NowTime, StartTime)); + else if (difftime (NowTime, StartTime) > 3600.0) + fprintf (stdout, "%g [h] ", difftime (NowTime, StartTime) / 3600.0); else - fprintf(stdout, "%g [min] ", difftime(NowTime,StartTime)/60.0); - fprintf(stdout, "\n"); + fprintf (stdout, "%g [min] ", difftime (NowTime, StartTime) / 60.0); + fprintf (stdout, "\n"); %} MCDISPLAY diff --git a/mcstas-comps/misc/Shape.comp b/mcstas-comps/misc/Shape.comp index af5fe9fd0..fd6e2be4b 100644 --- a/mcstas-comps/misc/Shape.comp +++ b/mcstas-comps/misc/Shape.comp @@ -75,14 +75,14 @@ SHARE DECLARE %{ -off_struct offdata; + off_struct offdata; %} INITIALIZE %{ -if (geometry && strlen(geometry) && strcmp(geometry, "NULL") && strcmp(geometry, "0")) { - if (off_init(geometry, xwidth, yheight, zdepth, !center, &offdata)) { - thickness=0; + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { + if (off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) { + thickness = 0; } } %} @@ -94,18 +94,14 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen(geometry) && strcmp(geometry, "NULL") && strcmp(geometry, "0")) { /* OFF file */ - off_display(offdata); - } - else - if (radius > 0 && yheight) { /* cylinder along y*/ - cylinder(0,0,0,radius,yheight,thickness, nx, ny, nz); - } - else if (xwidth && yheight) { /* box/rectangle */ - box(0,0,0,xwidth,yheight,zdepth,thickness, nx, ny, nz); - } - else if (radius > 0 && !yheight) { /* sphere */ - sphere(0,0,0,radius); - } + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ + off_display (offdata); + } else if (radius > 0 && yheight) { /* cylinder along y*/ + cylinder (0, 0, 0, radius, yheight, thickness, nx, ny, nz); + } else if (xwidth && yheight) { /* box/rectangle */ + box (0, 0, 0, xwidth, yheight, zdepth, thickness, nx, ny, nz); + } else if (radius > 0 && !yheight) { /* sphere */ + sphere (0, 0, 0, radius); + } %} END diff --git a/mcstas-comps/misc/Shape_simple.comp b/mcstas-comps/misc/Shape_simple.comp index b7279896d..b21694272 100644 --- a/mcstas-comps/misc/Shape_simple.comp +++ b/mcstas-comps/misc/Shape_simple.comp @@ -72,14 +72,14 @@ SHARE DECLARE %{ -off_struct offdata; + off_struct offdata; %} INITIALIZE %{ -if (geometry && strlen(geometry) && strcmp(geometry, "NULL") && strcmp(geometry, "0")) { - if (off_init(geometry, xwidth, yheight, zdepth, !center, &offdata)) { - thickness=0; + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { + if (off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) { + thickness = 0; } } %} @@ -91,17 +91,14 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen(geometry) && strcmp(geometry, "NULL") && strcmp(geometry, "0")) { /* OFF file */ - off_display(offdata); - } - else if (radius > 0 && yheight) { /* cylinder */ - cylinder(0,0,0,radius,yheight,thickness, 0,1,0); - } - else if (xwidth && yheight) { /* box/rectangle XY */ - box(0,0,0,xwidth, yheight, zdepth, 0, 0, 1, 0); - } - else if (radius > 0 && !yheight) { /* sphere */ - sphere(0,0,0,radius); + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ + off_display (offdata); + } else if (radius > 0 && yheight) { /* cylinder */ + cylinder (0, 0, 0, radius, yheight, thickness, 0, 1, 0); + } else if (xwidth && yheight) { /* box/rectangle XY */ + box (0, 0, 0, xwidth, yheight, zdepth, 0, 0, 1, 0); + } else if (radius > 0 && !yheight) { /* sphere */ + sphere (0, 0, 0, radius); } %}