-
|
Hi, I am using a MCPL file to generate a MCNP source for further transport calculation. When testing the source I am facing some MCNP issues due to a weight of particles equal to 0. I had a look at the possibilities given by mcpltool to modify files. I can convert the mcpl file into a text file and then modify it but my question is then how can I convert it back? Is there a way to modify directly the mcpl file filtering the particles of interest? Thank you in advance for the help in solving the issue. Best regards, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
|
mcpltool itself cannot do this (AFAIK). That said I can think of 3 other ways:
There are other ways of course but these are the first ones I could think of off the cuff |
Beta Was this translation helpful? Give feedback.
-
|
Let me initially +1 @ebknudsen's comment above. For the "McStas as converter" case there are a few examples here. On the other hand I also seem to remember being able to "filter" (together with @EsbenKlinkby) on particle properties using |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the question @AmChamb ! This is in fact pretty simple to do if you are able and willing to compile a small C programme, like @ebknudsen said. The C-code itself is basically the example at https://mctools.github.io/mcpl/usage_c/#extracting-subset-of-particles-from-file but with the filtering if-statement changed to reflect your desired requirement of having a nonzero particle weight. So in a file called #include "mcpl.h"
#include <stdio.h>
int main(int argc,char**argv) {
if (argc!=3) {
printf("Please supply input and output filenames\n");
return 1;
}
const char * infilename = argv[1];
const char * outfilename = argv[2];
mcpl_file_t fi = mcpl_open_file(infilename);
mcpl_outfile_t fo = mcpl_create_outfile(outfilename);
mcpl_transfer_metadata(fi, fo);
const mcpl_particle_t* particle;
while ( ( particle = mcpl_read(fi) ) ) {
if ( particle->weight > 0.0 ) { // This is the crucial line defining our filter.
mcpl_transfer_last_read_particle(fi,fo);
}
}
mcpl_close_outfile(fo);
mcpl_close_file(fi);
}In order to turn the code above into an actual programme that you can run, you must compile it with a C compiler. Furthermore, you must have installed the $> python3 -m venv my_venv
$> . ./my_venv/bin/activate
$> pip install mcplThe above is only needed the first time. If coming back later in a fresh terminal, you can simply type the second of the commands ( No matter how you have installed $> mcpltool --help
$> mcpl-config --summaryIf the commands above gave some sort of sensible output, and in particular if you did not get "command not found" or other errors, you are good to finally compile your filter programme. So in the folder where you have saved the $> gcc myfilter.c $(mcpl-config --show buildflags) -o myfilterappThis assumes you have the The result of the above command should be a binary programme called $> ./myfilterapp myoriginalfile.mcpl myfilteredfile.mcplNote that compared to other solutions suggested in this thread, I would personally always recommend the above since
Of course, once we have the often requested Py-API for writing such files, things might become even easier. But in the mean-time, I hope the above will work for you :-) |
Beta Was this translation helpful? Give feedback.
Thanks for the question @AmChamb ! This is in fact pretty simple to do if you are able and willing to compile a small C programme, like @ebknudsen said. The C-code itself is basically the example at https://mctools.github.io/mcpl/usage_c/#extracting-subset-of-particles-from-file but with the filtering if-statement changed to reflect your desired requirement of having a nonzero particle weight.
So in a file called
myfilter.cput the following code: