From 0ee4f206a1705e7e83c53a0f8eaa3327e6be904f Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Tue, 20 Aug 2024 12:21:19 -0700 Subject: [PATCH 1/8] get a cli interface for making a material --- hexrd/cli/make_material.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 hexrd/cli/make_material.py diff --git a/hexrd/cli/make_material.py b/hexrd/cli/make_material.py new file mode 100644 index 000000000..db8ff92cc --- /dev/null +++ b/hexrd/cli/make_material.py @@ -0,0 +1,34 @@ +from hexrd.material.mksupport import mk +import sys +from hexrd.cli import help + +descr = (f'make a new material file in the ' + f'hdf5 format using the command line. ' + f'user specifies the file and crystal ' + f'names') +example = """ +examples: + hexrd make_material --file material.h5 --xtal diamond +""" + +def make_material(): + + p = sub_parsers.add_parser('make_material', + description = descr, help = descr) + + p.set_defaults(func=execute) + + p.add_argument( + '-f', '--file', action='store', + help='name of h5 file' + ) + + p.add_argument( + '-x', '--xtal', action='store', + help='name of crystal' + ) + +def execute(parser): + file = parser.parse_args['--file'] + xtal = parser.parse_args['--xtal'] + mk(file, xtal) From 3add1ac20b81b2621bbade46ca7de396b330858b Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Tue, 20 Aug 2024 12:25:35 -0700 Subject: [PATCH 2/8] add to main --- hexrd/cli/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hexrd/cli/main.py b/hexrd/cli/main.py index 426a3fd04..d68042a57 100644 --- a/hexrd/cli/main.py +++ b/hexrd/cli/main.py @@ -16,7 +16,7 @@ from hexrd.cli import find_orientations from hexrd.cli import fit_grains from hexrd.cli import pickle23 - +from hexrd.cli import make_material try: _version = version("hexrd") From 70f564e9d8b0031f45fadb14e4eb18f563779904 Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Tue, 20 Aug 2024 12:35:25 -0700 Subject: [PATCH 3/8] make sub_parser --- hexrd/cli/main.py | 1 + hexrd/cli/make_material.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/hexrd/cli/main.py b/hexrd/cli/main.py index d68042a57..cb15ff5c6 100644 --- a/hexrd/cli/main.py +++ b/hexrd/cli/main.py @@ -66,6 +66,7 @@ def main(): find_orientations.configure_parser(sub_parsers) fit_grains.configure_parser(sub_parsers) pickle23.configure_parser(sub_parsers) + make_material.configure_parser(sub_parsers) try: import argcomplete diff --git a/hexrd/cli/make_material.py b/hexrd/cli/make_material.py index db8ff92cc..fc1ecb1d6 100644 --- a/hexrd/cli/make_material.py +++ b/hexrd/cli/make_material.py @@ -11,7 +11,7 @@ hexrd make_material --file material.h5 --xtal diamond """ -def make_material(): +def configure_parser(sub_parsers): p = sub_parsers.add_parser('make_material', description = descr, help = descr) From 5a1a8f4addeede111b29d7da9b14a3bad8c1edd3 Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Tue, 20 Aug 2024 15:01:17 -0700 Subject: [PATCH 4/8] cli interface to make a new material --- hexrd/cli/make_material.py | 25 +++++++++++++----------- hexrd/material/mksupport.py | 39 ++++++++++++++++++++++++------------- hexrd/material/unitcell.py | 12 ++++++++++++ 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/hexrd/cli/make_material.py b/hexrd/cli/make_material.py index fc1ecb1d6..42ca91eb6 100644 --- a/hexrd/cli/make_material.py +++ b/hexrd/cli/make_material.py @@ -1,4 +1,3 @@ -from hexrd.material.mksupport import mk import sys from hexrd.cli import help @@ -13,22 +12,26 @@ def configure_parser(sub_parsers): - p = sub_parsers.add_parser('make_material', + p = sub_parsers.add_parser('make-material', description = descr, help = descr) - p.set_defaults(func=execute) - p.add_argument( - '-f', '--file', action='store', - help='name of h5 file' + '-f', '--file', type = str, + default='test.h5', + help='name of h5 file, default = test.h5' ) p.add_argument( - '-x', '--xtal', action='store', - help='name of crystal' + '-x', '--xtal', type = str, + default='xtal', + help='name of crystal, default = xtal' ) -def execute(parser): - file = parser.parse_args['--file'] - xtal = parser.parse_args['--xtal'] + p.set_defaults(func=execute) + +def execute(args, parser): + from hexrd.material.mksupport import mk + + file = args.file + xtal = args.xtal mk(file, xtal) diff --git a/hexrd/material/mksupport.py b/hexrd/material/mksupport.py index 083272624..955f6737e 100644 --- a/hexrd/material/mksupport.py +++ b/hexrd/material/mksupport.py @@ -6,7 +6,10 @@ import numpy as np import datetime import getpass -from hexrd.material.unitcell import _StiffnessDict, _pgDict +from hexrd.material.unitcell import (_StiffnessDict, + _pgDict, + unitcell_volume) + def mk(filename, xtalname): @@ -23,7 +26,7 @@ def mk(filename, xtalname): AtomInfo = GetAtomInfo() AtomInfo.update({'file': filename, 'xtalname': xtalname, - 'xtal_sys': xtal_sys, 'SG': space_group,\ + 'xtal_sys': xtal_sys, 'SG': space_group, 'SGsetting': iset}) Write2H5File(AtomInfo, lat_param) @@ -326,8 +329,8 @@ def GetAtomInfo(): def GetAsymmetricPositions(aniU): asym = input( - "Enter asymmetric position of atom in unit cell \ - separated by comma (fractional coordinates) : ") + f'Enter asymmetric position of atom in unit cell ' + f'separated by comma (fractional coordinates) : ') asym = [x.strip() for x in asym.split(',')] for i, x in enumerate(asym): @@ -479,7 +482,6 @@ def WriteH5Data(fid, AtomInfo, lat_param, path=None): material.h5 file output @Date 01/14/2022 SS added tThWidth to materials file """ - # Add the path prefix if we have been given one if path is not None: path = f"{path}/{AtomInfo['xtalname']}" @@ -515,32 +517,41 @@ def WriteH5Data(fid, AtomInfo, lat_param, path=None): did = gid.create_dataset("stiffness", (6, 6), dtype=np.float64) did.write_direct(np.array(AtomInfo['stiffness'], dtype=np.float64)) + P = AtomInfo['pressure'] if 'pressure' in AtomInfo else 1.01325E-4 # 1 atm did = gid.create_dataset("pressure", (1,), dtype=np.float64) - did.write_direct(np.array(AtomInfo['pressure'], dtype=np.float64)) + did.write_direct(np.array(P, dtype=np.float64)) + T = AtomInfo['temperature'] if 'temperature' in AtomInfo else 293 # R.T. did = gid.create_dataset("temperature", (1,), dtype=np.float64) - did.write_direct(np.array(AtomInfo['temperature'], dtype=np.float64)) + did.write_direct(np.array(T, dtype=np.float64)) + k0 = AtomInfo['k0'] if 'k0' in AtomInfo else 100.0 did = gid.create_dataset("k0", (1,), dtype=np.float64) - did.write_direct(np.array([AtomInfo['k0']], dtype=np.float64)) + did.write_direct(np.array([k0], dtype=np.float64)) + k0p = AtomInfo['k0p'] if 'k0p' in AtomInfo else 0.0 did = gid.create_dataset("k0p", (1,), dtype=np.float64) - did.write_direct(np.array([AtomInfo['k0p']], dtype=np.float64)) + did.write_direct(np.array([k0p], dtype=np.float64)) + dk0dt = AtomInfo['dk0dt'] if 'dk0dt' in AtomInfo else 0.0 did = gid.create_dataset("dk0dt", (1,), dtype=np.float64) - did.write_direct(np.array([AtomInfo['dk0dt']], dtype=np.float64)) + did.write_direct(np.array([dk0dt], dtype=np.float64)) + dk0pdt = AtomInfo['dk0pdt'] if 'dk0pdt' in AtomInfo else 0.0 did = gid.create_dataset("dk0pdt", (1,), dtype=np.float64) - did.write_direct(np.array([AtomInfo['dk0pdt']], dtype=np.float64)) + did.write_direct(np.array([dk0pdt], dtype=np.float64)) + alpha_t = AtomInfo['alpha_t'] if 'alpha_t' in AtomInfo else 0.0 did = gid.create_dataset("alpha_t", (1,), dtype=np.float64) - did.write_direct(np.array([AtomInfo['alpha_t']], dtype=np.float64)) + did.write_direct(np.array([alpha_t], dtype=np.float64)) + dalpha_t_dt = AtomInfo['dalpha_t_dt'] if 'dalpha_t_dt' in AtomInfo else 0.0 did = gid.create_dataset("dalpha_t_dt", (1,), dtype=np.float64) - did.write_direct(np.array([AtomInfo['dalpha_t_dt']], dtype=np.float64)) + did.write_direct(np.array([dalpha_t_dt], dtype=np.float64)) + v0 = AtomInfo['v0'] if 'v0' in AtomInfo else unitcell_volume(lat_param) did = gid.create_dataset("v0", (1,), dtype=np.float64) - did.write_direct(np.array([AtomInfo['v0']], dtype=np.float64)) + did.write_direct(np.array([v0], dtype=np.float64)) if "tThWidth" in AtomInfo: did = gid.create_dataset("tThWidth", (1,), dtype=np.float64) diff --git a/hexrd/material/unitcell.py b/hexrd/material/unitcell.py index 26f3a2fef..ceee2b1db 100644 --- a/hexrd/material/unitcell.py +++ b/hexrd/material/unitcell.py @@ -48,6 +48,18 @@ def _calcstar(v, sym, mat): return vsym +def unitcell_volume(lp): + '''helper function to explicitly compute unitcell + volume (in A^3) using the forula. the unitcell class + value is set using the determinant of the metric tensor + ''' + ca = np.cos(np.radians(lp['alpha'])) + cb = np.cos(np.radians(lp['beta'])) + cg = np.cos(np.radians(lp['gamma'])) + + fact = np.sqrt(1 - ca**2 - cb**2 - cg**2 +2*ca*cb*cg) + # 1E3 factor to go from nm to A + return lp['a']*lp['b']*lp['c']*fact*1E3 class unitcell: From 138cdccf55c94c07a8386397ccf35855fa329136 Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Tue, 20 Aug 2024 15:09:08 -0700 Subject: [PATCH 5/8] edit some text. --- hexrd/material/symbols.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hexrd/material/symbols.py b/hexrd/material/symbols.py index ccc65ef36..17a4824ba 100644 --- a/hexrd/material/symbols.py +++ b/hexrd/material/symbols.py @@ -1,6 +1,5 @@ -pstr_mkxtal = "\n\n This is a program to create a HDF5 file for storing crystallographic information.\n " -pstr_mkxtal = pstr_mkxtal + " This format is the same format as used in the EMsoft (electron microscoy) suite.\n " +pstr_mkxtal = "\n\n This is a program to create a HDF5 file for storing crystallographic information.\n " pstr_mkxtal = pstr_mkxtal + " The following inputs are required:\n " pstr_mkxtal = pstr_mkxtal + " Crystal System:\n" pstr_mkxtal = pstr_mkxtal + " 1. Cubic\n" From e09a4148f43d2a863bb64fddfcb1cea3f6e3a1da Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Tue, 20 Aug 2024 15:13:36 -0700 Subject: [PATCH 6/8] PEP8 --- hexrd/cli/make_material.py | 14 ++++++----- hexrd/material/symbols.py | 49 ++++++++++++++++++++------------------ hexrd/material/unitcell.py | 4 ++-- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/hexrd/cli/make_material.py b/hexrd/cli/make_material.py index 42ca91eb6..40ac229ed 100644 --- a/hexrd/cli/make_material.py +++ b/hexrd/cli/make_material.py @@ -10,28 +10,30 @@ hexrd make_material --file material.h5 --xtal diamond """ + def configure_parser(sub_parsers): p = sub_parsers.add_parser('make-material', - description = descr, help = descr) + description=descr, help=descr) p.add_argument( - '-f', '--file', type = str, + '-f', '--file', type=str, default='test.h5', help='name of h5 file, default = test.h5' - ) + ) p.add_argument( - '-x', '--xtal', type = str, + '-x', '--xtal', type=str, default='xtal', help='name of crystal, default = xtal' - ) + ) p.set_defaults(func=execute) + def execute(args, parser): from hexrd.material.mksupport import mk - + file = args.file xtal = args.xtal mk(file, xtal) diff --git a/hexrd/material/symbols.py b/hexrd/material/symbols.py index 17a4824ba..fff240409 100644 --- a/hexrd/material/symbols.py +++ b/hexrd/material/symbols.py @@ -1,27 +1,30 @@ -pstr_mkxtal = "\n\n This is a program to create a HDF5 file for storing crystallographic information.\n " -pstr_mkxtal = pstr_mkxtal + " The following inputs are required:\n " -pstr_mkxtal = pstr_mkxtal + " Crystal System:\n" -pstr_mkxtal = pstr_mkxtal + " 1. Cubic\n" -pstr_mkxtal = pstr_mkxtal + " 2. Tetragonal\n" -pstr_mkxtal = pstr_mkxtal + " 3. Orthorhombic\n" -pstr_mkxtal = pstr_mkxtal + " 4. Hexagonal\n" -pstr_mkxtal = pstr_mkxtal + " 5. Trigonal\n" -pstr_mkxtal = pstr_mkxtal + " 6. Monoclinic\n" -pstr_mkxtal = pstr_mkxtal + " 7. Triclinic\n\n" -pstr_mkxtal = pstr_mkxtal + " Space group number\n" -pstr_mkxtal = pstr_mkxtal + " Atomic number (Z) for all species in unit cell\n" -pstr_mkxtal = pstr_mkxtal + " Asymmetric positions for all atoms in unit cell\n" -pstr_mkxtal = pstr_mkxtal + " Debye-Waller factors for all atoms in the unit cell\n" -pstr_mkxtal = pstr_mkxtal + " You'll be prompted for these values now\n\n" -pstr_mkxtal = pstr_mkxtal + "\n Note about the trigonal system:\n" -pstr_mkxtal = pstr_mkxtal + " -------------------------------\n" -pstr_mkxtal = pstr_mkxtal + " Primitive trigonal crystals are defined with respect to a HEXAGONAL\n" -pstr_mkxtal = pstr_mkxtal + " reference frame. Rhombohedral crystals can be referenced with\n" -pstr_mkxtal = pstr_mkxtal + " respect to a HEXAGONAL basis (first setting), or with respect to\n" -pstr_mkxtal = pstr_mkxtal + " a RHOMBOHEDRAL basis (second setting). The default setting for\n" -pstr_mkxtal = pstr_mkxtal + " trigonal symmetry is the hexagonal setting. When you select\n" -pstr_mkxtal = pstr_mkxtal + " crystal system 5 above, you will be prompted for the setting. \n" +pstr_mkxtal = ( + f'\n\n This is a program to create a HDF5 file for storing ' + f'crystallographic information.\n' + f' The following inputs are required:\n ' + f' Crystal System:\n' + f' 1. Cubic\n' + f' 2. Tetragonal\n' + f' 3. Orthorhombic\n' + f' 4. Hexagonal\n' + f' 5. Trigonal\n' + f' 6. Monoclinic\n' + f' 7. Triclinic\n\n' + f' Space group number\n' + f' Atomic number (Z) for all species in unit cell\n' + f' Asymmetric positions for all atoms in unit cell\n' + f' Debye-Waller factors for all atoms in the unit cell\n' + f' You\'ll be prompted for these values now \n\n' + f'\n Note about the trigonal system:\n' + f' -------------------------------\n' + f' Primitive trigonal crystals are defined with respect to a HEXAGONAL\n' + f' reference frame. Rhombohedral crystals can be referenced with\n' + f' respect to a HEXAGONAL basis (first setting), or with respect to\n' + f' a RHOMBOHEDRAL basis (second setting). The default setting for\n' + f' trigonal symmetry is the hexagonal setting. When you select\n' + f' crystal system 5 above, you will be prompted for the setting. \n') + pstr_spacegroup = [ " P 1 " ," P -1 ", \ # MONOCLINIC SPACE GROUPS diff --git a/hexrd/material/unitcell.py b/hexrd/material/unitcell.py index ceee2b1db..b3ab1c32c 100644 --- a/hexrd/material/unitcell.py +++ b/hexrd/material/unitcell.py @@ -57,9 +57,9 @@ def unitcell_volume(lp): cb = np.cos(np.radians(lp['beta'])) cg = np.cos(np.radians(lp['gamma'])) - fact = np.sqrt(1 - ca**2 - cb**2 - cg**2 +2*ca*cb*cg) + fact = np.sqrt(1 - ca**2 - cb**2 - cg**2 + 2*ca*cb*cg) # 1E3 factor to go from nm to A - return lp['a']*lp['b']*lp['c']*fact*1E3 + return lp['a']*lp['b']*lp['c']*fact*1E3 class unitcell: From 21f0b62f35989cfe07ff5783fae565a8c03a9df0 Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Tue, 20 Aug 2024 15:14:23 -0700 Subject: [PATCH 7/8] final PEP8 --- hexrd/material/unitcell.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hexrd/material/unitcell.py b/hexrd/material/unitcell.py index b3ab1c32c..1e54961b0 100644 --- a/hexrd/material/unitcell.py +++ b/hexrd/material/unitcell.py @@ -48,6 +48,7 @@ def _calcstar(v, sym, mat): return vsym + def unitcell_volume(lp): '''helper function to explicitly compute unitcell volume (in A^3) using the forula. the unitcell class From b7f2ef31f44e34f6e0fd206cbbed6f17e436be29 Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Tue, 20 Aug 2024 15:16:07 -0700 Subject: [PATCH 8/8] space formatting --- hexrd/material/symbols.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hexrd/material/symbols.py b/hexrd/material/symbols.py index fff240409..66150157f 100644 --- a/hexrd/material/symbols.py +++ b/hexrd/material/symbols.py @@ -1,6 +1,6 @@ pstr_mkxtal = ( - f'\n\n This is a program to create a HDF5 file for storing ' + f'\n\n This is a program to create a HDF5 file for storing ' f'crystallographic information.\n' f' The following inputs are required:\n ' f' Crystal System:\n'