Spaces:
Running

File size: 5,849 Bytes
0a53b5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
/* Steady-state commands and solutions are distinct from Gallery classification. */
(function(root) {
  'use strict';

  let registry=typeof module!=='undefined'?require('./operating-conditions.json'):null;
  let loading;
  function configure(definitions) {
    if (definitions.schema_version!==1) throw Error('Unsupported operating-conditions schema.');
    registry=definitions;
    return registry;
  }
  function load(url) {
    return registry?Promise.resolve(registry):loading??=(fetch(url,{cache:'no-store'}).then(response=>{
      if (!response.ok) throw Error('Operating conditions could not be loaded.');
      return response.json();
    }).then(configure));
  }
  const definition=id=>registry?.topologies[id];
  function voltageCoefficients(law, values) {
    const coefficients=[0,0];
    for (const [key,[constant,slope]] of Object.entries(law.loaded_voltage_terms)) {
      if (!Number.isFinite(values[key])) throw Error('Missing voltage-balance input '+key+'.');
      coefficients[0]+=values[key]*constant;
      coefficients[1]+=values[key]*slope;
    }
    return coefficients;
  }
  function buildRecord(def, inputs, duty, idealGain, analyticalVoltage, status, range, physical={}) {
    const vin=inputs.input_voltage_v, idealVoltage=vin*idealGain;
    const loadedGain=analyticalVoltage==null?null:analyticalVoltage/vin;
    return {schema_version:1,nominal_ratio:def.nominal_ratio,inputs,
      derived:{
        applied_duty:duty,ideal_output_voltage_v:idealVoltage,ideal_gain:idealGain,ideal_operating_ratio:1/idealGain,
        analytical_output_voltage_v:analyticalVoltage,loaded_gain:loadedGain,
        operating_ratio:loadedGain>0?1/loadedGain:null,conduction_mode:physical.conduction_mode??null,
      },
      range,solver:{status,valid:status==='ideal_only'?null:status==='resolved',diagnostics:physical.diagnostics??[]},
    };
  }

  function resolve(id, inputs, adapter={}) {
    const def=definition(id);
    const {mode,input_voltage_v:vin,load,switching_frequency_hz:fs,target_output_v:target,duty}=inputs;
    if (!def?.modes?.includes(mode)) throw Error('Select a supported operating mode.');
    if (![vin,load?.value,fs].every(value=>Number.isFinite(value)&&value>0) || !def.load_kinds.includes(load.kind))
      throw Error('Enter a positive input voltage, supported load and switching frequency.');
    if ((mode==='output_voltage' && (duty!=null || !Number.isFinite(target) || target<=0)) ||
        (mode==='duty_cycle' && target!=null) || (mode==='fixed_ratio' && (target!=null || duty!=null)))
      throw Error('Supply only the independent command for the selected operating mode.');

    const {law,range}=def;
    const {min,max}=range.duty;
    const gain=adapter.ideal_gain??(d=>law.ideal_gain.constant+law.ideal_gain.duty*d);
    const coefficients=adapter.balance&&!adapter.balance_at_duty?voltageCoefficients(law,adapter.balance):null;
    function loaded(d) {
      if (coefficients) return coefficients[0]+coefficients[1]*d;
      if (adapter.balance_at_duty) {
        const [offset,slope]=voltageCoefficients(law,adapter.balance_at_duty(d));
        return offset+slope*d;
      }
      return adapter.loaded_voltage?.(d)??null;
    }

    const low=loaded(min),high=loaded(max);
    let applied=duty;
    if (mode==='fixed_ratio') applied=min;
    if (mode==='output_voltage') {
      const tolerance=1e-12*Math.max(1,vin);
      if (!Number.isFinite(low) || !Number.isFinite(high) || high<=low || target<low-tolerance || target>high+tolerance)
        throw RangeError('The target voltage is outside the loaded voltage range for the supported duty limits.');
      if (coefficients) applied=(target-coefficients[0])/coefficients[1];
      else {
        let lo=min,hi=max;
        for (let i=0;i<40;i++) {
          const mid=(lo+hi)/2;
          if (loaded(mid)<target) lo=mid;
          else hi=mid;
        }
        applied=(lo+hi)/2;
      }
      applied=Math.max(min,Math.min(max,applied));
    }
    if (!Number.isFinite(applied) || applied<min || applied>max)
      throw RangeError(`Enter a duty from ${min} to ${max}.`);

    const solved=loaded(applied);
    if (mode==='output_voltage' && (!Number.isFinite(solved) || Math.abs(solved-target)>1e-9*Math.max(1,vin)))
      throw RangeError('The loaded voltage solver did not converge.');
    const physical=adapter.screen?.(applied)??{};
    if (solved!=null && !(solved>0)) {
      physical.diagnostics=[...(physical.diagnostics??[]),'The analytical output voltage is nonpositive.'];
    }
    const status=physical.diagnostics?.length?'infeasible':solved==null?'ideal_only':'resolved';
    const result=buildRecord(def,inputs,applied,gain(applied),solved,status,
      {...range,loaded_voltage_v:low==null?null:{min:low,max:high}},physical);
    // Preserve the existing SI Analysis / handoff fields during migration.
    return {...result,mode,vin,target:target??null,duty:applied,
      idealVoltage:result.derived.ideal_output_voltage_v,idealGain:gain(applied),
      analyticalVoltage:solved,loadedGain:result.derived.loaded_gain};
  }

  function sweepCommand(inputs,axis,value) {
    if (axis==='duty_cycle' || axis==='output_voltage') {
      const {duty,target_output_v,...shared}=inputs;
      return {...shared,mode:axis,...(axis==='duty_cycle'?{duty:value}:{target_output_v:value})};
    }
    if (axis==='output_current_a') return {...inputs,load:{kind:'current',value}};
    if (axis==='switching_frequency_mhz') return {...inputs,switching_frequency_hz:value*1e6};
    if (axis==='input_voltage_v') return {...inputs,input_voltage_v:value};
    throw Error('Choose a supported operating sweep axis.');
  }
  const api={configure,load,definition,resolve,sweepCommand};
  root.OpenPELSOperatingPoint=api;
  if (typeof module!=='undefined') module.exports=api;
})(typeof window!=='undefined'?window:globalThis);