# How to define a source in the shape of a spherical shell?

**URL:** <https://openmc.discourse.group/t/how-to-define-a-source-in-the-shape-of-a-spherical-shell/1014>\
**Category:** User Support\
**Created:** [February 15, 2021, 1:07pm UTC](https://openmc.discourse.group/t/how-to-define-a-source-in-the-shape-of-a-spherical-shell/1014 "2021-02-15T13:07:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SophiaVT](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SophiaVT](https://openmc.discourse.group/u/SophiaVT)\
**Post date:** [February 15, 2021, 1:07pm UTC](https://openmc.discourse.group/t/how-to-define-a-source-in-the-shape-of-a-spherical-shell/1014/1 "2021-02-15T13:07:37Z")

</div>

Hi all,

I am trying a few things out for my project and need to define a neutron source which has the shape of a spherical shell. I am also wondering what the difference is between openmc.stats.CartesianIndependent and openmc.stats.Point, if I can only specify cartesian point coordinates in both? Is it possible to use CartesianIndependent to put in a formula for a volume (like x^2 + y^2 + z^2 = r^2, separately defining radius and origin)? If I can make a sphere that way, I am hoping I could make a shell too. Or is there some way of defining surfaces and instead of filling them with a material, I can fill them with a source? Help would be greatly appreciated!

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Pranto](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/pranto/32/489_2.png) [@Pranto](https://openmc.discourse.group/u/Pranto)\
**Post date:** [February 19, 2021, 9:45am UTC](https://openmc.discourse.group/t/how-to-define-a-source-in-the-shape-of-a-spherical-shell/1014/2 "2021-02-19T09:45:56Z")

</div>

@SophiaVT You can start with this [custom source example](https://github.com/openmc-dev/openmc/tree/develop/examples/custom_source)

`openmc.stats.Point()` class is used for point source definition or delta function by giving Cartesian coordinates whereas `openmc.stats.CartesianIndependent()` is used for spatial distribution of x,y,z coordinates independently.

```python
x = openmc.stats.Uniform(-10., 10.)
y = openmc.stats.Uniform(-10., 10.)
z = openmc.stats.Uniform(0., 20.)
d = openmc.stats.CartesianIndependent(x, y, z)

```

---

<div class="post-metadata">

**Author:** ![SophiaVT](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SophiaVT](https://openmc.discourse.group/u/SophiaVT)\
**Post date:** [February 19, 2021, 2:44pm UTC](https://openmc.discourse.group/t/how-to-define-a-source-in-the-shape-of-a-spherical-shell/1014/3 "2021-02-19T14:44:23Z")

</div>

Thank you for the reply! How can I plot what the source looks like so that I can double-check?

---

<div class="post-metadata">

**Author:** ![Pranto](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/pranto/32/489_2.png) [@Pranto](https://openmc.discourse.group/u/Pranto)\
**Post date:** [February 20, 2021, 6:17am UTC](https://openmc.discourse.group/t/how-to-define-a-source-in-the-shape-of-a-spherical-shell/1014/4 "2021-02-20T06:17:01Z")

</div>

@SophiaVT You can plot your custom source sample distribution after running the simulation

```python
import h5py
f = h5py.File('initial_source.h5','r')
s_bank = f['source_bank']

x_values = []
y_values = []
z_values = []

for particle in s_bank:
    x_values.append(particle[0][0])
    y_values.append(particle[0][1])
    z_values.append(particle[0][2])

import matplotlib.pyplot as plt
plt.scatter(x_values, y_values)
plt.show()

```

 ![source](https://global.discourse-cdn.com/free1/uploads/openmc/original/2X/c/cc4a569913fedadd7fdc985a07813f5102531deb.png)  
Note that you have to make sure `write_initial_source=True` in your `settings.xml` file.

```xml
<write_initial_source>true</write_initial_source>

```

Hope this will help.

---

<div class="post-metadata">

**Author:** ![SophiaVT](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SophiaVT](https://openmc.discourse.group/u/SophiaVT)\
**Post date:** [February 22, 2021, 4:02pm UTC](https://openmc.discourse.group/t/how-to-define-a-source-in-the-shape-of-a-spherical-shell/1014/5 "2021-02-22T16:02:31Z")

</div>

@Pranto thank you. I have not used a custom source before, in the example you provided in the link I can only see how to specify the radius in the x,y,z direction. How do I specify the shell source to have a certain width? I basically want to simulate a D-T plasma neutron source but in a purely spherical shape with a thickness that I can change.

Thanks again

---

<div class="post-metadata">

**Author:** ![paulromano](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/paulromano/32/486_2.png) [@paulromano](https://openmc.discourse.group/u/paulromano)\
**Post date:** [February 24, 2021, 1:46pm UTC](https://openmc.discourse.group/t/how-to-define-a-source-in-the-shape-of-a-spherical-shell/1014/6 "2021-02-24T13:46:57Z")

</div>

@SophiaVT To have a shell source, you’ll also need to sample the radius in between a minimum and maximum radius. The appropriate PDF for the radius is uniform in r², as explained in arguments [here](https://stats.stackexchange.com/questions/481543/generating-random-points-uniformly-on-a-disk). Thus, your sampling scheme will look something like:

```auto
double r_min = ...;
double r_max = ...;
double angle = 2 * M_PI * openmc::prn(seed);
double radius_sq = r_min*r_min + openmc::prn(seed)*(r_max*r_max - r_min*r_min);
double radius = std::sqrt(radius_sq);
particle.r.x = radius * std::cos(angle);
particle.r.y = radius * std::sin(angle);
particle.r.z = 0.0;

```
