Files
CoolProp/dev/scripts/generate_constants_module.py
2014-05-14 12:46:24 +02:00

147 lines
7.0 KiB
Python

import os,shutil
"""
A little script to wrap the params enum for use in Cython code
Ian Bell, Feb 2013
"""
def params_constants():
fName = os.path.join('..','..','CoolProp','GlobalConstants.h')
contents = open(fName,'r').read().replace('\n','')
# Find the block that is something like :
"""
/* These are constants for the input and output parameters */
enum params {
/* Properties and others */
iB, iT, iP, iD, iC, iC0, iO, iU, iH, iS, iA, iG, iQ, iV, iL, iTfreeze, iPsat, iI, iDpdT, iDrhodT_p, iCritSplineT, iPrandtl,
/* Constants */
iMM, iTmax, iTmin, iAccentric, iDipole, iODP, iGWP20, iGWP100, iGWP500,
/* Reduced quantities; triple point and critical point */
iRhoreduce, iTreduce, iPtriple, iTtriple, iHcrit, iPcrit, iRhocrit, iScrit, iTcrit,
/* Phase identifiers */
iPhase, iPHASE_LIQUID, iPHASE_GAS, iPHASE_SUPERCRITICAL, iPHASE_TWOPHASE,
/* Derivatives */
iDERdh_dp__rho, iDERdh_dp__v, iDERZ, iDERdZ_dDelta, iDERdZ_dTau, iDERB, iDERdB_dT, iDERC, iDERdC_dT, iDERphir,
iDERdphir_dTau, iDERdphir_dDelta, iDERd2phir_dTau2, iDERd2phir_dDelta2, iDERd2phir_dDelta_dTau, iDERd3phir_dDelta3,
iDERd3phir_dDelta2_dTau, iDERd3phir_dDelta_dTau2, iDERd3phir_dTau3, iDERphi0, iDERdphi0_dTau, iDERd2phi0_dTau2,
iDERdphi0_dDelta, iDERd2phi0_dDelta2, iDERd2phi0_dDelta_dTau, iDERd3phi0_dTau3, iDERdp_dT__rho,
iDERdp_drho__T, iDERdh_dT__rho, iDERdh_drho__T, iDERdrho_dT__p, iDERdrho_dh__p,
iDERdrho_dp__h, iDERrho_smoothed, iDERdrho_smoothed_dh, iDERdrho_smoothed_dp, iDERdrhodh_constp_smoothed,
iDERdrhodp_consth_smoothed, iDERIsothermalCompressibility,
};
"""
left = contents.find('{', contents.find('enum params'));
right = contents.find('}')
entries = contents[left+1:right].split(',')
print entries
import re
entries = [re.sub(r"/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/", "", e.strip()) for e in entries]
entries = filter(lambda e: len(e) > 0, entries)
#Write the PXD definition file
pxd_output_file = open('param_constants_header.pxd','w')
pxd_output_file.write('#This file is automatically generated by the generate_constants_module.py script in dev/scripts.\n#DO NOT MODIFY THE CONTENTS OF THIS FILE!\n\ncdef extern from "CoolProp.h":\n\tenum params:\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
pyx_output_file = open('param_constants.pyx','w')
pyx_output_file.write('#This file is automatically generated by the generate_constants_module.py script in dev/scripts.\n#DO NOT MODIFY THE CONTENTS OF THIS FILE!\ncimport param_constants_header\n\n')
for param in entries:
param = param.strip()
pyx_output_file.write(param+' = '+'param_constants_header.'+param+'\n')
pyx_output_file.close()
shutil.copy2('param_constants_header.pxd',os.path.join('..','..','wrappers','Python','CoolProp','param_constants_header.pxd'))
shutil.copy2('param_constants.pyx',os.path.join('..','..','wrappers','Python','CoolProp','param_constants.pyx'))
def phase_constants():
fName = os.path.join('..','..','CoolProp','GlobalConstants.h')
with open(fName,'r') as f:
lines = f.readlines()
#Find the line that is something like : enum phases {iLiquid, iSupercritical, iGas, iTwoPhase};
the_line = None
for line in lines:
if line.find('enum phases') > -1:
the_line = line.strip()
break
if the_line is None:
raise ValueError('enum params line was not found in CoolProp.h')
else:
the_line = the_line.replace('}','{')
the_params = the_line.split('{')[1].split(',')
#Write the PXD definition file
pxd_output_file = open('phase_constants_header.pxd','w')
pxd_output_file.write('#This file is automatically generated by the generate_constants_module.py script in dev/scripts.\n#DO NOT MODIFY THE CONTENTS OF THIS FILE!\n\ncdef extern from "CoolProp.h":\n\tenum phase:\n')
for param in the_params:
param = param.strip()
pxd_output_file.write('\t\t'+param+'\n')
pxd_output_file.close()
#Write the PYX implementation file
pyx_output_file = open('phase_constants.pyx','w')
pyx_output_file.write('#This file is automatically generated by the generate_constants_module.py script in dev/scripts.\n#DO NOT MODIFY THE CONTENTS OF THIS FILE!\ncimport phase_constants_header\n\n')
for param in the_params:
param = param.strip()
pyx_output_file.write(param+' = '+'phase_constants_header.'+param+'\n')
pyx_output_file.close()
shutil.copy2('phase_constants_header.pxd',os.path.join('..','..','wrappers','Python','CoolProp','phase_constants_header.pxd'))
shutil.copy2('phase_constants.pyx',os.path.join('..','..','wrappers','Python','CoolProp','phase_constants.pyx'))
def unit_systems_constants():
fName = os.path.join('..','..','CoolProp','GlobalConstants.h')
with open(fName,'r') as f:
lines = f.readlines()
#Find the line that is something like : enum phases {iLiquid, iSupercritical, iGas, iTwoPhase};
the_line = None
for line in lines:
if line.find('enum unit_systems') > -1:
the_line = line.strip()
break
if the_line is None:
raise ValueError('unit_systems line was not found in CoolProp.h')
else:
the_line = the_line.replace('}','{')
the_params = the_line.split('{')[1].split(',')
#Write the PXD definition file
pxd_output_file = open('unit_systems_constants_header.pxd','w')
pxd_output_file.write('#This file is automatically generated by the generate_constants_module.py script in dev/scripts.\n#DO NOT MODIFY THE CONTENTS OF THIS FILE!\n\ncdef extern from "CoolProp.h":\n\tenum unit_systems:\n')
for param in the_params:
param = param.strip()
pxd_output_file.write('\t\t'+param+'\n')
pxd_output_file.close()
#Write the PYX implementation file
pyx_output_file = open('unit_systems_constants.pyx','w')
pyx_output_file.write('#This file is automatically generated by the generate_constants_module.py script in dev/scripts.\n#DO NOT MODIFY THE CONTENTS OF THIS FILE!\ncimport unit_systems_constants_header\n\n')
for param in the_params:
param = param.strip()
pyx_output_file.write(param+' = '+'unit_systems_constants_header.'+param+'\n')
pyx_output_file.close()
shutil.copy2('unit_systems_constants_header.pxd',os.path.join('..','..','wrappers','Python','CoolProp','unit_systems_constants_header.pxd'))
shutil.copy2('unit_systems_constants.pyx',os.path.join('..','..','wrappers','Python','CoolProp','unit_systems_constants.pyx'))
if __name__=='__main__':
params_constants()
phase_constants()
unit_systems_constants()