Compiling & Linking Custom Source Definition

Hi,

I am working on a project which requires a custom source. I have a few questions.

About custom sdef,
I am hoping the source will be sampled as isotropic thermal neutrons between r_min and r_max.
Here I attached the custom source definition (adjusted from the example),

#include <cmath> // for M_PI
#include <memory> // for unique_ptr

#include "openmc/distribution_multi.h"
#include "openmc/random_lcg.h"
#include "openmc/source.h"
#include "openmc/particle.h"

class HollowSphereSource : public openmc::Source
{
  openmc::Particle::Bank sample(uint64_t* seed) const
  {
    openmc::Particle::Bank particle;
    // particle type and weight
    particle.particle = openmc::Particle::Type::neutron;
    particle.wgt = 1.0;

    // position
    double phi = 2.0 * M_PI * openmc::prn(seed); // [0, 2pi)
    double costheta = 2*openmc::prn(seed) - 1; // [-1, 1)
    double u = openmc::prn(seed);

    double r_min = 0.98;
    double r_max = 30.0;
    double r_diff = r_max - r_min;
    double theta = std::acos(costheta);
    double r = r_diff * std::cbrt(u);

    particle.r.x = r_min + (r * std::sin(theta) * std::cos(phi));
    particle.r.y = r_min + (r * std::sin(theta) * std::sin(phi));
    particle.r.z = r_min + (r * std::cos(theta));

    // angle and energy
    particle.u = openmc::Isotropic().sample(seed);
    particle.E = 253.e-4;
    particle.delayed_group = 0;
    return particle;
  }
};

// A function to create a unique pointer to an instance of this class when generated
// via a plugin call using dlopen/dlsym.
// You must have external C linkage here otherwise dlopen will not find the file
extern "C" std::unique_ptr<HollowSphereSource> openmc_create_source(std::string parameters)
{
  return std::make_unique<HollowSphereSource>();
}

Do I make any mistake on the sdef?

About linking the custom sdef to the OpenMC library,
I am using WSL on Windows. I installed openmc with Anaconda3 prior to the linking step.

After the hollow_sphere.cpp created, I created a CMakeLists.txt file below,

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(openmc_sources CXX)
add_library(source SHARED hollow_sphere.cpp)
find_package(OpenMC REQUIRED HINTS /home/(username)/codes/anaconda3/bin/openmc)
target_link_libraries(source OpenMC::libopenmc)

Once created, I executed the cmake command below,

It seems that there are a few files missing.
I have a few questions,

  1. Did I make any mistakes here?
  2. Is there a way to link the custom sdef to the existing openmc (compiled in Anaconda)?
  3. Correct me if I am wrong, it seems that these files exist only on the OpenMC source code. Is there a way to circumvent this issue?

Thanks.

As you are using WSL, you have to set the full path of openmc executable file and it should look like this,

C:\Users\USERNAME\AppData\Local\Packages\{DIST}\LocalState\rootfs\home\(username)\codes/anaconda3/bin/openmc

one minor comment on your input file

class HollowSphereSource : public openmc::Source
{
openmc::Particle::Bank sample(uint64_t* seed) const
{
openmc::Particle::Bank particle;
// particle type and weight
particle.particle = openmc::Particle::Type::neutron;
particle.wgt = 1.0;

class HollowSphereSource : public openmc::Source
{
  openmc::SourceSite sample(uint64_t* seed) const
  {
    openmc::SourceSite particle;
    // particle type and weight
    particle.particle = openmc::ParticleType::neutron;
    particle.wgt = 1.0;

source class must inherit from openmc::Source

custom source docs

Hope this will fix your problem

Welcome to the forum, @arief.rahman. The problem here is that the find_package call is not able to find the installation of OpenMC. If it was installed inside a conda environment, I would suggest using the CONDA_PREFIX environment variable as a hint, i.e.

find_package(OpenMC REQUIRED HINTS $ENV{CONDA_PREFIX})

Thanks a lot @Pranto or the feedback on the cpp file!

About linking the custom sdef,
I tried to modify the path as you suggested here,

with a minor modification (forward slash).
however, it still printed the same error as above (cmake files).

Furthermore,

I tried to use the CONDA_PREFIX as @paulromano suggested, but still got the same error.

After a quick search, I found that the cmake files are located within this dir,

C:\Users\{USERNAME}\AppData\Local\Packages\{DIST}\LocalState\rootfs\home\{USERNAME}\codes\anaconda3\lib64\cmake\OpenMC

I changed the path to the dir, but it is still failed with the same error. Any suggestions?

edit:
I installed OpenMC on the base(root) anaconda environment if that helps.

@arief.rahman I was able to reproduce the behavior you are seeing. I think a solution here is to explicitly specify the directory that contains the OpenMCConfig.cmake file:

find_package(OpenMC REQUIRED HINTS $ENV{CONDA_PREFIX}/lib64/cmake/OpenMC)

The problem seems to be that on some Linux distributions it doesn’t search for files in the lib64 directory (only lib). An alternative solution here would be to instruct it to specifically search lib64 directories:

set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS TRUE)
find_package(OpenMC REQUIRED HINTS $ENV{CONDA_PREFIX})

Either way should hopefully work. In the meantime, I’m going to submit a change to our conda distribution to install the cmake files in lib instead of lib64.

I couldn’t reproduce the same error either explicitly set full path or CONDA_PREFIX environment variable. Strange!

BTW, @arief.rahman a new update is available, use conda update openmc -y command to update.

Thanks for the corrections on the help @paulromano @Pranto. It works fine now by setting the dir to the following path,

I have a follow up question,
If I have 2nd custom sdef (in different dir to the current custom) and link that to the openmc, would that break the link between openmc and the 1st custom sdef?

If you have a second custom source linked to OpenMC, that will not interfere with the first one. You can create as many custom source definitions as you like.