From f38d24810d11ff7e978a052c445bb475f1cb5c53 Mon Sep 17 00:00:00 2001 From: Katie Mummah Date: Tue, 9 Jul 2024 19:25:32 -0500 Subject: [PATCH] warning and limit for splitting resources into too many packages --- src/package.h | 8 ++++++ src/toolkit/matl_sell_policy.cc | 10 +++++++- tests/toolkit/matl_sell_policy_tests.cc | 33 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/package.h b/src/package.h index ae7555acca..48d5b4091c 100644 --- a/src/package.h +++ b/src/package.h @@ -55,6 +55,14 @@ class Package { // returns the unpackaged singleton object static Ptr& unpackaged(); + // When a resource is split into individual items, warn when more than + // one million items are trying to be created at once + static int SplitWarn() { return 1000000; } + + // Numeric limits for splitting resources is based on vector limits and + // memory constraints. Use unsigned int max / 10 to be safe + static int SplitLimit() { return std::numeric_limits::max() / 10; } + private: Package(std::string name, double fill_min = 0, diff --git a/src/toolkit/matl_sell_policy.cc b/src/toolkit/matl_sell_policy.cc index 7b8677bdbd..d790d0ba82 100644 --- a/src/toolkit/matl_sell_policy.cc +++ b/src/toolkit/matl_sell_policy.cc @@ -180,7 +180,7 @@ std::set::Ptr> MatlSellPolicy::GetMatlBids( Request* req; Material::Ptr m, offer; double qty; - int n_full_bids; + int n_full_bids = 0; double bid_qty; double remaining_qty; std::vector bids; @@ -199,6 +199,14 @@ std::set::Ptr> MatlSellPolicy::GetMatlBids( bid_qty = excl ? quantize_ : package_->GetFillMass(qty); if (bid_qty != 0) { n_full_bids = static_cast(std::floor(qty / bid_qty)); + if (n_full_bids > Package::SplitLimit()) { + throw ValueError("splitting resource into more than " + + std::to_string(Package::SplitLimit()) + + " bids is not allowed due to vector limits"); + } else if (n_full_bids > Package::SplitWarn()) { + LGH(WARN) << "splitting resource into " << n_full_bids << " bids for " + << commod << ", is this intended?"; + } bids.assign(n_full_bids, bid_qty); remaining_qty = fmod(qty, bid_qty); diff --git a/tests/toolkit/matl_sell_policy_tests.cc b/tests/toolkit/matl_sell_policy_tests.cc index 5e9808996e..10e8a69f96 100644 --- a/tests/toolkit/matl_sell_policy_tests.cc +++ b/tests/toolkit/matl_sell_policy_tests.cc @@ -276,5 +276,38 @@ TEST_F(MatlSellPolicyTests, TransportUnit) { EXPECT_EQ(0, buf.quantity()); } +TEST_F(MatlSellPolicyTests, PackageLimit) { + using cyclus::QueryResult; + + int dur = 2; + + cyclus::MockSim sim(dur); + cyclus::Agent* a = new TestFacility(sim.context()); + + sim.context()->AddPrototype(a->prototype(), a); + sim.agent = sim.context()->CreateAgent(a->prototype()); + sim.AddSink("commod").Finalize(); + TestFacility* fac = dynamic_cast(sim.agent); + + cyclus::toolkit::ResBuf buf; + + double qty = 1e299; + CompMap cm; + cm[922380000] = 1; + Composition::Ptr comp = Composition::CreateFromMass(cm); + mat = Material::Create(a, qty, comp, Package::unpackaged_name()); + + buf.Push(mat); + + sim.context()->AddPackage("foo", 1, 2, "first"); + Package::Ptr p = sim.context()->GetPackage("foo"); + + cyclus::toolkit::MatlSellPolicy sellpol; + sellpol.Init(fac, &buf, "buf", 1e299, false, 0, p->name()) + .Set("commod").Start(); + + ASSERT_THROW(sim.Run(), cyclus::ValueError); +} + } } \ No newline at end of file