"""Copyright (c) 2024 MPI-M, Clara Bayley----- CLEO -----File: cxx2py.pyProject: pySDCreated Date: Friday 13th October 2023Author: Clara Bayley (CB)Additional Contributors:-----Last Modified: Tuesday 7th May 2024Modified By: CB-----License: BSD 3-Clause "New" or "Revised" Licensehttps://opensource.org/licenses/BSD-3-Clause-----File Description:"""######## function(s) for converting c++ values into python ones ########
[docs]defprint_dict_statement(filename,dictname,dict):"""print key, value pairs for dictionary created by reading filename"""print("\n---- "+dictname+" from ",filename,"-----")forcindict:print(c,"=",dict[c])print("---------------------------------------------\n")
[docs]defremove_excess_line(line):"""removes white spaces and and comments from a line"""line=line.strip()line=line.replace(" ","")if"#"inline:line=line[:line.find("#")]returnline
[docs]defline_with_assignment(line):"""find all the lines in a file which have form [...] = [...] ; ie. lines which assign variables. return list of these lines truncatd at ;"""line=line.replace(" ","")if"double"inlineor"int"inline:ind1=line.find("=")ind2=line.find(";")ifind2!=-1andind1!=-1andline[0]!="/":goodline=line[:ind2+1]returngoodlineelse:returnFalse
[docs]defwhere_typename_ends(line):"""finds index where c++ name for variable type ends for a const double, double, int or const int."""ifline[:3]=="int":x="int"elifline[:6]=="double":x="double"elifline[:11]=="constdouble":x="constdouble"elifline[:8]=="constint":x="constint"elifline[:12]=="constexprint":x="constexprint"elifline[:15]=="constexprdouble":x="constexprdouble"elifline[:20]=="constexprunsignedint":x="constexprunsignedint"elifline[:17]=="constexpruint64_t":x="constexpruint64_t"else:raiseException("c++ type for variable not understood")returnlen(x)
[docs]defread_cxxconsts_into_floats(filename):"""returns dictionary of value: float from (const) doubles and (const) ints assigned in a c++ file. Also returns dictionary of notfloats for values that couldn't be converted"""floats={}notfloats={}withopen(filename)asfile:rlines=[]filelines=file.readlines()forlineinfilelines:goodline=line_with_assignment(line)ifgoodline:rlines.append(goodline)forlineinrlines:x=where_typename_ends(line)name=line[x:line.find("=")]value=line[line.find("=")+1:line.find(";")]try:floats[name]=float(value)exceptValueError:notfloats[name]=valuereturnfloats
[docs]defderive_more_floats(consts):"""return mconsts dictionary containing some derived key,values from values in consts dictionary"""mconsts={"COORD0":consts["TIME0"]*consts["W0"],# characteristic coordinate grid scale [m]"RGAS_DRY":consts["RGAS_UNIV"]/consts["MR_DRY"],# specific gas constant for dry air [J/Kg/K]"RGAS_V":consts["RGAS_UNIV"]/consts["MR_WATER"],# specific gas constant for water [J/Kg/K]"CP0":consts["CP_DRY"],# characteristic Heat capacity [J/Kg/K]"Mr_ratio":consts["MR_WATER"]/consts["MR_DRY"],}mconsts["RHO0"]=consts["P0"]/(mconsts["CP0"]*consts["TEMP0"])# characteristic density [Kg/m^3]mconsts["MASS0"]=(consts["R0"]**3)*mconsts["RHO0"]# characteristic mass [Kg]returnmconsts