Commit 9faf98b
committed
Invalidate and lock FLow system based on solution existance (#518)
* fix: Link status parameters to flow_system
* ⏺ Summary
I've implemented the FlowSystem locking behavior as discussed. Here's what was done:
Core Concept
A FlowSystem is locked (read-only) when solution is not None.
New Features
1. is_locked property (flow_system.py:1127-1131)
- Returns True if the FlowSystem has a solution
2. reset() method (flow_system.py:1145-1168)
- Clears: solution, model, element submodels, variable/constraint names
- Returns self for method chaining
- Allows the FlowSystem to be modified and re-optimized
3. _invalidate_model() helper (flow_system.py:1133-1143)
- Called when adding elements/carriers to a FlowSystem with a model (but no solution)
- Clears model and element state
4. copy() method (flow_system.py:733-767)
- Creates a fresh FlowSystem copy without solution/model
- Supports copy.copy() and copy.deepcopy()
Behavior Changes
| Operation | Before Optimization | After build_model() | After optimize() |
|----------------|-------------------------------|------------------------------------|-------------------------------|
| add_elements() | ✓ Works | ⚠ Works, warns & invalidates model | ✗ RuntimeError |
| add_carriers() | ✓ Works | ⚠ Works, warns & invalidates model | ✗ RuntimeError |
| copy() | Returns copy without solution | Returns copy without solution | Returns copy without solution |
| reset() | No-op | Clears model | Clears solution & model |
Bug Fix
Fixed an issue where StatusParameters created during modeling (for prevent_simultaneous_flows and component-level status) weren't linked to the FlowSystem
(elements.py:957-964, components.py:731-732).
Tests
Added comprehensive tests in tests/test_flow_system_locking.py (28 tests) covering:
- is_locked property behavior
- add_elements() locking and invalidation
- add_carriers() locking and invalidation
- reset() method functionality
- copy() method functionality
- Loaded FlowSystem behavior
* Add invalidation tests
* Add link_to_flow_system method
* Add link_to_flow_system method
* New invalidate() method (flow_system.py:1232-1275)
A public method for manual model invalidation when modifying element attributes after connect_and_transform():
def invalidate(self) -> FlowSystem:
"""Invalidate the model to allow re-transformation after modifying elements."""
- Raises RuntimeError if FlowSystem has a solution (must call reset() first)
- Returns self for method chaining
- Useful when you need to modify after connect_and_transform() but before optimize()
2. Updated docstrings
connect_and_transform() - Added comprehensive docstring explaining:
- What steps it performs
- Warning that attributes become xarray DataArrays after transformation
- Note about idempotency and how to reset with invalidate()
_invalidate_model() - Clarified its role and relationship to public methods
3. New tests (test_flow_system_locking.py:285-409)
Added TestInvalidate class with 8 tests:
- test_invalidate_resets_connected_and_transformed
- test_invalidate_clears_model
- test_invalidate_raises_when_locked
- test_invalidate_returns_self
- test_invalidate_allows_retransformation
- test_modify_element_and_invalidate - full workflow with reset
- test_invalidate_needed_after_transform_before_optimize - pre-optimization modification
- test_reset_already_invalidates - confirms reset already handles invalidation
Key insight from testing
reset() already calls _invalidate_model(), so modifications after reset() automatically take effect on the next optimize(). The new invalidate() method is primarily for the case where:
1. You've called connect_and_transform() manually
2. Haven't optimized yet (no solution)
3. Need to modify element attributes
* Typo1 parent 1601cac commit 9faf98b
7 files changed
Lines changed: 793 additions & 152 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
| 183 | + | |
184 | 184 | | |
185 | | - | |
| 185 | + | |
186 | 186 | | |
187 | | - | |
| 187 | + | |
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
| |||
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
219 | | - | |
220 | | - | |
221 | | - | |
| 219 | + | |
| 220 | + | |
222 | 221 | | |
223 | 222 | | |
224 | 223 | | |
225 | 224 | | |
226 | | - | |
| 225 | + | |
227 | 226 | | |
228 | 227 | | |
229 | 228 | | |
| |||
427 | 426 | | |
428 | 427 | | |
429 | 428 | | |
430 | | - | |
| 429 | + | |
431 | 430 | | |
432 | | - | |
| 431 | + | |
433 | 432 | | |
434 | | - | |
| 433 | + | |
435 | 434 | | |
436 | | - | |
437 | | - | |
438 | | - | |
| 435 | + | |
| 436 | + | |
439 | 437 | | |
440 | | - | |
| 438 | + | |
441 | 439 | | |
442 | 440 | | |
443 | | - | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
444 | 447 | | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | 448 | | |
449 | 449 | | |
450 | | - | |
| 450 | + | |
451 | 451 | | |
452 | 452 | | |
453 | | - | |
| 453 | + | |
454 | 454 | | |
455 | 455 | | |
456 | | - | |
| 456 | + | |
457 | 457 | | |
458 | 458 | | |
459 | | - | |
| 459 | + | |
460 | 460 | | |
461 | 461 | | |
462 | 462 | | |
463 | 463 | | |
464 | | - | |
| 464 | + | |
465 | 465 | | |
466 | 466 | | |
467 | 467 | | |
468 | 468 | | |
469 | | - | |
| 469 | + | |
470 | 470 | | |
471 | 471 | | |
472 | | - | |
| 472 | + | |
473 | 473 | | |
474 | 474 | | |
475 | 475 | | |
| |||
714 | 714 | | |
715 | 715 | | |
716 | 716 | | |
717 | | - | |
718 | | - | |
719 | | - | |
720 | | - | |
721 | | - | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
722 | 721 | | |
723 | 722 | | |
724 | 723 | | |
| |||
729 | 728 | | |
730 | 729 | | |
731 | 730 | | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
732 | 734 | | |
733 | 735 | | |
734 | 736 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
244 | 250 | | |
245 | 251 | | |
246 | 252 | | |
247 | 253 | | |
248 | | - | |
| 254 | + | |
249 | 255 | | |
250 | 256 | | |
251 | 257 | | |
252 | 258 | | |
253 | 259 | | |
254 | | - | |
| 260 | + | |
255 | 261 | | |
256 | 262 | | |
257 | 263 | | |
258 | 264 | | |
259 | | - | |
| 265 | + | |
260 | 266 | | |
261 | 267 | | |
262 | | - | |
| 268 | + | |
263 | 269 | | |
264 | 270 | | |
265 | | - | |
| 271 | + | |
266 | 272 | | |
267 | 273 | | |
268 | | - | |
| 274 | + | |
269 | 275 | | |
270 | 276 | | |
271 | | - | |
| 277 | + | |
272 | 278 | | |
273 | 279 | | |
274 | | - | |
| 280 | + | |
275 | 281 | | |
276 | 282 | | |
277 | | - | |
| 283 | + | |
278 | 284 | | |
279 | 285 | | |
280 | | - | |
| 286 | + | |
281 | 287 | | |
282 | 288 | | |
283 | | - | |
| 289 | + | |
284 | 290 | | |
285 | 291 | | |
286 | 292 | | |
| |||
670 | 676 | | |
671 | 677 | | |
672 | 678 | | |
673 | | - | |
| 679 | + | |
674 | 680 | | |
675 | 681 | | |
676 | 682 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
| |||
111 | 110 | | |
112 | 111 | | |
113 | 112 | | |
114 | | - | |
115 | | - | |
116 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
117 | 119 | | |
118 | | - | |
| 120 | + | |
119 | 121 | | |
120 | | - | |
| 122 | + | |
121 | 123 | | |
122 | | - | |
123 | | - | |
| 124 | + | |
124 | 125 | | |
125 | | - | |
| 126 | + | |
126 | 127 | | |
127 | 128 | | |
128 | | - | |
| 129 | + | |
129 | 130 | | |
130 | 131 | | |
131 | 132 | | |
| |||
269 | 270 | | |
270 | 271 | | |
271 | 272 | | |
272 | | - | |
273 | | - | |
274 | | - | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
275 | 279 | | |
276 | | - | |
| 280 | + | |
277 | 281 | | |
278 | | - | |
279 | | - | |
| 282 | + | |
280 | 283 | | |
281 | | - | |
| 284 | + | |
282 | 285 | | |
283 | 286 | | |
284 | 287 | | |
| |||
505 | 508 | | |
506 | 509 | | |
507 | 510 | | |
508 | | - | |
509 | | - | |
510 | | - | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
511 | 517 | | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
516 | | - | |
517 | | - | |
518 | | - | |
519 | | - | |
520 | | - | |
521 | | - | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
522 | 529 | | |
523 | | - | |
| 530 | + | |
524 | 531 | | |
525 | 532 | | |
526 | | - | |
| 533 | + | |
527 | 534 | | |
528 | 535 | | |
529 | | - | |
| 536 | + | |
530 | 537 | | |
531 | 538 | | |
532 | | - | |
| 539 | + | |
533 | 540 | | |
534 | 541 | | |
535 | | - | |
| 542 | + | |
536 | 543 | | |
537 | 544 | | |
538 | | - | |
| 545 | + | |
539 | 546 | | |
540 | 547 | | |
541 | 548 | | |
542 | | - | |
| 549 | + | |
543 | 550 | | |
544 | | - | |
| 551 | + | |
545 | 552 | | |
546 | | - | |
| 553 | + | |
547 | 554 | | |
548 | 555 | | |
549 | 556 | | |
| |||
955 | 962 | | |
956 | 963 | | |
957 | 964 | | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
958 | 968 | | |
959 | 969 | | |
960 | 970 | | |
961 | 971 | | |
962 | 972 | | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
963 | 976 | | |
964 | 977 | | |
965 | 978 | | |
| |||
0 commit comments