提交代码
This commit is contained in:
@@ -0,0 +1,416 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CfLibs
|
||||
{
|
||||
public partial class CfLibsIdentifier
|
||||
{
|
||||
// ===== 定量专用字段(与 C++ 全局变量 temprature / electroD / g_lastRsq 对应)=====
|
||||
private double _temperature; // 全局等离子体温度 K
|
||||
private double _electronDensity; // 全局电子密度 cm^-3
|
||||
private double _lastRsq; // 最近一次 Boltzmann 拟合 R²
|
||||
private double _forcedTemperature; // 强制温度(SetTemperature 用,0=自动)
|
||||
private bool _enableCalibration; // 标样校正开关
|
||||
private List<ElementData> _lastElem = new List<ElementData>();
|
||||
|
||||
// 懒加载缓存:避免每次定量都开文件
|
||||
private Dictionary<string, double> _ionizationEnergies;
|
||||
private Dictionary<string, double> _atomicWeights;
|
||||
|
||||
// ===== 对外只读属性 =====
|
||||
public double Temperature => _temperature;
|
||||
public double ElectronDensity => _electronDensity;
|
||||
|
||||
// 定量结果(对外的 MatchedLine 列表形式)
|
||||
public IReadOnlyList<ElementResult> LastElements
|
||||
{
|
||||
get
|
||||
{
|
||||
var list = new List<ElementResult>(_lastElem.Count);
|
||||
foreach (var e in _lastElem)
|
||||
{
|
||||
list.Add(new ElementResult
|
||||
{
|
||||
Element = e.EleName,
|
||||
Abundance = e.Abundence,
|
||||
RawAbundance = e.RawAbundence,
|
||||
AbundanceAtom = 0,
|
||||
Csa = e.Csa,
|
||||
Csit = e.Csit,
|
||||
Temperature = _temperature,
|
||||
ElectronDensity = _electronDensity
|
||||
});
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 配置 =====
|
||||
public void EnableCalibration(bool enable) => _enableCalibration = enable;
|
||||
|
||||
/// <summary>强制设置温度 K,0 = 使用自动计算</summary>
|
||||
public void SetTemperature(double t) => _forcedTemperature = t;
|
||||
|
||||
// =================================================================
|
||||
// Quantify 主入口(对应 CF_LIBS_Quantify)
|
||||
// =================================================================
|
||||
public int Quantify(SpectralLines lines, out Result outResult)
|
||||
{
|
||||
outResult = null;
|
||||
if (lines?.Lines == null) return -1;
|
||||
|
||||
// 1. 对外结构 → 内部 SpecData
|
||||
var spec = new List<SpecData>(lines.Lines.Count);
|
||||
foreach (var l in lines.Lines)
|
||||
{
|
||||
spec.Add(new SpecData
|
||||
{
|
||||
EleName = l.Element,
|
||||
WavelengthSource = l.WavelengthSource,
|
||||
WaveLength = l.Wavelength,
|
||||
Intensity = l.Intensity,
|
||||
TransPro = l.TransProb,
|
||||
UpperLevel = l.UpperLevel,
|
||||
Statis = l.StatWeight,
|
||||
Ionization = l.Ionization
|
||||
});
|
||||
}
|
||||
if (spec.Count == 0) return -1;
|
||||
|
||||
// 2. 三步核心计算
|
||||
var nameList = SpecNameLst(spec);
|
||||
AverageTemperElect(spec, nameList);
|
||||
var elem = CalculateConcentration(spec, nameList);
|
||||
|
||||
// 3. 低浓度过滤:<0.01% 归零
|
||||
foreach (var e in elem)
|
||||
if (e.Abundence < 0.01) e.Abundence = 0.0;
|
||||
|
||||
// 4. 输出
|
||||
outResult = new Result
|
||||
{
|
||||
Status = 0,
|
||||
ErrorMsg = "",
|
||||
Temperature = _temperature,
|
||||
ElectronDensity = _electronDensity,
|
||||
Elements = new List<ElementResult>(elem.Count)
|
||||
};
|
||||
foreach (var e in elem)
|
||||
{
|
||||
outResult.Elements.Add(new ElementResult
|
||||
{
|
||||
Element = e.EleName,
|
||||
Abundance = e.Abundence,
|
||||
RawAbundance = e.RawAbundence,
|
||||
AbundanceAtom = 0,
|
||||
Csa = e.Csa,
|
||||
Csit = e.Csit,
|
||||
Temperature = _temperature,
|
||||
ElectronDensity = _electronDensity
|
||||
});
|
||||
}
|
||||
|
||||
_lastElem = elem;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// specNameLst
|
||||
// =================================================================
|
||||
private static List<ElementCount> SpecNameLst(List<SpecData> specLst)
|
||||
{
|
||||
// 用 SortedDictionary 保证迭代顺序与 C++ std::map 一致
|
||||
var count = new SortedDictionary<string, int>();
|
||||
foreach (var s in specLst)
|
||||
{
|
||||
count.TryGetValue(s.EleName, out var c);
|
||||
count[s.EleName] = c + 1;
|
||||
}
|
||||
var list = new List<ElementCount>(count.Count);
|
||||
foreach (var kv in count)
|
||||
list.Add(new ElementCount { EleName = kv.Key, Count = kv.Value });
|
||||
return list;
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// averageTemperElect
|
||||
// =================================================================
|
||||
private void AverageTemperElect(List<SpecData> specLst, List<ElementCount> specName)
|
||||
{
|
||||
// ===== P0 优化:收集每个元素的 (T, R², 有效线数),R²·n 加权平均 =====
|
||||
var elemTemps = new List<(double T, double R2, int N)>();
|
||||
|
||||
// count1 与 C++ 一致:每处理完一个元素重置为 0
|
||||
int count1 = 0;
|
||||
const int casee = 1;
|
||||
var spect = new List<SpecData>();
|
||||
|
||||
foreach (var item in specName)
|
||||
{
|
||||
if (item.Count < 2) continue;
|
||||
|
||||
spect.Clear();
|
||||
for (int j = 0; j < item.Count; j++)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (count1 >= specLst.Count) break; // 防御:C++ 里没有但加安全
|
||||
if (specLst[count1].EleName == item.EleName)
|
||||
{
|
||||
spect.Add(specLst[count1]);
|
||||
count1++;
|
||||
break;
|
||||
}
|
||||
count1++;
|
||||
}
|
||||
}
|
||||
|
||||
CalculateElementTempne(spect, item, casee);
|
||||
// _temperature / _lastRsq 已由 CalculateElementTempne 更新
|
||||
|
||||
double rsqNow = _lastRsq;
|
||||
double tempNow = _temperature;
|
||||
|
||||
// 统一有效性判定:R² ≥ 0.3,温度 ∈ [3000, 25000]K
|
||||
if (rsqNow >= 0.3 && tempNow >= 3000 && tempNow <= 25000)
|
||||
{
|
||||
elemTemps.Add((tempNow, rsqNow, item.Count));
|
||||
}
|
||||
|
||||
count1 = 0;
|
||||
}
|
||||
|
||||
if (elemTemps.Count == 0)
|
||||
{
|
||||
_temperature = 10000.0;
|
||||
_electronDensity = 1.0e17;
|
||||
return;
|
||||
}
|
||||
|
||||
// ===== R² · n 加权平均(取代旧的等权 + Al 50/50 折中)=====
|
||||
double sumW = 0, sumWT = 0;
|
||||
foreach (var (T, R2, N) in elemTemps)
|
||||
{
|
||||
// 权重 = R² 贡献 + 有效谱线数贡献
|
||||
double w = Math.Max(0.0, R2) * Math.Max(1, N);
|
||||
sumW += w;
|
||||
sumWT += w * T;
|
||||
}
|
||||
|
||||
_temperature = sumW > 1e-12 ? sumWT / sumW : 10000.0;
|
||||
|
||||
// 简化 Saha:Ne ≈ 1e17·(T/1e4)^1.5
|
||||
_electronDensity = 1.0e17 * Math.Pow(_temperature / 10000.0, 1.5);
|
||||
|
||||
// 物理范围钳位(从 [8500, 12000] 放宽到 [5000, 20000])
|
||||
if (_temperature > 20000.0) _temperature = 20000.0;
|
||||
if (_temperature < 5000.0) _temperature = 5000.0;
|
||||
if (_electronDensity > 5.0e17) _electronDensity = 5.0e17;
|
||||
if (_electronDensity < 1.0e16) _electronDensity = 1.0e16;
|
||||
|
||||
// 物理范围钳位
|
||||
if (_temperature > 20000.0) _temperature = 20000.0;
|
||||
if (_temperature < 5000.0) _temperature = 5000.0;
|
||||
if (_electronDensity > 1.0e19) _electronDensity = 1.0e19;
|
||||
if (_electronDensity < 1.0e15) _electronDensity = 1.0e15;
|
||||
|
||||
// 强制温度覆盖(若启用)
|
||||
if (_forcedTemperature > 0)
|
||||
{
|
||||
_temperature = _forcedTemperature;
|
||||
_electronDensity = 1.0e17 * Math.Pow(_temperature / 10000.0, 1.5);
|
||||
}
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// calculateElementTempne — 单元素 Boltzmann 拟合 + Saha Ne
|
||||
// =================================================================
|
||||
private void CalculateElementTempne(List<SpecData> fixedLst, ElementCount specName, int casee)
|
||||
{
|
||||
const double Kb = 8.61738573e-5;
|
||||
|
||||
// 分原子线 / 离子线
|
||||
var atomLines = new List<SpecData>();
|
||||
var ionLines = new List<SpecData>();
|
||||
foreach (var ln in fixedLst)
|
||||
{
|
||||
if (ln.Ionization == 0) atomLines.Add(ln);
|
||||
else if (ln.Ionization == 1) ionLines.Add(ln);
|
||||
}
|
||||
|
||||
// 读取电离能
|
||||
double EiIon = 0.0;
|
||||
var eis = LoadIonizationEnergies();
|
||||
if (eis.TryGetValue(specName.EleName, out var eiVal)) EiIon = eiVal;
|
||||
|
||||
FitBoltzmann(atomLines, out double T_a, out double bsa_fit, out double R2_a, out int n_a, out double ekRa);
|
||||
FitBoltzmann(ionLines, out double T_i, out double bsi_fit, out double R2_i, out int n_i, out double ekRi);
|
||||
|
||||
// 决策
|
||||
const double minEkRange_a = 1.5;
|
||||
const double minEkRange_i = 2.0;
|
||||
const int minLines_a = 3;
|
||||
const int minLines_i = 4;
|
||||
|
||||
bool ok_a = T_a > 5000 && T_a < 20000 && R2_a > 0.5 && ekRa >= minEkRange_a && n_a >= minLines_a;
|
||||
bool ok_i = T_i > 5000 && T_i < 20000 && R2_i > 0.5 && ekRi >= minEkRange_i && n_i >= minLines_i;
|
||||
|
||||
double weight_a = ok_a ? R2_a * n_a * (ekRa + 0.1) : 0;
|
||||
double weight_i = ok_i ? R2_i * n_i * (ekRi + 0.1) : 0;
|
||||
|
||||
double T_final, R2_final;
|
||||
if (ok_a && ok_i)
|
||||
{
|
||||
T_final = (weight_a * T_a + weight_i * T_i) / (weight_a + weight_i);
|
||||
R2_final = (weight_a * R2_a + weight_i * R2_i) / (weight_a + weight_i);
|
||||
}
|
||||
else if (ok_a) { T_final = T_a; R2_final = R2_a; }
|
||||
else if (ok_i) { T_final = T_i; R2_final = R2_i; }
|
||||
else
|
||||
{
|
||||
int n = fixedLst.Count;
|
||||
T_final = 12000.0;
|
||||
R2_final = n >= 3 ? 0.15 : 0.1;
|
||||
if (T_final < 5000 || T_final > 25000) T_final = 12000;
|
||||
}
|
||||
|
||||
// ===== Ne 计算 =====
|
||||
double Ne_final;
|
||||
if (ok_a && ok_i && EiIon > 0)
|
||||
{
|
||||
double u0 = CalculateU(specName.EleName, T_final, 0);
|
||||
double u1 = CalculateU(specName.EleName, T_final, 1);
|
||||
if (u0 <= 0 || u1 <= 0) u0 = u1 = 1.0;
|
||||
|
||||
double arg = bsa_fit - bsi_fit - EiIon / (Kb * T_final);
|
||||
if (arg > 40) arg = 40;
|
||||
if (arg < -40) arg = -40;
|
||||
|
||||
Ne_final = 4.83e15 * Math.Sqrt(Math.Pow(T_final, 3)) * (u1 / u0) * Math.Exp(arg);
|
||||
if (Ne_final < 1e15) Ne_final = 1e15;
|
||||
if (Ne_final > 1e19) Ne_final = 1e19;
|
||||
}
|
||||
else
|
||||
{
|
||||
Ne_final = 1.0e17 * Math.Pow(T_final / 10000.0, 1.5);
|
||||
}
|
||||
|
||||
_temperature = T_final;
|
||||
_electronDensity = Ne_final;
|
||||
_lastRsq = R2_final;
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// fitBoltzmann — 4 轮 MAD 剔除 + 线性回归
|
||||
// =================================================================
|
||||
private static void FitBoltzmann(
|
||||
List<SpecData> lines,
|
||||
out double T_out, out double bs_out, out double R2_out,
|
||||
out int n_out, out double ekRange_out)
|
||||
{
|
||||
int n = lines.Count;
|
||||
if (n < 2)
|
||||
{
|
||||
T_out = 0; bs_out = 0; R2_out = 0; n_out = n; ekRange_out = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
var xx = new double[n];
|
||||
var yy = new double[n];
|
||||
double ekMin = 1e9, ekMax = -1e9;
|
||||
for (int k = 0; k < n; ++k)
|
||||
{
|
||||
yy[k] = Math.Log(lines[k].Intensity * lines[k].WaveLength * 1e7
|
||||
/ (lines[k].Statis * lines[k].TransPro));
|
||||
xx[k] = lines[k].UpperLevel;
|
||||
if (xx[k] < ekMin) ekMin = xx[k];
|
||||
if (xx[k] > ekMax) ekMax = xx[k];
|
||||
}
|
||||
ekRange_out = ekMax - ekMin;
|
||||
|
||||
var valid = new bool[n];
|
||||
for (int k = 0; k < n; ++k) valid[k] = true;
|
||||
|
||||
double best_slope = 0, best_intercept = 0, best_R2 = 0;
|
||||
|
||||
for (int it = 0; it < 4; ++it)
|
||||
{
|
||||
double sx = 0, sx2 = 0, sy = 0, sxy = 0;
|
||||
int nv = 0;
|
||||
for (int k = 0; k < n; ++k)
|
||||
{
|
||||
if (!valid[k]) continue;
|
||||
sx += xx[k]; sx2 += xx[k] * xx[k];
|
||||
sy += yy[k]; sxy += xx[k] * yy[k]; nv++;
|
||||
}
|
||||
if (nv < 2) break;
|
||||
|
||||
double d = nv * sx2 - sx * sx;
|
||||
if (Math.Abs(d) < 1e-12) break;
|
||||
double slope = (nv * sxy - sx * sy) / d;
|
||||
double intercept = (sy - slope * sx) / nv;
|
||||
|
||||
// 残差中位数 + MAD
|
||||
var resid = new List<double>(nv);
|
||||
for (int k = 0; k < n; ++k)
|
||||
if (valid[k]) resid.Add(yy[k] - (slope * xx[k] + intercept));
|
||||
resid.Sort();
|
||||
int m = resid.Count;
|
||||
double median_r = (m % 2 == 1) ? resid[m / 2]
|
||||
: 0.5 * (resid[m / 2 - 1] + resid[m / 2]);
|
||||
var absdev = new List<double>(m);
|
||||
foreach (double r in resid) absdev.Add(Math.Abs(r - median_r));
|
||||
absdev.Sort();
|
||||
double mad = (m % 2 == 1) ? absdev[m / 2]
|
||||
: 0.5 * (absdev[m / 2 - 1] + absdev[m / 2]);
|
||||
if (mad < 0.1) mad = 0.1;
|
||||
double thr = 2.0 * mad;
|
||||
|
||||
// 剔除(前 3 条不动,保守)
|
||||
int removed = 0, remaining = 0;
|
||||
for (int k = 0; k < n; ++k)
|
||||
{
|
||||
if (!valid[k]) continue;
|
||||
double r = yy[k] - (slope * xx[k] + intercept);
|
||||
if (Math.Abs(r - median_r) > thr && remaining > 2)
|
||||
{
|
||||
valid[k] = false; removed++;
|
||||
}
|
||||
else remaining++;
|
||||
}
|
||||
|
||||
best_slope = slope;
|
||||
best_intercept = intercept;
|
||||
|
||||
// R²
|
||||
double meany = sy / nv;
|
||||
double ss_tot = 0, ss_res = 0;
|
||||
for (int k = 0; k < n; ++k)
|
||||
{
|
||||
if (!valid[k]) continue;
|
||||
ss_tot += Math.Pow(yy[k] - meany, 2);
|
||||
ss_res += Math.Pow(yy[k] - (slope * xx[k] + intercept), 2);
|
||||
}
|
||||
best_R2 = ss_tot > 1e-12 ? 1 - ss_res / ss_tot : 0;
|
||||
|
||||
if (removed == 0) break;
|
||||
}
|
||||
|
||||
if (best_slope >= 0)
|
||||
{
|
||||
T_out = 0; bs_out = 0; R2_out = 0; n_out = n;
|
||||
return;
|
||||
}
|
||||
|
||||
T_out = -1 / best_slope * 11604.45; // eV → K
|
||||
bs_out = best_intercept;
|
||||
R2_out = best_R2;
|
||||
int nv_final = 0;
|
||||
foreach (bool v in valid) if (v) nv_final++;
|
||||
n_out = nv_final;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user