Multi-model low-voltage distribution diagnostic engine in MoonBit

moon update
moon check --target wasm-gc --deny-warn
moon test --target wasm-gc --deny-warn
moon test --target js --deny-warn
# Portable JavaScript backend:
moon run cmd/griddiagnose --target js -- diagnose examples/griddiagnose_multimodel.json
# Machine-readable report:
moon run cmd/griddiagnose --target js -- diagnose examples/griddiagnose_multimodel.json --json
# Legacy outage commands remain available:
moon run cmd/griddiagnose --target js -- analyze examples/branch_fault.json
moon run cmd/griddiagnose --target js -- verify examples/restore_case.jsonmoon run cmd/griddiagnose --target native -- diagnose examples/griddiagnose_multimodel.json{
"analysis_time": 500,
"network": {
"root": "T1",
"nodes": [
{"id": "T1", "kind": "source"},
{"id": "J1", "kind": "junction"},
{"id": "M1", "kind": "meter"}
],
"edges": [
{"id": "E0", "from": "T1", "to": "J1", "section": "feeder"},
{"id": "E1", "from": "J1", "to": "M1", "section": "service M1"}
]
},
"unknown_topology_edge_ids": ["X-NEW"],
"observations": [
{"node_id": "M1", "state": "outage", "confidence": 0.97}
],
"phase_readings": [
{
"node_id": "M1",
"timestamp": 499,
"a_voltage": 230,
"b_voltage": 230,
"c_voltage": 0
}
],
"voltage_series": [
{
"node_id": "M1",
"nominal_voltage": 230,
"samples": [
{"timestamp": 490.00, "voltage": 230},
{"timestamp": 490.01, "voltage": 178},
{"timestamp": 490.02, "voltage": 230}
]
}
]
}GridDiagnose combined diagnostic report
Analysis time: 500 s
Critical findings: 6
Warnings: 3
Primary diagnosis: supply_fault (89.8%)
Decision margin: 81.5%
[supply_fault] 89.8% -> J3, M31, M32, M33, M21
[topology_issue] 8.3% -> M22, network, X-NEW-DEVICE
[measurement_issue] 1.9% -> M22
conflict: outage candidates rely on a topology with registration uncertainty
Topology: partial
nodes=9 edges=8 components=1 cycles=0
Outage hypotheses: 2
1. E2 [J1-J3 cable] stage confidence 49.85%
affected: M31, M32, M33
2. E3 [M21 service] stage confidence 80.16%
affected: M21
Phase findings: 2
[critical] M21 phase_loss
[critical] M31 voltage_unbalance
Power-quality events: 2
[critical] M33 voltage_sag
[warning] M33 voltage_swell
Telemetry findings: 3
[warning] M22 clock_anomaly
[warning] M22 constant_series
[critical] M22 topology_mismatch| Metric | Result |
|---|---|
| Overall Top-1 accuracy | 91.7% |
| Top-3 recall | 94.4% |
| Mean affected-meter Jaccard | 92.6% |
| Clean-scenario Top-1 | 100% |
| Mixed-scenario Top-1 | 91.7% |
| Severe-scenario Top-1 | 83.3% |
| Model | Passed | Accuracy |
|---|---|---|
| Multi-fault outage | 12/12 | 100% |
| Phase diagnosis | 12/12 | 100% |
| Power quality | 12/12 | 100% |
| Telemetry integrity | 12/12 | 100% |
| Causal arbitration | 12/12 | 100% |
| Contrast cases | 12/12 | 100% |
moon build cmd/griddiagnose --target js --release
python scripts/evaluate.py \
--bin _build/js/release/build/cmd/griddiagnose/griddiagnose.js \
--cases 36
python scripts/evaluate_multimodel.py \
--bin _build/js/release/build/cmd/griddiagnose/griddiagnose.js \
--cases-per-model 12moon build cmd/griddiagnose --target js --release
python scripts/build_demo_data.pypub suberror AnalysisError {
Parse(String)
InvalidInput(String)
Topology(String)
}pub struct CausalArbitration {
primary_class : String
overall_confidence : Double
decision_margin : Double
causes : Array[CauseHypothesis]
contributions : Array[EvidenceContribution]
conflicts : Array[String]
independent_evidence_sources : Array[String]
undetermined_reasons : Array[String]
}pub struct DiagnosticInput {
analysis_time : Double
network : Network?
outage_observations : Array[Observation]
phase_readings : Array[PhaseReading]
voltage_series : Array[VoltageSeries]
telemetry_series : Array[TelemetrySeries]
topology_links : Array[TopologyLinkObservation]
unknown_topology_edge_ids : Array[String]
}pub struct DiagnosticReport {
version : String
analysis_time : Double
topology : TopologyAssessment?
outage : MultiFaultReport?
phase : PhaseReport
power_quality : PowerQualityReport
telemetry : TelemetryReport
arbitration : CausalArbitration
warnings : Array[String]
critical_count : Int
warning_count : Int
}pub struct Edge {
id : String
from_id : String
to_id : String
section : String
}pub struct EvidenceContribution {
cause_class : String
feature : String
weight : Double
detail : String
}pub struct InferencePolicy {
half_life_sec : Double
structural_prior : Double
outage_penalty : Double
powered_penalty : Double
}pub struct InputDocument {
network : Network
observations : Array[Observation]
analysis_time : Double
confirmed_fault_edge_id : String?
}pub struct MultiFaultReport {
faults : Array[FaultHypothesis]
residual_outage_meter_ids : Array[String]
iterations : Int
warnings : Array[String]
}pub(all) enum NodeKind {
Source
Junction
Meter
}pub struct Observation {
node_id : String
state : PowerState
confidence : Double
observed_at : Double
}fn Observation::new(node_id : String, state : PowerState, confidence : Double, observed_at : Double) -> Observationpub struct PhaseFinding {
node_id : String
kind : String
severity : String
metric : Double
affected_phases : Array[String]
summary : String
}pub struct PhaseReading {
node_id : String
timestamp : Double
a_voltage : Double
b_voltage : Double
c_voltage : Double
a_current : Double
b_current : Double
c_current : Double
}fn PhaseReading::new(node_id : String, timestamp : Double, a_voltage : Double, b_voltage : Double, c_voltage : Double, a_current? : Double, b_current? : Double, c_current? : Double) -> PhaseReadingpub struct PowerQualityEvent {
node_id : String
source_id : String
phase : String
kind : String
severity : String
start_time : Double
end_time : Double
duration_sec : Double
extreme_voltage : Double
deviation_percent : Double
minimum_voltage : Double
maximum_voltage : Double
variation_percent : Double
}pub(all) enum PowerState {
Energized
Outage
Unknown
}pub(all) enum RestoreState {
Complete
Partial
UnexpectedOutage
Indeterminate
}pub struct TelemetryFinding {
node_id : String
kind : String
severity : String
summary : String
metric : Double
}pub struct TelemetryPoint {
timestamp : Double
value : Double
}pub enum TopologyError {
EmptyNetwork
RootNotFound(String)
DuplicateNodeId(String)
DuplicateEdgeId(String)
UnknownEndpoint(String)
NotRadial
Disconnected(String)
}pub struct TopologyLinkObservation {
node_id : String
declared_parent_id : String
observed_parent_id : String
}fn TopologyLinkObservation::new(node_id : String, declared_parent_id : String, observed_parent_id : String) -> TopologyLinkObservationpub struct VoltagePoint {
timestamp : Double
voltage : Double
}pub struct VoltageSeries {
node_id : String
source_id : String
phase : String
nominal_voltage : Double
samples : Array[VoltagePoint]
}fn VoltageSeries::new(node_id : String, nominal_voltage : Double, samples : Array[VoltagePoint], source_id? : String, phase? : String) -> VoltageSeriesfn arbitrate_causes(topology : TopologyAssessment?, outage : MultiFaultReport?, phase : PhaseReport, power_quality : PowerQualityReport, telemetry : TelemetryReport, phase_readings : Array[PhaseReading], voltage_series? : Array[VoltageSeries]) -> CausalArbitrationfn diagnose_power_quality(series : Array[VoltageSeries], minimum_duration_sec? : Double) -> PowerQualityReportfn diagnose_telemetry(series : Array[TelemetrySeries], links : Array[TopologyLinkObservation], analysis_time : Double, stale_after_sec? : Double, stuck_window_sec? : Double, constant_tolerance? : Double) -> TelemetryReportfn infer_faults(network : Network, observations : Array[Observation], analysis_time : Double, policy? : InferencePolicy) -> InferenceResult raise AnalysisErrorfn infer_multiple_faults(network : Network, observations : Array[Observation], analysis_time : Double, max_faults? : Int, minimum_confidence? : Double) -> MultiFaultReport raise AnalysisErrorfn verify_restoration(network : Network, fault_edge_id : String, observations : Array[Observation], analysis_time : Double) -> RestoreReport raise AnalysisErrorInstall
Download zipMulti-model low-voltage distribution diagnostic engine in MoonBit