Skip to content

Commit ee90aeb

Browse files
author
Elisa Rebolini
committed
Merge branch 'feature-python_api-wyckoff' into 'master'
Feature python api wyckoff See merge request scientific-software/crysfml!16
2 parents 2763634 + 4be5a96 commit ee90aeb

10 files changed

+281
-17
lines changed

Python_API/Src/API_Crystallographic_Symmetry.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,39 @@ def str_orbit(self):
307307
ret = []
308308
for k in range(48):
309309
ret.append(CFML_api.crysfml_api.crystallographic_symmetry_get_wyckoff_str_orbit(self.get_fortran_address(), k+1)["str_orbit"])
310-
return ret
310+
return ret
311+
312+
313+
314+
def multiplicity_pos(pos, spg: SpaceGroup):
315+
"""
316+
Return the multiplicity of a real space point given the Shubnikov/space group.
317+
...
318+
Parameters
319+
----------
320+
pos : np.array[dtype='float32', ndim=q]
321+
x,y,z coordinates
322+
spg : CMFL_api.crysfml_api.SpaceGroup
323+
324+
Returns:
325+
int: Multiplicity of the point
326+
327+
"""
328+
return CFML_api.crysfml_api.crystallographic_symmetry_get_multip_pos_crys(pos, spg.get_fortran_address())["mult"]
329+
330+
331+
def occupancy_site(pos, spg):
332+
"""
333+
Return the occupancy foctor given the position and Shubnikov/space group.
334+
...
335+
Parameters
336+
----------
337+
pos : np.array[dtype='float32', ndim=1]
338+
x,y,z coordinates
339+
spg : CMFL_api.crysfml_api.SpaceGroup
340+
341+
Returns:
342+
float: occupancy factor (site multiplicity/multiplicity)
343+
344+
"""
345+
return CFML_api.crysfml_api.crystallographic_symmetry_get_occ_site(pos, spg.get_fortran_address())["occ"]

Python_API/Src/API_Reflections_Utilities.py

+38-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@
1616
import CFML_api.FortranBindedClass
1717

1818
class ReflectionList(CFML_api.FortranBindedClass):
19+
"""
20+
A class used to describe the reflection list type(Reflection_List_Type) in CFML.
21+
22+
...
23+
Attributes
24+
----------
25+
cell : CMFL_api.crysfml_api.Cell
26+
spg : CMFL_api.crysfml_api.SpaceGroup
27+
lfriedel : boolean
28+
job : CMFL_api.crysfml_api.JobInfo
29+
30+
Methods
31+
-------
32+
print_description
33+
Prints the reflection lsit
34+
compute_structure_factors
35+
Compute the structure factor for each reflection of the list
36+
37+
"""
38+
1939
def __init__(self, cell, spg, lfriedel, job):
2040
CFML_api.FortranBindedClass.__init__(self)
2141
(stlmin, stlmax) = job.range_stl
@@ -38,12 +58,28 @@ def nref(self):
3858
return CFML_api.crysfml_api.reflections_utilities_get_nref(self.get_fortran_address())["nref"]
3959

4060
def compute_structure_factors(self, space_group, atom_list, job):
41-
61+
"""
62+
Compute all structure factors
63+
...
64+
Parameters
65+
----------
66+
space_group : CMFL_api.crysfml_api.SpaceGroup
67+
atom_list : CMFL_api.crysfml_api.AtomList
68+
job : CMFL_api.crysfml_api.JobInfo
69+
"""
4270
CFML_api.crysfml_api.structure_factors_structure_factors(
4371
atom_list.get_fortran_address(), space_group.get_fortran_address(),
4472
self.get_fortran_address(), job.get_fortran_address())
45-
4673

74+
75+
def print_description(self):
76+
"""
77+
Write the list of reflections and structure factors
78+
"""
79+
print("\n")
80+
CFML_api.crysfml_api.reflections_utilities_write_reflist_info(self.get_fortran_address())
81+
82+
4783
class Reflection(CFML_api.FortranBindedClass):
4884
""" Class for a given Reflection in CFML
4985

Python_API/Src/Extensions/API_Crystallographic_Symmetry.f90

+113-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ module API_Crystallographic_Symmetry
1717
use, intrinsic :: iso_c_binding
1818
use, intrinsic :: iso_fortran_env
1919

20-
use CFML_Crystallographic_Symmetry, only: Space_Group_Type, set_SpaceGroup, Write_SpaceGroup, Sym_Oper_Type, Wyckoff_Type, Wyck_Pos_Type
20+
use CFML_GlobalDeps, only : Cp
21+
use CFML_Crystallographic_Symmetry, only: &
22+
Space_Group_Type, &
23+
set_SpaceGroup, &
24+
Write_SpaceGroup, &
25+
Sym_Oper_Type, &
26+
Wyckoff_Type, &
27+
Wyck_Pos_Type, &
28+
Get_Multip_Pos, &
29+
Get_Occ_Site
2130

2231
implicit none
2332

@@ -1515,4 +1524,107 @@ function crystallographic_symmetry_get_wyckoff_str_orbit(self_ptr, args_ptr) res
15151524

15161525
end function crystallographic_symmetry_get_wyckoff_str_orbit
15171526

1527+
1528+
1529+
function crystallographic_symmetry_get_multip_pos_crys(self_ptr, args_ptr) result(r) bind(c)
1530+
1531+
type(c_ptr), value :: self_ptr
1532+
type(c_ptr), value :: args_ptr
1533+
type(c_ptr) :: r
1534+
type(tuple) :: args
1535+
type(dict) :: retval
1536+
integer :: num_args
1537+
integer :: ierror
1538+
1539+
type(Space_Group_type_p) :: spg
1540+
type(object) :: pos_obj
1541+
type(ndarray) :: pos_nd
1542+
real(kind=cp), dimension(:), pointer :: pos_p
1543+
1544+
integer :: mult
1545+
1546+
r = C_NULL_PTR ! in case of an exception return C_NULL_PTR
1547+
! use unsafe_cast_from_c_ptr to cast from c_ptr to tuple
1548+
call unsafe_cast_from_c_ptr(args, args_ptr)
1549+
! Check if the arguments are OK
1550+
ierror = args%len(num_args)
1551+
! we should also check ierror, but this example does not do complete error checking for simplicity
1552+
if (num_args /= 2) then
1553+
call raise_exception(TypeError, "get_multip_pos_crys expects exactly 2 arguments")
1554+
call args%destroy
1555+
return
1556+
endif
1557+
1558+
! Doing boring stuff
1559+
ierror = args%getitem(pos_obj, 0) !pos -> x,y,z
1560+
ierror = cast(pos_nd, pos_obj)
1561+
ierror = pos_nd%get_data(pos_p)
1562+
1563+
call get_space_group_type_from_arg(args, spg, 1)
1564+
1565+
mult = Get_Multip_Pos(pos_p, spg%p)
1566+
!write(*,*) "f90 mult",mult
1567+
1568+
ierror = dict_create(retval)
1569+
ierror = retval%setitem("mult", mult)
1570+
1571+
r = retval%get_c_ptr()
1572+
1573+
call args%destroy
1574+
call pos_obj%destroy
1575+
1576+
1577+
end function crystallographic_symmetry_get_multip_pos_crys
1578+
1579+
1580+
function crystallographic_symmetry_get_occ_site(self_ptr, args_ptr) result(r) bind(c)
1581+
1582+
type(c_ptr), value :: self_ptr
1583+
type(c_ptr), value :: args_ptr
1584+
type(c_ptr) :: r
1585+
type(tuple) :: args
1586+
type(dict) :: retval
1587+
integer :: num_args
1588+
integer :: ierror
1589+
1590+
type(Space_Group_type_p) :: spg
1591+
type(object) :: pos_obj
1592+
type(ndarray) :: pos_nd
1593+
real(kind=cp), dimension(:), pointer :: pos_p
1594+
1595+
real(kind=cp) :: occ
1596+
1597+
r = C_NULL_PTR ! in case of an exception return C_NULL_PTR
1598+
! use unsafe_cast_from_c_ptr to cast from c_ptr to tuple
1599+
call unsafe_cast_from_c_ptr(args, args_ptr)
1600+
! Check if the arguments are OK
1601+
ierror = args%len(num_args)
1602+
! we should also check ierror, but this example does not do complete error checking for simplicity
1603+
if (num_args /= 2) then
1604+
call raise_exception(TypeError, "get_multip_pos_crys expects exactly 2 arguments")
1605+
call args%destroy
1606+
return
1607+
endif
1608+
1609+
! Doing boring stuff
1610+
ierror = args%getitem(pos_obj, 0) !pos -> x,y,z
1611+
ierror = cast(pos_nd, pos_obj)
1612+
ierror = pos_nd%get_data(pos_p)
1613+
1614+
call get_space_group_type_from_arg(args, spg, 1)
1615+
1616+
occ= Get_Occ_site(pos_p, spg%p)
1617+
!write(*,*) "f90 mult",mult
1618+
1619+
ierror = dict_create(retval)
1620+
ierror = retval%setitem("occ", occ)
1621+
1622+
r = retval%get_c_ptr()
1623+
1624+
call args%destroy
1625+
call pos_obj%destroy
1626+
1627+
1628+
end function crystallographic_symmetry_get_occ_site
1629+
15181630
end module API_Crystallographic_Symmetry

Python_API/Src/Extensions/API_Reflections_Utilities.f90

+40-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module API_Reflections_Utilities
2424
Reflection_Type, &
2525
Reflection_List_Type, &
2626
Hkl_uni, &
27-
get_maxnumref
27+
get_maxnumref, &
28+
Write_RefList_Info
2829

2930
use API_Crystallographic_Symmetry, only: &
3031
Space_Group_Type_p, &
@@ -143,6 +144,42 @@ function reflections_utilities_get_nref(self_ptr, args_ptr) result(r) bind(c)
143144

144145
end function reflections_utilities_get_nref
145146

147+
! @brief Print the list of reflections and structure factors to standard output
148+
function reflections_utilities_write_reflist_info(self_ptr, args_ptr) result(r) bind(c)
149+
150+
type(c_ptr), value :: self_ptr
151+
type(c_ptr), value :: args_ptr
152+
type(c_ptr) :: r
153+
154+
type(tuple) :: args
155+
type(dict) :: retval
156+
integer :: num_args
157+
integer :: ierror
158+
159+
type(Reflection_List_Type_p) :: reflist_p
160+
161+
r = C_NULL_PTR
162+
call unsafe_cast_from_c_ptr(args, args_ptr)
163+
164+
ierror = args%len(num_args)
165+
166+
if (num_args /= 1) then
167+
call raise_exception(TypeError, "write_reflist_info expects exactly 1 argument")
168+
call args%destroy
169+
return
170+
endif
171+
172+
call get_reflection_list_from_arg(args, reflist_p)
173+
174+
call Write_RefList_Info(reflist_p%p)
175+
176+
ierror = dict_create(retval)
177+
r = retval%get_c_ptr()
178+
179+
call args%destroy
180+
181+
end function reflections_utilities_write_reflist_info
182+
146183

147184
function reflections_utilities_hkl_uni_reflist(self_ptr, args_ptr) result(r) bind(c)
148185

@@ -666,7 +703,8 @@ function reflections_utilities_get_B(self_ptr, args_ptr) result(r) bind(c)
666703
call args%destroy
667704

668705
end function reflections_utilities_get_B
669-
706+
707+
670708

671709

672710
end module API_Reflections_Utilities

Python_API/Src/Extensions/API_init.f90

+40-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ module API_init
5656
crystallographic_symmetry_get_wyckoff_site, &
5757
crystallographic_symmetry_get_wyckoff_norb, &
5858
crystallographic_symmetry_get_wyckoff_str_orig, &
59-
crystallographic_symmetry_get_wyckoff_str_orbit
59+
crystallographic_symmetry_get_wyckoff_str_orbit, &
60+
crystallographic_symmetry_get_multip_pos_crys, &
61+
crystallographic_symmetry_get_occ_site
6062

6163
use API_IO_Formats, only: &
6264
IO_formats_readn_set_xtal_structure, &
@@ -174,6 +176,7 @@ module API_init
174176
reflections_utilities_hkl_uni_reflist, &
175177
reflections_utilities_del_reflection_list, &
176178
reflections_utilities_get_nref, &
179+
reflections_utilities_write_reflist_info, &
177180
reflections_utilities_del_reflection, &
178181
reflections_utilities_get_item, &
179182
reflections_utilities_get_H, &
@@ -257,9 +260,17 @@ function init() result(m)
257260
type(c_ptr) :: m
258261
integer :: ierror
259262
ierror = forpy_initialize()
260-
261-
call method_table%init(200)
262263

264+
265+
266+
!--------------------------
267+
!Total number of method in the binding
268+
!--------------------------
269+
call method_table%init(203)
270+
271+
272+
273+
263274
!--------------------------
264275
! Error Messages (1)
265276
!--------------------------
@@ -268,9 +279,13 @@ function init() result(m)
268279
METH_VARARGS, & ! this method takes arguments but no keyword arguments
269280
c_funloc(error_messages)) ! address of Fortran function to add
270281

282+
283+
284+
271285
!--------------------------
272286
! Diffraction Patterns (43)
273287
!--------------------------
288+
274289
call method_table%add_method("diffraction_patterns_compute_powder_pattern", & ! method name
275290
"compute the powder diffraction pattern from some experimental conditions and a set of reflections", & !doc-string
276291
METH_VARARGS, & ! this method takes arguments but no keyword arguments
@@ -486,9 +501,12 @@ function init() result(m)
486501
METH_VARARGS, & ! this method takes arguments but no keyword arguments
487502
c_funloc(diffraction_patterns_get_nd)) ! address of Fortran function to add
488503

504+
505+
489506
!--------------------------
490-
! Crystallographic Symmetry (37)
507+
! Crystallographic Symmetry (39)
491508
!--------------------------
509+
492510
call method_table%add_method("crystallographic_symmetry_set_spacegroup", & ! method name
493511
"Creates the space group", & !doc-string
494512
METH_VARARGS, & ! this method takes arguments but no keyword arguments
@@ -674,6 +692,15 @@ function init() result(m)
674692
METH_VARARGS, & ! this method takes arguments but no keyword arguments
675693
c_funloc(crystallographic_symmetry_get_wyckoff_str_orbit)) ! address of Fortran function to add
676694

695+
call method_table%add_method("crystallographic_symmetry_get_multip_pos_crys", & ! method name
696+
"Mutiplicity of an x,y,z point for a given Space Group", & !doc-string
697+
METH_VARARGS, & ! this method takes arguments but no keyword arguments
698+
c_funloc(crystallographic_symmetry_get_multip_pos_crys)) ! address of Fortran function to add
699+
call method_table%add_method("crystallographic_symmetry_get_occ_site", & ! method name
700+
"Occupancy factor of an x,y,z point for a given Space Group", & !doc-string
701+
METH_VARARGS, & ! this method takes arguments but no keyword arguments
702+
c_funloc(crystallographic_symmetry_get_occ_site)) ! address of Fortran function to add
703+
677704

678705
!--------------------------
679706
! IO formats (35)
@@ -1085,8 +1112,11 @@ function init() result(m)
10851112
"Lm_xyz getter", & !doc-string
10861113
METH_VARARGS, & ! this method takes arguments but no keyword arguments
10871114
c_funloc(atom_typedef_get_Lm_xyz)) ! address of Fortran function to add
1115+
1116+
1117+
10881118
!--------------------------
1089-
! Reflection Utilities (13)
1119+
! Reflection Utilities (14)
10901120
!--------------------------
10911121
call method_table%add_method("reflections_utilities_hkl_uni_reflist", & ! method name
10921122
"Return the list of reflections", & !doc-string
@@ -1102,6 +1132,11 @@ function init() result(m)
11021132
"nref getter", & !doc-string
11031133
METH_VARARGS, & ! this method takes arguments but no keyword arguments
11041134
c_funloc(reflections_utilities_get_nref)) ! address of Fortran function to add
1135+
1136+
call method_table%add_method("reflections_utilities_write_reflist_info", & ! method name
1137+
"print reflist info", & !doc-string
1138+
METH_VARARGS, & ! this method takes arguments but no keyword arguments
1139+
c_funloc(reflections_utilities_write_reflist_info)) ! address of Fortran function to add
11051140

11061141
call method_table%add_method("reflections_utilities_del_reflection", & ! method name
11071142
"Reflection deallocation", & !doc-string

0 commit comments

Comments
 (0)