using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CfLibs { public partial class CfLibsIdentifier { // ===== 经验配分函数表(T=10000K 参考值 + 温度系数)===== private static readonly Dictionary ExpUI_a = new Dictionary(); private static readonly Dictionary ExpUI_b = new Dictionary(); private static readonly Dictionary ExpUI_c = new Dictionary(); private static readonly Dictionary ExpUII_a = new Dictionary(); private static readonly Dictionary ExpUII_b = new Dictionary(); private static readonly Dictionary ExpUII_c = new Dictionary(); static CfLibsIdentifier() { ExpUI_a["Al"] = 5.85; ExpUI_b["Al"] = 0.10; ExpUI_c["Al"] = 0.000; ExpUI_a["Cu"] = 1.97; ExpUI_b["Cu"] = 0.02; ExpUI_c["Cu"] = 0.000; ExpUI_a["Fe"] = 26.4; ExpUI_b["Fe"] = 0.50; ExpUI_c["Fe"] = 0.020; ExpUI_a["Mg"] = 1.10; ExpUI_b["Mg"] = 0.00; ExpUI_c["Mg"] = 0.000; ExpUI_a["Si"] = 9.45; ExpUI_b["Si"] = 0.30; ExpUI_c["Si"] = 0.010; ExpUI_a["Ca"] = 1.32; ExpUI_b["Ca"] = 0.01; ExpUI_c["Ca"] = 0.000; ExpUI_a["Mn"] = 6.20; ExpUI_b["Mn"] = 0.15; ExpUI_c["Mn"] = 0.005; ExpUI_a["Zn"] = 1.05; ExpUI_b["Zn"] = 0.00; ExpUI_c["Zn"] = 0.000; ExpUI_a["Ni"] = 28.5; ExpUI_b["Ni"] = 0.50; ExpUI_c["Ni"] = 0.020; ExpUI_a["Ti"] = 21.5; ExpUI_b["Ti"] = 0.50; ExpUI_c["Ti"] = 0.020; ExpUI_a["Cr"] = 9.30; ExpUI_b["Cr"] = 0.20; ExpUI_c["Cr"] = 0.005; ExpUI_a["Pb"] = 1.90; ExpUI_b["Pb"] = 0.02; ExpUI_c["Pb"] = 0.000; ExpUI_a["Sn"] = 2.40; ExpUI_b["Sn"] = 0.05; ExpUI_c["Sn"] = 0.000; ExpUI_a["Na"] = 2.05; ExpUI_b["Na"] = 0.00; ExpUI_c["Na"] = 0.000; ExpUI_a["K"] = 2.10; ExpUI_b["K"] = 0.00; ExpUI_c["K"] = 0.000; ExpUI_a["Sr"] = 1.55; ExpUI_b["Sr"] = 0.02; ExpUI_c["Sr"] = 0.000; ExpUI_a["Zr"] = 25.0; ExpUI_b["Zr"] = 0.50; ExpUI_c["Zr"] = 0.020; ExpUI_a["Sb"] = 4.60; ExpUI_b["Sb"] = 0.05; ExpUI_c["Sb"] = 0.000; ExpUI_a["Be"] = 1.05; ExpUI_b["Be"] = 0.00; ExpUI_c["Be"] = 0.000; ExpUI_a["C"] = 9.30; ExpUI_b["C"] = 0.10; ExpUI_c["C"] = 0.000; ExpUII_a["Al"] = 1.00; ExpUII_b["Al"] = 0.00; ExpUII_c["Al"] = 0.000; ExpUII_a["Cu"] = 1.10; ExpUII_b["Cu"] = 0.00; ExpUII_c["Cu"] = 0.000; ExpUII_a["Fe"] = 31.5; ExpUII_b["Fe"] = 0.60; ExpUII_c["Fe"] = 0.020; ExpUII_a["Mg"] = 2.05; ExpUII_b["Mg"] = 0.02; ExpUII_c["Mg"] = 0.000; ExpUII_a["Si"] = 9.20; ExpUII_b["Si"] = 0.20; ExpUII_c["Si"] = 0.005; ExpUII_a["Ca"] = 1.85; ExpUII_b["Ca"] = 0.02; ExpUII_c["Ca"] = 0.000; ExpUII_a["Mn"] = 7.20; ExpUII_b["Mn"] = 0.15; ExpUII_c["Mn"] = 0.005; ExpUII_a["Zn"] = 2.00; ExpUII_b["Zn"] = 0.02; ExpUII_c["Zn"] = 0.000; ExpUII_a["Ni"] = 32.0; ExpUII_b["Ni"] = 0.60; ExpUII_c["Ni"] = 0.020; ExpUII_a["Ti"] = 31.5; ExpUII_b["Ti"] = 0.70; ExpUII_c["Ti"] = 0.020; ExpUII_a["Cr"] = 11.5; ExpUII_b["Cr"] = 0.20; ExpUII_c["Cr"] = 0.005; ExpUII_a["Pb"] = 2.40; ExpUII_b["Pb"] = 0.02; ExpUII_c["Pb"] = 0.000; ExpUII_a["Sn"] = 3.20; ExpUII_b["Sn"] = 0.05; ExpUII_c["Sn"] = 0.000; ExpUII_a["Na"] = 1.00; ExpUII_b["Na"] = 0.00; ExpUII_c["Na"] = 0.000; ExpUII_a["K"] = 1.00; ExpUII_b["K"] = 0.00; ExpUII_c["K"] = 0.000; ExpUII_a["Sr"] = 2.10; ExpUII_b["Sr"] = 0.02; ExpUII_c["Sr"] = 0.000; ExpUII_a["Zr"] = 28.0; ExpUII_b["Zr"] = 0.50; ExpUII_c["Zr"] = 0.020; ExpUII_a["Sb"] = 5.00; ExpUII_b["Sb"] = 0.05; ExpUII_c["Sb"] = 0.000; ExpUII_a["Be"] = 1.05; ExpUII_b["Be"] = 0.00; ExpUII_c["Be"] = 0.000; ExpUII_a["C"] = 5.95; ExpUII_b["C"] = 0.05; ExpUII_c["C"] = 0.000; } // ================================================================= // calculateU // ================================================================= private double CalculateU(string element, double temperature, int ionization) { double Tk = temperature / 1000.0; if (ionization == 0) { if (ExpUI_a.TryGetValue(element, out var a) && ExpUI_b.TryGetValue(element, out var b) && ExpUI_c.TryGetValue(element, out var c)) return a + b * Tk + c * Tk * Tk; } else { if (ExpUII_a.TryGetValue(element, out var a) && ExpUII_b.TryGetValue(element, out var b) && ExpUII_c.TryGetValue(element, out var c)) return a + b * Tk + c * Tk * Tk; } // 兜底:从 db/*I.dat 或 *II.dat 读 string file = GetPath(element + (ionization == 0 ? "I.dat" : "II.dat")); if (!File.Exists(file)) return 0; var gew = new List(); var gei = new List(); foreach (var raw in File.ReadLines(file)) { var parts = raw.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) continue; if (double.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out var g) && double.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out var e)) { gew.Add(g); gei.Add(e); } } const double Kb = 8.61738573e-5; double U = 0; for (int j = 0; j < gew.Count; j++) U += (2.0 * gew[j] + 1.0) * Math.Exp(-gei[j] / (Kb * temperature)); return U; } // ================================================================= // isSelfAbsorbedLine — 硬编码共振线表 // ================================================================= private static bool IsSelfAbsorbedLine(string elem, double wavelength, int ionization, double lineIntensity, double majorLineIntensity) { if (ionization != 0) return false; // 痕量元素不标记(<参考线 5%) if (majorLineIntensity > 0 && lineIntensity < 0.05 * majorLineIntensity) return false; if (elem == "Al") { if (Math.Abs(wavelength - 394.40) < 0.8 || Math.Abs(wavelength - 396.15) < 0.8) return true; if (Math.Abs(wavelength - 308.22) < 0.5 || Math.Abs(wavelength - 309.27) < 0.5) if (majorLineIntensity > 0 && lineIntensity > 0.30 * majorLineIntensity) return true; } if (elem == "Ca") { if (Math.Abs(wavelength - 422.67) < 0.8 || Math.Abs(wavelength - 430.25) < 0.8) return true; if (Math.Abs(wavelength - 442.54) < 0.8) return true; if (Math.Abs(wavelength - 393.37) < 0.8 || Math.Abs(wavelength - 396.85) < 0.8) return true; } if (elem == "Na") { if (Math.Abs(wavelength - 589.23) < 0.5 || Math.Abs(wavelength - 589.86) < 0.5) return true; } if (elem == "Mg") { if (Math.Abs(wavelength - 285.26) < 0.5) return true; if (Math.Abs(wavelength - 279.53) < 0.5 || Math.Abs(wavelength - 280.25) < 0.5) return true; } if (elem == "K") { if (Math.Abs(wavelength - 766.5) < 0.5 || Math.Abs(wavelength - 769.9) < 0.5) return true; } if (elem == "Pb") { if (Math.Abs(wavelength - 368.34) < 0.8 || Math.Abs(wavelength - 405.78) < 0.8) return true; } if (elem == "Ni") { if (Math.Abs(wavelength - 341.48) < 0.5) return true; } return false; } // ================================================================= // detectSelfAbsorptionByResiduals // ================================================================= private List DetectSelfAbsorptionByResiduals( List atomLines, out List bsValues, out List residuals, out double rmse) { var result = new List(); bsValues = new List(); residuals = new List(); rmse = 0; if (atomLines.Count < 3) return result; const double Kb = 8.61738573e-5; double invKt = 1.0 / (Kb * _temperature); foreach (var line in atomLines) { double arg = line.Intensity * line.WaveLength / (line.Statis * line.TransPro); bsValues.Add(arg <= 0 ? 0 : Math.Log(arg) + line.UpperLevel * invKt); } double sum_x = 0, sum_x2 = 0, sum_y = 0, sum_xy = 0; int n = 0; for (int i = 0; i < atomLines.Count; i++) { if (bsValues[i] == 0) continue; sum_x += atomLines[i].UpperLevel; sum_x2 += atomLines[i].UpperLevel * atomLines[i].UpperLevel; sum_y += bsValues[i]; sum_xy += atomLines[i].UpperLevel * bsValues[i]; n++; } if (n < 3) return result; double denom = n * sum_x2 - sum_x * sum_x; if (Math.Abs(denom) < 1e-12) return result; double slope = (n * sum_xy - sum_x * sum_y) / denom; double intercept = (sum_y - slope * sum_x) / n; double sum_res2 = 0; for (int i = 0; i < atomLines.Count; i++) { if (bsValues[i] == 0) { residuals.Add(0); continue; } double fitted = slope * atomLines[i].UpperLevel + intercept; double res = bsValues[i] - fitted; residuals.Add(res); sum_res2 += res * res; } rmse = Math.Sqrt(sum_res2 / n); double threshold = -2.0 * rmse; for (int i = 0; i < residuals.Count; i++) if (residuals[i] < threshold && residuals[i] < 0) result.Add(i); return result; } // ================================================================= // MarkResidualSelfAbsorbed — DetectSelfAbsorptionByResiduals + 全局索引回映射 // ================================================================= private void MarkResidualSelfAbsorbed( List subset, List fullList, Dictionary> residualSelfAbsorbed) { var saIdx = DetectSelfAbsorptionByResiduals(subset, out _, out _, out _); foreach (int localIdx in saIdx) { if (localIdx < 0 || localIdx >= subset.Count) continue; var sa = subset[localIdx]; // 全局回映射 for (int j = 0; j < fullList.Count; j++) { if (fullList[j].EleName == sa.EleName && fullList[j].Ionization == sa.Ionization && Math.Abs(fullList[j].WaveLength - sa.WaveLength) < 0.01 && Math.Abs(fullList[j].Intensity - sa.Intensity) < 0.01) { if (!residualSelfAbsorbed.TryGetValue(sa.EleName, out var set)) { set = new HashSet(); residualSelfAbsorbed[sa.EleName] = set; } set.Add(j); break; } } } } // ================================================================= // 数据加载(懒缓存) // ================================================================= private Dictionary LoadIonizationEnergies() { if (_ionizationEnergies != null) return _ionizationEnergies; _ionizationEnergies = new Dictionary(); string file = GetPath("ionization energy"); if (!File.Exists(file)) return _ionizationEnergies; foreach (var raw in File.ReadLines(file)) { var parts = raw.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) continue; if (double.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out var v)) _ionizationEnergies[parts[0]] = v; } return _ionizationEnergies; } private Dictionary LoadAtomicWeights() { if (_atomicWeights != null) return _atomicWeights; _atomicWeights = new Dictionary(); string file = GetPath("atomic weight"); if (!File.Exists(file)) return _atomicWeights; foreach (var raw in File.ReadLines(file)) { var parts = raw.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) continue; if (double.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out var v)) _atomicWeights[parts[0]] = v; } return _atomicWeights; } // ===== 备用:Saha / CS 法(保持对外一致)===== private double Saha(string element, double temperature, double electronDensity, double csa) { double Ei = LoadIonizationEnergies().TryGetValue(element, out var v) ? v : 0; if (Ei <= 0 || electronDensity <= 0 || temperature <= 0) return 0; double u0 = CalculateU(element, temperature, 0); double u1 = CalculateU(element, temperature, 1); if (u0 <= 0 || u1 <= 0) return 0; const double Kb = 8.6173303e-5; double expArg = -Ei / (Kb * temperature); if (expArg < -700) return 0; if (expArg > 700) expArg = 700; double csit = 4.83e15 * Math.Sqrt(Math.Pow(temperature, 3)) / electronDensity * (u1 / u0) * Math.Exp(expArg) * csa; if (double.IsNaN(csit) || csit < 0 || csit > 1e20) return 0; return csit; } private double CalculateConcentrationByIntercept( double inten, double transpro, double upplevel, double statis, double waveLength, double temperature) { const double Kb = 8.61738573e-5; if (transpro <= 0 || statis <= 0 || temperature <= 0 || inten <= 0 || waveLength <= 0) return 0; double arg = inten * waveLength / (transpro * statis); if (arg <= 0) return 0; return Math.Log(arg) + upplevel / (Kb * temperature); } private double CalculateConcentrationByCS( string element, double temperature, int ionization, double electronDensity, double bs) { if (bs == 0 || electronDensity <= 0 || temperature <= 0) return 0; if (bs > 200.0) bs = 200.0; if (bs < -200.0) return 0; double u = CalculateU(element, temperature, ionization); if (u <= 0) return 0; double cs = u * Math.Exp(bs); if (double.IsNaN(cs) || cs > 1e20) return 0; return cs; } } }