|
| 1 | +#include <catch2/catch_test_macros.hpp> |
| 2 | +#include <cstdio> |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include "openmc/bank.h" |
| 7 | +#include "openmc/mcpl_interface.h" |
| 8 | + |
| 9 | +// Test the MCPL stat:sum functionality (issue #3514) |
| 10 | +TEST_CASE("MCPL stat:sum field") |
| 11 | +{ |
| 12 | + // Check if MCPL interface is available |
| 13 | + if (!openmc::is_mcpl_interface_available()) { |
| 14 | + SKIP("MCPL library not available"); |
| 15 | + } |
| 16 | + |
| 17 | + SECTION("stat:sum field is written to MCPL files") |
| 18 | + { |
| 19 | + // Create a temporary filename |
| 20 | + std::string filename = "test_stat_sum.mcpl"; |
| 21 | + |
| 22 | + // Create some test particles |
| 23 | + std::vector<openmc::SourceSite> source_bank(100); |
| 24 | + std::vector<int64_t> bank_index = {0, 100}; // 100 particles total |
| 25 | + |
| 26 | + // Initialize test particles |
| 27 | + for (int i = 0; i < 100; ++i) { |
| 28 | + source_bank[i].particle = openmc::ParticleType::neutron; |
| 29 | + source_bank[i].r = {i * 0.1, i * 0.2, i * 0.3}; |
| 30 | + source_bank[i].u = {0.0, 0.0, 1.0}; |
| 31 | + source_bank[i].E = 2.0e6; // 2 MeV |
| 32 | + source_bank[i].time = 0.0; |
| 33 | + source_bank[i].wgt = 1.0; |
| 34 | + } |
| 35 | + |
| 36 | + // Write the MCPL file |
| 37 | + openmc::write_mcpl_source_point(filename.c_str(), source_bank, bank_index); |
| 38 | + |
| 39 | + // Verify the file was created |
| 40 | + FILE* f = std::fopen(filename.c_str(), "r"); |
| 41 | + REQUIRE(f != nullptr); |
| 42 | + std::fclose(f); |
| 43 | + |
| 44 | + // Read the file back to check stat:sum |
| 45 | + // Note: This would require mcpl_open_file and checking the header |
| 46 | + // Since we can't easily read MCPL headers in C++ without the full MCPL API, |
| 47 | + // we rely on the Python test to verify the actual content |
| 48 | + |
| 49 | + // Clean up |
| 50 | + std::remove(filename.c_str()); |
| 51 | + } |
| 52 | + |
| 53 | + SECTION("stat:sum uses correct particle count") |
| 54 | + { |
| 55 | + std::string filename = "test_count.mcpl"; |
| 56 | + |
| 57 | + // Test with different particle counts |
| 58 | + std::vector<int> test_counts = {1, 10, 100, 1000}; |
| 59 | + |
| 60 | + for (int count : test_counts) { |
| 61 | + std::vector<openmc::SourceSite> source_bank(count); |
| 62 | + std::vector<int64_t> bank_index = {0, count}; |
| 63 | + |
| 64 | + // Initialize particles |
| 65 | + for (int i = 0; i < count; ++i) { |
| 66 | + source_bank[i].particle = openmc::ParticleType::neutron; |
| 67 | + source_bank[i].r = {0.0, 0.0, 0.0}; |
| 68 | + source_bank[i].u = {0.0, 0.0, 1.0}; |
| 69 | + source_bank[i].E = 1.0e6; |
| 70 | + source_bank[i].time = 0.0; |
| 71 | + source_bank[i].wgt = 1.0; |
| 72 | + } |
| 73 | + |
| 74 | + // Write MCPL file |
| 75 | + openmc::write_mcpl_source_point( |
| 76 | + filename.c_str(), source_bank, bank_index); |
| 77 | + |
| 78 | + // The stat:sum should equal count (verified by Python test) |
| 79 | + // Here we just verify the file was created successfully |
| 80 | + FILE* f = std::fopen(filename.c_str(), "r"); |
| 81 | + REQUIRE(f != nullptr); |
| 82 | + std::fclose(f); |
| 83 | + |
| 84 | + // Clean up |
| 85 | + std::remove(filename.c_str()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + SECTION("stat:sum handles empty particle bank") |
| 90 | + { |
| 91 | + std::string filename = "test_empty.mcpl"; |
| 92 | + |
| 93 | + // Create empty particle bank |
| 94 | + std::vector<openmc::SourceSite> source_bank; |
| 95 | + std::vector<int64_t> bank_index = {0}; |
| 96 | + |
| 97 | + // This should still create a valid MCPL file with stat:sum = 0 |
| 98 | + openmc::write_mcpl_source_point(filename.c_str(), source_bank, bank_index); |
| 99 | + |
| 100 | + // Verify file was created |
| 101 | + FILE* f = std::fopen(filename.c_str(), "r"); |
| 102 | + REQUIRE(f != nullptr); |
| 103 | + std::fclose(f); |
| 104 | + |
| 105 | + // Clean up |
| 106 | + std::remove(filename.c_str()); |
| 107 | + } |
| 108 | +} |
0 commit comments