KineticPreProcessor / KPP

The KPP kinetic preprocessor is a software tool that assists the computer simulation of chemical kinetic systems
GNU General Public License v3.0
19 stars 11 forks source link

$KPP_ROOT_Fun( ) and $KPP_ROOT_Fun_Chem( ) bug still in KPP v3.1.0 for matlab code #96

Closed jnavarla closed 2 months ago

jnavarla commented 4 months ago

Hi every one, Nice improvements in the v3.1.0 for debbuging .spc and .eqn files.

But, the problem on the generation of the matlab code from version 3.0 still is not corrected. The generated $KKP_ROOT_Fun.m file defines the function as: function [ Aout ] = $KPP_ROOT_Fun ( V , F , RCT , Vdot ) while the function is called in %KKP_ROOT_Fun_Chem.m as: P = $KPP_ROOT_Fun( Y, FIX, RCONST ); Then the matlab function is obiously bad defined.

I would suggest to define the Fun.m function as: function [ Vdot, Aout ] = $KKP_ROOT_Fun ( V , F , RCT ) Defined in this way the function could be called in Fun_Chem.m as: [P, ~]= $KPP_ROOT_Fun( Y, FIX, RCONST ); and the same function could be useful for the calculation of all the reaction rates after the integration of the model as: [~, RR]= $KPP_ROOT_Fun( Y, FIX, RCONST );

I recognize that this is the previous #56 Issue where apparently there is proposed a solution but still is not implemented. Best regards, Javier

yantosca commented 4 months ago

Thanks for writing @jnavarla. I'll push a bugfix branch and ask you to verify that it works. Stay tuned!

jnavarla commented 4 months ago

Hello, I'm not sure if this can help, but after the matlab code generation with KPP v3.1.0, I patch the code with the following python code:

import sys

def patch_mat_fun(model_name):
    # Define function names
    fun_name = model_name + "_Fun"
    fun_chem_name = model_name + "_Fun_Chem"

    # Modify and save function files
    modify_fun_file(fun_name + ".m", "function  [ Vdot,Aout ] =  " + fun_name + " ( V , F , RCT )", 26, model_name + "_Fun.m")
    modify_fun_file(fun_chem_name + ".m", "   [P,~]= " + fun_name + " ( Y , FIX , RCONST );", 14, model_name + "_Fun_Chem.m")
    # insert lines
    insert_fun_file(fun_name + ".m", "   Aout = A(:);\n",6, model_name + "_Fun.m")
    insert_fun_file(fun_name + ".m", "   Vdot = Vdot(:);\n",6, model_name + "_Fun.m")
    insert_fun_file(fun_name + ".m", "\n",6, model_name + "_Fun.m")

def insert_fun_file(file_name, new_line, relative_line_num, output_file):
    # Read original file
    with open(file_name, 'r') as file:
        file_lines = file.readlines()

    # Modify lines
    # file_lines[26] = new_line + "\n"
    file_lines.insert(-relative_line_num, new_line)

    # Save modified file
    with open(output_file, 'w') as file:
        file.writelines(file_lines)

def modify_fun_file(file_name, new_line, line_num, output_file):
    # Read original file
    with open(file_name, 'r') as file:
        file_lines = file.readlines()

    # Modify lines
    file_lines[line_num] = new_line + "\n"

    # Save modified file
    with open(output_file, 'w') as file:
        file.writelines(file_lines)

def main():
    if len(sys.argv) != 2:
        print("Usage: python patch_mat_fun.py ModelName")
        sys.exit(1)

    model_name = sys.argv[1]
    patch_mat_fun(model_name)

if __name__ == "__main__":
    main()

Is not maybe the most efficient python code, but allows running the matlab files. Best regards,

yantosca commented 4 months ago

Thanks for your patience @jnavarla. I was looking into it but then had to move on to a few other things. I'll get back to it hophefully soon. I see where we need to make the updates in KPP.

yantosca commented 2 months ago

We can close this issue since PR #99 is now merged into version 3.1.1.