# Dear all, Question on openmc program using custom\_source example

**URL:** <https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948>\
**Category:** User Support\
**Created:** [January 17, 2021, 6:17am UTC](https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948 "2021-01-17T06:17:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Xia](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@William\_Xia](https://openmc.discourse.group/u/William_Xia)\
**Post date:** [January 17, 2021, 6:17am UTC](https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948/1 "2021-01-17T06:17:38Z")

</div>

Dear all：  
I tried to use the openmc program (version 0.12.1) under Ubuntu 18.04 system to run the example of external source——custom\_source.

I followed the steps in the manual and produced the libsource.so file.

But when we run the openmc program, the program reports an error, stating that the libsource.so file cannot be opened. The screenshot of the error is as follows.

How to solve this problem?

Best wishes,  
William Xia

![image](https://global.discourse-cdn.com/free1/uploads/openmc/original/2X/d/da0d89a156648bd5aa9ef2a0020b20bb44e110d2.png)

---

<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:** [January 25, 2021, 1:14pm UTC](https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948/2 "2021-01-25T13:14:32Z")

</div>

Hi @William_Xia and welcome to the forum! The setup for custom sources did change recently. If you look at the documentation for the [developmental branch](https://docs.openmc.org/en/latest/usersguide/settings.html#custom-sources), you’ll see that it now expects you to write a class that derives from `openmc::Source` and overrides the `source` method. In addition, you need to have a function called `openmc_create_source` that creates a unique pointer to an instance of this class. If you were using the previous convention (a `sample_source` function that returns a `openmc::Particle::Bank`) it shouldn’t be too difficult to modify it to what OpenMC expects now.

---

<div class="post-metadata">

**Author:** ![William\_Xia](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@William\_Xia](https://openmc.discourse.group/u/William_Xia)\
**Post date:** [January 25, 2021, 2:09pm UTC](https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948/3 "2021-01-25T14:09:46Z")

</div>

Dear paul romano,  
Thank you very much for your Selfless teaching.  
And I have another question on coupling MCNPX and OpenMC（version 0.12.0）.  
I would like to calculate an ADS problem by coupling MCNPX and OpenMC.  
But an example of the external source of OpenMC(version 0.12.0) is cpp file.  
Could the OpenMC of version 0.12.0 read the external source sampling wssa file generated by the MCNPX？  
Or is there any other way to realize the coupling MCNPX and OpenMC（version 0.12.0）?  
Best wishes,  
William Xia

---

<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:** [January 25, 2021, 3:33pm UTC](https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948/4 "2021-01-25T15:33:56Z")

</div>

Our next release (0.12.1) includes a surface source reading/writing ability that is similar to what appears in MCNP. I think a possible path forward would be to use the [SurfSrc](http://pyne.io/pyapi/mcnp.html#pyne.mcnp.SurfSrc) class from PyNE to read the MCNP wssa file and then use [openmc.write\_source\_file](https://docs.openmc.org/en/latest/pythonapi/generated/openmc.write_source_file.html#openmc.write_source_file) to create a source file that OpenMC could read.

---

<div class="post-metadata">

**Author:** ![William\_Xia](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@William\_Xia](https://openmc.discourse.group/u/William_Xia)\
**Post date:** [February 1, 2021, 4:18am UTC](https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948/5 "2021-02-01T04:18:05Z")

</div>

Dear paul romano,  
@paulromano  
Thank you very much for your Selfless teaching.  
I have read the wssa file using the PyNE SurfSrc class (provided by PyNE), but it cannot directly generate source files readable by openmc.  
For the reading of wssa file data, I have used ssw2txt.py in mc-tools ([GitHub - kbat/mc-tools: Some Monte Carlo tools](https://github.com/kbat/mc-tools)) to convert the wssa file into a text file—wssa.txt.  
Part of the results of wssa can be seen in the screenshot below.

 ![image](https://global.discourse-cdn.com/free1/uploads/openmc/original/2X/b/b22d7717f26e7e94444dc1b65afc7c83f1021c3e.jpeg)  
How to process wssa file into the file needed by openmc.write\_source\_file?  
Should the hexadecimal wssa file be processed directly or the text file wssa.txt?  
Is there a related python script?  
Best wishes,  
William Xia

---

<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 5, 2021, 9:05pm UTC](https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948/6 "2021-02-05T21:05:21Z")

</div>

You’ll need to read the data from that file, create a list of `SourceParticle` objects, and then use `write_source_file`. Altogether, it should look something like:

```auto
fh = open('wssa.txt', 'r')
next(fh) # skip first line
particles = []
for line in fh:
    values = line.split()
    weight = float(values[2])
    energy = float(values[3])
    x = float(values[4])
    y = float(values[5])
    z = float(values[6])
    ...
    p = openmc.SourceParticle(E=energy, wgt=weight, r=(x, y, z), ...)
    particles.append(p)

openmc.write_source_file(particles, 'source.h5')

```

---

<div class="post-metadata">

**Author:** ![William\_Xia](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@William\_Xia](https://openmc.discourse.group/u/William_Xia)\
**Post date:** [February 17, 2021, 8:21am UTC](https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948/7 "2021-02-17T08:21:16Z")

</div>

Dear @paulromano,  
I have generated the source.h5 file according to your suggestions (See the screenshot below for its structure).  
 ![image](https://global.discourse-cdn.com/free1/uploads/openmc/original/2X/9/9934b1881598debcaa7736b1671422d3ce3e8e60.jpeg)  
But the program still does not run successfully. (See the screenshot below for the error).  
 ![image](https://global.discourse-cdn.com/free1/uploads/openmc/original/2X/5/597bde975d35045039bedf811ec14f4580fce979.jpeg)  
Can you help me find out what the reason is?  
The relevant documents are attached, thank you very much.  
Best wishes,  
William Xia  
[MUSE4\_external\_source.py](https://openmc.discourse.group/uploads/short-url/fjHKPxrvhD9U11EcVTLxnQzcFT7.py) (6.5 KB)  
[source\_8.h5](https://drive.google.com/file/d/19-0RZvGVnj7bpwrw8NPHWmZNun37kq2a/view?usp=sharing)

---

<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:23pm UTC](https://openmc.discourse.group/t/dear-all-question-on-openmc-program-using-custom-source-example/948/8 "2021-02-24T13:23:30Z")

</div>

I suspect the error you’re seeing may be due to the same problem described here:

> [@How to use external source file in fixed source calculation?](https://openmc.discourse.group/t/how-to-use-external-source-file-in-fixed-source-calculation/996/4):
>
> The internal format used for source sites recently changed (there is now a surf\_id attribute that is used for surface source reading/writing). I’m not sure how you are creating the ADS source file, but if you are using openmc.write\_source\_file, you should make sure that you use that function from the same version of OpenMC that is used to eventually read the source file. Either way, running into a segfault is not ideal behavior. I’m working on adding a check on the source file format to ensure …

If you’re recently updated OpenMC, you may need to regenerate the source file using the `write_source_file` function.
