Simplify javascript code, towards unification of online and standalone versions.

This commit is contained in:
2023-01-09 11:41:12 +01:00
parent 78fb7c616d
commit ce5aafd076
2 changed files with 152 additions and 96 deletions

View File

@@ -215,17 +215,9 @@
<ol>
<li value="1">Nonlocal (Lindhard-Scharff).</li>
<li value="2">Local (Oen-Robinson).</li>
<li value="3">
Equipartition of local and nonlocal models (i.e., options 1 & 2).
</li>
<li value="4">
Nonlocal (Anderson-Ziegler tables for hydrogen);
must be used for hydrogen-like projectile with energies > 10 keV.
</li>
<li value="5">
Nonlocal (Ziegler tables for helium);
must be used for helium-like projectiles with energies > 50 keV.
</li>
<li value="3">Equipartition of local and nonlocal models (i.e., options 1 & 2).</li>
<li value="4">Nonlocal (Anderson-Ziegler tables for hydrogen); must be used for hydrogen-like projectile with energies > 10 keV.</li>
<li value="5">Nonlocal (Ziegler tables for helium); must be used for helium-like projectiles with energies > 50 keV.</li>
</ol>
Note: options 1, 2, and 3 can only be used at energies below the stopping power maximum.
</span>
@@ -252,8 +244,7 @@
<ol>
<li value="1">Nonlocal (Lindhard-Scharff).</li>
<li value="2">Local (Oen-Robinson).</li>
<li value="3">
Equipartition of local and nonlocal models (i.e., options 1 & 2).
<li value="3">Equipartition of local and nonlocal models (i.e., options 1 & 2).
</li>
</ol>
Note: options 1, 2, and 3 can only be used at energies below the stopping power maximum.

View File

@@ -677,7 +677,7 @@ function rho_fun()
}
document.getElementById(rhoLi).value = density_estimate;
alert("Warning: Density for this layer is only an estimate!")
alert("Warning: The density for this layer is only an estimate!")
}
}
@@ -1054,7 +1054,7 @@ function startSequence(All) {
trimBin = "trimspNL";
}
console.log(trimBin);
// Check if workPath exists otherwise create it
checkDir(All['workPath']); // from TrimSPelec.js, Electron/Node specific
@@ -1065,6 +1065,8 @@ function startSequence(All) {
// This is a flag to indicate whether the lists of values and inclements are the same size
All["SdzFlag"]=0;
var SValues = [];
let ScanName = "E";
let ScanAttrib = "valEnergy";
if (All["scanSeq"]) {
// For multiple runs or a scan
if (All["scanType"]=="scanVals") {
@@ -1077,10 +1079,6 @@ function startSequence(All) {
}
}
let ScanName = "";
let ScanAttrib = "";
ScanName = "E";
ScanAttrib = "valEnergy";
if (All["comboScan"]==1) {
ScanName = "SigE";
ScanAttrib = "sigEnergy";
@@ -1097,88 +1095,42 @@ function startSequence(All) {
ScanName = "Ld"+All["scandL"];
ScanAttrib = "L"+All["scandL"]+"d";
}
let iScan=0;
for (var SValue of SValues) {
document.getElementById(ScanAttrib).value = SValue;
All[ScanAttrib]=SValue;
if ( All["SdzFlag"] == 1) {
if (All["comboScan"]=="Energy") {
document.getElementById(ScanAttrib).value = SdzValues[iScan];
} else if (All["comboScan"]=="") {
document.getElementById(ScanAttrib).value = SdzValues[iScan];
}
}
let eingabe1=CreateInpFile(All);
if (eingabe1=="ERROR") {return(0);}
let FILENAME=All["workPath"]+"/"+All["fileNamePrefix"]+"_"+ScanName+SValue;
writeAsciiFile(FILENAME+".inp",eingabe1)
// Use Linux version
Progress=Progress+90/SValues.length;
// Update GUI progress bar
cmd = "cd "+All["workPath"]+";cp "+FILENAME+".inp eingabe1.inp;"+trimBin;
// Execute command
execute(cmd);
for (let ext of [".err",".out",".rge"]) {
cmd = "cd "+All["workPath"]+";mv -f ausgabe1"+ext+" "+FILENAME+ext;
execute(cmd);
}
let [cols,data]= readDatFile(FILENAME+".rge");
// convert depth to nm and normalize stopping profile
let depth=data[0];
let nmuons=data[1];
let dz = (depth[1]-depth[0])/10;
let norm = dz*sum(nmuons)/100;
for (let i=0; i<depth.length; i++) {
depth[i]=depth[i]/10;
nmuons[i]=nmuons[i]/norm;
}
Plot_xy("plotRge",depth,nmuons,['Depth (nm)','Stopped muons (%/nm)',ScanName+'='+Number(SValue)/1000+'keV']);
// Read the sequence data
let [cfort,cdata]= readDatFile(All["workPath"]+"/fort.33");
let lyrs = [6]; // back scattered muons
for (let ilayer=18;ilayer<cdata.length;ilayer++){lyrs.push(ilayer);} // the rest of the layers
// Now loop over all
Plotly.purge("plotFrac")
for (let ilayer of lyrs) {
// Normalize
let idata = cdata[ilayer];
for (let i=0; i<idata.length; i++) {
idata[i]=100*idata[i]/cdata[4][i];
}
let chem = cfort[ilayer];
if (ilayer != 6) {
let LComp = "L"+(ilayer-17)+"Comp";
chem = All[LComp];
}
Plot_xy("plotFrac",cdata[0],cdata[ilayer],['E (keV)','Fraction of muons (%)',chem]);
}
iScan++;
}
} else {
// For a single run
// For single run, array with single value of valEnergy
SValues.push(parseInt(All["valEnergy"]));
}
let iScan=0;
for (var SValue of SValues) {
document.getElementById(ScanAttrib).value = SValue;
All[ScanAttrib]=SValue;
if ( All["SdzFlag"] == 1) {
if (All["comboScan"]=="Energy") {
document.getElementById(ScanAttrib).value = SdzValues[iScan];
} else if (All["comboScan"]=="") {
document.getElementById(ScanAttrib).value = SdzValues[iScan];
}
}
let eingabe1=CreateInpFile(All);
if (eingabe1=="ERROR") {return(0);}
let FILENAME=All["workPath"]+"/"+All["fileNamePrefix"];
writeAsciiFile(FILENAME+".inp", eingabe1);
Progress=20;
// Update progress on GUI
let FILENAME=All["workPath"]+"/"+All["fileNamePrefix"]+"_"+ScanName+SValue;
writeAsciiFile(FILENAME+".inp",eingabe1)
// Update GUI progress bar
Progress=Progress+90/SValues.length;
// Prepare command and execute
cmd = "cd "+All["workPath"]+";cp "+FILENAME+".inp eingabe1.inp;"+trimBin;
execute(cmd);
// document.getElementById("myBar").style.width = Progress + "%";
// document.getElementById("myBar").innerHTML = Progress + "%";
// Rename files to standarized format
for (let ext of [".err",".out",".rge"]) {
cmd = "cd "+All["workPath"]+";mv -f ausgabe1"+ext+" "+FILENAME+ext;
execute(cmd);
}
Progress=90;
// Update progress on GUI
// document.getElementById("myBar").style.width = Progress + "%";
// document.getElementById("myBar").innerHTML = Progress + "%";
// Make plot (only fractions make sense)
// Read stopping profiels
let [cols,data]= readDatFile(FILENAME+".rge");
// convert depth to nm
// convert depth to nm and normalize stopping profile
let depth=data[0];
let nmuons=data[1];
let dz = (depth[1]-depth[0])/10;
@@ -1187,11 +1139,13 @@ function startSequence(All) {
depth[i]=depth[i]/10;
nmuons[i]=nmuons[i]/norm;
}
Plot_xy("plotRge",depth,nmuons,['Depth (nm)','Stopped muons (%/nm)','E='+All['valEnergy']+'eV']);
Plot_xy("plotRge",depth,nmuons,['Depth (nm)','Stopped muons (%/nm)',ScanName+'='+Number(SValue)/1000+'keV']);
// Read the sequence data
let [cfort,cdata]= readDatFile(All["workPath"]+"/fort.33");
let lyrs = [6]; // back scattered muons
for (let ilayer=18;ilayer<cdata.length;ilayer++){lyrs.push(ilayer);} // the rest of the layers
// Now loop over all
Plotly.purge("plotFrac")
for (let ilayer of lyrs) {
// Normalize
let idata = cdata[ilayer];
@@ -1205,6 +1159,7 @@ function startSequence(All) {
}
Plot_xy("plotFrac",cdata[0],cdata[ilayer],['E (keV)','Fraction of muons (%)',chem]);
}
iScan++;
}
// An easy way to keep track of chemical formula is to replace impL_i by the corresponding chemical formula of layer i
@@ -1232,11 +1187,18 @@ function startSequence(All) {
return(0);
}
function readDatFile(filename) {
function readDatFile(filename,flag) {
// Read column data file and return
// flag=1 use XMLHttpRequest
// cols - labels of columns
// data - 2D array with the columns
let lines = readAsciiFile(filename);
// data - 2D array with the columns
let lines = "";
if (flag) {
lines = loadAscii(filename);
} else {
lines = readAsciiFile(filename);
}
console.log(lines);
let data = [];
let i=0; // line counter
for (let line of lines.trim().split('\n')) {
@@ -1355,7 +1317,7 @@ function tester() {
Plotly.purge("plotFrac")
Plotly.purge("plotRge")
//var test = All["L1Comp"];
//var test = All["L1Comp"];
//console.log('test='+test);
startSequence(All)
//CreateInpFile(All);
@@ -1369,3 +1331,106 @@ function tester() {
// });
}
// Function to load and ascii file
// async read, be careful with use
function loadAscii(file){
var xhttp;
if (file == "" || file == undefined || file == null) {
return 0;
} else {
// Make an HTTP request using the attribute value as the file name:
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 404) {
console.log("File "+file+" not found.");
}
}
}
file += "?" + Date();
xhttp.open("GET", file, false);
xhttp.send();
return(xhttp.responseText);
}
}
function sendToCGI(){
let args = "";
let All = prep_cfg(1);
for (key of Object.keys(All)) {
args += "&" + key + "=" + All[key] ;
}
let url = "http://musruser.psi.ch/cgi-bin/TrimSP2.cgi?go=start" + args;
console.log(url);
// document.getElementById("form_id").action = url;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Action to be performed when the document is read;
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xhttp.responseText,"text/html");
console.log(xmlDoc.getElementById("links").innerHTML);
document.getElementById("links").innerHTML = xmlDoc.getElementById("links").innerHTML;
// Now we can plot things
plotThings(All);
}
};
xhttp.open("GET",url,true);
//xhttp.responseType = 'document';
xhttp.send();
}
function plotThings(All) {
let fileNamePrefix = All["fileNamePrefix"];
let path = "/tmp/"+fileNamePrefix+"/";
//let fileName = path + fileNamePrefix + ".rge";
//let rgeDat = loadAscii(fileName);
//console.log(rgeDat);
if (All["scanSeq"]) {
// For multiple runs or a scan
if (All["scanType"]=="scanVals") {
var SValues=All["scanList"].split(/,/);
var SdzValues=All["scanListdz"].split(/,/);
if (SValues.length == SdzValues.length) {All["SdzFlag"]=1;}
} else {
for (let Val=parseInt(All["scanFrom"]);Val<=parseInt(All["scanTo"]);Val=Val+parseInt(All["scanStep"])) {
SValues.push(Val);
}
}
let ScanName = "";
let ScanAttrib = "";
ScanName = "E";
ScanAttrib = "valEnergy";
if (All["comboScan"]==1) {
ScanName = "SigE";
ScanAttrib = "sigEnergy";
} else if (All["comboScan"]==2) {
ScanName = "Angle";
ScanAttrib = "valAngle";
} else if (All["comboScan"]==3) {
ScanName = "SigAngle";
ScanAttrib = "sigAngle";
} else if (All["comboScan"]==4) {
ScanName = "N";
ScanAttrib = "numberProj";
} else if (All["comboScan"]==5) {
ScanName = "Ld"+All["scandL"];
ScanAttrib = "L"+All["scandL"]+"d";
}
let iScan=0;
for (var SValue of SValues) {
let fileName = path + fileNamePrefix + "_" + ScanName + SValue + ".rge";
let [cols,data]= readDatFile(fileName,1);
console.log(fileName,cols,data);
}
} else {
// For a single run
let fileName = path + fileNamePrefix + ".rge";
let [cols,data]= readDatFile(fileName,1);
console.log(fileName,cols,data);
}
}