STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
report.py
Go to the documentation of this file.
1"""A solved network, written out so a person can read it.
2
3The pressure ladder is the answer to the question that started this project:
4where does the pressure actually go between the tank and the injector. It lists
5every component in flow order with what it cost, so the largest loss is
6obvious rather than inferred.
7
8Two things are always printed, whether or not anyone asked:
9
10* **Mass conservation.** A converged solve that does not conserve mass has
11 found the wrong answer confidently, and that is exactly the failure that
12 looks like success.
13* **Assumed parameters and violated limits.** A number resting on twelve
14 library defaults is a different claim from one resting on twelve
15 measurements, and the difference should never be invisible.
16"""
17
18from __future__ import annotations
19
20from feedtwin.solve.network import Network
21from feedtwin.solve.steady import SteadyResult
22
23_BAR = 1.0e5
24
25
26def pressure_ladder(network: Network, result: SteadyResult) -> str:
27 """A text report of where the pressure went."""
28 lines: list[str] = []
29 status = "converged" if result.converged else "DID NOT CONVERGE"
30 lines.append(
31 f"Steady solve: {status} in {result.iterations} iterations "
32 f"({result.elapsed * 1e3:.1f} ms)"
33 )
34 lines.append("")
35
36 lines.append(
37 f"{'branch':<14} {'from':<10} {'to':<10} "
38 f"{'mdot':>10} {'dp':>10} {'p_out':>10}"
39 )
40 lines.append(f"{'':14} {'':10} {'':10} {'kg/s':>10} {'bar':>10} {'bar':>10}")
41 lines.append("-" * 68)
42
43 ordered = sorted(
44 network.branches.values(),
45 key=lambda b: -result.pressures[b.upstream],
46 )
47 for branch in ordered:
48 dp = result.dp(branch.id, network)
49 lines.append(
50 f"{branch.id:<14} {branch.upstream:<10} {branch.downstream:<10} "
51 f"{result.flows[branch.id]:>10.4f} {dp / _BAR:>10.4f} "
52 f"{result.pressures[branch.downstream] / _BAR:>10.4f}"
53 )
54
55 lines.append("")
56 lines.append(
57 f"mass conservation: worst node imbalance "
58 f"{result.max_mass_residual:.3e} kg/s"
59 )
60
61 if result.regularised_branches:
62 lines.append(
63 "derivative floor active at convergence on: "
64 + ", ".join(result.regularised_branches)
65 + " (branches carrying essentially no flow)"
66 )
67
68 assumed = sorted(
69 {
70 name
71 for branch in network.branches.values()
72 for name in branch.component.instance.assumptions()
73 }
74 )
75 if assumed:
76 lines.append(f"resting on library defaults: {', '.join(assumed)}")
77
78 for violation in result.violations:
79 lines.append(str(violation))
80
81 return "\n".join(lines)
str pressure_ladder(Network network, SteadyResult result)
Definition report.py:26