mctools / ncrystal

NCrystal : a library for thermal neutron transport in crystals and other materials
https://mctools.github.io/ncrystal/
Other
41 stars 18 forks source link

return information from hklList in a python structure #164

Closed soerenschmidts closed 3 months ago

soerenschmidts commented 9 months ago

Is your feature request related to a problem? Please describe. No

Describe the solution you'd like easy access to components in hklList from python

tkittel commented 9 months ago

Hi @soerenschmidts ,

I guess it makes sense to have a more convenient method that returns data in e.g. numpy arrays (assuming one does not need the list of normals or specific hkl indices of a particular hkl family).

But a question is of course more specifically what you (and other users) would find useful? Do you just want d-spacings, multiplicities and f-squared, or also a representative hkl index. Do you need the ability to request just some (e.g. only those above some d-cutoff or the N entries with the longest d-spacing), or is the dcutoff parameter that one can apply in the cfg-string already sufficient for this?

Cheers, Thomas

tkittel commented 5 months ago

Hi @soerenschmidts,

Could you let me know exactly what form you had in mind?

Cheers, Thomas

tkittel commented 3 months ago

Hi @soerenschmidts,

So after our chat today, I added a object-oriented interface for this (I did NOT mark it as hidden or experimental despite what I said earlier today).

So here is an example of how one might use it once NCrystal 3.9.0 is out in a few days time:

import NCrystal as NC
info = NC.createInfo('Al_sg225.ncmat;temp=250K')

for e in info.hklObjects():
    print()
    print( e )#<- a quick look
    print( e.hkl_label, e.mult, e.d, e.f2 )
    print( e.h, e.k, e.l )#all Miller indices as Numpy arrays.
    #Implement whatever selection logic suits you:
    if e.d < 1.5:
        break

This gives the output:

HKL( hkl_label=(1,1,1), d=2.33803Aa, F2=1.78972barn, N=8 )
(1, 1, 1) 8 2.3380261031049243 1.7897164633055231
[1 1 1 1] [ 1  1 -1 -1] [ 1 -1  1 -1]

HKL( hkl_label=(2,0,0), d=2.02479Aa, F2=1.75338barn, N=6 )
(2, 0, 0) 6 2.02479 1.7533832510151894
[2 0 0] [0 2 0] [0 0 2]

HKL( hkl_label=(2,2,0), d=1.43174Aa, F2=1.61528barn, N=12 )
(2, 2, 0) 12 1.4317427394787092 1.6152782155251113
[2 2 2 2 0 0] [ 2  0  0 -2  2  2] [ 0  2 -2  0  2 -2]

Notice that the arrays with Miller indices have length of multiplicity/2, not multiplicity, since we don't waste array space on the obvious symmetry (h,k,l)->(-h,-k,-l).

You can access the usual inline documentation for more information about the objects. For instance, by typing:

help(next(info.hklObjects()))

Which gives the help text:

Help on HKLEntry in module NCrystal._hklobjects object:

class HKLEntry(builtins.object)
 |  HKLEntry(hh, kk, ll, mult, dsp, fsq, hklinfotype, issymeqv)
 |
 |  A group or family of HKL planes, all sharing the same value of d-spacing
 |  and structure factor (fsquared). If .is_symequiv evaluates to True, these
 |  exactly represent a group of symmetry-equivalent planes.
 |
 |  Methods defined here:
 |
 |  __init__(self, hh, kk, ll, mult, dsp, fsq, hklinfotype, issymeqv)
 |      For internal usage only, do not create therse objects manually.
 |
 |  __str__(self)
 |      Return str(self).
 |
 |  ----------------------------------------------------------------------
 |  Readonly properties defined here:
 |
 |  d
 |      The d-spacing value in units of angstrom.
 |
 |  dspacing
 |      The d-spacing value in units of angstrom.
 |
 |  f2
 |      The squared structure factor (F^2) in units of barn.
 |
 |  fsquared
 |      The squared structure factor (F^2) in units of barn.
 |
 |  h
 |      An array of h values. Note that this has half the length of
 |      .multiplicity, since we exclude entries that can be generated from each
 |      other by a mere sign flip.
 |
 |  hkl_label
 |      Returns a hkl label for the entry, i.e. one of the hkl points in the
 |      group as a tuple of three integers: (h,k,l).
 |
 |  hkl_type
 |      The type of HKL group represented by this entry. Returns
 |      HKLInfoType.SymEqvGroup if this is a group of symmetry-equivalent
 |      planes.
 |
 |  is_symequiv
 |      Returns True if .hkl_type equals HKLInfoType.SymEqvGroup.
 |
 |  k
 |      An array of k values. Note that this has half the length of
 |      .multiplicity, since we exclude entries that can be generated from each
 |      other by a mere sign flip.
 |
 |  l
 |      An array of l values. Note that this has half the length of
 |      .multiplicity, since we exclude entries that can be generated from each
 |      other by a mere sign flip.
 |
 |  mult
 |      The number of hkl points in the group.
 |
 |  multiplicity
 |      The number of hkl points in the group.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object
soerenschmidts commented 3 months ago

Hi Thomas,

Tnx, the new interface will supply the info needed nicely

Soeren

tkittel commented 3 months ago

Great, thanks for checking! There was an issue with this functionality in release 3.9.0, but release 3.9.1 is now out so it should work there!