chemunited-sim 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. chemunited_sim/__init__.py +4 -0
  2. chemunited_sim/adapter/__init__.py +17 -0
  3. chemunited_sim/adapter/compilers.py +545 -0
  4. chemunited_sim/adapter/graph.py +293 -0
  5. chemunited_sim/adapter/models.py +141 -0
  6. chemunited_sim/cli/__init__.py +1 -0
  7. chemunited_sim/cli/builder.py +204 -0
  8. chemunited_sim/cli/clock.py +25 -0
  9. chemunited_sim/cli/loader.py +214 -0
  10. chemunited_sim/cli/main.py +124 -0
  11. chemunited_sim/cli/server.py +712 -0
  12. chemunited_sim/cli/sim_client.py +254 -0
  13. chemunited_sim/cli/tray.py +157 -0
  14. chemunited_sim/common/__init__.py +1 -0
  15. chemunited_sim/common/constant.py +69 -0
  16. chemunited_sim/common/edges.py +18 -0
  17. chemunited_sim/hydraulics/__init__.py +4 -0
  18. chemunited_sim/hydraulics/models.py +40 -0
  19. chemunited_sim/hydraulics/solver.py +242 -0
  20. chemunited_sim/inventory/__init__.py +19 -0
  21. chemunited_sim/inventory/engine.py +576 -0
  22. chemunited_sim/inventory/initialiser.py +81 -0
  23. chemunited_sim/inventory/models.py +59 -0
  24. chemunited_sim/inventory/port_map.py +167 -0
  25. chemunited_sim/inventory/source_map.py +88 -0
  26. chemunited_sim/mcp/__init__.py +30 -0
  27. chemunited_sim/mcp/tools.py +230 -0
  28. chemunited_sim/reactions/__init__.py +20 -0
  29. chemunited_sim/reactions/engine.py +90 -0
  30. chemunited_sim/reactions/models.py +229 -0
  31. chemunited_sim/recorder/__init__.py +12 -0
  32. chemunited_sim/recorder/cells.py +75 -0
  33. chemunited_sim/recorder/models.py +32 -0
  34. chemunited_sim/recorder/schema.py +198 -0
  35. chemunited_sim/recorder/writer.py +386 -0
  36. chemunited_sim/transport/__init__.py +12 -0
  37. chemunited_sim/transport/engine.py +698 -0
  38. chemunited_sim/transport/initialiser.py +62 -0
  39. chemunited_sim/transport/junction.py +71 -0
  40. chemunited_sim/transport/models.py +93 -0
  41. chemunited_sim/visualization/__init__.py +33 -0
  42. chemunited_sim/visualization/dashboard.py +2414 -0
  43. chemunited_sim/visualization/pyvis_export.py +471 -0
  44. chemunited_sim/visualization/query.py +190 -0
  45. chemunited_sim/worker/__init__.py +6 -0
  46. chemunited_sim/worker/config.py +30 -0
  47. chemunited_sim/worker/runner.py +781 -0
  48. chemunited_sim-0.0.2.dist-info/METADATA +45 -0
  49. chemunited_sim-0.0.2.dist-info/RECORD +52 -0
  50. chemunited_sim-0.0.2.dist-info/WHEEL +4 -0
  51. chemunited_sim-0.0.2.dist-info/entry_points.txt +2 -0
  52. chemunited_sim-0.0.2.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,4 @@
1
+ """chemunited-sim - Dynamic simulation engine for fluidic automation platforms.
2
+
3
+ No Qt. No GUI. No circular imports.
4
+ """
@@ -0,0 +1,17 @@
1
+ """Adapter module - compiles ComponentData + EdgeData into a HydraulicGraph.
2
+
3
+ Public interface:
4
+ compile_graph(components, edges) -> HydraulicGraph
5
+ resync_component(graph, comp) -> None
6
+ """
7
+
8
+ from .graph import compile_graph, resync_component
9
+ from .models import HydraulicEdge, HydraulicGraph, HydraulicNode
10
+
11
+ __all__ = [
12
+ "compile_graph",
13
+ "resync_component",
14
+ "HydraulicNode",
15
+ "HydraulicEdge",
16
+ "HydraulicGraph",
17
+ ]
@@ -0,0 +1,545 @@
1
+ """Per-component-type compilers that translate core ComponentData objects into
2
+ hydraulic graph primitives (HydraulicNode / HydraulicEdge).
3
+
4
+ Each compiler function accepts one typed component and returns a
5
+ ``(nodes, edges)`` tuple. Inventory nodes are NOT returned here; they are
6
+ handled by :func:`~chemunited_sim.adapter.graph.compile_graph` after dispatch.
7
+
8
+ Filtering rules applied in every compiler
9
+ ------------------------------------------
10
+ - Ports where ``port.category != ConnectionType.HYDRAULIC`` are skipped.
11
+ - Ports where ``port.closure == PortClosure.CAPPED`` are skipped.
12
+ - Any internal edge whose endpoint port was skipped is also skipped.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from typing import Callable
18
+
19
+ from chemunited_core.common.enums import ConnectionType
20
+ from chemunited_core.components import (
21
+ ComponentData,
22
+ FlowSourceData,
23
+ JunctionData,
24
+ PlugFlowComponentData,
25
+ PressureControlData,
26
+ ValveComponentData,
27
+ VesselComponentData,
28
+ )
29
+ from chemunited_core.components.enums import InternalEdgeRole, PortClosure
30
+ from chemunited_core.figure_registry import Gantry3DData, VialData
31
+ from loguru import logger
32
+
33
+ from .models import HydraulicEdge, HydraulicNode
34
+
35
+ # ---------------------------------------------------------------------------
36
+ # Shared helper
37
+ # ---------------------------------------------------------------------------
38
+
39
+
40
+ def _is_hydraulic_active(port, comp=None) -> bool: # type: ignore[no-untyped-def]
41
+ """Return ``True`` if *port* should become a hydraulic node.
42
+
43
+ Standard hydraulic ports are included. Gantry/vial MOVEMENT ports are
44
+ also included so autosampler tray contacts can participate in hydraulics
45
+ without making every MOVEMENT edge a fluid path.
46
+ """
47
+ if port.closure == PortClosure.CAPPED:
48
+ return False
49
+ if port.category == ConnectionType.HYDRAULIC:
50
+ return True
51
+ return port.category == ConnectionType.MOVEMENT and isinstance(
52
+ comp, (Gantry3DData, VialData)
53
+ )
54
+
55
+
56
+ def _is_hub_port(port, port_number: int | str, comp) -> bool: # type: ignore[no-untyped-def]
57
+ """Return True when a port should stage and route transport pockets."""
58
+ return bool(getattr(port, "is_hub", False)) or (
59
+ isinstance(comp, ValveComponentData) and port_number == 0
60
+ )
61
+
62
+
63
+ # ---------------------------------------------------------------------------
64
+ # PlugFlowComponentData
65
+ # ---------------------------------------------------------------------------
66
+
67
+
68
+ def compile_plugflow(
69
+ comp: PlugFlowComponentData,
70
+ ) -> tuple[list[HydraulicNode], list[HydraulicEdge]]:
71
+ """Compile a plug-flow tube segment into one transport edge.
72
+
73
+ A plug-flow component has exactly two hydraulic ports (1 and 2) connected
74
+ by a single internal transport edge whose geometry determines resistance
75
+ via Hagen–Poiseuille.
76
+
77
+ Parameters
78
+ ----------
79
+ comp:
80
+ The source plug-flow component.
81
+
82
+ Returns
83
+ -------
84
+ tuple[list[HydraulicNode], list[HydraulicEdge]]
85
+ Nodes for any non-filtered ports and at most one transport edge.
86
+ """
87
+ nodes: list[HydraulicNode] = []
88
+ valid_port_numbers: set[int | str] = set()
89
+
90
+ for port_number, port in comp.ports_by_number.items():
91
+ if not _is_hydraulic_active(port, comp):
92
+ continue
93
+ nodes.append(
94
+ HydraulicNode(
95
+ node_id=f"{comp.name}.{port_number}",
96
+ boundary=port.boundary,
97
+ is_hub=False,
98
+ component=comp.name,
99
+ )
100
+ )
101
+ valid_port_numbers.add(port_number)
102
+
103
+ edges: list[HydraulicEdge] = []
104
+ if 1 in valid_port_numbers and 2 in valid_port_numbers:
105
+ edge = comp.internal_edges[(1, 2)]
106
+ edges.append(
107
+ HydraulicEdge(
108
+ edge_id=f"{comp.name}.1.2",
109
+ origin_node_id=f"{comp.name}.1",
110
+ destination_node_id=f"{comp.name}.2",
111
+ length=edge.length,
112
+ diameter=edge.diameter,
113
+ role=InternalEdgeRole.TRANSPORT,
114
+ resistance_override=edge.resistance_override,
115
+ component=comp.name,
116
+ is_external=False,
117
+ )
118
+ )
119
+
120
+ return nodes, edges
121
+
122
+
123
+ # ---------------------------------------------------------------------------
124
+ # VesselComponentData
125
+ # ---------------------------------------------------------------------------
126
+
127
+
128
+ def compile_vessel(
129
+ comp: VesselComponentData,
130
+ ) -> tuple[list[HydraulicNode], list[HydraulicEdge]]:
131
+ """Compile a vessel (tank / reservoir) into port nodes plus an inventory hub.
132
+
133
+ The vessel's internal inventory node acts as the common pressure point for
134
+ all hydraulic ports. Each port is connected to the inventory node by a
135
+ short junction edge. The heat port (``ConnectionType.HEAT``) is skipped
136
+ by the category filter automatically.
137
+
138
+ Parameters
139
+ ----------
140
+ comp:
141
+ The source vessel component.
142
+
143
+ Returns
144
+ -------
145
+ tuple[list[HydraulicNode], list[HydraulicEdge]]
146
+ Port nodes, the inventory node, and junction edges from each port to
147
+ the inventory.
148
+ """
149
+ nodes: list[HydraulicNode] = []
150
+ valid_port_numbers: set[int | str] = set()
151
+
152
+ for port_number, port in comp.ports_by_number.items():
153
+ if not _is_hydraulic_active(port, comp):
154
+ continue
155
+ nodes.append(
156
+ HydraulicNode(
157
+ node_id=f"{comp.name}.{port_number}",
158
+ boundary=port.boundary,
159
+ is_hub=False,
160
+ component=comp.name,
161
+ )
162
+ )
163
+ valid_port_numbers.add(port_number)
164
+
165
+ for inventory_key in comp.internal_inventories:
166
+ nodes.append(
167
+ HydraulicNode(
168
+ node_id=f"{comp.name}.{inventory_key}",
169
+ boundary=None,
170
+ is_hub=False,
171
+ component=comp.name,
172
+ )
173
+ )
174
+
175
+ edges: list[HydraulicEdge] = []
176
+ for (port_number, dest), edge in comp.internal_edges.items():
177
+ if dest not in comp.internal_inventories:
178
+ continue
179
+ if port_number not in valid_port_numbers:
180
+ continue
181
+ edges.append(
182
+ HydraulicEdge(
183
+ edge_id=f"{comp.name}.{port_number}.{dest}",
184
+ origin_node_id=f"{comp.name}.{port_number}",
185
+ destination_node_id=f"{comp.name}.{dest}",
186
+ length=edge.length,
187
+ diameter=edge.diameter,
188
+ role=InternalEdgeRole.JUNCTION,
189
+ resistance_override=edge.resistance_override,
190
+ component=comp.name,
191
+ is_external=False,
192
+ )
193
+ )
194
+
195
+ return nodes, edges
196
+
197
+
198
+ # ---------------------------------------------------------------------------
199
+ # ValveComponentData
200
+ # ---------------------------------------------------------------------------
201
+
202
+
203
+ def compile_valve(
204
+ comp: ValveComponentData,
205
+ ) -> tuple[list[HydraulicNode], list[HydraulicEdge]]:
206
+ """Compile a valve into junction edges between its hydraulic ports.
207
+
208
+ Valve channels are modelled as lossless junctions whose resistance is
209
+ controlled entirely by ``resistance_override``: ``None`` means open
210
+ (R = 0 in the JUNCTION role), ``R_MAX_HYDRAULIC`` means closed.
211
+
212
+ Parameters
213
+ ----------
214
+ comp:
215
+ The source valve component.
216
+
217
+ Returns
218
+ -------
219
+ tuple[list[HydraulicNode], list[HydraulicEdge]]
220
+ Nodes for active ports and junction edges for open channels.
221
+ """
222
+ nodes: list[HydraulicNode] = []
223
+ valid_port_numbers: set[int | str] = set()
224
+
225
+ for port_number, port in comp.ports_by_number.items():
226
+ if not _is_hydraulic_active(port, comp):
227
+ continue
228
+ nodes.append(
229
+ HydraulicNode(
230
+ node_id=f"{comp.name}.{port_number}",
231
+ boundary=port.boundary,
232
+ is_hub=_is_hub_port(port, port_number, comp),
233
+ component=comp.name,
234
+ )
235
+ )
236
+ valid_port_numbers.add(port_number)
237
+
238
+ edges: list[HydraulicEdge] = []
239
+ for (origin, dest), edge in comp.internal_edges.items():
240
+ if origin not in valid_port_numbers or dest not in valid_port_numbers:
241
+ continue
242
+ edges.append(
243
+ HydraulicEdge(
244
+ edge_id=f"{comp.name}.{origin}.{dest}",
245
+ origin_node_id=f"{comp.name}.{origin}",
246
+ destination_node_id=f"{comp.name}.{dest}",
247
+ length=edge.length,
248
+ diameter=edge.diameter,
249
+ role=InternalEdgeRole.JUNCTION,
250
+ resistance_override=edge.resistance_override,
251
+ component=comp.name,
252
+ is_external=False,
253
+ )
254
+ )
255
+
256
+ return nodes, edges
257
+
258
+
259
+ # ---------------------------------------------------------------------------
260
+ # FlowSourceData
261
+ # ---------------------------------------------------------------------------
262
+
263
+
264
+ def compile_flow_source(
265
+ comp: FlowSourceData,
266
+ ) -> tuple[list[HydraulicNode], list[HydraulicEdge]]:
267
+ """Compile a flow source into a passive port node + Inventory node + nozzle edge.
268
+
269
+ Port 1 is passive (boundary=None) — it is solved by the network.
270
+ The Inventory node carries the FLOW BC that drives fluid through the nozzle.
271
+ The nozzle (InternalEdge) starts closed; resync_component() propagates its
272
+ resistance_override each tick after sync_internal_state() opens or closes it.
273
+
274
+ Parameters
275
+ ----------
276
+ comp:
277
+ The source flow-source component.
278
+
279
+ Returns
280
+ -------
281
+ tuple[list[HydraulicNode], list[HydraulicEdge]]
282
+ Two nodes (port + inventory) and one internal nozzle edge.
283
+ """
284
+ nodes: list[HydraulicNode] = []
285
+ port = comp.ports_by_number.get(1)
286
+
287
+ if port is None:
288
+ return nodes, []
289
+
290
+ if not _is_hydraulic_active(port, comp):
291
+ logger.warning(
292
+ "compile_flow_source: component '{}' port 1 is CAPPED - "
293
+ "this flow source is degenerate and will have no effect on the network.",
294
+ comp.name,
295
+ )
296
+ return nodes, []
297
+
298
+ # Port 1 — passive junction node, no boundary condition
299
+ nodes.append(
300
+ HydraulicNode(
301
+ node_id=f"{comp.name}.1",
302
+ boundary=port.boundary, # None
303
+ is_hub=False,
304
+ component=comp.name,
305
+ )
306
+ )
307
+
308
+ # Inventory node — FLOW BC drives fluid through the nozzle
309
+ nodes.append(
310
+ HydraulicNode(
311
+ node_id=f"{comp.name}.Inventory",
312
+ boundary=comp._inventory_bc,
313
+ is_hub=False,
314
+ component=comp.name,
315
+ )
316
+ )
317
+
318
+ # Nozzle edge — JUNCTION, starts closed (resistance_override = R_MAX)
319
+ nozzle = comp.internal_edges[(1, "Inventory")]
320
+ edges: list[HydraulicEdge] = [
321
+ HydraulicEdge(
322
+ edge_id=f"{comp.name}.1.Inventory",
323
+ origin_node_id=f"{comp.name}.1",
324
+ destination_node_id=f"{comp.name}.Inventory",
325
+ length=0.0,
326
+ diameter=0.0,
327
+ role=InternalEdgeRole.JUNCTION,
328
+ resistance_override=nozzle.resistance_override,
329
+ component=comp.name,
330
+ is_external=False,
331
+ )
332
+ ]
333
+ return nodes, edges
334
+
335
+
336
+ # ---------------------------------------------------------------------------
337
+ # PressureControlData
338
+ # ---------------------------------------------------------------------------
339
+
340
+
341
+ def compile_pressure_control(
342
+ comp: PressureControlData,
343
+ ) -> tuple[list[HydraulicNode], list[HydraulicEdge]]:
344
+ """Compile a pressure-control boundary into a single node with no internal edges.
345
+
346
+ Pressure-control components impose a fixed pressure at port 1 via their
347
+ boundary condition. They contribute no internal edges to the graph.
348
+
349
+ Parameters
350
+ ----------
351
+ comp:
352
+ The source pressure-control component.
353
+
354
+ Returns
355
+ -------
356
+ tuple[list[HydraulicNode], list[HydraulicEdge]]
357
+ At most one node; no edges.
358
+ """
359
+ nodes: list[HydraulicNode] = []
360
+ port = comp.ports_by_number.get(1)
361
+
362
+ if port is None:
363
+ return nodes, []
364
+
365
+ if not _is_hydraulic_active(port, comp):
366
+ return nodes, []
367
+
368
+ nodes.append(
369
+ HydraulicNode(
370
+ node_id=f"{comp.name}.1",
371
+ boundary=port.boundary,
372
+ is_hub=False,
373
+ component=comp.name,
374
+ )
375
+ )
376
+ return nodes, []
377
+
378
+
379
+ # ---------------------------------------------------------------------------
380
+ # JunctionData
381
+ # ---------------------------------------------------------------------------
382
+
383
+
384
+ def compile_junction(
385
+ comp: JunctionData,
386
+ ) -> tuple[list[HydraulicNode], list[HydraulicEdge]]:
387
+ """Compile a fluidic junction (T-piece, manifold, etc.) into a star topology.
388
+
389
+ A junction has a synthetic hub port (port 0) that is never filtered — it is
390
+ the common pressure node to which all external ports connect. External
391
+ ports 1..N are subject to the standard hydraulic-active filter.
392
+
393
+ Parameters
394
+ ----------
395
+ comp:
396
+ The source junction component.
397
+
398
+ Returns
399
+ -------
400
+ tuple[list[HydraulicNode], list[HydraulicEdge]]
401
+ Hub node, any active external-port nodes, and junction edges from each
402
+ active external port to the hub.
403
+ """
404
+ # Hub node — always emitted, never filtered
405
+ hub_node = HydraulicNode(
406
+ node_id=f"{comp.name}.0",
407
+ boundary=None,
408
+ is_hub=True,
409
+ component=comp.name,
410
+ )
411
+ nodes: list[HydraulicNode] = [hub_node]
412
+ valid_port_numbers: set[int | str] = set()
413
+
414
+ for port_number, port in comp.ports_by_number.items():
415
+ if port_number == 0:
416
+ continue # hub is handled above
417
+ if not _is_hydraulic_active(port, comp):
418
+ continue
419
+ nodes.append(
420
+ HydraulicNode(
421
+ node_id=f"{comp.name}.{port_number}",
422
+ boundary=port.boundary,
423
+ is_hub=False,
424
+ component=comp.name,
425
+ )
426
+ )
427
+ valid_port_numbers.add(port_number)
428
+
429
+ edges: list[HydraulicEdge] = []
430
+ for (ext_port, hub), edge in comp.internal_edges.items():
431
+ if hub != 0:
432
+ continue
433
+ if ext_port not in valid_port_numbers:
434
+ continue
435
+ edges.append(
436
+ HydraulicEdge(
437
+ edge_id=f"{comp.name}.{ext_port}.0",
438
+ origin_node_id=f"{comp.name}.{ext_port}",
439
+ destination_node_id=f"{comp.name}.0",
440
+ length=edge.length,
441
+ diameter=edge.diameter,
442
+ role=InternalEdgeRole.JUNCTION,
443
+ resistance_override=edge.resistance_override,
444
+ component=comp.name,
445
+ is_external=False,
446
+ )
447
+ )
448
+
449
+ return nodes, edges
450
+
451
+
452
+ # ---------------------------------------------------------------------------
453
+ # Generic fallback
454
+ # ---------------------------------------------------------------------------
455
+
456
+
457
+ def compile_component(
458
+ comp: ComponentData,
459
+ ) -> tuple[list[HydraulicNode], list[HydraulicEdge]]:
460
+ """Generic fallback compiler for any ComponentData subclass.
461
+
462
+ Iterates ``comp.ports_by_number`` and ``comp.internal_edges`` applying the
463
+ standard hydraulic-active filter. Supports both integer–integer and
464
+ integer–``"Inventory"`` edge keys.
465
+
466
+ Use this compiler when no specific compiler exists for a component type.
467
+ It degrades gracefully: unrecognised port types are simply skipped.
468
+
469
+ Parameters
470
+ ----------
471
+ comp:
472
+ Any ComponentData subclass not handled by a specific compiler.
473
+
474
+ Returns
475
+ -------
476
+ tuple[list[HydraulicNode], list[HydraulicEdge]]
477
+ Nodes for all active hydraulic ports and edges whose both endpoints
478
+ are active (or whose destination is ``"Inventory"`` and the origin is
479
+ active).
480
+ """
481
+ nodes: list[HydraulicNode] = []
482
+ valid_node_ids: set[str] = set()
483
+
484
+ for port_number, port in comp.ports_by_number.items():
485
+ if not _is_hydraulic_active(port, comp):
486
+ continue
487
+ node_id = f"{comp.name}.{port_number}"
488
+ nodes.append(
489
+ HydraulicNode(
490
+ node_id=node_id,
491
+ boundary=port.boundary,
492
+ is_hub=_is_hub_port(port, port_number, comp),
493
+ component=comp.name,
494
+ )
495
+ )
496
+ valid_node_ids.add(node_id)
497
+
498
+ edges: list[HydraulicEdge] = []
499
+ for (origin, dest), edge in comp.internal_edges.items():
500
+ origin_node_id = f"{comp.name}.{origin}"
501
+ if origin_node_id not in valid_node_ids:
502
+ continue
503
+
504
+ if dest == "Inventory":
505
+ destination_node_id = f"{comp.name}.Inventory"
506
+ else:
507
+ destination_node_id = f"{comp.name}.{dest}"
508
+ if destination_node_id not in valid_node_ids:
509
+ continue
510
+
511
+ edges.append(
512
+ HydraulicEdge(
513
+ edge_id=f"{comp.name}.{origin}.{dest}",
514
+ origin_node_id=origin_node_id,
515
+ destination_node_id=destination_node_id,
516
+ length=edge.length,
517
+ diameter=edge.diameter,
518
+ role=edge.role,
519
+ resistance_override=edge.resistance_override,
520
+ component=comp.name,
521
+ is_external=False,
522
+ forced_flow=edge.forced_flow_override,
523
+ )
524
+ )
525
+
526
+ return nodes, edges
527
+
528
+
529
+ # ---------------------------------------------------------------------------
530
+ # Dispatch registry
531
+ # ---------------------------------------------------------------------------
532
+
533
+ # Mapping from ComponentData subtype to its dedicated compiler function.
534
+ # Falls back to compile_component for any type not listed here.
535
+ # Dispatch uses exact type(comp) -- not isinstance/MRO.
536
+ _COMPILERS: dict[
537
+ type, Callable[..., tuple[list[HydraulicNode], list[HydraulicEdge]]]
538
+ ] = {
539
+ PlugFlowComponentData: compile_plugflow,
540
+ VesselComponentData: compile_vessel,
541
+ ValveComponentData: compile_valve,
542
+ FlowSourceData: compile_flow_source,
543
+ PressureControlData: compile_pressure_control,
544
+ JunctionData: compile_junction,
545
+ }