Skip to content

Commit

Permalink
Lattice operator<<
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethassogba committed Nov 26, 2024
1 parent 2bbc1d8 commit 86cbab6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ The physics is coded in C++, with Python as user interface for easy and fast usa
- [x] Setup clang-format with Google style.
- [x] Setup clang-tidy.
- [x] Fix how to launch clang-tidy locally and on action.
- [ ] Move bindings elsewhere
- [ ] Choose how to handle asserts
- [ ] Add a Logger (spdlog, or nanolog or fmtlog) and std::format.
- [ ] Use doctest for unit tests in c++ and python.
- [ ] Doc style (eg doxygen) and the way to generate it automatically.
Expand Down
11 changes: 6 additions & 5 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ int main() {

for (const auto* c : {&UO2cell, &H2Ocell}) std::cout << *c << '\n';

Lattice assm00{{UO2cell, H2Ocell}, "assm00"};
// Lattice assm01{{uo2_cell, uo2_cell}};
// Lattice assm10{{uo2_cell, uo2_cell}};
// Lattice assm11{{uo2_cell, uo2_cell}};
Lattice assm00{{UO2cell, H2Ocell, UO2cell, H2Ocell}, "assm00"};
Lattice assm01{{UO2cell, UO2cell, H2Ocell, H2Ocell}, "assm01"};
Lattice assm10{{UO2cell, H2Ocell, UO2cell, UO2cell}, "assm10"};
Lattice assm11{{UO2cell, UO2cell, H2Ocell, UO2cell}, "assm11"};

// Lattice core{{assm00, assm01, assm10, assm11}};
Lattice core{{assm00, assm01, assm10, assm11}, "core"};
std::cout << core << '\n';

return 0;
}
25 changes: 16 additions & 9 deletions src/demeter/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@
#pragma once

#include <iostream>
#include <cassert>

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

// Use (void) to silence unused warnings.
#define assertm(exp, msg) assert(((void)msg, exp))
// usage: assertm(exp, "message");

namespace Demeter {

void CheckOpenMP();

// auto watch =
// [&](std::vector<std::variant<std::reference_wrapper<Material>,
// std::reference_wrapper<Cell>,
// std::reference_wrapper<Lattice>>>&& vec) {
// for (const auto& v : vec) {
// std::visit([](auto&& v) { std::cout << v.get() << std::endl; }, v);
// }
// };
/*auto watch =
[&](std::vector<std::variant<std::reference_wrapper<Material>,
std::reference_wrapper<Cell>,
std::reference_wrapper<Lattice>>>&& vec) {
for (const auto& v : vec) {
std::visit([](auto&& v) { std::cout << v.get() << std::endl; }, v);
}
};
*/

} // namespace Demeter
25 changes: 22 additions & 3 deletions src/demeter/model/lattice.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
#include "lattice.hpp"

#include <cmath>

namespace Demeter {
void Lattice::check() const {
// check if the number of component is a perfect square
auto n = components_.size();
auto root = static_cast<decltype(n)>(std::round(std::sqrt(n)));
if (root * root != n) {
throw std::runtime_error("Number of cell/lattice is not a perfect square");
}
}

std::string Lattice::print() const {
std::ostringstream s;
s << "Lattice:" << name_ << "\n";
s << "Lattice: " << name_;
// print names in a matrix view
// for (const auto& c : components_) {
// }
auto n = components_.size();
auto root = static_cast<size_t>(std::round(std::sqrt(n)));
for (size_t i = 0; i < root; ++i) {
s << '\n';
for (size_t j = 0; j < root; ++j) {
std::visit([&](auto&& v) { s << v.get().name() << " "; },
components_[i * root + j]);
}
}

return s.str();
}

Expand Down
10 changes: 7 additions & 3 deletions src/demeter/model/lattice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ class Lattice {

public:
Lattice(std::vector<FillType>&& components, std::string_view name = "")
: components_(std::move(components)), name_(name) {}
: components_(std::move(components)), name_(name) {
check();
}

auto& components() { return components_; }
const auto& components() const { return components_; }

friend std::ostream& operator<<(std::ostream& os, const Lattice& l) {
return os << l.print();
}
std::string print() const;

const auto& components() const { return components_; }
std::string_view name() const { return name_; }

private:
void check() const;

private:
std::vector<FillType> components_; // TODO use matrix

Expand Down

0 comments on commit 86cbab6

Please sign in to comment.