-
Notifications
You must be signed in to change notification settings - Fork 0
/
matinfio.min.js
26 lines (26 loc) · 12.6 KB
/
matinfio.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
String.prototype.startswith=function(l){return 0===this.indexOf(l)};String.prototype.endswith=function(l,p){var w=this.toString();if(void 0===p||p>w.length)p=w.length;p-=l.length;l=w.indexOf(l,p);return-1!==l&&l===p};String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")};String.prototype.isnumeric=function(){return!isNaN(parseFloat(this))&&isFinite(this)};
var MatinfIO=function(l,p){function w(a){if(0<a.indexOf("_cell_angle_gamma ")&&0<a.indexOf("loop_"))return"CIF";if(0<a.indexOf('"immutable_id"')&&0<a.indexOf('"cartesian_site_positions"')&&0<a.indexOf('"lattice_vectors"'))return"OPTIMADE";a=a.toString().replace(/(\r\n|\r)/gm,"\n").split("\n");for(var b=6;9>b&&a[b];b++)if("direct"==a[b].trim().toLowerCase().substr(0,6))return"POSCAR";return"unknown"}function A(a,b,h,k,e,f){if(!(a&&b&&h&&k&&e&&f))return p.error("Error: invalid cell definition!"),!1;
k=k*Math.PI/180;e=e*Math.PI/180;f=f*Math.PI/180;var d=[0,0,1],n=[1,0,0];d=l.divide(d,l.norm(d));n=l.subtract(n,l.multiply(l.dot(n,d),d));n=l.divide(n,l.norm(n));var q=l.cross(d,n);a=l.multiply(a,[1,0,0]);b=l.multiply(b,[l.cos(f),l.sin(f),0]);var c=l.cos(e);k=l.divide(l.subtract(l.cos(k),l.multiply(l.cos(e),l.cos(f))),l.sin(f));e=l.sqrt(l.subtract(l.subtract(1,l.multiply(c,c)),l.multiply(k,k)));h=l.multiply(h,[c,k,e]);return l.multiply([a,b,h],[n,q,d])}function G(a){var b=[],h=[],k=0;for(a.forEach(function(n){b.push(l.norm(n))});3>
k;k++){var e=k-1;var f=k-2;var d=b[e]*b[f];e=1E-16<d?180/Math.PI*Math.acos(l.dot(a[e],a[f])/d):90;h.push(e)}return b.concat(h)}function H(a){var b=!1;if(6==Object.keys(a.cell).length){var h=A(a.cell.a,a.cell.b,a.cell.c,a.cell.alpha,a.cell.beta,a.cell.gamma);b=a.cell;var k=a.sg_name||a.ng_name?(a.sg_name?a.sg_name:"")+(a.ng_name?" ("+a.ng_name+")":""):!1;k&&(b.symlabel=k)}else h=a.cell;a.atoms.length||p.warning("Note: no atomic coordinates supplied");b={atoms:[],cell:h,descr:b,overlayed:{},info:a.info,
mpds_data:a.mpds_data,mpds_demo:a.mpds_demo};var e;k={};for(var f=0,d=a.atoms.length,n={},q={};f<d;f++){var c=[a.atoms[f].x,a.atoms[f].y,a.atoms[f].z],g=c.map(function(r){return r.toFixed(2)}).join(",");if(q.hasOwnProperty(g)){var u="";for(e in b.atoms[q[g]].overlays)"S"==e?-1==n[g].indexOf(a.atoms[f].symbol)&&(u=" "+a.atoms[f].symbol,n[g].push(a.atoms[f].symbol)):u="N"==e?", "+(f+1):"_atom_site_occupancy"==e?"+"+a.atoms[f].overlays[e]:" "+a.atoms[f].overlays[e],b.atoms[q[g]].overlays[e]+=u}else{u=
y.JmolColors[a.atoms[f].symbol]?y.JmolColors[a.atoms[f].symbol]:"#FFFF00";var m=y.AseRadii[a.atoms[f].symbol]?y.AseRadii[a.atoms[f].symbol]:.66;k={S:a.atoms[f].symbol,N:f+1};for(e in a.atoms[f].overlays)k[e]=a.atoms[f].overlays[e];c=a.cartesian?c:l.multiply(c,h);b.atoms.push({x:c[0],y:c[1],z:c[2],c:u,r:m,overlays:k});q[g]=b.atoms.length-1;n[g]=[a.atoms[f].symbol]}}for(var v in k)"S"!==v&&"N"!==v&&(b.overlayed[v]=C[v]);return b}function I(a){var b="data_matinfio\n";if(6==Object.keys(a.cell).length){var h=
a.cell;var k=A(a.cell)}else h=G(a.cell),k=a.cell;b+="_cell_length_a "+h[0].toFixed(6)+"\n";b+="_cell_length_b "+h[1].toFixed(6)+"\n";b+="_cell_length_c "+h[2].toFixed(6)+"\n";b+="_cell_angle_alpha "+h[3].toFixed(6)+"\n";b+="_cell_angle_beta "+h[4].toFixed(6)+"\n";b+="_cell_angle_gamma "+h[5].toFixed(6)+"\n";b+="_symmetry_space_group_name_H-M 'P1'\n_symmetry_Int_Tables_number 1\n";b+="\nloop_\n";b+=" _symmetry_equiv_pos_as_xyz\n";b+=" +x,+y,+z\n";b+="\nloop_\n";b+=" _atom_site_label\n";b+=
" _atom_site_type_symbol\n";b+=" _atom_site_fract_x\n";b+=" _atom_site_fract_y\n";b+=" _atom_site_fract_z\n";if(a.cartesian){var e=k;a.atoms.forEach(function(f,d){var n=l.lusolve(e,[f.x,f.y,f.z]);n=l.transpose(n)[0];b+=" "+f.symbol+(d+1)+" "+f.symbol+" "+n[0].toFixed(3)+" "+n[1].toFixed(3)+" "+n[2].toFixed(3)+"\n"})}else a.atoms.forEach(function(f,d){b+=" "+f.symbol+(d+1)+" "+f.symbol+" "+f.x.toFixed(3)+" "+f.y.toFixed(3)+" "+f.z.toFixed(3)+"\n"});return b}function J(a){a.symops&&p.warning("Warning! Reading of symmetry operations is not yet implemented, expect error now.");
var b=6==Object.keys(a.cell).length?A(a.cell.a,a.cell.b,a.cell.c,a.cell.alpha,a.cell.beta,a.cell.gamma):a.cell;var h=[],k=[],e=[],f=[];h=b[0].map(function(g,u){return b.map(function(m){return m[u]})});k=k.concat.apply(k,h);var d=0,n=a.atoms.length;if(a.types)for(;d<n;d++)f.push([a.atoms[d].x,a.atoms[d].y,a.atoms[d].z]);else{var q=[];for(h={};d<n;d++)-1==Object.keys(h).indexOf(a.atoms[d].symbol)?h[a.atoms[d].symbol]=[[a.atoms[d].x,a.atoms[d].y,a.atoms[d].z]]:h[a.atoms[d].symbol].push([a.atoms[d].x,
a.atoms[d].y,a.atoms[d].z]);d=1;for(var c in h){for(n=0;n<h[c].length;n++)f.push(h[c][n]),q.push(d);d++}}e=e.concat.apply(e,f);return{cell:k,atoms:e,types:q||a.types,symlabel:a.sg_name||a.ng_name?(a.sg_name?a.sg_name:"")+(a.ng_name?" ("+a.ng_name+")":""):!1}}function D(a){var b=[],h=[],k=[];a=a.toString().replace(/(\r\n|\r)/gm,"\n").split("\n");var e={cell:{},atoms:[],cartesian:!1},f=!1,d=!1,n=!1,q="",c="",g="";d=[];var u="a b c alpha beta gamma".split(" "),m=["_atom_site_label","_atom_site_type_symbol",
"_atom_site_fract_x","_atom_site_fract_y","_atom_site_fract_z"],v=["label","symbol","x","y","z"],r=[0,1],t=[];for(x in C)t.push(m.length),m.push(x);var x=0;for(var K=a.length;x<K;x++)if(!a[x].startswith("#"))if(c=a[x].trim()){g=c.toLowerCase();d=!1;if(g.startswith("data_"))d=!0,f=!1,k=[],n=!1,q=c.substr(5);else if(g.startswith("_cell_")){f=!1;d=c.split(" ");c=d[0].split("_");c=c[c.length-1];-1!==u.indexOf(c)&&d[d.length-1]&&("a"==c&&(g=d[d.length-1].split("."),2==g[g.length-1].length&&(e.mpds_demo=
!0)),e.cell[c]=parseFloat(d[d.length-1]));continue}else if(g.startswith("_symmetry_space_group_name_h-m")||g.startswith("_space_group.patterson_name_h-m")){f=!1;e.sg_name=a[x].substr(31).replace(/"/g,"").replace(/'/g,"").trim();continue}else if(g.startswith("_space_group.it_number")||g.startswith("_space_group_it_number")||g.startswith("_symmetry_int_tables_number")){f=!1;d=c.split(" ");e.ng_name=d[d.length-1].trim();continue}else{if(g.startswith("_cif_error"))return p.error(c.substr(12,c.length-
13)),!1;if(g.startswith("_pauling_file_entry")){e.mpds_data=!0;continue}else if(g.startswith("loop_")){f=!0;k=[];n=!1;continue}}if(f)if(c.startswith("_symmetry_equiv")||c.startswith("_space_group"))n=!0;else if(c.startswith("_"))k.push(c);else if(n)h.push(c.replace(/"/g,"").replace(/'/g,""));else{d=c.replace(/\t/g," ").split(" ").filter(function(L){return L?!0:!1});c={overlays:{}};g=0;for(var M=k.length;g<M;g++){var z=m.indexOf(k[g]);if(-1!=z){var E=r.indexOf(z);1==E?d[g]=d[g].charAt(0).toUpperCase()+
d[g].slice(1).toLowerCase():0>E&&(d[g]=parseFloat(d[g]));-1<t.indexOf(z)?c.overlays[m[z]]=d[g]:c[v[z]]=d[g]}}void 0!==c.x&&void 0!==c.y&&void 0!==c.z&&(c.label&&(c.overlays.label=c.label,c.symbol||(c.symbol=c.label.replace(/[0-9]/g,""))),!y.JmolColors[c.symbol]&&c.symbol&&1<c.symbol.length&&(c.symbol=c.symbol.substr(0,c.symbol.length-1)),c.symbol&&e.atoms.push(c))}else d&&e.atoms.length&&(e.info=q,1<h.length&&(e.symops=h),b.push(e),e={cell:{},atoms:[],cartesian:!1},h=[])}else f=!1,k=[],n=!1;e.cell.gamma&&
(e.info=q,1<h.length&&(e.symops=h),b.push(e));if(b.length)return b[b.length-1];p.error("Error: unexpected CIF format!");return!1}function B(a){a=a.toString().replace(/(\r\n|\r)/gm,"\n").split("\n");var b=[],h=[],k=1,e=[],f=[],d=[],n=[],q=["x","y","z","symbol"],c=0,g=0,u=a.length,m=[],v=0;m=[];var r=[];a:for(;c<u;c++)if(0==c){m=a[c].split(" ").filter(function(t){return t?!0:!1});r=Object.keys(y.AseRadii);for(g=0;g<m.length;g++)if(-1==r.indexOf(m[g]))continue a;f=m}else if(1==c)k=parseFloat(a[c]);else if(-1!==
[2,3,4].indexOf(c))b.push(a[c].split(" ").filter(function(t){return t?!0:!1}).map(Number));else if(5==c)if(m=a[c].split(" ").filter(function(t){return t?!0:!1}),m[0].isnumeric())for(e=m.map(Number),g=0;g<e.length;g++)for(m=0;m<e[g];m++)n.push(g+1);else f=m;else if(6==c){if("direct"==a[c].trim().toLowerCase().substr(0,6))continue a;m=a[c].split(" ").filter(function(t){return t?!0:!1});if(m[0].isnumeric())for(e=m.map(Number),g=0;g<e.length;g++)for(m=0;m<e[g];m++)n.push(g+1);else f=m}else if(6<c){if(9>
c){if(f.length&&!d.length)for(g=0;g<f.length;g++)for(m=0;m<e[g];m++)d.push(f[g]);if(-1!==["direct","select"].indexOf(a[c].trim().toLowerCase().substr(0,6)))continue a}r={};m=a[c].replace("#","").replace("!","").split(" ").filter(function(t){return t?!0:!1});if(!m.length)break;else if(3==m.length)d.length?m.push(d[v]):m.push("Xx");else if(3>m.length)return p.error("Error: invalid atom definition!"),!1;for(g=0;4>g;g++)r[q[g]]=3>g?parseFloat(m[g]):m[g];r.symbol=r.symbol.replace(/\W+/,"").replace(/\d+/,
"");r.symbol.length||(r.symbol="Xx");h.push(r);v++}b=l.multiply(b,k);if(h.length)return{cell:b,atoms:h,types:n,cartesian:!1};p.error("Error: unexpected POSCAR format!");return!1}function F(a){a=JSON.parse(a);var b=[];try{var h=a.data[0]}catch(k){h=a}if(!h)return p.error("Error: unexpected OPTIMADE format!"),!1;a=h.attributes.cartesian_site_positions.length;if(!a)return p.error("Error: no atomic positions found!"),!1;if(h.attributes.species&&h.attributes.species[a-1]&&h.attributes.species[a-1].chemical_symbols)h.attributes.species.forEach(function(k,
e){b.push({x:h.attributes.cartesian_site_positions[e][0],y:h.attributes.cartesian_site_positions[e][1],z:h.attributes.cartesian_site_positions[e][2],symbol:k.chemical_symbols[0]})});else if(h.attributes.species_at_sites)h.attributes.species_at_sites.forEach(function(k,e){b.push({x:h.attributes.cartesian_site_positions[e][0],y:h.attributes.cartesian_site_positions[e][1],z:h.attributes.cartesian_site_positions[e][2],symbol:k.replace(/\W+/,"").replace(/\d+/,""),overlays:{label:k}})});else return p.error("Error: no atomic data found!"),
!1;return{cell:h.attributes.lattice_vectors,atoms:b,info:"id="+h.id,cartesian:!0}}var y={JmolColors:{D:"#FFFFC0",H:"#FFFFFF",He:"#D9FFFF",Li:"#CC80FF",Be:"#C2FF00",B:"#FFB5B5",C:"#909090",N:"#3050F8",O:"#FF0D0D",F:"#90E050",Ne:"#B3E3F5",Na:"#AB5CF2",Mg:"#8AFF00",Al:"#BFA6A6",Si:"#F0C8A0",P:"#FF8000",S:"#FFFF30",Cl:"#1FF01F",Ar:"#80D1E3",K:"#8F40D4",Ca:"#3DFF00",Sc:"#E6E6E6",Ti:"#BFC2C7",V:"#A6A6AB",Cr:"#8A99C7",Mn:"#9C7AC7",Fe:"#E06633",Co:"#F090A0",Ni:"#50D050",Cu:"#C88033",Zn:"#7D80B0",Ga:"#C28F8F",
Ge:"#668F8F",As:"#BD80E3",Se:"#FFA100",Br:"#A62929",Kr:"#5CB8D1",Rb:"#702EB0",Sr:"#00FF00",Y:"#94FFFF",Zr:"#94E0E0",Nb:"#73C2C9",Mo:"#54B5B5",Tc:"#3B9E9E",Ru:"#248F8F",Rh:"#0A7D8C",Pd:"#006985",Ag:"#C0C0C0",Cd:"#FFD98F",In:"#A67573",Sn:"#668080",Sb:"#9E63B5",Te:"#D47A00",I:"#940094",Xe:"#429EB0",Cs:"#57178F",Ba:"#00C900",La:"#70D4FF",Ce:"#FFFFC7",Pr:"#D9FFC7",Nd:"#C7FFC7",Pm:"#A3FFC7",Sm:"#8FFFC7",Eu:"#61FFC7",Gd:"#45FFC7",Tb:"#30FFC7",Dy:"#1FFFC7",Ho:"#00FF9C",Er:"#00E675",Tm:"#00D452",Yb:"#00BF38",
Lu:"#00AB24",Hf:"#4DC2FF",Ta:"#4DA6FF",W:"#2194D6",Re:"#267DAB",Os:"#266696",Ir:"#175487",Pt:"#D0D0E0",Au:"#FFD123",Hg:"#B8B8D0",Tl:"#A6544D",Pb:"#575961",Bi:"#9E4FB5",Po:"#AB5C00",At:"#754F45",Rn:"#428296",Fr:"#420066",Ra:"#007D00",Ac:"#70ABFA",Th:"#00BAFF",Pa:"#00A1FF",U:"#008FFF",Np:"#0080FF",Pu:"#006BFF",Am:"#545CF2",Cm:"#785CE3",Bk:"#8A4FE3",Cf:"#A136D4",Es:"#B31FD4",Fm:"#B31FBA",Md:"#B30DA6",No:"#BD0D87",Lr:"#C70066",Rf:"#CC0059",Db:"#D1004F",Sg:"#D90045",Bh:"#E00038",Hs:"#E6002E",Mt:"#EB0026"},
AseRadii:{X:.66,H:.31,He:.28,Li:1.28,Be:.96,B:.84,C:.76,N:.71,O:.66,F:.57,Ne:.58,Na:1.66,Mg:1.41,Al:1.21,Si:1.11,P:1.07,S:1.05,Cl:1.02,Ar:1.06,K:2.03,Ca:1.76,Sc:1.7,Ti:1.6,V:1.53,Cr:1.39,Mn:1.39,Fe:1.32,Co:1.26,Ni:1.24,Cu:1.32,Zn:1.22,Ga:1.22,Ge:1.2,As:1.19,Se:1.2,Br:1.2,Kr:1.16,Rb:2.2,Sr:1.95,Y:1.9,Zr:1.75,Nb:1.64,Mo:1.54,Tc:1.47,Ru:1.46,Rh:1.42,Pd:1.39,Ag:1.45,Cd:1.44,In:1.42,Sn:1.39,Sb:1.39,Te:1.38,I:1.39,Xe:1.4,Cs:2.44,Ba:2.15,La:2.07,Ce:2.04,Pr:2.03,Nd:2.01,Pm:1.99,Sm:1.98,Eu:1.98,Gd:1.96,Tb:1.94,
Dy:1.92,Ho:1.92,Er:1.89,Tm:1.9,Yb:1.87,Lu:1.87,Hf:1.75,Ta:1.7,W:1.62,Re:1.51,Os:1.44,Ir:1.41,Pt:1.36,Au:1.36,Hg:1.32,Tl:1.45,Pb:1.46,Bi:1.48,Po:1.4,At:1.5,Rn:1.5,Fr:2.6,Ra:2.21,Ac:2.15,Th:2.06,Pa:2,U:1.96,Np:1.9,Pu:1.87,Am:1.8,Cm:1.69,Bk:3,Cf:3,Es:3,Fm:3,Md:3,No:3,Lr:3,Rf:3,Db:3,Sg:3,Bh:3,Hs:3,Mt:3}},C={_atom_site_occupancy:"site occupancies",_atom_site_charge:"formal charges",label:"labels"};return{to_player:function(a){switch(w(a)){case "CIF":var b=D(a);break;case "POSCAR":b=B(a);break;case "OPTIMADE":b=
F(a);break;default:p.error("Error: file format not recognized!")}return b?H(b):!1},to_flatten:function(a){switch(w(a)){case "CIF":var b=D(a);break;case "POSCAR":b=B(a);break;case "OPTIMADE":p.error("OPTIMADE not supported!");break;default:p.error("Error: file format not recognized!")}return b?J(b):!1},to_cif:function(a){switch(w(a)){case "CIF":return a;case "POSCAR":var b=B(a);break;case "OPTIMADE":b=F(a);break;default:p.error("Error: file format not recognized!")}return b?I(b):!1},version:"0.5.1"}};
"undefined"!==typeof module&&module.exports?module.exports=MatinfIO:"function"===typeof require&&"function"===typeof require.specified&&define(function(){return MatinfIO});