Debian Codegraph @

ncrystal_python/ncrystal_vdos2ncmat

raw
1 #!/usr/bin/env python3
2
3 ################################################################################
4 ## ##
5 ## This file is part of NCrystal (see https://mctools.github.io/ncrystal/) ##
6 ## ##
7 ## Copyright 2015-2022 NCrystal developers ##
8 ## ##
9 ## Licensed under the Apache License, Version 2.0 (the "License"); ##
10 ## you may not use this file except in compliance with the License. ##
11 ## You may obtain a copy of the License at ##
12 ## ##
13 ## http://www.apache.org/licenses/LICENSE-2.0 ##
14 ## ##
15 ## Unless required by applicable law or agreed to in writing, software ##
16 ## distributed under the License is distributed on an "AS IS" BASIS, ##
17 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ##
18 ## See the License for the specific language governing permissions and ##
19 ## limitations under the License. ##
20 ## ##
21 ################################################################################
22
23 """
24
25 Script which can read vdos curves from either .ncmat files or simple
26 two-column ascii text files (the two columns being energy grid and density) and
27 help users prepare output suitable for inclusion in .ncmat files
28
29 """
30
31 ################################################################################################
32 ####### Common code for all NCrystal cmdline scripts needing to import NCrystal modules ########
33 import sys
34 pyversion = sys.version_info[0:3]
35 _minpyversion=(3,6,0)
36 if pyversion < _minpyversion:
37 raise SystemExit('Unsupported python version %i.%i.%i detected (needs %i.%i.%i or later).'%(pyversion+_minpyversion))
38 import os
39 import pathlib
40
41 def maybeThisIsConda():
42 return ( os.environ.get('CONDA_PREFIX',None) or
43 os.path.exists(os.path.join(sys.base_prefix, 'conda-meta')) )
44
45 def fixSysPathAndImportNCrystal( *, allowfail = False ):
46 thisdir = pathlib.Path( __file__ ).parent
47 def extract_cmake_pymodloc():
48 p = thisdir / 'ncrystal-config'
49 if not p.exists():
50 return
51 with p.open('rt') as fh:
52 for i,l in enumerate(fh):
53 if i==30:
54 break
55 if l.startswith('#CMAKE_RELPATH_TO_PYMOD:'):
56 l = l[24:].strip()
57 return ( thisdir / l ) if l else None
58 pml = extract_cmake_pymodloc()
59 hack_syspath = pml and ( pml / 'NCrystal' / '__init__.py' ).exists()
60 if hack_syspath:
61 sys.path.insert(0,str(pml.absolute().resolve()))
62 try:
63 import NCrystal
64 except ImportError:
65 if allowfail:
66 return
67 msg = 'ERROR: Could not import the NCrystal Python module'
68 if maybeThisIsConda():
69 msg += ' (if using conda it might help to close your terminal and activate your environment again)'
70 elif not hack_syspath:
71 msg += ' (perhaps your PYTHONPATH is misconfigured)'
72 raise SystemExit(msg)
73 return NCrystal
74 ################################################################################################
75 NC = fixSysPathAndImportNCrystal()
76
77 import numpy as np
78 import argparse
79
80 units_2_fact = {
81 'eV' : 1.0,
82 'meV' : 1e-3,
83 'keV' : 1e3,
84 'MeV' : 1e6,
85 'THz' : NC.constant_planck*1e12,
86 'GHz' : NC.constant_planck*1e9,
87 'MHz' : NC.constant_planck*1e6,
88 'kHz' : NC.constant_planck*1e3,
89 'Hz' : NC.constant_planck,
90 }
91 units_opts = ', '.join(sorted(units_2_fact.keys()))
92
93 def decodeFileName(filename):
94 if '@@' in filename:
95 path,select = filename.split('@@',1)
96 else:
97 path,select = filename,None
98 bn=os.path.basename(path)
99 return dict(path=path,select=select,basename=bn,
100 title=bn if not select else '%s in %s'%((select if not select.isdigit(
101 ) else f'column #{select}'),bn))
102
103 def getVDOSFromFile(fn):
104 fnd = decodeFileName(fn)
105 if fnd['basename'].endswith('.ncmat'):
106 return getVDOSFromNCMAT(fn)
107 return getVDOSFromTXT(fn)
108
109 def getVDOSFromTXT(fn):
110 fn = decodeFileName(fn)
111 select=None
112 if fn['select']:
113 assert fn['select'].isdigit(),"selection must be column number"
114 select = int(fn['select'])
115 #figure out unit:
116 unit=None
117 for l in open(fn['path']):
118 if l.startswith('#') and 'unit' in l:
119 _=l.split('#',1)[1].split(':',1)
120 if not len(_)==2:
121 continue
122 unit=_[1].strip()
123 if not unit in units_2_fact.keys():
124 raise SystemExit(f'Unknown unit "{unit}" specified in {fn["path"]}. Valid choices are: {units_opts}')
125 break
126 if not unit:
127 raise SystemExit(f'Missing energy/frequency unit in {fn["path"]}. Please put initial line with content like "#unit:THz". Valid units are: {units_opts}')
128 usecols=None
129 if select is not None:
130 assert select>0
131 usecols=(0,select)
132 _ = np.genfromtxt(fn['path'],dtype=[('egrid','f8'),('density','f8')],usecols=usecols)
133 egrid=_['egrid'].copy()
134 density=_['density'].copy()
135 density /= density.max()
136 return egrid.copy()*units_2_fact[unit],density.copy()
137
138 def getVDOSFromNCMAT(fn):
139 fnd = decodeFileName(fn)
140 info = NC.createInfo(fnd['path'])
141 select = fnd['select']
142 di_vdos = [di for di in info.dyninfos if isinstance(di,NC.Info.DI_VDOS)]
143 if not select is None:
144 l=[]
145 for di in di_vdos:
146 dl=di.atomData.displayLabel()
147 if dl!=select:
148 print(f"NB: Ignoring (due to selection) VDOS for element {dl} in file {fn}.")
149 else:
150 l+=[di]
151 if not l:
152 raise SystemExit(f'ERROR: Could not find VDOS in file {fn} for selected element: {select}')
153 if not len(l)==1:
154 raise SystemExit(f'ERROR: Multiple VDOS entries in file {fn} for selected element: {select} (which is rather odd!)')
155 di_vdos = l
156 if len(di_vdos)>1:
157 s=' '.join(di.atomData.displayLabel() for di in di_vdos)
158 raise SystemExit(f"ERROR: Multiple VDOS entries found in file {fn}. Please select one of them (by putting \"@@<element>\" after the file-name): {s}")
159 elif not di_vdos:
160 raise SystemExit(f"ERROR: No vdos found in file {fn}")
161 eg,ds = di_vdos[0].vdosOrigEgrid(),di_vdos[0].vdosOrigDensity()
162 ds /= ds.max()
163 if len(eg)==2:
164 eg = np.linspace(eg[0],eg[1],len(ds))
165 return eg.copy(),ds.copy()
166
167 def parseArgs():
168 descr="""
169
170 Script which can read vdos curves from either .ncmat files or simple two-column
171 ascii text files (the two columns being energy grid and density) and help users
172 prepare output suitable for inclusion in .ncmat files. When the input is not an
173 .ncmat file, the user must specify the energy grid units by adding a line in the file
174 like for instance "#unit:THz" or "#unit:meV".
175
176 In case of NCMAT files with more than one VDOS, just post-fix the filename with
177 the element to investigate, separated by '@@', e.g. "Al2O3.ncmat@@O". In case of
178 .txt files with more than 2 columns, select the column representing the VDOS
179 density in the same manner, e.g. "Al2O3.txt@2" (the first column is always the
180 energy or frequency).
181
182 Thus it is possible to plot the curve, compare against vdos curves from other
183 .ncmat files, apply low-E truncation (cutoff), regularise binning, and overlay
184 with an ideal Debye spectrum for a given Debye energy or Debye
185 temperature. Finally, when running without --plot, it will output the resulting
186 spectrum into format which is ready for inclusion in .ncmat files.
187
188 """
189 parser = argparse.ArgumentParser(description=descr)
190 parser.add_argument("FILE",help="Either .ncmat file with VDOS or a text file with two colums of data: egrid and density.")
191 parser.add_argument('--plot','-p', action='store_true',help='Plot extracted spectrum')
192 parser.add_argument("--cutoff",'-c',nargs='+',default=None,type=float,
193 help="""Emin cutoff points in eV (more than one can be provided for simultaneous
194 inspection with --plot).""")
195 parser.add_argument("--unit",'-u',default='meV',type=str,
196 help=f"Visualisation unit (ignored unless --plot is supplied). Defaults to meV. Possible options are {units_opts}.")
197 parser.add_argument("--ref",'-r',nargs='+',
198 action='append',type=str,
199 help="""Optionally provide list of .ncmat files with
200 vdos data, to superimpose on plots.""")
201 parser.add_argument("--forceregular",'-f',type=int,nargs='?',default=0,
202 help="""Optionally provide this argument to
203 reparameterise with that amount of linearly spaced
204 points in [emin,emax+epsilon], where epsilon is chosen
205 so the grid can be extended to 0 with a whole number of
206 bins. This format will be directly used by NCrystal
207 without on-the-fly reparameterisation upon loading.""")
208 parser.add_argument("--debye",'-d',nargs='?',default='',type=str,
209 help="""Set to debye temperature (unit K) or egrid point
210 (units like meV, eV, THz, ...) in order to plot Debye
211 spectrum with that parameter on top.""")
212 parser.add_argument('--g1',default=0.0,type=float,
213 help="""Use with --plot to show Sjolander's G1 function at the indicated temperature
214 (in kelvin) instead of the DOS directly. This is the Symmetric G1 fct without a
215 detailed balance factor, and it will be plotted assuming gamma0=1.0.""")
216 parser.add_argument('--stdout',action='store_true',help="""Produce no output file but
217 print vdos_egrid and vdos_density lines to stdout (for scripting)""")
218
219 dpi_default=200
220 parser.add_argument('--dpi', default=-1,type=int,
221 help="""Change plot resolution. Set to 0 to leave matplotlib defaults alone.
222 (default value is %i, or whatever the NCRYSTAL_DPI env var is set to)."""%dpi_default)
223
224 args=parser.parse_args()
225 if args.dpi>3000:
226 parser.error('Too high DPI value requested.')
227 if args.dpi==-1:
228 _=os.environ.get('NCRYSTAL_DPI',None)
229 if _:
230 try:
231 _=int(_)
232 if _<0:
233 raise ValueError
234 except ValueError:
235 print("ERROR: NCRYSTAL_DPI environment variable must be set to integer >=0")
236 raise SystemExit
237 if _>3000:
238 parser.error('Too high DPI value requested via NCRYSTAL_DPI environment variable.')
239 args.dpi=_
240 else:
241 args.dpi=dpi_default
242
243 if args.ref:
244 args.ref = [item for sublist in args.ref for item in sublist]
245 if args.ref and not args.plot:
246 parser.error(f'Option --ref requires --plot')
247 if args.unit and not args.unit in units_2_fact:
248 parser.error(f'Unknown unit {args.unit}. Valid options are {units_opts}')
249 if args.debye and not args.plot:
250 parser.error(f'Option --debye requires --plot')
251 if args.stdout and args.plot:
252 parser.error(f'Option --stdout can not be used with --plot')
253 if args.cutoff and len(args.cutoff)>1 and not args.plot:
254 parser.error(f'Option --cutoff can only have one argument when not using --plot')
255 if args.cutoff and len(args.cutoff)>1 and args.forceregular:
256 parser.error(f'Option --cutoff can only have one argument when using --forceregular')
257
258 if args.debye:
259 if args.debye.endswith('K'):
260 args.debye = float(args.debye[0:-1])*NC.constant_boltzmann
261 else:
262 #find (longest, so "meV" does not trigger "eV") fitting unit:
263 l=[ (len(u),u) for u in units_2_fact.keys() if args.debye.endswith(u) ]
264 l.sort()
265 if not l:
266 parser.error("Option --debye requires unit (see --help)")
267 unit = l[-1][1]
268 args.debye = units_2_fact[unit] * float(args.debye[0:-len(unit)])
269 return args
270
271 args=parseArgs()
272 file_decoded = decodeFileName(args.FILE)
273 args_file_basename=file_decoded['basename']
274 egrid,density = getVDOSFromFile(args.FILE)
275
276 assert len(egrid) == len(density)
277 print (f"Loaded VDOS with {len(density)} grid points from {args_file_basename}")
278
279 numpy_is_sorted = lambda a: np.all(a[:-1] <= a[1:])
280 numpy_is_strongly_sorted = lambda a: np.all(a[:-1] < a[1:])
281
282 if not numpy_is_strongly_sorted(egrid):
283 for i in range(len(egrid)-1):
284 if not egrid[i] < egrid[i+1]:
285 print("Problems detected in egrid points with values ",egrid[i],"and",egrid[i+1])
286 raise SystemExit('ERROR: egrid values (first column) of input file are not in sorted'
287 +' (ascending) order, or there are identical elements.')
288
289 cutoffs=[]
290 if args.cutoff:
291 for c in args.cutoff:
292 if c >= egrid[-1]:
293 raise SystemExit(f'ERROR: Cutoff value {c} is higher than highest point in egrid')
294 i=np.searchsorted(egrid,c)
295 assert i==0 or egrid[i-1]<c
296 assert egrid[i]>=c
297 cutoffs+=[ (i, egrid[i] ) ]
298 print(f" => Mapping cutoff value of {c} to grid point at {cutoffs[-1][1]}")
299
300 def applyCutoff(egrid,density,cutoffs):
301 if cutoffs:
302 assert len(cutoffs)==1
303 c_idx,c_val = cutoffs[0]
304 return egrid[c_idx:], density[c_idx:]
305 return egrid,density
306
307 if args.forceregular or (not args.plot):
308 if applyCutoff(egrid,density,cutoffs)[0][0]<=1e-5:
309 raise SystemExit(f"""
310 ERROR: The first value in the loaded egrid is {egrid[0]} which is less than 1e-5eV.
311 This is not allowed when using --plot or --forceregular.
312 Please use the --cutoff parameter to remove lowest part of input spectrum (perhaps
313 after investigating the cutoff value with --plots).
314 """)
315
316 def regularise(egrid,density,n):
317 #first step back from any zeroes at the upper end:
318 i=1
319 while density[-i]==0.0:
320 i=i+1
321 safepeel = i-2
322 if safepeel>=1:
323 print (f"Ignoring {safepeel} last points while regularising since last {safepeel+1} points are 0.")
324 egrid,density = egrid[0:-(safepeel)],density[0:-(safepeel)]
325 emin,emax=egrid[0],egrid[-1]
326 print('old range',emin,emax)
327 THZ=NC.constant_planck*1e12,
328 print('old range [THZ]',emin/THZ,emax/THZ)
329
330 for k in range(1,1000000000):
331 #k is number of bins below emin, an integral number by definition in a regularised grid.
332 binwidth = emin/k
333 nbins=int(np.floor((emax-emin)/binwidth))+1
334 eps = (emin+nbins*binwidth)-emax
335 assert eps>=0.0
336 if nbins+1 >= n:
337 break
338 n=nbins+1
339 binwidth = emin/k
340 new_emax = emin + (n-1) * binwidth
341 if abs( (new_emax-binwidth) - emax ) < 1e-3*binwidth:
342 nbins -= 1
343 n -= 1
344 new_emax -= binwidth
345 print (f" ==> Choosing regular grid with n={n} pts from emin={emin} to emax={new_emax} ({new_emax-emax} beyond old emax)")
346 assert new_emax >= emax-binwidth*1.001e-3
347 new_egrid = np.linspace(emin,new_emax,n)
348 test=new_egrid[0] / ( (new_egrid[-1]-new_egrid[0])/(len(new_egrid)-1) )
349 assert abs(round(test)-test)<1e-6,f'{test}'
350 new_density = np.interp(new_egrid,egrid,density, left=0.0, right=0.0)
351 print(f'last density values in new grid: ',new_density[-5:])
352 return new_egrid,new_density
353
354 if args.forceregular:
355 regularised_egrid,regularised_density = regularise(*applyCutoff(egrid,density,cutoffs),args.forceregular)
356
357 if args.plot:
358 vis_unit=args.unit
359 vis_unit_fact = 1.0 / units_2_fact[vis_unit]
360
361 def plt_plot(egrid,dos,*aargs,**kwargs):
362 if args.g1 and args.g1 > 0.0:
363 #G1 = f(E)/(E*2*gamma0*sinh(E/2kT)) [symmetric G1 that is]
364 #u = E/2kT
365 #asymmetric means another factor of exp(-u).
366 #And exp(-u)/2sinh(u) = exp(-u) / (exp(u)-exp(-u) ) = 1 / ( exp(2u)-1)
367 #And exp(+u)/2sinh(u) = exp(+u) / (exp(u)-exp(-u) ) = 1 / ( 1-exp(-2u) )
368 #
369 #So with gamma0=0 we get:
370 egrid_eV = egrid.copy() / vis_unit_fact
371 T = args.g1
372 gamma0 = 1.0
373 two_u = egrid_eV / ( NC.constant_boltzmann * T )
374 #g1asym_neg = dos / ( egrid_eV * -np.expm1(-two_u) )
375 #g1asym_pos = dos / ( egrid_eV * np.expm1(two_u) )
376 g1sym = dos / (egrid_eV*np.sinh(0.5*two_u))
377 plt.plot(egrid,g1sym,*aargs,**kwargs)
378 else:
379 plt.plot(egrid,dos,*aargs,**kwargs)
380
381 import matplotlib as mpl
382 mpl.rcParams['figure.dpi']=args.dpi
383 #ability to quit plot windows with Q:
384 if 'keymap.quit' in mpl.rcParams and not 'q' in mpl.rcParams['keymap.quit']:
385 mpl.rcParams['keymap.quit'] = tuple(list(mpl.rcParams['keymap.quit'])+['q','Q'])
386 import matplotlib.pyplot as plt
387 plt.xlabel(vis_unit)
388 plt_plot(egrid*vis_unit_fact,density,'o-',label=file_decoded['title'])
389 if args.forceregular:
390 plt_plot(regularised_egrid*vis_unit_fact,regularised_density,'x-',label='regularised')
391 for c_idx, c_val in cutoffs:
392 d=density[c_idx]
393 # f(x)=k*x^2, f(c_val)=d<=> k*c_val^2 = d <=> k = d/c_val^2
394 x=np.linspace(0.0,c_val,3000)
395 plt_plot(x*vis_unit_fact,(d/c_val**2)*(x**2),label=f'with cutoff {c_val} eV')
396 if args.debye:
397 x=np.linspace(0.0,max(egrid.max(),args.debye),1000)
398 y = np.where( x<=args.debye, x**2 * ( density.max() / args.debye**2 ), 0.0 )
399 plt_plot(x*vis_unit_fact,y,
400 label=f'Debye spectrum (E_Debye={1000*args.debye:.5}meV, T_Debye={args.debye/NC.constant_boltzmann:.5}K)')
401 for r in (args.ref or []):
402 eg,ds = getVDOSFromFile(r)
403 plt_plot(eg*vis_unit_fact,ds,label=decodeFileName(r)['title'])
404 plt.legend()
405 plt.title(file_decoded['title'])
406 plt.grid(ls=':')
407 plt.show()
408 sys.exit(0)
409
410 if args.forceregular:
411 egrid, density = regularised_egrid,regularised_density
412 else:
413 egrid, density = applyCutoff(egrid,density,cutoffs)
414
415 #Check if egrid is linspace:
416 binwidth = (egrid[-1]-egrid[0])/(len(egrid)-1)
417 is_linspace=True
418 if not args.forceregular:
419 for i in range(len(egrid)-1):
420 bw=egrid[i+1]-egrid[i]
421 if abs(binwidth-bw)>0.01*binwidth:
422 is_linspace=False
423 break
424 if is_linspace:
425 print('NB: Detected linearly spaced input egrid')
426
427 #normalise so unity at highest point (gives smaller file sizes):
428 density /= density.max()
429
430 #remove excess trailing zeros
431 while len(density)>10 and density[-2]==0.0 and density[-1]==0.0:
432 density = density[0:-1]
433 egrid = egrid[0:-1]
434
435 egrid_cnt =''
436 if is_linspace:
437 egrid_cnt += f' vdos_egrid {egrid[0]:.14} {egrid[-1]:.14}'
438 else:
439 egrid_cnt += NC.formatVectorForNCMAT('vdos_egrid',egrid)
440 egrid_cnt += '\n'
441 egrid_cnt += NC.formatVectorForNCMAT('vdos_density',density)
442
443 if args.stdout:
444 print("<<<GENERATED-CONTENT-BEGIN>>>")
445 print(egrid_cnt,end='')
446 else:
447 outfn=pathlib.Path('converted_output.ncmat')
448 with outfn.open('wt') as fn:
449 fn.write(f"""NCMAT v5
450 #Autogenerated file from {args_file_basename}.
451 @DENSITY
452 1.0 g_per_cm3 #FIX{'ME'}!! Please replace with proper value, or remove and optionally provide crystal structure!
453 @DYNINFO
454 element <UNKNOWN-PLEASE-EDIT>
455 fraction 1
456 type vdos\n""")
457 fn.write(egrid_cnt)
458 fn.write('\n')
459 print(f"Wrote {outfn}")