Wrap all configuration variables into enumerations and a python module

This commit is contained in:
Ian Bell
2016-03-21 21:50:23 -07:00
parent 02e0323b67
commit 2e373c4ca9
2 changed files with 64 additions and 3 deletions

View File

@@ -39,6 +39,14 @@ cdef extern from "Configuration.h" namespace "CoolProp":
string _get_config_as_json_string "CoolProp::get_config_as_json_string"() except +
void _set_config_as_json_string "CoolProp::set_config_as_json_string"(string) except +
string _config_key_description "CoolProp::config_key_description"(string) except +
void _set_config_string "CoolProp::set_config_string"(constants_header.configuration_keys,string) except +
void _set_config_double "CoolProp::set_config_double"(constants_header.configuration_keys,double) except +
void _set_config_bool "CoolProp::set_config_bool"(constants_header.configuration_keys,bint) except +
string _get_config_string "CoolProp::get_config_string"(constants_header.configuration_keys) except +
double _get_config_double "CoolProp::get_config_double"(constants_header.configuration_keys) except +
bint _get_config_bool "CoolProp::get_config_bool"(constants_header.configuration_keys) except +
cdef extern from "DataStructures.h" namespace "CoolProp":
string _get_parameter_information "CoolProp::get_parameter_information"(int, string) except +
@@ -202,6 +210,30 @@ cpdef set_config_as_json_string(string s):
Current state can be obtained by calling get_config_as_json_string
"""
_set_config_as_json_string(s)
cpdef set_config_double(constants_header.configuration_keys key, double value):
""" Set configuration key that is a double-precision float; wrapper of wrapper of C++ function :cpapi:`CoolProp::set_config_double` """
_set_config_double(key, value)
cpdef set_config_string(constants_header.configuration_keys key, string value):
""" Set a configuration key that is a string; wrapper of wrapper of C++ function :cpapi:`CoolProp::set_config_string` """
_set_config_string(key, value)
cpdef set_config_bool(constants_header.configuration_keys key, bint value):
""" Set a configuration key that is a boolean; wrapper of wrapper of C++ function :cpapi:`CoolProp::set_config_bool` """
_set_config_bool(key, value)
cpdef double get_config_double(constants_header.configuration_keys key):
""" Get a configuration key that is a double-precision float; wrapper of wrapper of C++ function :cpapi:`CoolProp::get_config_double` """
_get_config_double(key)
cpdef string get_config_string(constants_header.configuration_keys key):
""" Get a configuration key that is a string; wrapper of wrapper of C++ function :cpapi:`CoolProp::get_config_string` """
_get_config_string(key)
cpdef bint get_config_bool(constants_header.configuration_keys key):
""" Get a configuration key that is a boolean; wrapper of wrapper of C++ function :cpapi:`CoolProp::get_config_bool` """
return _get_config_bool(key)
cpdef int get_parameter_index(string key):
return _get_parameter_index(key)

View File

@@ -34,20 +34,41 @@ def params_constants(enum_key):
keys = [k for k in keys if k]
return keys
def config_constants():
fName = os.path.join('..','..','include','Configuration.h')
contents = open(fName,'r').readlines()
matching_lines = [i for i,line in enumerate(contents) if "#define CONFIGURATION_KEYS_ENUM" in line]
assert(len(matching_lines)==1)
iline = matching_lines[0] + 1
keys = []
while iline < 1000 and contents[iline].strip().startswith('X('):
line = contents[iline].strip()[2::]
key = line.split(',')[0]
keys.append(key)
iline += 1
return ('configuration_keys',keys)
def generate_cython(data):
def generate_cython(data, config_data):
print('****** Writing the constants module ******')
# Write the PXD definition file
pxd_output_file = open('CoolProp/constants_header.pxd','w')
pxd_output_file.write('# This file is automatically generated by the generate_constants_module.py script in wrappers/Python.\n# DO NOT MODIFY THE CONTENTS OF THIS FILE!\n\ncdef extern from "DataStructures.h" namespace "CoolProp":\n')
pxd_output_file.write('# This file is automatically generated by the generate_constants_module.py script in wrappers/Python.\n# DO NOT MODIFY THE CONTENTS OF THIS FILE!\n\ncdef extern from "DataStructures.h" namespace "CoolProp":\n')
for enum_key, entries in data:
pxd_output_file.write('\tctypedef enum '+enum_key+':\n')
for param in entries:
param = param.strip()
pxd_output_file.write('\t\t'+param+'\n')
pxd_output_file.write('\n\ncdef extern from "Configuration.h":\n')
enum_key, entries = config_data
pxd_output_file.write('\tctypedef enum '+enum_key+':\n')
for param in entries:
param = param.strip()
pxd_output_file.write('\t\t'+param+'\n')
pxd_output_file.close()
# Write the PYX implementation file
@@ -57,6 +78,10 @@ def generate_cython(data):
for param in entries:
param = param.strip()
pyx_output_file.write(param+' = '+'constants_header.'+param+'\n')
enum_key, entries = config_data
for param in entries:
param = param.strip()
pyx_output_file.write(param+' = '+'constants_header.'+param+'\n')
pyx_output_file.close()
# Write the PY implementation file
@@ -66,11 +91,15 @@ def generate_cython(data):
for param in entries:
param = param.strip()
py_output_file.write(param+' = '+'_constants.'+param+'\n')
enum_key, entries = config_data
for param in entries:
param = param.strip()
py_output_file.write(param+' = '+'_constants.'+param+'\n')
py_output_file.close()
def generate():
data = [(enum,params_constants(enum)) for enum in ['parameters', 'input_pairs', 'fluid_types', 'phases']]
generate_cython(data)
generate_cython(data,config_constants())
if __name__=='__main__':
generate()