42 const map<unsigned int, double> &compositionMols,
43 map<unsigned int, double> &compositionMass)
45 map<unsigned int, double> massValues;
47 for(map<unsigned int, double>::const_iterator it=compositionMols.begin(); it!=compositionMols.end();++it)
53 for(map<unsigned int, double>::const_iterator it=compositionMols.begin();
54 it!=compositionMols.end(); ++it)
55 totalMols+=it->second;
59 for(map<unsigned int, double>::const_iterator it=compositionMols.begin();
60 it!=compositionMols.end(); ++it)
61 totalMass+=massValues.at(it->first)*it->second/totalMols;
63 for(map<unsigned int, double>::const_iterator it=compositionMols.begin();
64 it!=compositionMols.end(); ++it)
66 ASSERT(compositionMass.find(it->first) == compositionMass.end());
67 compositionMass[it->first] = it->second/totalMols*massValues.at(it->first)/totalMass;
75 const map<unsigned int, double> &compositionMass,
76 map<unsigned int, double> &compositionMols)
78 map<unsigned int, double> massValues;
80 for(map<unsigned int,double>::const_iterator it=compositionMass.begin(); it!=compositionMass.end();++it)
84 for(map<unsigned int,double>::const_iterator it=compositionMass.begin(); it!=compositionMass.end();++it)
87 thisMol= (it->second/massValues.at(it->first));
88 compositionMols[it->first] = thisMol;
92 for(map<unsigned int,double>::const_iterator it=compositionMass.begin(); it!=compositionMass.end();++it)
94 compositionMols[it->first]/=totalMol;
101 const AbundanceData &massTable, std::map<unsigned int, double> &atomData)
104 const double BALANCE_TOKEN = -128.0f;
106 double totalComp=0.0f;
108 unsigned int balanceNumber=0;
110 for(
unsigned int ui=0;ui<atomList.size();ui++)
133 else if( strs[1] ==
"bal" && balanceNumber)
138 atomicNumber= massTable.
symbolIndex(strs[0].c_str())+1;
142 if(atomData.find(atomicNumber) != atomData.end())
148 atomData[atomicNumber-1] = value;
153 balanceNumber=atomicNumber-1;
154 atomData[atomicNumber-1] = BALANCE_TOKEN;
160 if(totalComp > 1.0f )
165 atomData[balanceNumber] = 1.0-totalComp;
177 bool testMassToMolConversion()
180 if(natData.
open(
"naturalAbundance.xml"))
183 map<unsigned int, double> molComp,massComp;
185 unsigned int idxFe,idxC;
187 massComp[idxFe]=0.96;
193 TEST(massComp.size() ==2,
"Mass Composition data size");
195 for(
auto it=molComp.begin();it!=molComp.end(); it++)
197 TEST(massComp.find(it->first) != molComp.end(),
"Symbol present in both mass and molar comp");
198 TEST(it->second <=1 && it->second >=0,
"Composition should be in 0->1");
202 TEST(TOL_EQ(total,1.0f),
"total composition sums to 1");
204 TEST(TOL_EQ(molComp[idxFe],0.8377079),
"Fe composition");
205 TEST(TOL_EQ(molComp[idxC],0.162292),
"C composition");
210 bool testMolToMassConversion()
213 if(natData.
open(
"naturalAbundance.xml"))
216 map<unsigned int, double> molComp,massComp;
218 unsigned int idxFe,idxC;
226 TEST(massComp.size() ==2,
"Mass Composition data size");
228 for(
auto it=massComp.begin();it!=massComp.end(); it++)
230 TEST(molComp.find(it->first) != molComp.end(),
"Symbol present in both mass and molar comp");
234 TEST(TOL_EQ(total,1.0f),
"total composition sums to 1");
236 TEST(TOL_EQ(massComp[idxFe],0.99115),
"Fe composition");
237 TEST(TOL_EQ(massComp[idxC],0.008849557),
"C composition");
242 bool testMassMolConversion()
244 TEST(testMolToMassConversion(),
"Mol->mass conversion");
245 TEST(testMassToMolConversion(),
"Mass->Mol conversion");
size_t symbolIndex(const char *symbol, bool caseSensitive=true) const
Return the element's position in table, starting from 0.
std::string stripWhite(const std::string &str)
Strip whitespace, (eg tab,space) from either side of a string.
float getNominalMass(size_t elementIdx) const
Obtain the isotope weighted mass for the given element.
void convertMolToMass(const AbundanceData &atomTable, const std::map< unsigned int, double > &compositionMols, std::map< unsigned int, double > &compositionMass)
Convert the given composition from molar fraction data to mass fraction.
void convertMassToMol(const AbundanceData &atomTable, const std::map< unsigned int, double > &compositionMass, std::map< unsigned int, double > &compositionMols)
Convert the given composition from mass fraction data to molar fraction.
void splitStrsRef(const char *cpStr, const char delim, std::vector< std::string > &v)
Split string references using a single delimiter.
size_t open(const char *file, bool strict=false)
Attempt to open the abundance data file, return 0 on success, nonzero on failure. ...
Class to load abundance information for natural isotopes.
std::string lowercase(std::string s)
Return a lowercase version for a given string.
bool stream_cast(T1 &result, const T2 &obj)
Template function to cast and object to another by the stringstream.
void stripZeroEntries(std::vector< std::string > &s)
unsigned int parseCompositionData(const std::vector< std::string > &s, const AbundanceData &atomTable, std::map< unsigned int, double > &atomData)
Convert atomic string label data, in the form "Fe 0.81" to fraction map.