diff --git a/core/python/spirit/hamiltonian.py b/core/python/spirit/hamiltonian.py index 2e4b83a3d..7ef58468e 100644 --- a/core/python/spirit/hamiltonian.py +++ b/core/python/spirit/hamiltonian.py @@ -160,9 +160,64 @@ def get_ddi(p_state, idx_image=-1, idx_chain=-1): "cutoff_radius" : cutoff_radius.value, "pb_zero_padding" : pb_zero_padding.value } +_Get_Anisotropy = _spirit.Hamiltonian_Get_Anisotropy +_Get_Anisotropy.argtypes = [ctypes.c_void_p,ctypes.POINTER(ctypes.c_float) + , ctypes.POINTER(ctypes.c_float) + , ctypes.c_int, ctypes.c_int] +_Get_Anisotropy.restype = None +def get_anisotropy(p_state, idx_image=-1, idx_chain=-1): + """Returns the magnitude and an array of `shape(3)` containing the direction of + the (homogeneous) magnetocrystalline anisotropy. + """ + magnitude = ctypes.c_float() + normal = (3*ctypes.c_float)() + _Get_Anisotropy(ctypes.c_void_p(p_state), ctypes.byref(magnitude), normal + ,ctypes.c_int(idx_image), ctypes.c_int(idx_chain)) + return float(magnitude.value), [n for n in normal] + +_Get_Exchange_Shells = _spirit.Hamiltonian_Get_Exchange_Shells +_Get_Exchange_Shells.argtypes = [ctypes.c_void_p,ctypes.POINTER(ctypes.c_int) + , ctypes.POINTER(ctypes.c_float) + , ctypes.c_int, ctypes.c_int] +_Get_Exchange_Shells.restype = None +def get_Exchange_shells(p_state, idx_image=-1, idx_chain=-1): + """Returns the magnitude and an array of `n_shells` containing the direction of + the Exchange interaction. + """ + NULL = ctypes.POINTER(ctypes.c_float)() + #Null = None + n_shells = ctypes.c_int() + #Only write n_shells and chirality + _Get_Exchange_Shells(ctypes.c_void_p(p_state),ctypes.byref(n_shells),NULL + ,ctypes.c_int(idx_image), ctypes.c_int(idx_chain)) + jij = (n_shells.value*ctypes.c_float)() + _Get_Exchange_Shells(ctypes.c_void_p(p_state),ctypes.byref(n_shells),jij + ,ctypes.c_int(idx_image), ctypes.c_int(idx_chain)) + return int(n_shells.value), [float(j) for j in jij] + +_Get_DMI_Shells = _spirit.Hamiltonian_Get_DMI_Shells +_Get_DMI_Shells.argtypes = [ctypes.c_void_p,ctypes.POINTER(ctypes.c_int) + , ctypes.POINTER(ctypes.c_float) + , ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.c_int] +_Get_DMI_Shells.restype = None +def get_DMI_shells(p_state, idx_image=-1, idx_chain=-1): + """Returns the magnitude and an array of `n_shells` containing the direction of + the DMI interaction. + """ + NULL = ctypes.POINTER(ctypes.c_float)() + #Null = None + n_shells = ctypes.c_int() + chirality = ctypes.c_int() + #Only write n_shells and chirality + _Get_DMI_Shells(ctypes.c_void_p(p_state),ctypes.byref(n_shells),NULL + ,ctypes.byref(chirality),ctypes.c_int(idx_image), ctypes.c_int(idx_chain)) + dij = (n_shells.value*ctypes.c_float)() + _Get_DMI_Shells(ctypes.c_void_p(p_state),ctypes.byref(n_shells),dij + ,ctypes.byref(chirality),ctypes.c_int(idx_image), ctypes.c_int(idx_chain)) + return int(n_shells.value),[float(d) for d in dij], int(chirality.value) _Write_Hessian = _spirit.Hamiltonian_Write_Hessian _Write_Hessian.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_bool, ctypes.c_int, ctypes.c_int] _Write_Hessian.restype = None def write_hessian(p_state, filename, triplet_format=True, idx_image=-1, idx_chain=-1): """RWrites the embedding Hessian to a file""" - _Write_Hessian(ctypes.c_void_p(p_state), ctypes.c_char_p(filename.encode('utf-8')), ctypes.c_bool(triplet_format), ctypes.c_int(idx_image), ctypes.c_int(idx_chain)) \ No newline at end of file + _Write_Hessian(ctypes.c_void_p(p_state), ctypes.c_char_p(filename.encode('utf-8')), ctypes.c_bool(triplet_format), ctypes.c_int(idx_image), ctypes.c_int(idx_chain)) diff --git a/core/src/Spirit/Hamiltonian.cpp b/core/src/Spirit/Hamiltonian.cpp index a65056ff7..9a38465d0 100644 --- a/core/src/Spirit/Hamiltonian.cpp +++ b/core/src/Spirit/Hamiltonian.cpp @@ -442,9 +442,12 @@ try *n_shells = ham->exchange_shell_magnitudes.size(); // Note the array needs to be correctly allocated beforehand! - for( std::size_t i = 0; i < ham->exchange_shell_magnitudes.size(); ++i ) + if(jij != nullptr) { - jij[i] = (float)ham->exchange_shell_magnitudes[i]; + for( int i = 0; i < ham->exchange_shell_magnitudes.size(); ++i ) + { + jij[i] = (float)ham->exchange_shell_magnitudes[i]; + } } } } @@ -523,10 +526,12 @@ try *n_shells = ham->dmi_shell_magnitudes.size(); *chirality = ham->dmi_shell_chirality; - - for( int i = 0; i < *n_shells; ++i ) + if(dij != nullptr) { - dij[i] = (float)ham->dmi_shell_magnitudes[i]; + for( int i = 0; i < *n_shells; ++i ) + { + dij[i] = (float)ham->dmi_shell_magnitudes[i]; + } } } }