public trans_calc_input_text Load(string fileName) { trans_calc_input_text input_text = new trans_calc_input_text(); using (StreamReader sr = new StreamReader(fileName)) { while (sr.Peek() >= 0) { ProcessLine(sr.ReadLine(), ref input_text); } } return(input_text); }
public void SaveSettings(trans_calc_input_text strin, string fileName) { using (StreamWriter sw = new StreamWriter(fileName)) { sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.V_IN]}={strin.Vin}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.B_MAX]}={strin.Bmax}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.H]}={strin.H}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.PERMEABILITY]}={strin.permeability}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.I_EX]}={strin.I_ex}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.CORE_W]}={strin.core_W}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.CORE_H]}={strin.core_H}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.CORE_L]}={strin.core_L}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.Ae_W]}={strin.Ae_W}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.Ae_H]}={strin.Ae_H}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.MPATH_W]}={strin.mpath_W}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.MPATH_H]}={strin.mpath_H}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.WINDOW_SIZE]}={strin.window_size}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.COUPLING_COEFF]}={strin.coupling_coeff}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.STACKING_FACTOR]}={strin.stackingFactor}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.INSULATION]}={strin.insulationThickness}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.V_OUT]}={strin.Vout}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.IS_V_OUT_AT_MAX_LOAD]}={strin.isVoutAtFullLoad}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.I_OUT]}={strin.Iout_max}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.MAX_TEMP]}={strin.maxTemp}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.MAX_EQ_R]}={strin.max_eq_R}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.AWG_1]}={strin.awg1}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.W_FACTOR_1]}={strin.wfactor1}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.N_1]}={strin.N1}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.N_PER_LAYER_1]}={strin.N_per_layer1}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.PF_1]}={strin.pf}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.CM_PER_AMP_1]}={strin.ampacity1}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.AWG_2]}={strin.awg2}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.W_FACTOR_2]}={strin.wfactor2}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.N_2]}={strin.N2}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.N_PER_LAYER_2]}={strin.N_per_layer2}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.CM_PER_AMP_2]}={strin.ampacity2}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.H_UNITS]}={(int)tc.H_Units}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.TEMP_UNITS]}={(tc.IsTempUnitsC ? "1" : "0")}"); sw.WriteLine($"{cfg_keywords[CONFIG_KEYWORDS.WEIGHT_UNITS]}={(tc.IsMassUnits_g ? "1" : "0")}"); } }
public void ConvertTextToInput(trans_calc_input_text strin) { common = new trans_calc_input_common(); primary = new trans_calc_input_winding(); if (strin.Vin == "") { throw new Exception("Vin not set"); } common.Vin = double.Parse(strin.Vin, NumberStyles.Float); if (strin.Vin == "120") { common.Freq = 60; } else if (strin.Vin == "220") { common.Freq = 50; } else { throw new Exception($"Unexpected mains voltage: {strin.Vin}, 110 or 220 expected"); } common.IsVoutAtFullLoad = strin.isVoutAtFullLoad; if (strin.Bmax == "" && strin.N1 == "") { throw new Exception("Either Bmax or Primary turns must be set"); } if (strin.Bmax != "") { common.B_max = double.Parse(strin.Bmax, NumberStyles.Float); if (common.B_max < 0.1 || common.B_max > 2) { throw new Exception("Bmax, has to be in the range of 0.1T to 2T"); } } if (strin.permeability != "") { common.permeability = double.Parse(strin.permeability, NumberStyles.Float); if (common.permeability < 1000 || common.permeability > 10000) { throw new Exception("Permeability has to be in the range of 1000 to 10000"); } } if (strin.I_ex != "") { // from mA to A common.I_ex = double.Parse(strin.I_ex, NumberStyles.Float) / 1000; if (common.I_ex < 0.01 || common.I_ex > 2) { throw new Exception("Iex has to be in the range of 10mA to 2A"); } } if (strin.H != "") { common.H = double.Parse(strin.H, NumberStyles.Float); if (tc.H_Units == TransCalc.H_UNITS.OERSTEDS) { common.H = TransCalc.Oe_to_Ampturns(common.H); } else if (tc.H_Units == TransCalc.H_UNITS.AMP_TURNS_IN) { common.H /= 0.0254; } if (common.H < 1 || common.H > 2000) { throw new Exception("Amp t / m has to be in the range of 1 to 2000 Amp-t-m"); } } if (strin.core_L == "" || strin.core_W == "" || strin.core_H == "") { throw new Exception("Core W/H/L not set"); } if (strin.pf != "") { common.pf1 = double.Parse(strin.pf, NumberStyles.Float); if (common.pf1 < 0 || common.pf1 > 1.0) { throw new Exception("pf has to be between 0 and 1"); } } //to m common.Core_L = double.Parse(strin.core_L, NumberStyles.Float) / 100; common.Core_W = double.Parse(strin.core_W, NumberStyles.Float) / 100; common.Core_H = double.Parse(strin.core_H, NumberStyles.Float) / 100; if (common.Core_L < 0.01 || common.Core_L > 0.5) { throw new Exception("Invalid core L"); } if (common.Core_W < 0.01 || common.Core_W > 0.5) { throw new Exception("Invalid core W"); } if (common.Core_H < 0.01 || common.Core_H > 0.5) { throw new Exception("Invalid core H"); } if (strin.Ae_W == "" || strin.Ae_H == "") { throw new Exception("Ae W/H not set"); } //to m common.Ae_W = double.Parse(strin.Ae_W) / 100; if (common.Ae_W < 0.01 || common.Ae_W > 0.5) { throw new Exception("Invalid Ae width"); } common.Ae_H = double.Parse(strin.Ae_H) / 100; if (common.Ae_H < 0.01 || common.Ae_H > 0.5) { throw new Exception("Invalid Ae height"); } if (common.Ae_W > common.Core_W) { throw new Exception("Ae_W > bobbin W"); } if (common.Ae_H > common.Core_H) { throw new Exception("Ae_H > bobbin H"); } if (strin.mpath_H != "" && strin.mpath_W != "") { //cm double Mpath_H = double.Parse(strin.mpath_H, NumberStyles.Float) / 100; double Mpath_W = double.Parse(strin.mpath_W, NumberStyles.Float) / 100; if (Mpath_H < 0.01 || Mpath_H > 0.5) { throw new Exception("Invalid magnetic path value: H"); } if (Mpath_W < 0.01 || Mpath_W > 0.5) { throw new Exception("Invalid magnetic path value: W"); } common.mpath_l_cm = (Mpath_H + Mpath_W) * 2; } if (strin.window_size != "") { // in cm common.WindowSize = double.Parse(strin.window_size, NumberStyles.Float); if (common.WindowSize < 0.1 || common.WindowSize > 50) { throw new Exception("Invalid window size in cm"); } } if (strin.coupling_coeff == "") { throw new Exception("Coupling coefficient not set"); } common.CouplingCoeff = double.Parse(strin.coupling_coeff, NumberStyles.Float); if (common.CouplingCoeff < 0.5 || common.CouplingCoeff > 1.0) { throw new Exception("Coupling coefficient has to be in the range of 0.5 to 1.0"); } if (strin.stackingFactor == "") { throw new Exception("Stacking factor not set"); } common.StackingFactor = double.Parse(strin.stackingFactor, NumberStyles.Float); if (common.StackingFactor < 0.5 || common.StackingFactor > 1.0) { throw new Exception("Stacking factor has to be in the range of 0.5 to 1.0"); } if (strin.insulationThickness != "") { common.InsulationThickness = double.Parse(strin.insulationThickness, NumberStyles.Float); } if (common.WindowSize > 0.0000001) { if (common.InsulationThickness < 0 || common.InsulationThickness > common.WindowSize) { throw new Exception("Insulation thickness in mm cannot exceed window size"); } } if (strin.Vout != "") { common.Vout = double.Parse(strin.Vout, NumberStyles.Float); if (common.Vout < 0.1) { throw new Exception("Invalid Vout value"); } } if (strin.Iout_max != "") { common.Iout_max = double.Parse(strin.Iout_max, NumberStyles.Float); if (common.Iout_max < 0.00000000001) { throw new Exception("Invalid Iout_max value"); } } common.max_temp = double.Parse(strin.maxTemp, NumberStyles.Float); if (!tc.IsTempUnitsC) { common.max_temp = TransCalc.F_to_C(common.max_temp); } if (common.max_temp < 20) { common.max_temp = 20; } if (strin.max_eq_R != "") { common.max_res_R = double.Parse(strin.max_eq_R, NumberStyles.Float); if (common.max_res_R < 1) { throw new Exception("Invalid value of max equivalent R"); } } primary = convertWinding("Primary", strin.awg1, strin.wfactor1, strin.N1, strin.N_per_layer1, strin.ampacity1); if ((strin.N2 != "" || strin.Vout != "")) { secondary = convertWinding("Secondary", strin.awg2, strin.wfactor2, strin.N2, strin.N_per_layer2, strin.ampacity2); processSecondary = true; } else { processSecondary = false; } isMinimizeRegulation = strin.isMinimizeRegulation; }
private void ProcessLine(string line, ref trans_calc_input_text strin) { string[] parts = line.Split('='); var key = cfg_keywords.FirstOrDefault(e => e.Value.Equals(parts[0].Trim(), StringComparison.OrdinalIgnoreCase)).Key; if (key == CONFIG_KEYWORDS.UNKNOWN_KEYWORD) { throw new Exception($"Error parsing cfg file, invalid keyword: {line}"); } switch (key) { case CONFIG_KEYWORDS.V_IN: strin.Vin = parts[1]; break; case CONFIG_KEYWORDS.B_MAX: strin.Bmax = parts[1]; break; case CONFIG_KEYWORDS.H: strin.H = parts[1]; break; case CONFIG_KEYWORDS.PERMEABILITY: strin.permeability = parts[1]; break; case CONFIG_KEYWORDS.I_EX: strin.I_ex = parts[1]; break; case CONFIG_KEYWORDS.CORE_W: strin.core_W = parts[1]; break; case CONFIG_KEYWORDS.CORE_H: strin.core_H = parts[1]; break; case CONFIG_KEYWORDS.CORE_L: strin.core_L = parts[1]; break; case CONFIG_KEYWORDS.Ae_W: strin.Ae_W = parts[1]; break; case CONFIG_KEYWORDS.Ae_H: strin.Ae_H = parts[1]; break; case CONFIG_KEYWORDS.MPATH_W: strin.mpath_W = parts[1]; break; case CONFIG_KEYWORDS.MPATH_H: strin.mpath_H = parts[1]; break; case CONFIG_KEYWORDS.WINDOW_SIZE: strin.window_size = parts[1]; break; case CONFIG_KEYWORDS.COUPLING_COEFF: strin.coupling_coeff = parts[1]; break; case CONFIG_KEYWORDS.STACKING_FACTOR: strin.stackingFactor = parts[1]; break; case CONFIG_KEYWORDS.INSULATION: strin.insulationThickness = parts[1]; break; case CONFIG_KEYWORDS.V_OUT: strin.Vout = parts[1]; break; case CONFIG_KEYWORDS.IS_V_OUT_AT_MAX_LOAD: strin.isVoutAtFullLoad = bool.Parse(parts[1]); break; case CONFIG_KEYWORDS.I_OUT: strin.Iout_max = parts[1]; break; case CONFIG_KEYWORDS.MAX_TEMP: strin.maxTemp = parts[1]; break; case CONFIG_KEYWORDS.MAX_EQ_R: strin.max_eq_R = parts[1]; break; case CONFIG_KEYWORDS.AWG_1: strin.awg1 = parts[1]; break; case CONFIG_KEYWORDS.W_FACTOR_1: strin.wfactor1 = parts[1]; break; case CONFIG_KEYWORDS.N_1: strin.N1 = parts[1]; break; case CONFIG_KEYWORDS.N_PER_LAYER_1: strin.N_per_layer1 = parts[1]; break; case CONFIG_KEYWORDS.PF_1: strin.pf = parts[1]; break; case CONFIG_KEYWORDS.CM_PER_AMP_1: strin.ampacity1 = parts[1]; break; case CONFIG_KEYWORDS.AWG_2: strin.awg2 = parts[1]; break; case CONFIG_KEYWORDS.W_FACTOR_2: strin.wfactor2 = parts[1]; break; case CONFIG_KEYWORDS.N_2: strin.N2 = parts[1]; break; case CONFIG_KEYWORDS.N_PER_LAYER_2: strin.N_per_layer2 = parts[1]; break; case CONFIG_KEYWORDS.CM_PER_AMP_2: strin.ampacity2 = parts[1]; break; case CONFIG_KEYWORDS.H_UNITS: tc.SetHUnits(parts[1]); break; case CONFIG_KEYWORDS.TEMP_UNITS: tc.IsTempUnitsC = parts[1] == "1" ? true : false; break; case CONFIG_KEYWORDS.WEIGHT_UNITS: tc.IsMassUnits_g = parts[1] == "1" ? true : false; break; default: throw new Exception($"Unknown key: {key}"); } }
public void SaveResults(trans_calc_input_text strin, trans_calc_result_text result, string fileName) { TransCalcInput input = new TransCalcInput(tc); input.ConvertTextToInput(strin); using (StreamWriter sw = new StreamWriter(fileName)) { sw.WriteLine("Input:\n"); sw.WriteLine($"Core size WxHxL : {strin.core_W} x {strin.core_H} x {strin.core_L} cm"); sw.WriteLine($"Ae, WxH : {strin.Ae_W} x {strin.Ae_H} cm"); if (strin.window_size != "") { sw.WriteLine($"Window size : {strin.window_size} mm"); } if (input.common.mpath_l_cm > 0.0000001) { sw.WriteLine($"Mpath, WxH : {strin.mpath_W} x {strin.mpath_H} cm"); } if (strin.Bmax != "") { sw.WriteLine($"Bmax : {strin.Bmax} T"); } if (strin.H != "") { sw.WriteLine($"H : {strin.H} " + tc.HUnitlsLabel); } if (strin.permeability != "") { sw.WriteLine($"u/u0 : {strin.permeability}"); } if (strin.I_ex != "") { sw.WriteLine($"Iex : {strin.I_ex} A"); } sw.WriteLine($"L1/L2 coupling coeff : {strin.coupling_coeff}"); sw.WriteLine($"Stacking factor : {strin.stackingFactor}"); if (strin.insulationThickness != "") { sw.WriteLine($"Insulation : {strin.insulationThickness} mm"); } if (strin.Vout != "") { string fullLoad = strin.isVoutAtFullLoad ? "(full load)" : "(idle)"; sw.WriteLine($"Vout : {strin.Vout} {fullLoad}"); } if (strin.Iout_max != "") { sw.WriteLine($"Iout max : {strin.Iout_max} A"); } sw.WriteLine($"Max temperature : {strin.maxTemp} " + tc.TempUnitsLabel); if (strin.max_eq_R != "") { sw.WriteLine($"Max equivalent R : {strin.max_eq_R}"); } sw.WriteLine(); sw.WriteLine($"Primary : {strin.awg1} AWG, Wfactor: {strin.wfactor1}, N: {strin.N1}, " + $"N per layer: {strin.N_per_layer1} C.M. per amp: {strin.ampacity1}"); if (input.processSecondary) { sw.WriteLine($"Secondary : {strin.awg2} AWG, Wfactor: {strin.wfactor2}, N: {strin.N2}, " + $"N per layer: {strin.N_per_layer2} C.M. per amp: {strin.ampacity2}"); } sw.WriteLine($"\nMains: {input.common.Vin.ToString()}V / {input.common.Freq.ToString()}Hz"); sw.WriteLine("\n=== Results: =====================================\n"); sw.WriteLine("Primary:\n"); sw.WriteLine($"AWG : {result.AWG1}"); sw.WriteLine($"Turns : {result.N_1}"); sw.WriteLine($"Turns per layer : {result.N_per_layer_1}"); sw.WriteLine($"Total layers : {result.totalLayers_1}"); sw.WriteLine($"Last layer turns : {result.lastLayerTurns_1}"); sw.WriteLine($"Wire length : {result.length_m_1} m / {result.length_ft_1} ft"); sw.WriteLine($"Build-up : {result.buildup_mm_1} mm"); sw.WriteLine($"R : {result.R_1}"); if (result.mpath_l_m != Constants.EmptyValue) { sw.WriteLine($"Magnetic path : {result.mpath_l_m} m"); } if (result.L_1 != Constants.EmptyValue) { sw.WriteLine($"L : {result.L_1} H"); } sw.WriteLine($"Bmax : {result.B_max} T"); if (result.permeability != Constants.EmptyValue) { sw.WriteLine($"u/u0 : {result.permeability}"); } if (result.H != Constants.EmptyValue) { sw.WriteLine($"H : {result.H} " + tc.HUnitlsLabel); } if (result.I_ex != Constants.EmptyValue) { sw.WriteLine($"Iex : {result.I_ex} A"); } if (result.Ip_full_load != Constants.EmptyValue) { sw.WriteLine($"Ip, full load : {result.Ip_full_load} A"); } if (result.awg_max_current_amp_1 != Constants.EmptyValue) { sw.WriteLine($"AWG max current : {result.awg_max_current_amp_1} A"); } sw.WriteLine($"Weight : {result.weight_1} " + tc.MassUnitsLabel); if (input.processSecondary) { sw.WriteLine("\nSecondary:\n"); sw.WriteLine($"AWG : {result.AWG2}"); sw.WriteLine($"Turns : {result.N_2}"); sw.WriteLine($"Turns per layer : {result.N_per_layer_2}"); sw.WriteLine($"Total layers : {result.totalLayers_2}"); sw.WriteLine($"Last layer turns : {result.lastLayerTurns_2}"); sw.WriteLine($"Wire length : {result.length_m_2} m / {result.length_ft_2} ft"); sw.WriteLine($"Build-up : {result.buildup_mm_2} mm"); sw.WriteLine($"R : {result.R_2}"); sw.WriteLine($"Total build-up : {result.total_thickness_mm} mm"); sw.WriteLine($"L : {result.L_2} H"); sw.WriteLine($"Vout idle : {result.Vout_idle} V"); sw.WriteLine($"Vout full load : {result.Vout_load} V"); sw.WriteLine($"Iout full load : {result.Iout_max} A"); sw.WriteLine($"AWG max current : {result.awg_max_current_amp_2} A"); sw.WriteLine($"Weight : {result.weight_2} " + tc.MassUnitsLabel); sw.WriteLine($"\nTurns ratio : {result.turns_ratio}"); sw.WriteLine($"Wire c.s.a ratio : {result.wire_csa_ratio}"); sw.WriteLine($"Total weight : {result.wire_total_weight} " + tc.MassUnitsLabel); sw.WriteLine($"Weight ratio : {result.wire_weight_ratio}"); sw.WriteLine($"Output Power : {result.power_VA} VA"); sw.WriteLine($"Total equivalent R : {result.total_eq_R}"); sw.WriteLine($"% Regulation : {result.regulation}"); } if (result.warnings.Count > 0) { sw.WriteLine("\nWarnings:\n"); foreach (string msg in result.warnings) { sw.WriteLine(msg); } } } }
public trans_calc_result_text Calculate(trans_calc_input_text text_input) { _input.ConvertTextToInput(text_input); double L1; double Ae; CalculateCommon(out L1, out Ae); // Calculate primary bool retry_primary = false; int minAWG = 8; if (_input.primary.awg == null) { retry_primary = true; } if (_input.processSecondary) { CalculateTurnsRatio(); CalculatePrimaryCurrent(); } do { if (retry_primary) { _input.primary.awg = AutoSelectAWG(minAWG++, _input.isMinimizeRegulation, _input.primary, _result.Ip_full_load); } trans_calc_result_winding w1 = calculateWinding(_input.common, _input.primary); w1.L = L1; _result.primary = w1; //Calculate secondary if configured if (_input.processSecondary) { _result.Iout_max = _input.common.Iout_max; _input.common.Core_H += w1.thickness_mm / 1000; _input.common.Core_W += w1.thickness_mm / 1000; bool retry_secondary = false; minAWG = 8; if (_input.secondary.awg == null) { retry_secondary = true; } trans_calc_result_winding w2 = null; do { if (retry_secondary) { _input.secondary.awg = AutoSelectAWG(minAWG++, _input.isMinimizeRegulation, _input.secondary, _input.common.Iout_max); } w2 = CalculateSecondaryWithRetries(w1.resistance); if (retry_secondary && (_input.common.WindowSize < 0.000000001 || w1.thickness_mm + _input.common.InsulationThickness + w2.thickness_mm <= _input.common.WindowSize)) { retry_secondary = false; retry_primary = false; } } while (retry_secondary); if (_result.permeability > 0.00000000001 && _result.mpath_l_m > 0.0000001) { w2.L = w2.N * w2.N * _result.permeability * u0 * Ae / _result.mpath_l_m; } _result.secondary = w2; _result.total_thickness_mm = w1.thickness_mm + _input.common.InsulationThickness + w2.thickness_mm; _result.wire_total_weight = w1.mass + w2.mass; _result.wire_csa_ratio = w1.awg.Csa_m2 / w2.awg.Csa_m2; _result.wire_weight_ratio = w1.mass / w2.mass; } }while (retry_primary); ConvertUnits(); return(new trans_calc_result_text(_result, _input)); }
public void SaveSettings(trans_calc_input_text strin, string fileName) { _filePersistence.SaveSettings(strin, fileName); }
public void SaveResults(trans_calc_input_text strin, trans_calc_result_text result, string fileName) { _filePersistence.SaveResults(strin, result, fileName); }