1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
#!/usr/bin/env python3
import sys
if sys.version_info.minor < 6:
raise Exception("Execute this script with at least python 3.6 to ensure all dicts are ordered!")
XRL_FUNCTIONS = {
'AtomicWeight': {'Z': int},
'ElementDensity': {'Z': int},
'CS_Total': {'Z': int, 'E': float},
'CS_Photo': {'Z': int, 'E': float},
'CS_Rayl': {'Z': int, 'E': float},
'CS_Compt': {'Z': int, 'E': float},
'CS_Energy': {'Z': int, 'E': float},
'CS_KN': {'E': float},
'CSb_Total': {'Z': int, 'E': float},
'CSb_Photo': {'Z': int, 'E': float},
'CSb_Rayl': {'Z': int, 'E': float},
'CSb_Compt': {'Z': int, 'E': float},
'DCS_Thoms': {'theta': float},
'DCS_KN': {'E':float, 'theta': float},
'DCS_Rayl': {'Z': int, 'E':float, 'theta': float},
'DCS_Compt': {'Z': int, 'E':float, 'theta': float},
'DCSb_Rayl': {'Z': int, 'E':float, 'theta': float},
'DCSb_Compt': {'Z': int, 'E':float, 'theta': float},
'DCSP_Thoms': {'theta': float, 'phi': float},
'DCSP_KN': {'E': float, 'theta': float, 'phi': float},
'DCSP_Rayl': {'Z': int, 'E': float, 'theta': float, 'phi': float},
'DCSP_Compt': {'Z': int, 'E': float, 'theta': float, 'phi': float},
'DCSPb_Rayl': {'Z': int, 'E': float, 'theta': float, 'phi': float},
'DCSPb_Compt': {'Z': int, 'E': float, 'theta': float, 'phi': float},
'FF_Rayl': {'Z': int, 'q': float},
'SF_Compt': {'Z': int, 'q': float},
'MomentTransf': {'E': float, 'theta': float},
'LineEnergy': {'Z': int, 'line': int},
'FluorYield': {'Z': int, 'shell': int},
'CosKronTransProb': {'Z': int, 'trans': int},
'EdgeEnergy': {'Z': int, 'shell': int},
'JumpFactor': {'Z': int, 'shell': int},
'CS_FluorLine': {'Z': int, 'line': int, 'E': float},
'CSb_FluorLine': {'Z': int, 'line': int, 'E': float},
'RadRate': {'Z': int, 'line': int},
'ComptonEnergy': {'E0': float, 'theta': float},
'Fi': {'Z': int, 'E': float},
'Fii': {'Z': int, 'E': float},
'CS_Photo_Total': {'Z': int, 'E': float},
'CSb_Photo_Total': {'Z': int, 'E': float},
'CS_Photo_Partial': {'Z': int, 'shell': int, 'E': float},
'CSb_Photo_Partial': {'Z': int, 'shell': int, 'E': float},
'CS_Total_Kissel': {'Z': int, 'E': float},
'CSb_Total_Kissel': {'Z': int, 'E': float},
'ComptonProfile': {'Z': int, 'pz': float},
'ComptonProfile_Partial': {'Z': int, 'shell': int, 'pz': float},
'ElectronConfig': {'Z': int, 'shell': int},
'ElectronConfig_Biggs': {'Z': int, 'shell': int},
'AtomicLevelWidth': {'Z': int, 'shell': int},
'AugerRate': {'Z': int, 'auger_trans': int},
'AugerYield': {'Z': int, 'shell': int},
'CS_FluorLine_Kissel': {'Z': int, 'line': int, 'E': float},
'CSb_FluorLine_Kissel': {'Z': int, 'line': int, 'E': float},
'CS_FluorLine_Kissel_Cascade': {'Z': int, 'line': int, 'E': float},
'CSb_FluorLine_Kissel_Cascade': {'Z': int, 'line': int, 'E': float},
'CS_FluorLine_Kissel_Nonradiative_Cascade': {'Z': int, 'line': int, 'E': float},
'CSb_FluorLine_Kissel_Nonradiative_Cascade': {'Z': int, 'line': int, 'E': float},
'CS_FluorLine_Kissel_Radiative_Cascade': {'Z': int, 'line': int, 'E': float},
'CSb_FluorLine_Kissel_Radiative_Cascade': {'Z': int, 'line': int, 'E': float},
'CS_FluorLine_Kissel_no_Cascade': {'Z': int, 'line': int, 'E': float},
'CSb_FluorLine_Kissel_no_Cascade': {'Z': int, 'line': int, 'E': float},
'PL1_pure_kissel': {'Z': int, 'E': float},
'PL1_rad_cascade_kissel': {'Z': int, 'E': float, 'PK': float},
'PL1_auger_cascade_kissel': {'Z': int, 'E': float, 'PK': float},
'PL1_full_cascade_kissel': {'Z': int, 'E': float, 'PK': float},
'PL2_pure_kissel': {'Z': int, 'E': float, 'PL1': float},
'PL2_rad_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float},
'PL2_auger_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float},
'PL2_full_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float},
'PL3_pure_kissel': {'Z': int, 'E': float, 'PL1': float, 'PL2': float},
'PL3_rad_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float},
'PL3_auger_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float},
'PL3_full_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float},
'PM1_pure_kissel': {'Z': int, 'E': float},
'PM1_rad_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float},
'PM1_auger_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float},
'PM1_full_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float},
'PM2_pure_kissel': {'Z': int, 'E': float, 'PM1': float},
'PM2_rad_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float},
'PM2_auger_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float},
'PM2_full_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float},
'PM3_pure_kissel': {'Z': int, 'E': float, 'PM1': float, 'PM2': float},
'PM3_rad_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float, 'PM2': float},
'PM3_auger_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float, 'PM2': float},
'PM3_full_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float, 'PM2': float},
'PM4_pure_kissel': {'Z': int, 'E': float, 'PM1': float, 'PM2': float, 'PM3': float},
'PM4_rad_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float, 'PM2': float, 'PM3': float},
'PM4_auger_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float, 'PM2': float, 'PM3': float},
'PM4_full_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float, 'PM2': float, 'PM3': float},
'PM5_pure_kissel': {'Z': int, 'E': float, 'PM1': float, 'PM2': float, 'PM3': float, 'PM4': float},
'PM5_rad_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float, 'PM2': float, 'PM3': float, 'PM4': float},
'PM5_auger_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float, 'PM2': float, 'PM3': float, 'PM4': float},
'PM5_full_cascade_kissel': {'Z': int, 'E': float, 'PK': float, 'PL1': float, 'PL2': float, 'PL3': float, 'PM1': float, 'PM2': float, 'PM3': float, 'PM4': float},
'CS_Total_CP': {'compound': str, 'E': float},
'CS_Photo_CP': {'compound': str, 'E': float},
'CS_Rayl_CP': {'compound': str, 'E': float},
'CS_Compt_CP': {'compound': str, 'E': float},
'CS_Energy_CP': {'compound': str, 'E': float},
'CSb_Total_CP': {'compound': str, 'E': float},
'CSb_Photo_CP': {'compound': str, 'E': float},
'CSb_Rayl_CP': {'compound': str, 'E': float},
'CSb_Compt_CP': {'compound': str, 'E': float},
'DCS_Rayl_CP': {'compound': str, 'E': float, 'theta': float},
'DCS_Compt_CP': {'compound': str, 'E': float, 'theta': float},
'DCSb_Rayl_CP': {'compound': str, 'E': float, 'theta': float},
'DCSb_Compt_CP': {'compound': str, 'E': float, 'theta': float},
'DCSP_Rayl_CP': {'compound': str, 'E': float, 'theta': float, 'phi': float},
'DCSP_Compt_CP': {'compound': str, 'E': float, 'theta': float, 'phi': float},
'DCSPb_Rayl_CP': {'compound': str, 'E': float, 'theta': float, 'phi': float},
'DCSPb_Compt_CP': {'compound': str, 'E': float, 'theta': float, 'phi': float},
'CS_Photo_Total_CP': {'compound': str, 'E': float},
'CS_Total_Kissel_CP': {'compound': str, 'E': float},
'CSb_Photo_Total_CP': {'compound': str, 'E': float},
'CSb_Total_Kissel_CP': {'compound': str, 'E': float},
'Refractive_Index_Re': {'compound': str, 'E': float, 'density': float},
'Refractive_Index_Im': {'compound': str, 'E': float, 'density': float}
}
def generate_declaration_for_str(arg_name: str) -> str:
return f'''CHARACTER (KIND=C_CHAR,LEN=*), INTENT(IN) :: {arg_name}
CHARACTER (KIND=C_CHAR), DIMENSION(:), ALLOCATABLE, TARGET :: &
{arg_name}_F'''
def generate_declaration_for_float(arg_name: str) -> str:
return f"REAL (C_DOUBLE), INTENT(IN) :: {arg_name}"
def generate_declaration_for_int(arg_name: str) -> str:
return f"INTEGER (C_INT), INTENT(IN) :: {arg_name}"
def generate_iface_declaration_for_str(arg_name: str) -> str:
return f"TYPE (C_PTR), INTENT(IN), VALUE :: {arg_name}"
def generate_iface_declaration_for_float(arg_name: str) -> str:
return f"REAL (C_DOUBLE), INTENT(IN), VALUE :: {arg_name}"
def generate_iface_declaration_for_int(arg_name: str) -> str:
return f"INTEGER (C_INT), INTENT(IN), VALUE :: {arg_name}"
GENERATE_DECLARATION = {
str: generate_declaration_for_str,
float: generate_declaration_for_float,
int: generate_declaration_for_int
}
GENERATE_IFACE_DECLARATION = {
str: generate_iface_declaration_for_str,
float: generate_iface_declaration_for_float,
int: generate_iface_declaration_for_int
}
def generate_preprocess_for_str(arg_name: str) -> str:
return f'''CALL stringF2C({arg_name}, {arg_name}_F)
'''
GENERATE_PREPROCESS = {
str: generate_preprocess_for_str,
float: lambda arg_name: "",
int: lambda arg_name: ""
}
GENERATE_CALL = {
str: lambda arg_name: f"C_LOC({arg_name}_F)",
float: lambda arg_name: f"{arg_name}",
int: lambda arg_name: f"{arg_name}"
}
def process_function(name: str) -> str:
args = XRL_FUNCTIONS[name]
arg_list = ', '.join(args.keys())
arg_declarations = '\n '.join([GENERATE_DECLARATION[arg_type](arg_name) for arg_name, arg_type in args.items()])
arg_preprocess = ''.join([GENERATE_PREPROCESS[arg_type](arg_name) for arg_name, arg_type in args.items()])
arg_calls = ', '.join([GENERATE_CALL[arg_type](arg_name) for arg_name, arg_type in args.items()])
arg_iface_declarations = '\n '.join([GENERATE_IFACE_DECLARATION[arg_type](arg_name) for arg_name, arg_type in args.items()])
return f'''
FUNCTION {name}({arg_list}, error) RESULT(rv)
USE, INTRINSIC :: ISO_C_BINDING
USE, INTRINSIC :: ISO_FORTRAN_ENV
IMPLICIT NONE
{arg_declarations}
REAL (C_DOUBLE) :: rv
TYPE(xrl_error), POINTER, OPTIONAL :: error
TYPE (C_PTR) :: errorPtr, errorPtrLoc
TARGET :: errorPtr
INTERFACE
FUNCTION {name}C({arg_list}, error) &
BIND(C,NAME='{name}')
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
{arg_iface_declarations}
REAL (C_DOUBLE) :: {name}C
TYPE (C_PTR),INTENT(IN),VALUE :: error
ENDFUNCTION {name}C
ENDINTERFACE
errorPtr = C_NULL_PTR
errorPtrLoc = C_NULL_PTR
IF (PRESENT(error)) THEN
IF (.NOT. ASSOCIATED(error)) THEN
errorPtrLoc = C_LOC(errorPtr)
ELSE
! print warning
WRITE (error_unit, '(A)') &
'error POINTER must be disassociated!'
ENDIF
ENDIF
{arg_preprocess}
rv = {name}C({arg_calls}, errorPtrLoc)
IF (C_ASSOCIATED(errorPtr)) THEN
CALL process_error(errorPtr, error)
ENDIF
ENDFUNCTION {name}
'''
with open('xraylib_wrap_generated.F90', 'w') as f:
f.write('! This file has been generated automatically using generate-code.py\n\n')
f.write('\n'.join([process_function(function) for function in XRL_FUNCTIONS.keys()]))
|