fusion-energy / openmc_source_plotter

A Python package for extracting and plotting the locations, directions, energy distributions of OpenMC source particles
MIT License
10 stars 3 forks source link

FileNotFoundError when using plot_source_energy() #10

Closed RemDelaporteMathurin closed 2 years ago

RemDelaporteMathurin commented 2 years ago

MWE:

import openmc_source_plotter as osp
import openmc_plasma_source as ops

import numpy as np
import math

my_source = ops.FusionRingSource(
    fuel="DT",
    radius=1,
    z_placement=1,
    angles=(math.radians(-1), math.radians(1)),
)

plot = osp.plot_source_energy(
    source=my_source,
    number_of_particles=2000,
    energy_bins=np.linspace(0, 20e6, 100)
)

Produces:

(base) root@9cd1860b80ad:/home/fusion-neutronics-workflow# python plot_source.py 
                                %%%%%%%%%%%%%%%
                           %%%%%%%%%%%%%%%%%%%%%%%%
                        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                                    %%%%%%%%%%%%%%%%%%%%%%%%
                                     %%%%%%%%%%%%%%%%%%%%%%%%
                 ###############      %%%%%%%%%%%%%%%%%%%%%%%%
                ##################     %%%%%%%%%%%%%%%%%%%%%%%
                ###################     %%%%%%%%%%%%%%%%%%%%%%%
                ####################     %%%%%%%%%%%%%%%%%%%%%%
                #####################     %%%%%%%%%%%%%%%%%%%%%
                ######################     %%%%%%%%%%%%%%%%%%%%
                #######################     %%%%%%%%%%%%%%%%%%
                 #######################     %%%%%%%%%%%%%%%%%
                 ######################     %%%%%%%%%%%%%%%%%
                  ####################     %%%%%%%%%%%%%%%%%
                    #################     %%%%%%%%%%%%%%%%%
                     ###############     %%%%%%%%%%%%%%%%
                       ############     %%%%%%%%%%%%%%%
                          ########     %%%%%%%%%%%%%%
                                      %%%%%%%%%%%

                 | The OpenMC Monte Carlo Code
       Copyright | 2011-2022 MIT, UChicago Argonne LLC, and contributors
         License | https://docs.openmc.org/en/latest/license.html
         Version | 0.13.0
        Git SHA1 | cff247e35785e7236d67ccf64a3401f0fc50a469
       Date/Time | 2022-07-08 08:43:41
   MPI Processes | 1
  OpenMP Threads | 4

 Reading settings XML file...
 Reading cross sections XML file...
 Reading materials XML file...
 Reading geometry XML file...
 Minimum neutron data temperature: 1.7976931348623157e+308 K
 Maximum neutron data temperature: 0 K
 Preparing distributed cell instances...
 Reading plot XML file...
 Writing summary.h5 file...

 ===============>     FIXED SOURCE TRANSPORT SIMULATION     <===============

 Simulating batch 1
 Creating state point statepoint.1.h5...

 =======================>     TIMING STATISTICS     <=======================

 Total time for initialization     = 1.2467e-01 seconds
   Reading cross sections          = 7.5700e-05 seconds
 Total time in simulation          = 2.7996e-02 seconds
   Time in transport only          = 3.4304e-03 seconds
   Time in active batches          = 2.7996e-02 seconds
   Time accumulating tallies       = 1.3000e-06 seconds
   Time writing statepoints        = 2.4537e-02 seconds
 Total time for finalization       = 4.5000e-06 seconds
 Total time elapsed                = 1.5276e-01 seconds
 Calculation Rate (active)         = 71439 particles/second

 ============================>     RESULTS     <============================

 WARNING: Could not compute uncertainties -- only one active batch simulated!
 Leakage Fraction           = 1.00000

Traceback (most recent call last):
  File "/opt/conda/lib/python3.9/shutil.py", line 806, in move
    os.rename(src, real_dst)
OSError: [Errno 18] Invalid cross-device link: 'initial_source.h5' -> '/tmp/openmc_source_y85xaz55.h5'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/fusion-neutronics-workflow/plot_source.py", line 19, in <module>
    plot = osp.plot_source_energy(
  File "/opt/conda/lib/python3.9/site-packages/openmc_source_plotter/core.py", line 38, in plot_source_energy
    create_initial_particles(
  File "/opt/conda/lib/python3.9/site-packages/openmc_source_plotter/utils.py", line 100, in create_initial_particles
    shutil.move("initial_source.h5", output_source_filename)
  File "/opt/conda/lib/python3.9/shutil.py", line 826, in move
    copy_function(src, real_dst)
  File "/opt/conda/lib/python3.9/shutil.py", line 435, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/opt/conda/lib/python3.9/shutil.py", line 264, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'initial_source.h5'

openmc_source_plotter version: current openmc version 0.13.0

Should I use openmc 0.11 to have this work?

shimwell commented 2 years ago

Yep you currently need openmc 0.11for this to work, the readme has an example

RemDelaporteMathurin commented 2 years ago

Ok, is it because the current version doesn't write "initial_source.h5"? if so, why is the settings.write_initial_source attribute not deprecated?

shimwell commented 2 years ago

openmc 0.13.0 doesn't write the initial_source.h5 for fixed source simulations despite their being an attribute Related issue https://github.com/openmc-dev/openmc/issues/1537

The source sampling feature in the next release of openmc (that FLF funded) will help obtain the information required without the need to write a initial_source file

RemDelaporteMathurin commented 2 years ago

Ok I see. I shall close this now :-)

For those who are interested, my workaround was:

import matplotlib.pyplot as plt
import numpy as np

E = my_source.energy
n_samples = 20000
std_dev = np.sqrt(4.*E.e0*E.kt/E.m_rat)
energy = np.random.normal(E.e0, std_dev, n_samples)
energy *= 

plt.hist(energy, bins="auto", alpha=0.8)
plt.savefig("out.png")

For a source with a Muir energy spectrum