Skip to content

Commit b02ab2d

Browse files
committed
Align source sampling with parameterised source
Allows the source generator to be run by passing in the parameterised source. Also provides an example for how to read the output h5 file. Requires pugixml to align with OpenMC calls.
1 parent 43da2a6 commit b02ab2d

9 files changed

Lines changed: 81 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*.exe
3131
*.out
3232
*.app
33+
source_generator
3334

3435
# Build path
3536
build/
@@ -41,8 +42,6 @@ __pycache__/
4142
*.py[cod]
4243
.venv
4344

44-
source_generator
45-
4645
# Outputs
4746
*.xml
4847
*.h5

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "pybind11"]
22
path = pybind11
33
url = https://github.com/pybind/pybind11
4+
[submodule "pugixml"]
5+
path = pugixml
6+
url = https://github.com/zeux/pugixml.git

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ add_subdirectory(pybind11)
6666

6767
pybind11_add_module(plasma_source ${plasma_source_pybind_SOURCES})
6868

69+
add_subdirectory(pugixml)
70+
6971
# Build source_generator
7072
list(APPEND source_generator_SOURCES
7173
${SRC_DIR}/source_generator.cpp

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The package can be built and installed in editable mode by:
2929

3030
## Usage
3131

32-
The parametric plasma source can be sampled either directly in Python 3 or sampled in an OpenMC simulation.
32+
The parametric plasma source can be sampled either directly in Python 3, sampled in an OpenMC simulation, or sampled using OpenMC via a standalone executable without simulation.
3333

3434
For a better understanding of the varibles take a look at the [C. Fausser et al](https://www.sciencedirect.com/science/article/pii/S0920379612000853) paper.
3535

File renamed without changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
An example of how to read an initial_source.h5 file, as generated from source_generator
3+
4+
In order to create the initial source, first build the source_generator and then run
5+
the below, which will create an example source and sample it 1000 times.
6+
7+
../../build/source_generator -l ../../build/source_sampling.so -i "major_radius=9.06, \
8+
minor_radius=2.92258, elongation=1.557, triangularity=0.27, shafranov_shift=0.44789, \
9+
pedestal_radius=2.33806, ion_density_pedestal=1.09e+20, ion_density_separatrix=3e+19, \
10+
ion_density_origin=1.09e+20, ion_density_alpha=1, ion_temperature_pedestal=6.09, \
11+
ion_temperature_separatrix=0.1, ion_temperature_origin=45.9, \
12+
ion_temperature_alpha=8.06, ion_temperature_beta=6, plasma_type=plasma, plasma_id=1, \
13+
number_of_bins=100, minimum_toroidal_angle=0, maximum_toroidal_angle=360" \
14+
-v 10 -n 1000
15+
"""
16+
17+
import h5py
18+
import matplotlib.pyplot as plt
19+
import numpy as np
20+
21+
path_to_initial_source = "initial_source.h5"
22+
23+
with h5py.File(path_to_initial_source, "r") as f:
24+
r = np.sqrt(
25+
np.square(f["source_bank"][...]["r"]["x"])
26+
+ np.square(f["source_bank"][...]["r"]["y"])
27+
)
28+
z = f["source_bank"][...]["r"]["z"]
29+
30+
plt.scatter(r, z)
31+
32+
plt.ion()
33+
plt.show()
34+
plt.gca().set_aspect("equal")

parametric_plasma_source/source_generator.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <iostream>
22
#include <experimental/filesystem>
33

4+
#include "pugixml.hpp"
5+
46
#include "openmc/bank.h"
57
#include "openmc/constants.h"
68
#include "openmc/message_passing.h"
@@ -10,13 +12,14 @@
1012

1113
namespace source_generator
1214
{
13-
void print_settings()
15+
void print_settings(pugi::xml_node &root)
1416
{
1517
using namespace openmc;
1618

1719
std::cout << "Settings:" << std::endl;
1820
std::cout << " Number of particles: " << settings::n_particles << std::endl;
19-
std::cout << " Source library: " << settings::path_source_library << std::endl;
21+
std::cout << " Source library: " << root.child("source").attribute("library").value() << std::endl;
22+
std::cout << " Source parameters: " << root.child("source").attribute("parameters").value() << std::endl;
2023
std::cout << " Output path: " << settings::path_output << std::endl;
2124
std::cout << " Verbosity: " << settings::verbosity << std::endl;
2225
std::cout << std::endl;
@@ -29,6 +32,7 @@ namespace source_generator
2932
std::cout << std::endl;
3033
std::cout << "Options:" << std::endl;
3134
std::cout << " -l,--library Source library, mandatory" << std::endl;
35+
std::cout << " -i,--input Source definition, mandatory" << std::endl;
3236
std::cout << " -n,--particles Number of particles, default 1000" << std::endl;
3337
std::cout << " -o,--output Output directory, default {current directory}" << std::endl;
3438
std::cout << " -v,--verbosity Verbosity, default 5" << std::endl;
@@ -51,10 +55,12 @@ namespace source_generator
5155
settings::verbosity = 5;
5256
}
5357

54-
int parse_command_line(int argc, char* argv[])
58+
int parse_command_line(int argc, char* argv[], pugi::xml_node &root)
5559
{
5660
using namespace openmc;
5761

62+
root.append_child("source");
63+
5864
for (int i=1; i < argc; ++i)
5965
{
6066
std::string arg {argv[i]};
@@ -68,8 +74,14 @@ namespace source_generator
6874
else if (arg == "-l" || arg == "--library")
6975
{
7076
i += 1;
77+
root.child("source").append_attribute("library").set_value(argv[i]);
7178
settings::path_source_library = argv[i];
7279
}
80+
else if (arg == "-i" || arg == "--input")
81+
{
82+
i += 1;
83+
root.child("source").append_attribute("parameters").set_value(argv[i]);
84+
}
7385
else if (arg == "-o" || arg == "--output")
7486
{
7587
i += 1;
@@ -88,22 +100,40 @@ namespace source_generator
88100
}
89101
}
90102

91-
if (settings::path_source_library.empty())
103+
bool missing_arg = false;
104+
105+
if (!root.child("source").attribute("library"))
92106
{
93107
std::cout << "The --library or -l argument is mandatory and must be set." << std::endl;
108+
missing_arg = true;
109+
}
110+
111+
if (!root.child("source").attribute("parameters"))
112+
{
113+
std::cout << "The --input or -i argument is mandatory and must be set." << std::endl;
114+
missing_arg = true;
115+
}
116+
117+
if (missing_arg)
118+
{
94119
source_generator::print_help();
95120
return -1;
96121
}
97-
98-
return 0;
122+
else
123+
{
124+
return 0;
125+
}
99126
}
100127
}
101128

102129
int main(int argc, char* argv[])
103130
{
131+
pugi::xml_document doc;
132+
pugi::xml_node root = doc.append_child("settings");
133+
104134
source_generator::set_defaults();
105135

106-
int run = source_generator::parse_command_line(argc, argv);
136+
int run = source_generator::parse_command_line(argc, argv, root);
107137

108138
if (run < 0)
109139
{
@@ -112,10 +142,11 @@ int main(int argc, char* argv[])
112142

113143
if (openmc::settings::verbosity >= 5)
114144
{
115-
source_generator::print_settings();
145+
source_generator::print_settings(root);
116146
}
117147

118148
std::cout << "Sampling source:" << std::endl;
149+
openmc::SourceDistribution source = openmc::SourceDistribution(root.child("source"));
119150
openmc::calculate_work();
120151
openmc::allocate_banks();
121152
openmc::initialize_source();

pugixml

Submodule pugixml added at 22401ba

0 commit comments

Comments
 (0)