westpa
deepdrivewe.workflows.westpa ¶
WESTPA workflow agents for the Academy framework.
Provides abstract base agents for running weighted ensemble
(WESTPA) workflows. Users subclass SimulationAgent and
WestpaAgent to inject their own simulation and inference
logic, with full access to agent state (e.g., cached ML
models, loaded configurations).
Example
::
class MySimAgent(SimulationAgent):
def __init__(self, westpa_handle, model):
super().__init__(westpa_handle)
self.model = model
def run_simulation(self, metadata):
return simulate(metadata, self.model)
class MyWestpaAgent(WestpaAgent):
def __init__(self, *args, basis, **kwargs):
super().__init__(*args, **kwargs)
self.basis = basis
def run_inference(self, sim_results):
return resample(sim_results, self.basis)
await run_westpa_workflow(
manager=mgr,
sim_agent_type=MySimAgent,
westpa_agent_type=MyWestpaAgent,
max_iterations=100,
ensemble=ensemble,
sim_agent_kwargs={'model': my_model},
westpa_agent_kwargs={'basis': my_basis},
)
SimulationAgent ¶
Bases: Agent, ABC
Base agent for running simulations.
Subclass and override run_simulation to provide custom
simulation logic. Use agent_on_startup to initialize
expensive state (e.g., load an ML model).
The simulate action offloads run_simulation to a
thread pool via agent_run_sync since MD simulations
are typically blocking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
westpa_handle
|
Handle[WestpaAgent]
|
Handle to send results to the WESTPA agent. |
required |
Source code in deepdrivewe/workflows/westpa.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
agent_on_startup
async
¶
Initialize the agent.
Override to add custom startup logic (e.g., loading a
model). Always call await super().agent_on_startup()
first.
Source code in deepdrivewe/workflows/westpa.py
run_simulation
abstractmethod
¶
Run a simulation for the given metadata.
Override this method in a subclass to provide custom
simulation logic. Has access to self for any
state initialized in __init__ or
agent_on_startup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
SimMetadata
|
The simulation metadata (walker weight, restart file, parent progress coordinate, etc.). |
required |
Returns:
| Type | Description |
|---|---|
SimResult
|
The simulation result with data products and updated metadata. |
Source code in deepdrivewe/workflows/westpa.py
simulate
async
¶
Run the simulation and send result.
Source code in deepdrivewe/workflows/westpa.py
WestpaAgent ¶
Bases: Agent, ABC
Base agent for orchestrating the WESTPA iteration cycle.
Subclass and override run_inference to provide custom
resampling logic. The base class handles result collection,
ensemble state management, checkpointing, and dispatching.
Simulations are distributed to agents round-robin, so the number of walkers can differ from the number of agents (e.g., after resampling changes the walker count).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulation_handles
|
list[Handle[SimulationAgent]]
|
Handles to dispatch simulations to. |
required |
max_iterations
|
int
|
Stop after this many WE iterations. |
required |
ensemble
|
WeightedEnsemble
|
Ensemble state to maintain across iterations.
Provides |
required |
checkpointer
|
EnsembleCheckpointer
|
Checkpointer for saving ensemble state. |
None
|
Source code in deepdrivewe/workflows/westpa.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
agent_on_startup
async
¶
Initialize the agent.
Override to add custom startup logic. Always call
await super().agent_on_startup() first.
Source code in deepdrivewe/workflows/westpa.py
run_inference
abstractmethod
¶
run_inference(
sim_results: list[SimResult],
) -> tuple[
list[SimMetadata], list[SimMetadata], IterationMetadata
]
Run inference/resampling on the batch of results.
Override this method in a subclass to provide custom
resampling logic. Has access to self for any
state initialized in __init__ or
agent_on_startup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim_results
|
list[SimResult]
|
The simulation results for the current iteration. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[SimMetadata], list[SimMetadata], IterationMetadata]
|
A tuple of |
Source code in deepdrivewe/workflows/westpa.py
receive_simulation_data
async
¶
Buffer a simulation result.
Source code in deepdrivewe/workflows/westpa.py
run_westpa
async
¶
Run the WESTPA iteration loop.
Source code in deepdrivewe/workflows/westpa.py
dispatch_round_robin
async
¶
Dispatch simulations to agents round-robin.
Source code in deepdrivewe/workflows/westpa.py
run_westpa_workflow
async
¶
run_westpa_workflow(
manager: Manager,
sim_agent_type: type[SimulationAgent],
westpa_agent_type: type[WestpaAgent],
max_iterations: int,
ensemble: WeightedEnsemble,
checkpointer: EnsembleCheckpointer | None = None,
sim_agent_kwargs: dict[str, Any] | None = None,
westpa_agent_kwargs: dict[str, Any] | None = None,
sim_executor: str | None = None,
westpa_executor: str | None = None,
logfile: Path | None = None,
) -> None
Run a WESTPA workflow with user-defined agent types.
Registers and launches all agents, dispatches the first
iteration of simulations from ensemble.next_sims,
and waits for the workflow to complete. One
SimulationAgent is launched per initial simulation;
simulations are distributed round-robin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
Manager
|
The Academy manager (within |
required |
sim_agent_type
|
type[SimulationAgent]
|
Concrete |
required |
westpa_agent_type
|
type[WestpaAgent]
|
Concrete |
required |
max_iterations
|
int
|
Maximum number of WE iterations. |
required |
ensemble
|
WeightedEnsemble
|
Ensemble state to track across iterations.
|
required |
checkpointer
|
EnsembleCheckpointer
|
Save ensemble state each iteration. |
None
|
sim_agent_kwargs
|
dict
|
Extra keyword arguments for |
None
|
westpa_agent_kwargs
|
dict
|
Extra keyword arguments for |
None
|
sim_executor
|
str
|
Named executor for simulation agents (e.g., GPU). |
None
|
westpa_executor
|
str
|
Named executor for the WESTPA agent (e.g., CPU). |
None
|
logfile
|
Path
|
Log file path passed to each agent. Agents call
|
None
|
Source code in deepdrivewe/workflows/westpa.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | |