// // Created by Administrator on 2026/1/12. // #include "computer.h" #include #include #include #include Computer::Computer() { } Computer::~Computer() { } Computer::Spectrum Computer::readSpectrumCSV(const QString& filename) { QFile file(filename); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Cannot open file:" << filename; return Spectrum(); } QTextStream stream(&file); QString line = stream.readLine(); // Skip header Spectrum spec; while (!stream.atEnd()) { line = stream.readLine(); QStringList cells = line.split(','); QVector row; for (const QString& cell : cells) { row.append(cell.toDouble()); } spec.wavelength.append(row[0]); // Average multiple intensity columns double sum = 0.0; for (int i = 1; i < row.size(); ++i) { sum += row[i]; } spec.intensity.append(sum / (row.size() - 1)); } return spec; } double Computer::computeElectronTemperature( const QVector& lines) { const double k = 8.617333262e-5; // eV/K int n = lines.size(); double sumX = 0, sumY = 0, sumXX = 0, sumXY = 0; for (const auto& l : lines) { double x = l.E; double y = std::log(l.intensity * l.wavelength_nm / (l.A * l.g)); sumX += x; sumY += y; sumXX += x * x; sumXY += x * y; } double slope = (n * sumXY - sumX * sumY) / (n * sumXX - sumX * sumX); return -1.0 / (k * slope); } // 计算电子密度 double Computer::computeElectronDensity(double fwhm_nm, double w) { return (fwhm_nm / (2.0 * w)) * 1e16; } // 计算电离度 double Computer::computeIonizationRate(double Te, double ne) { const double k = 8.617333262e-5; const double Ei = 5.98; double saha = 2.07e21 * std::pow(Te, 1.5) * std::exp(-Ei / (k * Te)); double ratio = saha / ne; return ratio / (1.0 + ratio); } // 计算粒子总数 double Computer::computeTotalParticleDensity(double ne, double ionRate) { return ne / ionRate; } Computer::PlasmaParams Computer::computePlasmaParams(const QVector& lines, double fwhm_nm, double stark_w) { PlasmaParams p; p.Te = computeElectronTemperature(lines); p.ne = computeElectronDensity(fwhm_nm, stark_w); p.ionRate = computeIonizationRate(p.Te, p.ne); p.Ntot = computeTotalParticleDensity(p.ne, p.ionRate); return p; } // 线性背景拟合 void Computer::fitLinearBackground(const QVector& x, const QVector& y, double& a, double& b) { double sx = 0, sy = 0, sxx = 0, sxy = 0; int n = x.size(); for (int i = 0; i < n; ++i) { sx += x[i]; sy += y[i]; sxx += x[i] * x[i]; sxy += x[i] * y[i]; } a = (n * sxy - sx * sy) / (n * sxx - sx * sx); b = (sy - a * sx) / n; } // 计算强度积分:梯形面积 double Computer::integratePeak(const Spectrum& spec, double peak_nm, double half_window_nm) { QVector x, y; for (int i = 0; i < spec.wavelength.size(); ++i) { double wl = spec.wavelength[i]; if (wl >= peak_nm - half_window_nm && wl <= peak_nm + half_window_nm) { x.append(wl); y.append(spec.intensity[i]); } } if (x.size() < 8) return 0.0; // 计算常数背景(10% 分位) QVector y_sorted = y; std::sort(y_sorted.begin(), y_sorted.end()); double bg = y_sorted[y_sorted.size() * 0.10]; // 梯形积分 double area = 0.0; for (int i = 1; i < x.size(); ++i) { double y1 = y[i - 1] - bg; double y2 = y[i] - bg; if (y1 < 0) y1 = 0; if (y2 < 0) y2 = 0; area += 0.5 * (y1 + y2) * (x[i] - x[i - 1]); } return area; } // 查找指定谱线 FWHM double Computer::computeFWHMForPeak(const QVector& wavelengths, const QVector& intensities, double lambda_target, double window) { // 截取窗口数据 QVector wl_seg, inten_seg; for (int i = 0; i < wavelengths.size(); ++i) { if (wavelengths[i] >= lambda_target - window && wavelengths[i] <= lambda_target + window) { wl_seg.append(wavelengths[i]); inten_seg.append(intensities[i]); } } if (wl_seg.isEmpty()) { qDebug() << "目标波长附近无数据"; return 0.0; } // 找到最大强度 auto max_it = std::max_element(inten_seg.constBegin(), inten_seg.constEnd()); double Imax = *max_it; double half_max = Imax / 2.0; // 找最大强度对应索引 int max_index = std::distance(inten_seg.constBegin(), max_it); // 找左半峰点 double lambda_left = wl_seg.first(); for (int i = max_index; i > 0; --i) { if (inten_seg[i-1] < half_max && inten_seg[i] >= half_max) { double slope = (inten_seg[i] - inten_seg[i-1]) / (wl_seg[i] - wl_seg[i-1]); lambda_left = wl_seg[i-1] + (half_max - inten_seg[i-1]) / slope; break; } } // 找右半峰点 double lambda_right = wl_seg.last(); for (int i = max_index; i < inten_seg.size() - 1; ++i) { if (inten_seg[i] >= half_max && inten_seg[i+1] < half_max) { double slope = (inten_seg[i+1] - inten_seg[i]) / (wl_seg[i+1] - wl_seg[i]); lambda_right = wl_seg[i] + (half_max - inten_seg[i]) / slope; break; } } return lambda_right - lambda_left; } Computer::PlasmaParams Computer::computeInter(QString filePath) { Spectrum spectrum = readSpectrumCSV(filePath); double I_237 = integratePeak(spectrum, 237.312); double I_257 = integratePeak(spectrum, 257.509); double I_308 = integratePeak(spectrum, 308.215); double I_309 = integratePeak(spectrum, 309.271); QVector lines; SpectralLine spectralLine1 = {237.312, I_237, 9.07e7 , 2 * 5/2 + 1, 42237.783/8065.54}; SpectralLine spectralLine2 = {257.509, I_257, 3.60e7, 2 * 5/2 + 1, 38933.968/8065.54 }; SpectralLine spectralLine3 = {308.215, I_308, 5.87e7, 2 * 3/2 + 1, 32435.453/8065.54}; SpectralLine spectralLine4 = {309.271, I_309, 7.29e7, 2 * 3/2 + 1, 32436.796/8065.54}; lines.append(spectralLine1); lines.append(spectralLine2); lines.append(spectralLine3); lines.append(spectralLine4); double FWHM_237 = computeFWHMForPeak(spectrum.wavelength, spectrum.intensity, 237.312); double FWHM_257 = computeFWHMForPeak(spectrum.wavelength, spectrum.intensity, 257.509); double FWHM_308 = computeFWHMForPeak(spectrum.wavelength, spectrum.intensity, 308.215); double FWHM_309 = computeFWHMForPeak(spectrum.wavelength, spectrum.intensity, 309.271); double stark_w = 0.00628; PlasmaParams p = computePlasmaParams(lines, FWHM_308, stark_w); qDebug() << "计算得到电子温度=" << p.Te; qDebug() << "电子密度=" << p.ne; qDebug() << "电离度=" << p.ionRate; qDebug() << "粒子总数=" << p.Ntot; return p; }