How to define a spherical source

Hi All,

I want to ask how to define a spherical source of gammas with random emissions.

Thanks.

For this you’d have to define a custom source as described here. Below is an example of a custom source with a spherical distributed 500 keV photon source over a radius of 10 cm where each photon is emitted isotropically:

#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 SphereSource : public openmc::Source
{
  openmc::Particle::Bank sample(uint64_t* seed) const
  {
    openmc::Particle::Bank particle;
    // particle type and weight
    particle.particle = openmc::Particle::Type::photon;
    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 radius = 10.0;
    double theta = std::acos(costheta);
    double r = radius * std::cbrt(u);

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

    // angle and energy
    particle.u = openmc::Isotropic().sample(seed);
    particle.E = 500.e3;
    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<SphereSource> openmc_create_source(std::string parameters)
{
  return std::make_unique<SphereSource>();
}

@paulromano hello!
In the custom source, the Isotropic source is: particle.u = openmc::Isotropic().sample(seed);
What do we do if we use cylindrical coordinates to write the particle emission in the z+ direction?