1208 lines
37 KiB
C++
1208 lines
37 KiB
C++
#include <QDebug>
|
|
#include "elementanalysis.h"
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <algorithm>
|
|
#include <iomanip>
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
#include <QtMath>
|
|
#include <iostream>
|
|
#include <unordered_map>
|
|
#include "algorithmanalysis.h"
|
|
#include "specpeakservice.h"
|
|
#include <chrono>
|
|
|
|
using namespace std;
|
|
|
|
|
|
ElementAnalysis::ElementAnalysis(QObject *parent) : QObject(parent)
|
|
{
|
|
|
|
}
|
|
|
|
// 执行最小二乘法拟合
|
|
FitParameters ElementAnalysis::fitCurve(string elementName) {
|
|
//还需要Hg、Cd、As
|
|
// if(elementName=="Cu"){
|
|
// return { 0.9264999 , 2076.25141 };
|
|
// }
|
|
// else if(elementName=="Si"){
|
|
// return { 0.08158917 , 4132.57647 };
|
|
// }
|
|
// else if(elementName=="Mg"){
|
|
// return { 5.8795378 , 23241.37291 };
|
|
// }
|
|
// else if(elementName=="Fe"){
|
|
// return { 0.1099767 , 1399.90221 };
|
|
// }
|
|
// else if(elementName=="Sn"){
|
|
// return { 0.381702443 , 1508.40109 };
|
|
// }
|
|
// else if(elementName=="Cr"){
|
|
// return { 2.361287245 , 17588.47379 };
|
|
// }
|
|
// else if(elementName=="Ca"){
|
|
// return { 4.154634269 , 1471.77325 };
|
|
// }
|
|
// else if(elementName=="Ti"){
|
|
// return { 0.735593449 , 10366.2212 };
|
|
// }
|
|
// else{
|
|
// return {0 , 0};
|
|
// }
|
|
if(elementName=="Mg"){
|
|
return { 0.535593449 , 300.2212 };
|
|
}
|
|
else{
|
|
return {0 , 0};
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
* double转换为string
|
|
|
|
*/
|
|
|
|
string convertToString(double d) {
|
|
|
|
ostringstream os;
|
|
|
|
if (os << d)
|
|
|
|
return os.str();
|
|
|
|
return "invalid conversion";
|
|
|
|
}
|
|
|
|
vector<SpectrumElementQuantity> ElementAnalysis::analysisElementQuantity(vector<pair<int,vector<SpectrumChannelData>>> channelData)
|
|
{
|
|
//qDebug() << "[Logic]-元素分析计算" << endl;
|
|
|
|
|
|
SpecPeakService specPeakService;
|
|
|
|
//将获得的原始数据进行裁剪
|
|
|
|
|
|
|
|
for (auto& specPair : channelData) {
|
|
std::vector<SpectrumChannelData>& spec = specPair.second;
|
|
spec = vector<SpectrumChannelData>(spec.begin() + 15, spec.end() - 15);
|
|
}
|
|
//调用SpecPeakService类中算法进行寻找峰值下标
|
|
//将峰值下标转化为波长和强度
|
|
vector<pair<int, vector<SpectrumChannelData>>> everyPeakVector;
|
|
try {
|
|
for(auto oneChannel:channelData){
|
|
vector<int> oneMergeIndex = specPeakService.SpecPeak(oneChannel.second);
|
|
vector<SpectrumChannelData> onePeakVector = specPeakService.mapInvertInt(oneChannel.second,oneMergeIndex);
|
|
everyPeakVector.push_back(std::make_pair(oneChannel.first, onePeakVector));
|
|
}
|
|
} catch (invalid_argument) {
|
|
|
|
throw std::runtime_error(createErrorMessage(1, "谱线分析失败,请重新测量"));
|
|
|
|
}
|
|
// vector<SpectrumChannelData> peakVector1 = AlgorithmAnalysis::findPeakList(channelData1, 1);
|
|
// vector<SpectrumChannelData> peakVector2 = AlgorithmAnalysis::findPeakList(channelData2, 2);
|
|
// vector<SpectrumChannelData> peakVector3 = AlgorithmAnalysis::findPeakList(channelData3, 3);
|
|
|
|
//删除重叠区域的峰值
|
|
vector<SpectrumChannelData> peakVector = selectWavelength(everyPeakVector, 0.05);
|
|
|
|
|
|
|
|
|
|
//将峰值数据与曲线拟合的元素波长进行对比,寻找需要检测的元素
|
|
vector<SpectrumElementPeak> elementList = AlgorithmAnalysis::findElement(peakVector);
|
|
|
|
|
|
// vector<string> elementNameLst(AlgorithmAnalysis::elements.size());
|
|
|
|
// for (int i = 0; i < AlgorithmAnalysis::elements.size(); i++) {
|
|
// elementNameLst[i] = AlgorithmAnalysis::elements[i].name;
|
|
// }
|
|
|
|
|
|
|
|
|
|
vector<SpectrumElementQuantity> contentLst;
|
|
vector<SpectrumElementQuantity> contentCFLst;
|
|
vector<SpectrumElementQuantity> contentStandLst;
|
|
|
|
isCompletedCalculateByStandardCurve =false;
|
|
isCompletedCalculateByCFLibs = false;
|
|
|
|
vector<SpectrumElementQuantity> elementQuantityStandardLst;
|
|
vector<SpectrumElementQuantity> elementQuantityCFLibsLst;
|
|
|
|
//根据寻找的元素进行曲线拟合得到元素的PPM
|
|
if(!isCompletedCalculateByStandardCurve){
|
|
elementQuantityStandardLst = calculateElementConcentrationByStandardCurve(elementList);
|
|
}
|
|
|
|
|
|
//根据峰值信息利用CF-LIBS算法计算得到元素的百分比
|
|
if(!isCompletedCalculateByCFLibs){
|
|
elementQuantityCFLibsLst = calculateElementConcentrationByCFLibs(peakVector);
|
|
}
|
|
|
|
//元素信息整合
|
|
for(int i = 0 ; i < elementQuantityCFLibsLst.size(); i++){
|
|
contentLst.push_back({elementQuantityCFLibsLst[i].name,NULL, elementQuantityCFLibsLst[i].quantity});
|
|
|
|
}
|
|
|
|
if(!elementQuantityStandardLst.empty()){
|
|
double totalNumber = 0.0;
|
|
for (auto elementStandarLst : elementQuantityStandardLst){
|
|
for(int i = 0 ; i < contentLst.size(); i++ ){
|
|
if( elementStandarLst.name == "Mg" && elementStandarLst.name == contentLst[i].name ){
|
|
totalNumber = elementStandarLst.quantity / contentLst[i].percent;
|
|
totalNumber = int(totalNumber);
|
|
}
|
|
}
|
|
}
|
|
for(auto &totalLst : contentLst){
|
|
totalLst.quantity = totalLst.percent * totalNumber;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentLst;
|
|
}
|
|
|
|
std::string ElementAnalysis::createErrorMessage(int errorCode, const std::string& message)
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
oss << errorCode << ": " << message;
|
|
return oss.str();
|
|
|
|
}
|
|
//曲线拟合计算含量
|
|
vector<SpectrumElementQuantity> ElementAnalysis::calculateElementConcentrationByStandardCurve(vector<SpectrumElementPeak> peakLst){
|
|
//qDebug() << "[Logic]-曲线拟合计算含量" << endl;
|
|
|
|
vector<SpectrumElementQuantity> elementQuantityLst;
|
|
|
|
for(int i = 0 ; i < peakLst.size(); i++)
|
|
{
|
|
|
|
// 执行拟合
|
|
FitParameters fitParams = fitCurve(peakLst[i].elementName);
|
|
// 输出拟合结果
|
|
cout << "拟合参数:" << endl;
|
|
cout << "斜率(强度): " << fitParams.slope << endl;
|
|
cout << "截距(浓度): " << fitParams.intercept << endl;
|
|
cout << "函数1 " << " 的关系式为:" << "y=" << fitParams.slope << "*x+" << fitParams.intercept << endl;
|
|
|
|
double c_dectect = (peakLst[i].intensity - fitParams.intercept) / fitParams.slope ;
|
|
if (c_dectect - 0.0 < 1e-30)
|
|
c_dectect = 0 ;
|
|
|
|
elementQuantityLst.push_back({peakLst[i].elementName,c_dectect});
|
|
}
|
|
isCompletedCalculateByStandardCurve = true;
|
|
return elementQuantityLst;
|
|
|
|
|
|
}
|
|
|
|
void ElementAnalysis::save(vector<SpectrumChannelData> peakVector)
|
|
{
|
|
QFile datafile;
|
|
QString strFileName = "home/pi/Desktop/libs/peakVector.txt";
|
|
datafile.setFileName(strFileName);
|
|
if(datafile.exists())
|
|
{
|
|
|
|
datafile.remove();
|
|
}
|
|
datafile.open(QIODevice::WriteOnly);
|
|
|
|
QTextStream csvOutPut(&datafile);
|
|
csvOutPut.setGenerateByteOrderMark(true);
|
|
QString strData =QString::fromUtf8("");
|
|
|
|
|
|
|
|
for(int i=0;i < peakVector.size();i++)
|
|
{
|
|
QString temp=QString::number(peakVector[i].wavelength,'f',2) + "," + QString::number(peakVector[i].wavelength,'f',2) + "\n";
|
|
strData += temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strData=strData.left(strData.length()-1)+"\n";
|
|
|
|
|
|
|
|
|
|
csvOutPut << strData.toLocal8Bit();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
datafile.close();
|
|
}
|
|
void ElementAnalysis::saveSpecData(vector<SpecData> comparedLst)
|
|
{
|
|
QFile datafile;
|
|
QString strFileName = "home/pi/Desktop/libs/comparedLst.txt";
|
|
datafile.setFileName(strFileName);
|
|
if(datafile.exists())
|
|
{
|
|
|
|
datafile.remove();
|
|
}
|
|
datafile.open(QIODevice::WriteOnly);
|
|
|
|
QTextStream csvOutPut(&datafile);
|
|
csvOutPut.setGenerateByteOrderMark(true);
|
|
QString strData =QString::fromUtf8("");
|
|
|
|
|
|
|
|
for(int i=0;i < comparedLst.size();i++)
|
|
{
|
|
|
|
QString temp= QString::fromStdString(comparedLst[i].eleName) + ","
|
|
+ QString::number(comparedLst[i].waveLength,'f',2) + ","
|
|
+ QString::number(comparedLst[i].intensity,'f',2) + ","
|
|
+ QString::number(comparedLst[i].transpro,'f',2) + ","
|
|
+ QString::number(comparedLst[i].upplevel,'f',2) + ","
|
|
+ QString::number(comparedLst[i].statis,'f',2) + ","
|
|
+ QString::number(comparedLst[i].ionization) + "\n";
|
|
|
|
|
|
strData += temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strData=strData.left(strData.length()-1)+"\n";
|
|
|
|
|
|
|
|
|
|
csvOutPut << strData.toLocal8Bit();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
datafile.close();
|
|
}
|
|
void ElementAnalysis::saveTTname(vector<TTname> Telementname)
|
|
{
|
|
QFile datafile;
|
|
QString strFileName = "home/pi/Desktop/libs/TTname.txt";
|
|
datafile.setFileName(strFileName);
|
|
if(datafile.exists())
|
|
{
|
|
|
|
datafile.remove();
|
|
}
|
|
datafile.open(QIODevice::WriteOnly);
|
|
|
|
QTextStream csvOutPut(&datafile);
|
|
csvOutPut.setGenerateByteOrderMark(true);
|
|
QString strData =QString::fromUtf8("");
|
|
|
|
|
|
|
|
for(int i=0;i < Telementname.size();i++)
|
|
{
|
|
|
|
QString temp= QString::fromStdString(Telementname[i].Telementname) + ","
|
|
+ QString::number(Telementname[i].lines,'f',2) + "\n";
|
|
|
|
|
|
|
|
strData += temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strData=strData.left(strData.length()-1)+"\n";
|
|
|
|
|
|
|
|
|
|
csvOutPut << strData.toLocal8Bit();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
datafile.close();
|
|
}
|
|
void ElementAnalysis::saveElementLst(vector<ElementData> elementLst)
|
|
{
|
|
QFile datafile;
|
|
QString strFileName = "home/pi/Desktop/libs/elementLst.txt";
|
|
datafile.setFileName(strFileName);
|
|
if(datafile.exists())
|
|
{
|
|
|
|
datafile.remove();
|
|
}
|
|
datafile.open(QIODevice::WriteOnly);
|
|
|
|
QTextStream csvOutPut(&datafile);
|
|
csvOutPut.setGenerateByteOrderMark(true);
|
|
QString strData =QString::fromUtf8("");
|
|
|
|
|
|
|
|
for(int i=0;i < elementLst.size();i++)
|
|
{
|
|
|
|
QString temp= QString::fromStdString(elementLst[i].eleName) + ","
|
|
+ QString::number(elementLst[i].atomNum) + ","
|
|
+ QString::number(elementLst[i].ionNum) + ","
|
|
+ QString::number(elementLst[i].atomNumCS) + ","
|
|
+ QString::number(elementLst[i].ionNumCS) + ","
|
|
+ QString::number(elementLst[i].bsa) + ","
|
|
+ QString::number(elementLst[i].bsi) + ","
|
|
+ QString::number(elementLst[i].csa) + ","
|
|
+ QString::number(elementLst[i].csi) + ","
|
|
+ QString::number(elementLst[i].csit) + ","
|
|
+ QString::number(elementLst[i].abundence) + "\n";
|
|
|
|
|
|
|
|
strData += temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strData=strData.left(strData.length()-1)+"\n";
|
|
|
|
|
|
|
|
|
|
csvOutPut << strData.toLocal8Bit();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
datafile.close();
|
|
}
|
|
//CF计算元素含量百分比
|
|
vector<SpectrumElementQuantity> ElementAnalysis::calculateElementConcentrationByCFLibs(vector<SpectrumChannelData> peakVector){
|
|
//qDebug() << "[Logic]-CF计算元素含量百分比" << endl;
|
|
|
|
//vector<SpecData> peakLst = AlgorithmAnalysis::findPeak(backgroudVector);
|
|
|
|
|
|
// 2 输入 比对库:200行,7列,输出 到比对库的数据,16type
|
|
string compareFileName = "/home/pi/SpecData/test2.txt";
|
|
vector<SpecData> compareLst = findCompareLst(compareFileName);
|
|
|
|
//
|
|
|
|
|
|
//获取元素集合
|
|
vector<ElementData> elementLst = getElementLst(compareLst);
|
|
|
|
|
|
|
|
// 3 输入 1中波长和2中波长,输出 差值小于0.02的波长(2中的波长值), 1中的强度,2中的其他字段
|
|
double distance = 0.05;
|
|
|
|
|
|
vector<SpecData> comparedLst = doCompare(peakVector,compareLst, distance);
|
|
|
|
|
|
vector<TTname> Telementname = GetTTname(elementLst, comparedLst);
|
|
|
|
|
|
// 4 输入 3中的差值比较后的光谱数据,修正数据库,输出 修正后的强度
|
|
|
|
// int gratingNo = 2;
|
|
// vector<FixData> fixLst = getFixLst(gratingNo);
|
|
|
|
|
|
|
|
|
|
// double baseline = 0;
|
|
// fixIntensity(comparedLst, fixLst, baseline);
|
|
|
|
// 5 输入 3中fixedLst为修正完的元素数据,casee=计算方式,输出 用函数计算后的温度/密度
|
|
|
|
|
|
getTTMM(Telementname, comparedLst);
|
|
|
|
// 6 输入 温度/密度/强度/元素种类 输出用函数计算后的含量
|
|
calculateConcentration(comparedLst,elementLst);
|
|
|
|
|
|
isCompletedCalculateByCFLibs = true;
|
|
vector<SpectrumElementQuantity> elementQuantityLst;
|
|
for(ElementData ele : elementLst){
|
|
elementQuantityLst.push_back({ele.eleName,ele.abundence});
|
|
}
|
|
//save(peakVector);
|
|
//saveSpecData(comparedLst);
|
|
//saveTTname(Telementname);
|
|
//saveElementLst(elementLst);
|
|
return elementQuantityLst;
|
|
|
|
}
|
|
|
|
// 从光谱数据中获取到元素名称集合
|
|
vector<ElementData> ElementAnalysis::getElementLst(vector<SpecData> compareLst){
|
|
//qDebug() << "[Logic]-从光谱数据中获取到元素名称集合" << endl;
|
|
// for (int i = 0; i < compareLst.size(); i++) {
|
|
// for (int j = 1; j < compareLst.size(); j++) {
|
|
// if(compareLst[i].eleName == compareLst[j].eleName){
|
|
// compareLst.erase(compareLst.begin() + j);
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// 按照元素名称字段对对比后的光谱数据进行去重,得到元素列表
|
|
auto end = std::unique(compareLst.begin(), compareLst.end(), [](const SpecData& a, const SpecData& b) {
|
|
return a == b;
|
|
});
|
|
|
|
// 删除重复元素
|
|
compareLst.erase(end, compareLst.end());
|
|
|
|
|
|
vector<ElementData> elementLst;
|
|
ElementData elementData_vec_content;
|
|
for (int i = 0; i < compareLst.size(); ++i) {
|
|
elementData_vec_content.eleName = compareLst[i].eleName;
|
|
elementLst.push_back(elementData_vec_content);
|
|
elementData_vec_content.eleName.clear();
|
|
//elementLst.push_back({compareLst[i].eleName});
|
|
}
|
|
return elementLst;
|
|
|
|
}
|
|
|
|
|
|
// 分割字符串为数组
|
|
vector<string> ElementAnalysis::split(string str, char del) {
|
|
stringstream ss(str);
|
|
string temp;
|
|
vector<string> ret;
|
|
while (getline(ss, temp, del)) {
|
|
ret.push_back(temp);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
// 2 输入 比对库:200行,7列,输出 到比对库的数据
|
|
vector<SpecData> ElementAnalysis::findCompareLst(string compareFileName){
|
|
//qDebug() << "[Logic]-获取库的数据" << endl;
|
|
vector<SpecData> specLst;
|
|
ifstream infile1(compareFileName);
|
|
if(!infile1.is_open()){
|
|
cerr << "Failed open file!!!: " <<endl;
|
|
return specLst;
|
|
}
|
|
// unordered_map<string, int> counts1;
|
|
string line;
|
|
while(!infile1.eof()){
|
|
SpecData spec;
|
|
infile1 >> spec.eleName >> spec.waveLength >> spec.intensity >> spec.transpro >> spec.upplevel >>spec.statis >> spec.ionization ;
|
|
specLst.push_back(spec);
|
|
}
|
|
|
|
// while (getline(infile1, line)) {
|
|
// vector<string> res = split(line, '\t');
|
|
|
|
// struct SpecData spec;
|
|
// spec.eleName = res[0];
|
|
// spec.waveLength = std::stod(res[1]);
|
|
// spec.intensity = std::stod(res[2]);
|
|
// spec.transpro = std::stod(res[3]);
|
|
// spec.upplevel = std::stod(res[4]);
|
|
// spec.statis = std::stod(res[5]);
|
|
// spec.ionization = std::stod(res[6]);
|
|
|
|
// }
|
|
|
|
|
|
infile1.close();
|
|
return specLst;
|
|
}
|
|
|
|
|
|
// 3 输入 1中波长和2中波长,输出 差值小于0.02的波长(2中的波长值), 1中的强度,2中的其他字段
|
|
vector<SpecData> ElementAnalysis::doCompare(vector<SpectrumChannelData> peakLst,vector<SpecData> &compareLst,double distance){
|
|
//qDebug() << "[Logic]-比对库的数据" << endl;
|
|
vector<SpecData> specLst;
|
|
|
|
|
|
for(auto &comp: compareLst){
|
|
|
|
//SpecData comp = compareLst[j];
|
|
auto elementName = comp.eleName;
|
|
for (SpectrumChannelData &specData : peakLst) {
|
|
if(abs(specData.wavelength - comp.waveLength) < distance){
|
|
if(specData.intensity > 0){
|
|
SpecData spec;
|
|
spec.eleName = elementName;
|
|
spec.waveLength = comp.waveLength;
|
|
spec.intensity = specData.intensity;
|
|
spec.transpro = comp.transpro;
|
|
spec.upplevel = comp.upplevel;
|
|
spec.statis = comp.statis;
|
|
spec.ionization = comp.ionization;
|
|
|
|
specLst.push_back(spec);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
return specLst;
|
|
}
|
|
|
|
|
|
// 4 输入 3中的强度,输出 修正后的强度
|
|
void ElementAnalysis::fixIntensity(vector<SpecData> &comparedLst,vector<FixData> fixLst, double baseline){
|
|
//qDebug() << "[Logic]-数据修正" << endl;
|
|
for (int i = 0; i < comparedLst.size(); i++) {
|
|
comparedLst[i].intensity = comparedLst[i].intensity - baseline;
|
|
}
|
|
|
|
|
|
for (int i = 0; i < comparedLst.size(); i++) {
|
|
vector<double> distanceLst ;
|
|
|
|
for (int j = 0; j < fixLst.size(); j++) { //遍历实验数据
|
|
|
|
distanceLst.push_back( std::abs(fixLst[j].wavelength - comparedLst[i].waveLength));
|
|
|
|
|
|
}
|
|
double firstDistance = distanceLst[0];
|
|
int ng = 0;
|
|
|
|
for (int m = 1; m < fixLst.size(); ++m) { //查找最小值
|
|
if (firstDistance - distanceLst[m] >= 0) {
|
|
firstDistance = distanceLst[m];
|
|
ng = m;
|
|
}
|
|
}
|
|
comparedLst[i].intensity = comparedLst[i].intensity / fixLst[ng].fix; //根据计算得到的 ng 更新 intensity 数据
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 5 输入 3中的7个字段,输出 用函数计算后的温度/密度
|
|
void ElementAnalysis::calculateElementTempne(vector<SpecData> fixedLst,string Telement,int casee,int count7){
|
|
//qDebug() << "[Logic]-计算温度/密度" << endl;
|
|
double Tinitial = 10000;
|
|
double deltalabda = 0.83;
|
|
|
|
double DD = 6.49e-4;
|
|
double CC = 1.52e-7;
|
|
double BB = -2.53e-12;
|
|
double AA = 1.53e-17;
|
|
double oumiga = DD + CC * Tinitial + BB * pow(Tinitial, 2) + AA * pow(Tinitial, 3);
|
|
double Ne = deltalabda * 1E+16 / (2.0 * oumiga);
|
|
double k1 = 8.61738573e-5;
|
|
int iter = 1;
|
|
// double electroD,temprature;
|
|
|
|
|
|
// cout << "选择玻尔兹曼还是萨哈-玻尔兹曼来计算等离子体温度,玻尔兹曼输入1,萨哈-玻尔兹曼输入2? " << endl;
|
|
// cin >> casee;
|
|
|
|
double y = 0;
|
|
|
|
while (true) {
|
|
|
|
|
|
ofstream outfile("/home/pi/SpecData/plot data.dat", ios::trunc);
|
|
switch (casee) {
|
|
case 0:
|
|
|
|
for (int k = 0; k < fixedLst.size(); ++k) {
|
|
if (fixedLst[k].ionization == 0) {
|
|
y = log(fixedLst[k].intensity * 1E7 / (fixedLst[k].statis * fixedLst[k].transpro));
|
|
|
|
outfile << std::fixed << std::setprecision(2) << std::setw(9) << fixedLst[k].upplevel
|
|
<< " " << std::fixed << std::setprecision(5) << y << std::endl;
|
|
}
|
|
|
|
}
|
|
break;
|
|
|
|
case 1:
|
|
ifstream infile("/home/pi/SpecData/ionization energy");
|
|
if (!infile) {
|
|
cout << "Failed to open file!" << endl;
|
|
// return; // 或者根据你的需求做适当的错误处理
|
|
}
|
|
string line; //创建一个名为“line”的字符串,用于存储从文件中读取的每行数据
|
|
vector<std::string> element1;
|
|
vector<double> Ei;
|
|
while (getline(infile, line)) { // 从文件中逐行读取数据,直到读取完所有行
|
|
stringstream ss(line); // 创建一个字符串流对象 ss,并将当前行的数据作为其输入
|
|
string element; // 创建一个名为 "element" 的字符串,用于存储从当前行中读取的元素数据
|
|
double energy; // 创建一个名为 "energy" 的变量,用于存储从当前行中读取的能量数据
|
|
|
|
ss >> element >> energy; // 将字符串流 ss 中的数据分别读取到 element 和 energy 变量中
|
|
element1.push_back(element); // 将 element 添加到 element1 数组的末尾
|
|
Ei.push_back(energy); // 将 energy 添加到 Ei 数组的末尾
|
|
|
|
}
|
|
infile.close();
|
|
|
|
int k3 = 0;
|
|
for (int k = 0; k < fixedLst.size(); ++k) {
|
|
k3 = 0;
|
|
|
|
while (element1[k3] != Telement) { //在element1数组中找到与Telement相等的元素时,结束循环
|
|
++k3;
|
|
}
|
|
double Eion = Ei[k3]; //将 Eion 设置为 Ei 数组中索引为 k3 的元素的值
|
|
double BBB = fixedLst[k].ionization * log((4.83e+15) * sqrt(pow(Tinitial, 3)) / Ne);
|
|
y = log(fixedLst[k].intensity * 1E7 / (fixedLst[k].statis * fixedLst[k].transpro)) - BBB;
|
|
|
|
outfile << std::fixed << std::setprecision(6) << std::setw(9) << fixedLst[k].upplevel + fixedLst[k].ionization * Eion
|
|
<< " " << std::fixed << std::setprecision(5) << y << std::endl;
|
|
}
|
|
break;
|
|
}
|
|
outfile.clear();
|
|
outfile.seekp(0,ios::beg);
|
|
outfile.close();
|
|
|
|
|
|
ifstream infile("/home/pi/SpecData/plot data.dat"); //打开文件
|
|
if (!infile) {
|
|
cerr << "Failed to open file!" << endl;
|
|
// return; // 或者根据你的需求做适当的错误处理
|
|
}
|
|
int k11 = 0; //初始化变量
|
|
string line;
|
|
while (getline(infile,line)) {
|
|
++k11; //统计文件中的数据个数,每读取一个数值,k11加1
|
|
}
|
|
|
|
|
|
infile.close();
|
|
|
|
|
|
vector<double> xx, yy;
|
|
xx.resize(k11); //将向量xx调整大小为k11
|
|
yy.resize(k11); //将向量yy调整大小为k11
|
|
|
|
|
|
infile.open("/home/pi/SpecData/plot data.dat");
|
|
for (int jj = 0; jj < k11; ++jj) {
|
|
infile >> xx[jj] >> yy[jj]; //逐行读取文件中的数据,存储到xx和yy的向量中
|
|
}
|
|
infile.close();
|
|
|
|
double sum_xx = 0.0, sum_xx2 = 0.0, sum_yy = 0.0, sum_xy = 0.0, sum_yy2 = 0.0;
|
|
for (int jj = 0; jj < k11; ++jj) {
|
|
sum_xx += xx[jj];
|
|
sum_xx2 += xx[jj] * xx[jj];
|
|
sum_yy += yy[jj];
|
|
sum_xy += xx[jj] * yy[jj];
|
|
sum_yy2 += yy[jj] * yy[jj];
|
|
}
|
|
|
|
double a12 = sum_xx;
|
|
double a11 = sum_xx2;
|
|
double b2 = sum_yy;
|
|
double b1 = sum_xy;
|
|
double b11 = sum_yy2;
|
|
double a22 = static_cast<double>(k11);
|
|
double a21 = a12;
|
|
double kk = (a12 * b2 - b1 * a22) / (a21 * a12 - a11 * a22);
|
|
// cout <<"kk:"<< kk <<endl;
|
|
double b = (b1 * a21 - a11 * b2) / (a12 * a21 - a11 * a22);
|
|
double r = pow(((b1 / a22 - (a12 / a22) * (b2 / a22)) /
|
|
sqrt((a11 / a22 - pow(a12 / a22, 2)) * (b11 / a22 - pow(b2 / a22, 2)))), 2);
|
|
double mm = -1 / kk * 11604.45;
|
|
oumiga = DD + CC * mm + BB * pow(mm, 2) + AA * pow(mm, 3);
|
|
double Nee = deltalabda * 1E+16 / (2.0 * oumiga);
|
|
|
|
|
|
// oumiga = DD + CC * Tinitial + BB * pow(Tinitial, 2) + AA * pow(Tinitial, 3);
|
|
// Ne = deltalabda * 1E+16 / (2.0 * oumiga);
|
|
xx.clear();
|
|
yy.clear();
|
|
|
|
|
|
|
|
if (abs(mm - Tinitial) < 1.0 && abs(Nee - Ne) < (Ne / 1000000)) {
|
|
temprature = mm;
|
|
//TT.push_back(mm);
|
|
electroD = Nee;
|
|
break;
|
|
}
|
|
else {
|
|
Tinitial = mm;
|
|
Ne = Nee;
|
|
}
|
|
|
|
++iter;
|
|
|
|
if(iter >100){
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
// 6 输入 温度/密度/强度/元素种类 输出用函数计算后的含量
|
|
void ElementAnalysis::calculateConcentration(vector<SpecData> &tempneLst,vector<ElementData> &elementLst){
|
|
|
|
|
|
//qDebug() << "[Logic]-输入温度/密度/强度/元素种类计算含量" << endl;
|
|
|
|
for (int i = 0 ; i < elementLst.size(); i++) {
|
|
|
|
int atomNumSum = 0, ionNumSum = 0;
|
|
double atomBSSum = 0.0, ionBSSum = 0.0;
|
|
|
|
for (int j = 0; j < tempneLst.size(); j++) {
|
|
string elementName = elementLst[i].eleName;
|
|
double concentrationBS = calculateConcentrationByIntercept( tempneLst[j].intensity,
|
|
tempneLst[j].transpro,
|
|
tempneLst[j].upplevel,
|
|
tempneLst[j].statis,
|
|
temprature);
|
|
if (tempneLst[j].eleName == elementName && tempneLst[j].ionization == 0) {
|
|
|
|
//某个元素的原子BS
|
|
atomNumSum++;
|
|
atomBSSum += concentrationBS;
|
|
// elementLst[i].bsa = concentrationBS;
|
|
|
|
}
|
|
if (tempneLst[j].eleName == elementName && tempneLst[j].ionization == 1) {
|
|
//某个元素的离子BS
|
|
ionNumSum++;
|
|
ionBSSum += concentrationBS;
|
|
// elementLst[i].bsi = concentrationBS;
|
|
}
|
|
}
|
|
//cout <<
|
|
if (atomNumSum != 0) {
|
|
elementLst[i].bsa = atomBSSum / atomNumSum;
|
|
} else {
|
|
elementLst[i].bsa= 0;
|
|
}
|
|
|
|
|
|
if (ionNumSum != 0) {
|
|
elementLst[i].bsi = ionBSSum / ionNumSum;
|
|
} else {
|
|
elementLst[i].bsi= 0;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < elementLst.size(); i++) {
|
|
|
|
|
|
double csa = calculateConcentrationByCS(elementLst[i].eleName, temprature, 0, electroD, elementLst[i].bsa);
|
|
elementLst[i].csa = csa;
|
|
|
|
double csi = calculateConcentrationByCS(elementLst[i].eleName, temprature, 1, electroD, elementLst[i].bsi);
|
|
elementLst[i].csi = csi;
|
|
|
|
double csit = saha(elementLst[i].eleName, temprature, electroD, csa );
|
|
elementLst[i].csit = csit;
|
|
|
|
}
|
|
|
|
// char check8 = 'N';
|
|
//定义输入均为Y需要用saha方程来计算
|
|
double summ2 = 0;
|
|
// 浓度百分比
|
|
double abundence = 0;
|
|
|
|
// if(check8 == 'Y'){
|
|
// for (int i = 0; i < elementLst.size(); i++) {
|
|
// summ2 += (elementLst[i].csa + elementLst[i].csit);
|
|
// }
|
|
|
|
// for (int i = 0; i < elementLst.size(); i++) {
|
|
// abundence = (elementLst[i].csa + elementLst[i].csit) / summ2;
|
|
// elementLst[i].abundence = abundence;
|
|
// }
|
|
|
|
// } else{
|
|
for(int i = 0; i < elementLst.size(); i++) {
|
|
// if ((elementLst[i].csi * elementLst[i].csa * elementLst[i].csa) >= 1e-44) {
|
|
// elementLst[i].csi = 0.0005* elementLst[i].csit ;
|
|
// }
|
|
// if ( elementLst[i].csi == 0) {
|
|
// elementLst[i].csi = elementLst[i].csit ;
|
|
// }
|
|
summ2 +=(elementLst[i].csa + elementLst[i].csit);
|
|
}
|
|
for(int i = 0; i < elementLst.size(); i++) {
|
|
|
|
abundence = (elementLst[i].csa +elementLst[i].csit) / summ2;
|
|
elementLst[i].abundence = abundence;
|
|
}
|
|
|
|
// }
|
|
|
|
// Calculate weight concentration
|
|
|
|
// cout << "您想要得到每种元素的重量浓度(wt%)吗?(Y/N): "<< endl;
|
|
// cin >> check9;
|
|
|
|
|
|
// char check9 = 'Y'; // 定义输入始终为Y,,,需要得到每种元素的重量浓度
|
|
// if (check9 == 'Y') {
|
|
ifstream weightfile;
|
|
weightfile.open("/home/pi/SpecData/atomic_weight.txt");
|
|
if(!weightfile.is_open()){
|
|
cerr << "atomic_weight.txt文件打开失败" << endl;
|
|
}
|
|
|
|
int k6 = 0;
|
|
string line;
|
|
while (getline(weightfile, line)) {
|
|
k6++;
|
|
}
|
|
weightfile.clear();
|
|
weightfile.seekg(0, ios::beg);
|
|
vector<string> ele(k6);
|
|
vector<double> atomW(k6);
|
|
for (int i = 0; i < k6; i++) {
|
|
weightfile >> ele[i] >> atomW[i];
|
|
}
|
|
weightfile.close();
|
|
|
|
double summ3 = 0.0;
|
|
for (int i = 0; i < elementLst.size(); i++) {
|
|
|
|
for (int j = 0; j < k6; j++) {
|
|
if (ele[j] == elementLst[i].eleName) {
|
|
|
|
elementLst[i].abundence = elementLst[i].abundence * atomW[j];
|
|
break;
|
|
}
|
|
}
|
|
summ3 += elementLst[i].abundence;
|
|
|
|
}
|
|
|
|
cout << "各元素的重量浓度如下:" << endl;
|
|
for (int i = 0; i < elementLst.size(); i++)
|
|
{
|
|
//string elementName = elementLst[i].eleName;
|
|
double abu = elementLst[i].abundence;
|
|
|
|
// if (elementLst[i].eleName != "Cu" && elementLst[i].eleName != "Pb"
|
|
// && elementLst[i].eleName != "As" && elementLst[i].eleName != "Hg"
|
|
// && elementLst[i].eleName != "Cd" && elementLst[i].eleName != "Cr")
|
|
// {
|
|
// elementLst[i].abundence = elementLst[i].abundence / summ3 * 100 * 10000000;
|
|
// }
|
|
elementLst[i].abundence = abu / summ3 * 100 ;
|
|
|
|
// cout << elementLst[i].eleName << " " << elementLst[i].abundence / summ3 * 100 << "%" << endl;
|
|
}
|
|
}
|
|
// }
|
|
|
|
//计算saha
|
|
double ElementAnalysis::saha(string element,double temprature,double electroD,double csa){
|
|
//qDebug() << "[Logic]-saha计算含量" << endl;
|
|
string filename = "/home/pi/SpecData/ionization energy";
|
|
ifstream infile(filename);
|
|
if(!infile.is_open()){
|
|
cerr << "Failed fun to open file: " << filename <<endl;
|
|
return 0;
|
|
}
|
|
int k1 = 0;
|
|
string line;
|
|
while (getline(infile, line)) {
|
|
k1++;
|
|
}
|
|
infile.clear();
|
|
infile.seekg(0, ios::beg);
|
|
|
|
string element1;
|
|
vector<string> Element;
|
|
vector<double> Eio;
|
|
double ei;
|
|
while(infile >> element1 >> ei){
|
|
Element.push_back(element1);
|
|
Eio.push_back(ei);
|
|
}
|
|
infile.close();
|
|
|
|
double Eion = 0;
|
|
for (int k3 = 0; k3 < k1; k3++) {
|
|
if (Element[k3] == element) {
|
|
Eion = Eio[k3];
|
|
break;
|
|
}
|
|
}
|
|
|
|
double Kb = 8.6173303e-5;
|
|
return 4.83e15 * sqrt(temprature * temprature * temprature) / electroD * calculateU(element, temprature, 1) / calculateU(element, temprature, 0) * exp(-Eion / (Kb * temprature)) * csa;
|
|
}
|
|
|
|
|
|
// 计算U
|
|
double ElementAnalysis::calculateU(string element, double temprature, int ionization){
|
|
//qDebug() << "[Logic]-计算U" << endl;
|
|
string filename = " ";
|
|
if(ionization == 0){
|
|
filename = "/home/pi/SpecData/"+ element + "I.dat";
|
|
}else{
|
|
filename = "/home/pi/SpecData/"+ element + "II.dat";
|
|
}
|
|
ifstream file(filename);
|
|
if(!file.is_open()){
|
|
std::cerr << "Error:File not found:" << filename <<endl;
|
|
return 0;
|
|
}
|
|
int count = 0;
|
|
string lines;
|
|
while(getline(file,lines)){
|
|
count++;
|
|
}
|
|
vector<double> Gew;
|
|
vector<double> Gei;
|
|
file.clear();
|
|
file.seekg(0,ios::beg);
|
|
double temp1,temp2;
|
|
while(file >> temp1 >> temp2){
|
|
Gew.push_back(temp1);
|
|
Gei.push_back(temp2);
|
|
}
|
|
|
|
file.close();
|
|
|
|
double U = 0.0;
|
|
double Kb = 8.61738573e-5;
|
|
for(int j = 0; j < count; ++j){
|
|
U += (2.0 * Gew[j] + 1.0)* exp(-Gei[j] / (Kb * temprature));
|
|
}
|
|
|
|
return U;
|
|
}
|
|
|
|
// 根据截距计算元素含量
|
|
double ElementAnalysis::calculateConcentrationByIntercept(double inten,double transpro,double upplevel,double statis,double temprature)
|
|
{
|
|
//qDebug() << "[Logic]-根据截距计算元素含量" << endl;
|
|
double Kb = 8.61738573e-5;
|
|
//double Kb = 8.61738573e-5;
|
|
|
|
return log(inten*1e7/(transpro * statis)) + upplevel/(Kb * temprature); //传回数据求平均值
|
|
|
|
}
|
|
// 根据CS计算元素含量
|
|
double ElementAnalysis::calculateConcentrationByCS(string element,double temprature,int ionization,double electroD,double bs)
|
|
{
|
|
//qDebug() << "[Logic]-根据CS计算元素含量" << endl;
|
|
|
|
double cs = 0;
|
|
if (abs(bs - 0.0) < 1e-30)
|
|
{
|
|
cs = 0;
|
|
}
|
|
else
|
|
{
|
|
cs = calculateU(element, temprature, ionization) * exp(bs) / (electroD / 0.03); // Total particle density calculated according to 3% ionization
|
|
}
|
|
return cs;
|
|
}
|
|
|
|
// 获取修正数据
|
|
vector<FixData> ElementAnalysis::getFixLst(int gratingNo){
|
|
|
|
//qDebug() << "[Logic]-获取修正数据" << endl;
|
|
vector<FixData> fixLst;
|
|
|
|
string filename;
|
|
switch (gratingNo) {
|
|
case 1:
|
|
filename = "/home/pi/SpecData/600c";
|
|
break;
|
|
case 2:
|
|
filename = "/home/pi/SpecData/1200c";
|
|
break;
|
|
case 3:
|
|
filename = "/home/pi/SpecData/2400c";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
ifstream infile(filename);
|
|
if(!infile.is_open()){
|
|
cerr << "Failed to open file: " << filename <<endl;
|
|
|
|
}
|
|
|
|
infile.clear();
|
|
infile.seekg(0,ios::beg);
|
|
double temp1,temp2;
|
|
while(infile >> temp1 >> temp2){
|
|
fixLst.push_back({temp1,temp2});
|
|
|
|
}
|
|
infile.close();
|
|
return fixLst;
|
|
}
|
|
|
|
//获取元素的离子和原子条数
|
|
vector<TTname> ElementAnalysis::GetTTname(vector<ElementData> elementLst,vector<SpecData> comparedLst){
|
|
//qDebug() << "[Logic]-获取元素的离子和原子条数" << endl;
|
|
|
|
for (int i = 0 ; i < elementLst.size(); i++) {
|
|
int atomNumSum = 0, ionNumSum = 0;
|
|
|
|
for (int j = 0; j < comparedLst.size(); j++) {
|
|
string elementName = elementLst[i].eleName;
|
|
|
|
if (comparedLst[j].eleName == elementName && comparedLst[j].ionization == 0) {
|
|
//某个元素的原子BS
|
|
atomNumSum++;
|
|
|
|
}
|
|
if (comparedLst[j].eleName == elementName && comparedLst[j].ionization == 1) {
|
|
//某个元素的离子BS
|
|
ionNumSum++;
|
|
}
|
|
elementLst[i].atomNum = atomNumSum;
|
|
elementLst[i].ionNum = ionNumSum;
|
|
}
|
|
}
|
|
vector<TTname> Telementname;
|
|
TTname tTname_vec_content;
|
|
for (int m = 0; m < elementLst.size() ; m++){
|
|
int count7 = (elementLst[m].atomNum)+(elementLst[m].ionNum);
|
|
if(elementLst[m].atomNum > 0 && elementLst[m].ionNum > 0){
|
|
tTname_vec_content.Telementname = elementLst[m].eleName;
|
|
tTname_vec_content.lines = count7;
|
|
Telementname.push_back(tTname_vec_content);
|
|
tTname_vec_content.Telementname.clear();
|
|
//Telementname.push_back({elementLst[m].eleName,count7});
|
|
}else
|
|
continue;
|
|
}
|
|
return Telementname;
|
|
}
|
|
|
|
//获取平均温度和电离子密度
|
|
void ElementAnalysis::getTTMM(vector<TTname> Telementname,vector<SpecData> comparedLst){
|
|
//qDebug() << "[Logic]-获取平均温度和电离子密度" << endl;
|
|
|
|
double TsingleSum = 0;
|
|
double electroDSum = 0;
|
|
double Tsingle1 = 0;
|
|
double electroD1 = 0;
|
|
for (int m = 0; m < Telementname.size() ; m++){
|
|
int count8 = 0;
|
|
int count7 = Telementname[m].lines;
|
|
|
|
vector<SpecData> TTcomparedLst (count7);
|
|
|
|
|
|
for (int i = 0; i < count7; i++) {
|
|
while (true) {
|
|
if (comparedLst[count8].eleName == Telementname[m].Telementname) {
|
|
TTcomparedLst[i].eleName = comparedLst[count8].eleName;
|
|
TTcomparedLst[i].waveLength = comparedLst[count8].waveLength;
|
|
TTcomparedLst[i].intensity = comparedLst[count8].intensity;
|
|
TTcomparedLst[i].transpro = comparedLst[count8].transpro;
|
|
TTcomparedLst[i].upplevel = comparedLst[count8].upplevel;
|
|
TTcomparedLst[i].statis = comparedLst[count8].statis;
|
|
TTcomparedLst[i].ionization = comparedLst[count8].ionization;
|
|
count8++;
|
|
break;
|
|
}
|
|
else {
|
|
count8++;
|
|
}
|
|
}
|
|
}
|
|
|
|
calculateElementTempne(TTcomparedLst,Telementname[m].Telementname, 1,count7);
|
|
Tsingle1 = temprature;
|
|
electroD1 = electroD;
|
|
|
|
TsingleSum += Tsingle1;
|
|
electroDSum += electroD1;
|
|
}
|
|
|
|
temprature = TsingleSum / Telementname.size();
|
|
electroD = electroDSum / Telementname.size();
|
|
}
|
|
|
|
//删除重叠区域的峰值
|
|
vector<SpectrumChannelData> ElementAnalysis::selectWavelength(vector<pair<int, vector<SpectrumChannelData>>> peakVector,
|
|
int inSpacing)
|
|
{
|
|
//qDebug() << "[Logic]-删除重叠区域的峰值" << endl;
|
|
std::vector<SpectrumChannelData> channelData;
|
|
for (size_t i = 0; i < peakVector.size() - 1; ++i) {
|
|
std::vector<SpectrumChannelData>& channelData1 = peakVector[i].second;
|
|
std::vector<SpectrumChannelData>& channelData2 = peakVector[i+1].second;
|
|
for (size_t k = 0; k < channelData1.size(); ++k) {
|
|
for (size_t l = 0; l < channelData2.size(); ++l) {
|
|
if (std::abs(channelData1[k].wavelength - channelData2[l].wavelength) < inSpacing) {
|
|
if (channelData1[k].intensity > channelData2[l].intensity) {
|
|
channelData2.erase(channelData2.begin() + l);
|
|
--l;
|
|
} else {
|
|
channelData1.erase(channelData1.begin() + k);
|
|
--k;
|
|
break; // 重新检查 channelData1[k],所以跳出内层循环
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 合并所有处理过的 channelData
|
|
for (const auto& pair : peakVector) {
|
|
channelData.insert(channelData.end(), pair.second.begin(), pair.second.end());
|
|
}
|
|
|
|
return channelData;
|
|
|
|
}
|