Skip to content

Commit

Permalink
Cell Class
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethassogba committed Nov 25, 2024
1 parent 94f52f2 commit a4b6065
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 48 deletions.
18 changes: 10 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,7 @@ set_target_properties(demeter PROPERTIES POSITION_INDEPENDENT_CODE ON)
# Add demeter to LINK_LIBS
set(LINK_LIBS ${LINK_LIBS} demeter)

# nanobind extension
# Compile extension module with size optimization and add demeter library
nanobind_add_module(_demeter # Name of the extension
STABLE_ABI # Target the stable ABI for Python 3.12+
NB_DOMAIN demeter
# Extension source code
src/demeter/model/material.bind.cpp)
target_link_libraries(_demeter PRIVATE demeter)


# Executables
add_executable(main examples/main.cpp) # create an executable using the specified file
Expand All @@ -88,6 +81,15 @@ target_compile_options(main PRIVATE -Wall -Wextra -Wconversion -pedantic)

# Install directive for scikit-build-core
if (SKBUILD)
# nanobind extension
# Compile extension module with size optimization and add demeter library
nanobind_add_module(_demeter # Name of the extension
STABLE_ABI # Target the stable ABI for Python 3.12+
NB_DOMAIN demeter
# Extension source code
src/demeter/model/material.bind.cpp)
target_link_libraries(_demeter PRIVATE demeter)

install(TARGETS _demeter LIBRARY DESTINATION ${SKBUILD_PROJECT_NAME})
# install(TARGETS _demeter LIBRARY DESTINATION demeterpy)
endif()
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ The physics is coded in C++, with Python as user interface for easy and fast usa
- [x] Setup nanobind and write Python bindings for Material.
- [x] Write a python test for Material class.
- [ ] Code and test Geometry class.
- [ ] Cell
- [ ] PinCell
- [x] Cell
- [ ] Lattice.
- [ ] Test Geometry in python.
- [ ] Code Solver class.
Expand Down
27 changes: 3 additions & 24 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,10 @@
#include <Eigen/Dense>
#include "demeter/model/material.hpp"

void CheckOpenMP() {
#if defined(_OPENMP)
unsigned num_threads = 0;
#pragma omp parallel reduction(+ : num_threads)
num_threads += 1;
std::cerr << "OpenMP version " << _OPENMP << " number of threads "
<< num_threads << '\n';
#endif
}

int main() {
CheckOpenMP();

using Eigen::MatrixXd;
using namespace Demeter;

MatrixXd m(2, 2);
m(0, 0) = 3;
m(1, 0) = 2.5;
m(0, 1) = -1;
m(1, 1) = m(1, 0) + m(0, 1);
std::cout << m << std::endl;

using Eigen::ArrayXd;
using Eigen::ArrayXXd;
CheckOpenMP();

ArrayXd sigma_t{{0.222222, 0.833333}};
ArrayXd D = 1. / (3. * sigma_t);
Expand All @@ -37,8 +17,7 @@ int main() {
ArrayXd chi{{1., 0.}};
ArrayXXd sigma_s{{0.00, 0.02}, {0.00, 0.00}};

Demeter::Material uo2(sigma_t, sigma_s, sigma_a, sigma_f, nu_sigma_f, chi,
"UO2");
Material uo2(sigma_t, sigma_s, sigma_a, sigma_f, nu_sigma_f, chi, "UO2");

std::cout << uo2 << std::endl;

Expand Down
8 changes: 8 additions & 0 deletions src/demeter/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @brief Contains some common definition and typedefs
*/

#pragma once

#include "common/define.hpp"
#include "common/utils.hpp"
1 change: 1 addition & 0 deletions src/demeter/common/define.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "define.hpp"
10 changes: 10 additions & 0 deletions src/demeter/common/define.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @brief Contains some common definition and typedefs
*/
#pragma once

#include <Eigen/Core>


using Eigen::ArrayXd;
using Eigen::ArrayXXd;
15 changes: 15 additions & 0 deletions src/demeter/common/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "utils.hpp"

namespace Demeter {

void CheckOpenMP() {
#if defined(_OPENMP)
unsigned num_threads = 0;
#pragma omp parallel reduction(+ : num_threads)
num_threads += 1;
std::cerr << "OpenMP version " << _OPENMP << " number of threads "
<< num_threads << '\n';
#endif
}

} // namespace Demeter
16 changes: 16 additions & 0 deletions src/demeter/common/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @brief Contains some useful macros and typedefs
*/

#pragma once

#include <iostream>

/// @brief Print the name and value of a variable
#define PRINTER(x) #x << " = " << x

namespace Demeter {

void CheckOpenMP();

} // namespace Demeter
5 changes: 5 additions & 0 deletions src/demeter/model/cell.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "cell.hpp"

namespace Demeter {
void Cell::check() const { assert(materials_.size() == radii_.size() + 1); }
} // namespace Demeter
43 changes: 43 additions & 0 deletions src/demeter/model/cell.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <vector>
#include <string>
#include <string_view>
#include <memory>
#include <Eigen/Core>

#include "demeter/common.hpp"
#include "material.hpp"

namespace Demeter {

class Cell {
public:
Cell(double side, std::vector<std::shared_ptr<Material>>& materials,
std::string_view name = "")
: side(side), materials_(materials), name_(name) {}

Cell(double side, std::vector<double>& radii,
std::vector<std::shared_ptr<Material>>& materials,
std::string_view name = "")
: side(side), radii_(radii), materials_(materials), name_(name) {
check();
}

private:
void check() const;

private:
double side; // we assume that every cell is a square

std::vector<double> radii_;
// size_t num_rings_;

// size_t num_sectors_; //TODO

std::vector<std::shared_ptr<Material>> materials_;

/* A name for the Cell */
std::string name_;
};
} // namespace Demeter
23 changes: 12 additions & 11 deletions src/demeter/model/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,28 @@ Material::Material(Material&& other)

std::string Material::print() const {
std::ostringstream ss;
ss << PRINTER(name_) << "; " << PRINTER(num_groups_) << "; "
<< "fissile_" << (fissile_ ? "True" : "False") << '\n';
ss << "Material " << name_ + " has " << num_groups_
<< " energy groups and is " << (fissile_ ? "fissile" : "not fissile");
return ss.str();
}

// TODO change assert since we also want to check in release
// TODO some xs maybe zero
void Material::check() const { // TODO use fmt and a logger
if (not(num_groups_ > 0)) {
std::string msg = "Material " + name_ + " has " +
std::to_string(num_groups_) + " energy groups";
throw std::runtime_error(msg);
throw std::runtime_error(print());
}

auto cast = [&](long int x) { return static_cast<decltype(num_groups_)>(x); };
for (auto size :
{sigma_t_.size(), sigma_s_.rows(), sigma_s_.cols(), sigma_a_.size(),
sigma_f_.size(), nu_sigma_f_.size(), chi_.size()}) {
if (cast(size) != num_groups_) {

auto dims = {sigma_t_.size(), sigma_s_.rows(), sigma_s_.cols(),
sigma_a_.size(), sigma_f_.size(), nu_sigma_f_.size(),
chi_.size()};

for (auto d : dims) {
if (cast(d) != num_groups_) {
std::string msg =
"Material " + name_ + " has " + std::to_string(num_groups_) +
" energy groups but " +
print() + ", but cross-sections have different sizes " +
"sigma_t.size()=" + std::to_string(sigma_t_.size()) +
", sigma_s.rows()=" + std::to_string(sigma_s_.rows()) +
", sigma_s.cols()=" + std::to_string(sigma_s_.cols()) +
Expand Down
4 changes: 1 addition & 3 deletions src/demeter/model/material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
#include <string_view>
#include <Eigen/Core>

#define PRINTER(x) #x << " = " << x
#include "demeter/common.hpp"

// TODO add optional xs and name
// TODO some material maybe not need all xs, make them optional
namespace Demeter {
using Eigen::ArrayXd;
using Eigen::ArrayXXd;

/**
* @brief Describes the material properties. The cross-sections (xs) are:
Expand Down

0 comments on commit a4b6065

Please sign in to comment.