Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for keff search with openmc.lib
  • Loading branch information
paulromano committed Sep 15, 2025
commit 9dce3dfcdc550ff16685a9bfe97eb52433e2c1fe
26 changes: 18 additions & 8 deletions openmc/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,8 @@ def keff_search(
func_kwargs : dict, optional
Keyword-based arguments to pass to the `func` function.
run_kwargs : dict, optional
Keyword arguments to pass to :meth:`openmc.Model.run`.
Keyword arguments to pass to :meth:`openmc.Model.run` or
:meth:`openmc.lib.run`.

Returns
-------
Expand All @@ -2248,6 +2249,7 @@ def keff_search(
batches), plus convergence status and termination reason.

"""
import openmc.lib

check_type('model modifier', func, Callable)
check_type('target', target, Real)
Expand All @@ -2270,18 +2272,25 @@ def eval_at(x: float, batches: int) -> tuple[float, float]:
# Modify the model with the current guess
func(x, **func_kwargs)

# Change the number of batches
self.settings.batches = self.settings.inactive + batches
# Change the number of batches and run the model
batches = self.settings.inactive + batches
Comment thread
paulromano marked this conversation as resolved.
Resolved
if openmc.lib.is_initialized:
openmc.lib.settings.set_batches(batches)
openmc.lib.reset()
openmc.lib.run(**run_kwargs)
sp_filepath = f'statepoint.{batches}.h5'
else:
self.settings.batches = batches
sp_filepath = self.run(**run_kwargs)

# Run the model and obtain keff
sp_filepath = self.run(**run_kwargs)
# Extract keff and its uncertainty
with openmc.StatePoint(sp_filepath) as sp:
keff = sp.keff

if output:
nonlocal count
count += 1
print(f'Iteration {count}: {batches=}, {x=:.2e}, {keff=:.5f}')
print(f'Iteration {count}: {batches=}, {x=:.6g}, {keff=:.5f}')

xs.append(float(x))
fs.append(float(keff.n - target))
Expand All @@ -2297,7 +2306,8 @@ def eval_at(x: float, batches: int) -> tuple[float, float]:

# Perform the search (inlined GRsecant) in a temporary directory
with TemporaryDirectory() as tmpdir:
run_kwargs.setdefault('cwd', tmpdir)
if not openmc.lib.is_initialized:
run_kwargs.setdefault('cwd', tmpdir)

# ---- Seed with two evaluations
f0, s0 = eval_at(x0, b0)
Expand All @@ -2307,7 +2317,7 @@ def eval_at(x: float, batches: int) -> tuple[float, float]:
if abs(f1) <= k_tol and s1 <= sigma_final:
return SearchResult(x1, xs, fs, ss, gs, True, "converged")

for _ in range(maxiter):
for _ in range(maxiter - 2):
# ------ Step 1: propose next x via GRsecant
m = min(memory, len(xs))

Expand Down
5 changes: 0 additions & 5 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,11 +1210,6 @@ extern "C" int openmc_set_n_batches(
return OPENMC_E_INVALID_ARGUMENT;
}

if (simulation::current_batch >= n_batches) {
set_errmsg("Number of batches must be greater than current batch.");
return OPENMC_E_INVALID_ARGUMENT;
}

if (!settings::trigger_on) {
// Set n_batches and n_max_batches to same value
settings::n_batches = n_batches;
Expand Down