Adjust GUI to accept chemical formulae with floats instead of intigers

This commit is contained in:
2022-09-21 17:11:00 +02:00
parent 9724c86a40
commit bea03ac0cf
2 changed files with 22 additions and 10 deletions
+19 -8
View File
@@ -1,11 +1,14 @@
// This file contains function that are Javascript generic
// This file contains function that are generic Javascript for TrimSP
function parse_formula (mf) {
// This function takes a chemical formula and returns
// an object that contains the atoms and their number
// replace all types of brackets into ()
var mf = mf.replace(/\[/g,"(").replace(/\]/g,")").replace(/\{/g,"(").replace(/\}/g,")").replace(/\s+/g,"");
if(/^\d+/.test(mf)) throw new SyntaxError("Molecular formula should not start with a number!");
var mol = {};
// go into brackets and collect info
while(mf.includes("(")){
var x = mf.lastIndexOf("(");
var y = mf.indexOf(")", mf.lastIndexOf("("));
@@ -14,7 +17,7 @@ function parse_formula (mf) {
var c = mf.substr(y+1);
c = /^\d+/.exec(c);
c=(c!= null)?Number(c[0]):1;
// recursive
var a = this.parse_formula(z);
var b = "";
for(var k in a){
@@ -24,13 +27,19 @@ function parse_formula (mf) {
}
var m = "";
while(m = /([A-Z]{1}[a-z]{0,}[0-9]{0,})/.exec(mf)){
// Separate element name from stoichiometry
// while(m = /([A-Z]{1}[a-z]{0,}[0-9]{0,})/.exec(mf)){
while(m = /([A-Z]{1}[a-z]{0,}[0-9]{0,}\.?[0-9]{0,})/.exec(mf)){
// Element
var m1 = /([A-Z]{1,}[a-z]{0,})/.exec(m[0]);
var m2 = /\d+/.exec(m[0]);
// Stoichiometry
//var m2 = /\d+/.exec(m[0]);
var m2 = /\d+\.?\d{0,}/.exec(m[0]);
if(typeof mol[m1[0]] == 'undefined') mol[m1[0]]=(m2!=null)?Number(m2[0]):1;
else mol[m1[0]] += (m2!=null)?Number(m2[0]):1;
mf = mf.replace(m[0], "");
}
console.log("mol=",mol);
return mol;
}
@@ -836,14 +845,15 @@ function CreateInpFile(All) {
Check++;
}
}
if (Comp == "") {Check++;}
if (Comp == "" || typeof Comp == 'undefined') {Check++;}
// Write composition to results file header
// Densities of layers
let Lrho="layer"+i+"rho";
let rho = 1*All[Lrho];
// How to do this? All[Lrho]=sprintf("%6.2f",rho);
if (rho=="") {Check++;}
if (rho=="") {Check++;console.log("rho",rho);}
// Thickness of layers
let Ld ="L"+i+"d";
@@ -853,8 +863,8 @@ function CreateInpFile(All) {
// Sanity check, is the layer supposed to have value? are they all there?
if (Check!=0) {
ErrMsg="Error: Bad chemical formula in Layer $i.\nPleach check!\n";
//print STDERR $ErrMsg;
ErrMsg="Error: Bad chemical formula in Layer $i.\nPlease check!\n";
console.log($ErrMsg);
return ErrMsg;
}
@@ -866,6 +876,7 @@ function CreateInpFile(All) {
Sum=Sum+LElComp[key];
}
if (Sum==0) {Sum=1;}
console.log("Sum=",Sum);
let Els = Object.keys(LElComp);
+3 -2
View File
@@ -394,6 +394,7 @@ C This part reads the input file (new format)
OPEN(UNIT=99,file=errnam,STATUS='replace')
OPEN(UNIT=11,file=innam,STATUS='unknown',ERR=1359)
C First line: properties of projectile
C Ordered as: Z, mass number (amu), imp. energy, dist. of imp. energy, angle, dist. angles,
READ(11,*) Z1,M1,E0,Esig,ALPHA,ALPHASIG,EF,ESB,SHEATH,ERC
C Second line: simulation related parameters
C Ordered as: Number of particles, seed, seed, seed, initial depth, RD, depth increment, CA, KK0, KDEE1,KDEE2,IPOT
@@ -406,13 +407,13 @@ C Third line: Number of layers
C Here we read the NLayers structure
DO I=1,NLayers
C Thickness (DX), density (RHO), and correction factor (CK, it is
C always 1.0??) Atomic numbers
C always 1.0??)
READ(11,*) DX(I),RHO(I),CK(I)
C Atomic numbers
READ(11,*) ZT(I,1),ZT(I,2),ZT(I,3),ZT(I,4),ZT(I,5)
C Mass numbers (amu)
READ(11,*) MT(I,1),MT(I,2),MT(I,3),MT(I,4),MT(I,5)
C Concentration
C Concentration (stoichiometry)
READ(11,*) CO(I,1),CO(I,2),CO(I,3),CO(I,4),CO(I,5)
C Surface binding energy
READ(11,*) SBE(I,1),SBE(I,2),SBE(I,3),SBE(I,4),SBE(I,5)