CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

UDF call Python permission error

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 26, 2024, 17:35
Default UDF call Python permission error
  #1
New Member
 
Yifan Gu
Join Date: Oct 2023
Posts: 5
Rep Power: 3
yifan998 is on a distinguished road
Hello all,

I am trying to write a UDF to calculate the reaction rate. For my solution, I am writing a text file to output the parameter and call Python to do the calculation. The reason to do that is I want to fast coverage. Then, after I calculate the rate, I output the rates as a text file. the udf will read the file and put the rate to the reaction.

Here is my udf:
#include "udf.h"
//#include <stdlib.h>
#include <stdio.h>
// #include <iostream>

DEFINE_SR_RATE(my_rate, c, t, r, mw, yi, rr)
//DEFINE_ON_DEMAND(CalculateReactionRate)
{
int i;
real temp, pres;
//double ID_H2, ID_O2, ID_H2O, ID_CH4, ID_CO, ID_CO2;
//real concentration_H2, concentration_O2, concentration_H2O, concentration_CH4, concentration_CO, concentration_CO2;

/* Get the domain and thread */
temp = C_T(c, t);
pres = C_P(c, t);
int check_list[6] = { 0 };
if (yi != check_list) {
/* Write data to a file */
FILE* fp_out;
fp_out = fopen("fluent_data.txt", "w");
//fprintf(fp_out, "%f\n", zone_ID);
for (i = 0; i < sizeof(yi); i++)
{
fprintf(fp_out, "%f, ", yi[i]);
// Message0("species index=%d,species name=%s\n", i, spe_name);
}
fprintf(fp_out, "%f, ", temp);
fprintf(fp_out, "%f", pres);

// for (i = 0; i < MAX_SPE_EQNS; i++) {
// fprintf(fp_out, "%d %f\n", i, yi[i]);
// }
fclose(fp_out);
}


putenv("PYTHONHOME=C:/Users/guyifan/Anaconda3/envs/ct-env");
putenv("PATH=C:/Users/guyifan/Anaconda3/envs/ct-env");

int status = system("python python_data_process.py");

if (status != 0) {
Message("Error: Failed to execute Python script\n");
return;
}



FILE* fp_in;

fp_in = fopen("reaction_rate.txt", "r");
if (fp_in == NULL) {
Message("Error: Failed to open reaction rate file\n");
return;
}
double reaction_rate[77];

fscanf(fp_in, "%lf", &reaction_rate[0]);
fscanf(fp_in, "%lf", &reaction_rate[1]);
fscanf(fp_in, "%lf", &reaction_rate[2]);
fscanf(fp_in, "%lf", &reaction_rate[3]);
fscanf(fp_in, "%lf", &reaction_rate[4]);
fscanf(fp_in, "%lf", &reaction_rate[5]);
fscanf(fp_in, "%lf", &reaction_rate[6]);
fscanf(fp_in, "%lf", &reaction_rate[7]);
fscanf(fp_in, "%lf", &reaction_rate[8]);
fscanf(fp_in, "%lf", &reaction_rate[9]);
fscanf(fp_in, "%lf", &reaction_rate[10]);
fscanf(fp_in, "%lf", &reaction_rate[11]);
fscanf(fp_in, "%lf", &reaction_rate[12]);
fscanf(fp_in, "%lf", &reaction_rate[13]);
fscanf(fp_in, "%lf", &reaction_rate[14]);
fscanf(fp_in, "%lf", &reaction_rate[15]);
fscanf(fp_in, "%lf", &reaction_rate[16]);
fscanf(fp_in, "%lf", &reaction_rate[17]);
fscanf(fp_in, "%lf", &reaction_rate[18]);
fscanf(fp_in, "%lf", &reaction_rate[19]);
fscanf(fp_in, "%lf", &reaction_rate[20]);
fscanf(fp_in, "%lf", &reaction_rate[21]);
fscanf(fp_in, "%lf", &reaction_rate[22]);
fscanf(fp_in, "%lf", &reaction_rate[23]);
fscanf(fp_in, "%lf", &reaction_rate[24]);
fscanf(fp_in, "%lf", &reaction_rate[25]);
fscanf(fp_in, "%lf", &reaction_rate[26]);
fscanf(fp_in, "%lf", &reaction_rate[27]);
fscanf(fp_in, "%lf", &reaction_rate[28]);
fscanf(fp_in, "%lf", &reaction_rate[29]);
fscanf(fp_in, "%lf", &reaction_rate[30]);
fscanf(fp_in, "%lf", &reaction_rate[31]);
fscanf(fp_in, "%lf", &reaction_rate[32]);
fscanf(fp_in, "%lf", &reaction_rate[33]);
fscanf(fp_in, "%lf", &reaction_rate[34]);
fscanf(fp_in, "%lf", &reaction_rate[35]);
fscanf(fp_in, "%lf", &reaction_rate[36]);
fscanf(fp_in, "%lf", &reaction_rate[37]);
fscanf(fp_in, "%lf", &reaction_rate[38]);
fscanf(fp_in, "%lf", &reaction_rate[39]);
fscanf(fp_in, "%lf", &reaction_rate[40]);
fscanf(fp_in, "%lf", &reaction_rate[41]);
fscanf(fp_in, "%lf", &reaction_rate[42]);
fscanf(fp_in, "%lf", &reaction_rate[43]);
fscanf(fp_in, "%lf", &reaction_rate[44]);
fscanf(fp_in, "%lf", &reaction_rate[45]);
fscanf(fp_in, "%lf", &reaction_rate[46]);
fscanf(fp_in, "%lf", &reaction_rate[47]);
fscanf(fp_in, "%lf", &reaction_rate[48]);
fscanf(fp_in, "%lf", &reaction_rate[49]);
fscanf(fp_in, "%lf", &reaction_rate[50]);
fscanf(fp_in, "%lf", &reaction_rate[51]);
fscanf(fp_in, "%lf", &reaction_rate[52]);
fscanf(fp_in, "%lf", &reaction_rate[53]);
fscanf(fp_in, "%lf", &reaction_rate[54]);
fscanf(fp_in, "%lf", &reaction_rate[55]);
fscanf(fp_in, "%lf", &reaction_rate[56]);
fscanf(fp_in, "%lf", &reaction_rate[57]);
fscanf(fp_in, "%lf", &reaction_rate[58]);
fscanf(fp_in, "%lf", &reaction_rate[59]);
fscanf(fp_in, "%lf", &reaction_rate[60]);
fscanf(fp_in, "%lf", &reaction_rate[61]);
fscanf(fp_in, "%lf", &reaction_rate[62]);
fscanf(fp_in, "%lf", &reaction_rate[63]);
fscanf(fp_in, "%lf", &reaction_rate[64]);
fscanf(fp_in, "%lf", &reaction_rate[65]);
fscanf(fp_in, "%lf", &reaction_rate[66]);
fscanf(fp_in, "%lf", &reaction_rate[67]);
fscanf(fp_in, "%lf", &reaction_rate[68]);
fscanf(fp_in, "%lf", &reaction_rate[69]);
fscanf(fp_in, "%lf", &reaction_rate[70]);
fscanf(fp_in, "%lf", &reaction_rate[71]);
fscanf(fp_in, "%lf", &reaction_rate[72]);
fscanf(fp_in, "%lf", &reaction_rate[73]);
fscanf(fp_in, "%lf", &reaction_rate[74]);
fscanf(fp_in, "%lf", &reaction_rate[75]);
fscanf(fp_in, "%lf", &reaction_rate[76]);
fscanf(fp_in, "%lf", &reaction_rate[77]);

fclose(fp_in);



if (strcmp(r->name, "ATR1") == 0) {
*rr = reaction_rate[0];
}
else if (strcmp(r->name, "ATR2") == 0) {
*rr = reaction_rate[1];
}
else if (strcmp(r->name, "ATR3") == 0) {
*rr = reaction_rate[2];
}
else if (strcmp(r->name, "ATR4") == 0) {
*rr = reaction_rate[3];
}
else if (strcmp(r->name, "ATR5") == 0) {
*rr = reaction_rate[4];
}
else if (strcmp(r->name, "ATR6") == 0) {
*rr = reaction_rate[5];
}
else if (strcmp(r->name, "ATR7") == 0) {
*rr = reaction_rate[6];
}
else if (strcmp(r->name, "ATR8") == 0) {
*rr = reaction_rate[7];
}
else if (strcmp(r->name, "ATR9") == 0) {
*rr = reaction_rate[8];
}
else if (strcmp(r->name, "ATR10") == 0) {
*rr = reaction_rate[9];
}
else if (strcmp(r->name, "ATR11") == 0) {
*rr = reaction_rate[10];
}
else if (strcmp(r->name, "ATR12") == 0) {
*rr = reaction_rate[11];
}
else if (strcmp(r->name, "ATR13") == 0) {
*rr = reaction_rate[12];
}
else if (strcmp(r->name, "ATR14") == 0) {
*rr = reaction_rate[13];
}
else if (strcmp(r->name, "ATR15") == 0) {
*rr = reaction_rate[14];
}
else if (strcmp(r->name, "ATR16") == 0) {
*rr = reaction_rate[15];
}
else if (strcmp(r->name, "ATR17") == 0) {
*rr = reaction_rate[16];
}
else if (strcmp(r->name, "ATR18") == 0) {
*rr = reaction_rate[17];
}
else if (strcmp(r->name, "ATR19") == 0) {
*rr = reaction_rate[18];
}
else if (strcmp(r->name, "ATR20") == 0) {
*rr = reaction_rate[19];
}
else if (strcmp(r->name, "ATR21") == 0) {
*rr = reaction_rate[20];
}
else if (strcmp(r->name, "ATR22") == 0) {
*rr = reaction_rate[21];
}
else if (strcmp(r->name, "ATR23") == 0) {
*rr = reaction_rate[22];
}
else if (strcmp(r->name, "ATR24") == 0) {
*rr = reaction_rate[23];
}
else if (strcmp(r->name, "ATR25") == 0) {
*rr = reaction_rate[24];
}
else if (strcmp(r->name, "ATR26") == 0) {
*rr = reaction_rate[25];
}
else if (strcmp(r->name, "ATR27") == 0) {
*rr = reaction_rate[26];
}
else if (strcmp(r->name, "ATR28") == 0) {
*rr = reaction_rate[27];
}
else if (strcmp(r->name, "ATR29") == 0) {
*rr = reaction_rate[28];
}
else if (strcmp(r->name, "ATR30") == 0) {
*rr = reaction_rate[29];
}
else if (strcmp(r->name, "ATR31") == 0) {
*rr = reaction_rate[30];
}
else if (strcmp(r->name, "ATR32") == 0) {
*rr = reaction_rate[31];
}
else if (strcmp(r->name, "ATR33") == 0) {
*rr = reaction_rate[32];
}
else if (strcmp(r->name, "ATR34") == 0) {
*rr = reaction_rate[33];
}
else if (strcmp(r->name, "ATR35") == 0) {
*rr = reaction_rate[34];
}
else if (strcmp(r->name, "ATR36") == 0) {
*rr = reaction_rate[35];
}
else if (strcmp(r->name, "AIR1") == 0) {
*rr = reaction_rate[36];
}
else if (strcmp(r->name, "AIR2") == 0) {
*rr = reaction_rate[37];
}
else if (strcmp(r->name, "AIR3") == 0) {
*rr = reaction_rate[38];
}
else if (strcmp(r->name, "AIR4") == 0) {
*rr = reaction_rate[39];
}
else if (strcmp(r->name, "AIR5") == 0) {
*rr = reaction_rate[40];
}
else if (strcmp(r->name, "AIR6") == 0) {
*rr = reaction_rate[41];
}
else if (strcmp(r->name, "AIR7") == 0) {
*rr = reaction_rate[42];
}
else if (strcmp(r->name, "AIR8") == 0) {
*rr = reaction_rate[43];
}
else if (strcmp(r->name, "AIR9") == 0) {
*rr = reaction_rate[44];
}
else if (strcmp(r->name, "AIR10") == 0) {
*rr = reaction_rate[45];
}
else if (strcmp(r->name, "AIR11") == 0) {
*rr = reaction_rate[46];
}
else if (strcmp(r->name, "AIR12") == 0) {
*rr = reaction_rate[47];
}
else if (strcmp(r->name, "AIR13") == 0) {
*rr = reaction_rate[48];
}
else if (strcmp(r->name, "AIR14") == 0) {
*rr = reaction_rate[49];
}
else if (strcmp(r->name, "AIR15") == 0) {
*rr = reaction_rate[50];
}
else if (strcmp(r->name, "AIR16") == 0) {
*rr = reaction_rate[51];
}
else if (strcmp(r->name, "AIR17") == 0) {
*rr = reaction_rate[52];
}
else if (strcmp(r->name, "AIR18") == 0) {
*rr = reaction_rate[53];
}
else if (strcmp(r->name, "AIR19") == 0) {
*rr = reaction_rate[54];
}
else if (strcmp(r->name, "AIR20") == 0) {
*rr = reaction_rate[55];
}
else if (strcmp(r->name, "AIR21") == 0) {
*rr = reaction_rate[56];
}
else if (strcmp(r->name, "AIR22") == 0) {
*rr = reaction_rate[57];
}
else if (strcmp(r->name, "AIR23") == 0) {
*rr = reaction_rate[58];
}
else if (strcmp(r->name, "AIR24") == 0) {
*rr = reaction_rate[59];
}
else if (strcmp(r->name, "AIR25") == 0) {
*rr = reaction_rate[60];
}
else if (strcmp(r->name, "AIR26") == 0) {
*rr = reaction_rate[61];
}
else if (strcmp(r->name, "AIR27") == 0) {
*rr = reaction_rate[62];
}
else if (strcmp(r->name, "AIR28") == 0) {
*rr = reaction_rate[63];
}
else if (strcmp(r->name, "AIR29") == 0) {
*rr = reaction_rate[64];
}
else if (strcmp(r->name, "AIR30") == 0) {
*rr = reaction_rate[65];
}
else if (strcmp(r->name, "AIR31") == 0) {
*rr = reaction_rate[66];
}
else if (strcmp(r->name, "AIR32") == 0) {
*rr = reaction_rate[67];
}
else if (strcmp(r->name, "AIR33") == 0) {
*rr = reaction_rate[68];
}
else if (strcmp(r->name, "AIR34") == 0) {
*rr = reaction_rate[69];
}
else if (strcmp(r->name, "AIR35") == 0) {
*rr = reaction_rate[70];
}
else if (strcmp(r->name, "AIR36") == 0) {
*rr = reaction_rate[71];
}
else if (strcmp(r->name, "AIR37") == 0) {
*rr = reaction_rate[72];
}
else if (strcmp(r->name, "AIR38") == 0) {
*rr = reaction_rate[73];
}
else if (strcmp(r->name, "AIR39") == 0) {
*rr = reaction_rate[74];
}
else if (strcmp(r->name, "AIR40") == 0) {
*rr = reaction_rate[75];
}
else if (strcmp(r->name, "AIR41") == 0) {
*rr = reaction_rate[76];
}
else if (strcmp(r->name, "AIR42") == 0) {
*rr = reaction_rate[77];
}
else {
*rr = 0;
}
}

And here is my python code:


from tkinter import END
from IPython import get_ipython
import numpy as np
import cantera as ct
# import os
# import sys
# import time

# get_ipython().magic('reset -sf')

def calculate_reaction_rate_ATR(temperature, pressure, concentration):
gas = ct.Solution('methane_pox_on_pt.yaml','gas')
surf = ct.Interface('methane_pox_on_pt.yaml', 'Pt_surf',[gas])

surf.TP = temperature, pressure
# gas = surf.adjacent['gas']
gas.TPY = temperature, pressure, concentration

surf.advance_coverages(0.01)
# Get reaction rates
reaction_rates = surf.net_rates_of_progress
# def write_reaction_rates_to_file(reaction_rates, filename):
# with open(filename, 'w') as file:
# for rate in reaction_rates:
# file.write(str(rate) + '\n')
return reaction_rates


def calculate_reaction_rate_AIR(temperature, pressure, concentration):
gas = ct.Solution('sm_Ni.yaml','gas')
surf = ct.Interface('sm_Ni.yaml', 'surf',[gas])

surf.TP = temperature, pressure
# gas = surf.adjacent['gas']
gas.TPY = temperature, pressure, concentration

surf.advance_coverages(0.01)
# Get reaction rates
reaction_rates = surf.net_rates_of_progress
return reaction_rates

def calculate_reaction_rate_Other(temperature, pressure, concentration):
reaction_rates = [0]
return reaction_rates

with open("fluent_data.txt", "r") as f:
f_list = [float(i) for line in f for i in line.split(',') if i.strip()]
species_concentrations = f_list[0:7]
temperature = f_list[7]
pressure = f_list[8]
f.close()

test_array = [0,0,0,0,0,0,0]
if species_concentrations != test_array:
reaction_rate_ATR = calculate_reaction_rate_ATR(temperature, pressure, species_concentrations)
reaction_rate_AIR = calculate_reaction_rate_AIR(temperature, pressure, species_concentrations)
else:
reaction_rate_ATR = [0.5] * 36
reaction_rate_AIR = [0.5] * 42

with open("reaction_rate.txt", "w+") as f:
for rr in reaction_rate_ATR:
f.write(str(rr) + "\n")
for rr in reaction_rate_AIR:
f.write(str(rr) + "\n")
f.close()


When I compile the udf, the error is below:
PermissionError: [Errno 13] Permission denied: 'reaction_rate.txt'.

I hope I described the issue clearly, can anyone help me with this error?

Thank you
yifan998 is offline   Reply With Quote

Old   February 29, 2024, 13:01
Default
  #2
Senior Member
 
Daniel
Join Date: Feb 2017
Location: Germany
Posts: 172
Rep Power: 9
zacko is on a distinguished road
Can you put it in a code block instead of putting all lines of code into the text block here? Would be easier to look at.
zacko is offline   Reply With Quote

Old   February 29, 2024, 13:18
Default
  #3
New Member
 
Yifan Gu
Join Date: Oct 2023
Posts: 5
Rep Power: 3
yifan998 is on a distinguished road
test_v4.c
Quote:
Originally Posted by zacko View Post
Can you put it in a code block instead of putting all lines of code into the text block here? Would be easier to look at.

YEs. Here is c code. Thank you for your help!
test_v4.c
yifan998 is offline   Reply With Quote

Old   February 29, 2024, 20:34
Default
  #4
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,761
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
Code:
code block
LuckyTran is offline   Reply With Quote

Old   March 4, 2024, 17:39
Default
  #5
New Member
 
Yifan Gu
Join Date: Oct 2023
Posts: 5
Rep Power: 3
yifan998 is on a distinguished road
Quote:
Originally Posted by LuckyTran View Post
Code:
code block
Code:
#include "udf.h"
//#include <stdlib.h>
#include <stdio.h>
// #include <iostream>

DEFINE_SR_RATE(my_rate, c, t, r, mw, yi, rr)
//DEFINE_ON_DEMAND(CalculateReactionRate)
{
int i;
real temp, pres;
//double ID_H2, ID_O2, ID_H2O, ID_CH4, ID_CO, ID_CO2;
//real concentration_H2, concentration_O2, concentration_H2O, concentration_CH4, concentration_CO, concentration_CO2;

/* Get the domain and thread */
temp = C_T(c, t);
pres = C_P(c, t);
int check_list[6] = { 0 };
if (yi != check_list) {
/* Write data to a file */
FILE* fp_out;
fp_out = fopen("fluent_data.txt", "w");
//fprintf(fp_out, "%f\n", zone_ID);
for (i = 0; i < sizeof(yi); i++)
{
fprintf(fp_out, "%f, ", yi[i]);
// Message0("species index=%d,species name=%s\n", i, spe_name);
}
fprintf(fp_out, "%f, ", temp);
fprintf(fp_out, "%f", pres);

// for (i = 0; i < MAX_SPE_EQNS; i++) {
// fprintf(fp_out, "%d %f\n", i, yi[i]);
// }
fclose(fp_out);
}


putenv("PYTHONHOME=C:/Users/guyifan/Anaconda3/envs/ct-env");
putenv("PATH=C:/Users/guyifan/Anaconda3/envs/ct-env");

int status = system("python python_data_process.py");

if (status != 0) {
Message("Error: Failed to execute Python script\n");
return;
}



FILE* fp_in;

fp_in = fopen("reaction_rate.txt", "r");
if (fp_in == NULL) {
Message("Error: Failed to open reaction rate file\n");
return;
}
double reaction_rate[77];

fscanf(fp_in, "%lf", &reaction_rate[0]);
fscanf(fp_in, "%lf", &reaction_rate[1]);
fscanf(fp_in, "%lf", &reaction_rate[2]);
fscanf(fp_in, "%lf", &reaction_rate[3]);
fscanf(fp_in, "%lf", &reaction_rate[4]);
fscanf(fp_in, "%lf", &reaction_rate[5]);
fscanf(fp_in, "%lf", &reaction_rate[6]);
fscanf(fp_in, "%lf", &reaction_rate[7]);
fscanf(fp_in, "%lf", &reaction_rate[8]);
fscanf(fp_in, "%lf", &reaction_rate[9]);
fscanf(fp_in, "%lf", &reaction_rate[10]);
fscanf(fp_in, "%lf", &reaction_rate[11]);
fscanf(fp_in, "%lf", &reaction_rate[12]);
fscanf(fp_in, "%lf", &reaction_rate[13]);
fscanf(fp_in, "%lf", &reaction_rate[14]);
fscanf(fp_in, "%lf", &reaction_rate[15]);
fscanf(fp_in, "%lf", &reaction_rate[16]);
fscanf(fp_in, "%lf", &reaction_rate[17]);
fscanf(fp_in, "%lf", &reaction_rate[18]);
fscanf(fp_in, "%lf", &reaction_rate[19]);
fscanf(fp_in, "%lf", &reaction_rate[20]);
fscanf(fp_in, "%lf", &reaction_rate[21]);
fscanf(fp_in, "%lf", &reaction_rate[22]);
fscanf(fp_in, "%lf", &reaction_rate[23]);
fscanf(fp_in, "%lf", &reaction_rate[24]);
fscanf(fp_in, "%lf", &reaction_rate[25]);
fscanf(fp_in, "%lf", &reaction_rate[26]);
fscanf(fp_in, "%lf", &reaction_rate[27]);
fscanf(fp_in, "%lf", &reaction_rate[28]);
fscanf(fp_in, "%lf", &reaction_rate[29]);
fscanf(fp_in, "%lf", &reaction_rate[30]);
fscanf(fp_in, "%lf", &reaction_rate[31]);
fscanf(fp_in, "%lf", &reaction_rate[32]);
fscanf(fp_in, "%lf", &reaction_rate[33]);
fscanf(fp_in, "%lf", &reaction_rate[34]);
fscanf(fp_in, "%lf", &reaction_rate[35]);
fscanf(fp_in, "%lf", &reaction_rate[36]);
fscanf(fp_in, "%lf", &reaction_rate[37]);
fscanf(fp_in, "%lf", &reaction_rate[38]);
fscanf(fp_in, "%lf", &reaction_rate[39]);
fscanf(fp_in, "%lf", &reaction_rate[40]);
fscanf(fp_in, "%lf", &reaction_rate[41]);
fscanf(fp_in, "%lf", &reaction_rate[42]);
fscanf(fp_in, "%lf", &reaction_rate[43]);
fscanf(fp_in, "%lf", &reaction_rate[44]);
fscanf(fp_in, "%lf", &reaction_rate[45]);
fscanf(fp_in, "%lf", &reaction_rate[46]);
fscanf(fp_in, "%lf", &reaction_rate[47]);
fscanf(fp_in, "%lf", &reaction_rate[48]);
fscanf(fp_in, "%lf", &reaction_rate[49]);
fscanf(fp_in, "%lf", &reaction_rate[50]);
fscanf(fp_in, "%lf", &reaction_rate[51]);
fscanf(fp_in, "%lf", &reaction_rate[52]);
fscanf(fp_in, "%lf", &reaction_rate[53]);
fscanf(fp_in, "%lf", &reaction_rate[54]);
fscanf(fp_in, "%lf", &reaction_rate[55]);
fscanf(fp_in, "%lf", &reaction_rate[56]);
fscanf(fp_in, "%lf", &reaction_rate[57]);
fscanf(fp_in, "%lf", &reaction_rate[58]);
fscanf(fp_in, "%lf", &reaction_rate[59]);
fscanf(fp_in, "%lf", &reaction_rate[60]);
fscanf(fp_in, "%lf", &reaction_rate[61]);
fscanf(fp_in, "%lf", &reaction_rate[62]);
fscanf(fp_in, "%lf", &reaction_rate[63]);
fscanf(fp_in, "%lf", &reaction_rate[64]);
fscanf(fp_in, "%lf", &reaction_rate[65]);
fscanf(fp_in, "%lf", &reaction_rate[66]);
fscanf(fp_in, "%lf", &reaction_rate[67]);
fscanf(fp_in, "%lf", &reaction_rate[68]);
fscanf(fp_in, "%lf", &reaction_rate[69]);
fscanf(fp_in, "%lf", &reaction_rate[70]);
fscanf(fp_in, "%lf", &reaction_rate[71]);
fscanf(fp_in, "%lf", &reaction_rate[72]);
fscanf(fp_in, "%lf", &reaction_rate[73]);
fscanf(fp_in, "%lf", &reaction_rate[74]);
fscanf(fp_in, "%lf", &reaction_rate[75]);
fscanf(fp_in, "%lf", &reaction_rate[76]);
fscanf(fp_in, "%lf", &reaction_rate[77]);

fclose(fp_in);



if (strcmp(r->name, "ATR1") == 0) {
*rr = reaction_rate[0];
}
else if (strcmp(r->name, "ATR2") == 0) {
*rr = reaction_rate[1];
}
else if (strcmp(r->name, "ATR3") == 0) {
*rr = reaction_rate[2];
}
else if (strcmp(r->name, "ATR4") == 0) {
*rr = reaction_rate[3];
}
else if (strcmp(r->name, "ATR5") == 0) {
*rr = reaction_rate[4];
}
else if (strcmp(r->name, "ATR6") == 0) {
*rr = reaction_rate[5];
}
else if (strcmp(r->name, "ATR7") == 0) {
*rr = reaction_rate[6];
}
else if (strcmp(r->name, "ATR8") == 0) {
*rr = reaction_rate[7];
}
else if (strcmp(r->name, "ATR9") == 0) {
*rr = reaction_rate[8];
}
else if (strcmp(r->name, "ATR10") == 0) {
*rr = reaction_rate[9];
}
else if (strcmp(r->name, "ATR11") == 0) {
*rr = reaction_rate[10];
}
else if (strcmp(r->name, "ATR12") == 0) {
*rr = reaction_rate[11];
}
else if (strcmp(r->name, "ATR13") == 0) {
*rr = reaction_rate[12];
}
else if (strcmp(r->name, "ATR14") == 0) {
*rr = reaction_rate[13];
}
else if (strcmp(r->name, "ATR15") == 0) {
*rr = reaction_rate[14];
}
else if (strcmp(r->name, "ATR16") == 0) {
*rr = reaction_rate[15];
}
else if (strcmp(r->name, "ATR17") == 0) {
*rr = reaction_rate[16];
}
else if (strcmp(r->name, "ATR18") == 0) {
*rr = reaction_rate[17];
}
else if (strcmp(r->name, "ATR19") == 0) {
*rr = reaction_rate[18];
}
else if (strcmp(r->name, "ATR20") == 0) {
*rr = reaction_rate[19];
}
else if (strcmp(r->name, "ATR21") == 0) {
*rr = reaction_rate[20];
}
else if (strcmp(r->name, "ATR22") == 0) {
*rr = reaction_rate[21];
}
else if (strcmp(r->name, "ATR23") == 0) {
*rr = reaction_rate[22];
}
else if (strcmp(r->name, "ATR24") == 0) {
*rr = reaction_rate[23];
}
else if (strcmp(r->name, "ATR25") == 0) {
*rr = reaction_rate[24];
}
else if (strcmp(r->name, "ATR26") == 0) {
*rr = reaction_rate[25];
}
else if (strcmp(r->name, "ATR27") == 0) {
*rr = reaction_rate[26];
}
else if (strcmp(r->name, "ATR28") == 0) {
*rr = reaction_rate[27];
}
else if (strcmp(r->name, "ATR29") == 0) {
*rr = reaction_rate[28];
}
else if (strcmp(r->name, "ATR30") == 0) {
*rr = reaction_rate[29];
}
else if (strcmp(r->name, "ATR31") == 0) {
*rr = reaction_rate[30];
}
else if (strcmp(r->name, "ATR32") == 0) {
*rr = reaction_rate[31];
}
else if (strcmp(r->name, "ATR33") == 0) {
*rr = reaction_rate[32];
}
else if (strcmp(r->name, "ATR34") == 0) {
*rr = reaction_rate[33];
}
else if (strcmp(r->name, "ATR35") == 0) {
*rr = reaction_rate[34];
}
else if (strcmp(r->name, "ATR36") == 0) {
*rr = reaction_rate[35];
}
else if (strcmp(r->name, "AIR1") == 0) {
*rr = reaction_rate[36];
}
else if (strcmp(r->name, "AIR2") == 0) {
*rr = reaction_rate[37];
}
else if (strcmp(r->name, "AIR3") == 0) {
*rr = reaction_rate[38];
}
else if (strcmp(r->name, "AIR4") == 0) {
*rr = reaction_rate[39];
}
else if (strcmp(r->name, "AIR5") == 0) {
*rr = reaction_rate[40];
}
else if (strcmp(r->name, "AIR6") == 0) {
*rr = reaction_rate[41];
}
else if (strcmp(r->name, "AIR7") == 0) {
*rr = reaction_rate[42];
}
else if (strcmp(r->name, "AIR8") == 0) {
*rr = reaction_rate[43];
}
else if (strcmp(r->name, "AIR9") == 0) {
*rr = reaction_rate[44];
}
else if (strcmp(r->name, "AIR10") == 0) {
*rr = reaction_rate[45];
}
else if (strcmp(r->name, "AIR11") == 0) {
*rr = reaction_rate[46];
}
else if (strcmp(r->name, "AIR12") == 0) {
*rr = reaction_rate[47];
}
else if (strcmp(r->name, "AIR13") == 0) {
*rr = reaction_rate[48];
}
else if (strcmp(r->name, "AIR14") == 0) {
*rr = reaction_rate[49];
}
else if (strcmp(r->name, "AIR15") == 0) {
*rr = reaction_rate[50];
}
else if (strcmp(r->name, "AIR16") == 0) {
*rr = reaction_rate[51];
}
else if (strcmp(r->name, "AIR17") == 0) {
*rr = reaction_rate[52];
}
else if (strcmp(r->name, "AIR18") == 0) {
*rr = reaction_rate[53];
}
else if (strcmp(r->name, "AIR19") == 0) {
*rr = reaction_rate[54];
}
else if (strcmp(r->name, "AIR20") == 0) {
*rr = reaction_rate[55];
}
else if (strcmp(r->name, "AIR21") == 0) {
*rr = reaction_rate[56];
}
else if (strcmp(r->name, "AIR22") == 0) {
*rr = reaction_rate[57];
}
else if (strcmp(r->name, "AIR23") == 0) {
*rr = reaction_rate[58];
}
else if (strcmp(r->name, "AIR24") == 0) {
*rr = reaction_rate[59];
}
else if (strcmp(r->name, "AIR25") == 0) {
*rr = reaction_rate[60];
}
else if (strcmp(r->name, "AIR26") == 0) {
*rr = reaction_rate[61];
}
else if (strcmp(r->name, "AIR27") == 0) {
*rr = reaction_rate[62];
}
else if (strcmp(r->name, "AIR28") == 0) {
*rr = reaction_rate[63];
}
else if (strcmp(r->name, "AIR29") == 0) {
*rr = reaction_rate[64];
}
else if (strcmp(r->name, "AIR30") == 0) {
*rr = reaction_rate[65];
}
else if (strcmp(r->name, "AIR31") == 0) {
*rr = reaction_rate[66];
}
else if (strcmp(r->name, "AIR32") == 0) {
*rr = reaction_rate[67];
}
else if (strcmp(r->name, "AIR33") == 0) {
*rr = reaction_rate[68];
}
else if (strcmp(r->name, "AIR34") == 0) {
*rr = reaction_rate[69];
}
else if (strcmp(r->name, "AIR35") == 0) {
*rr = reaction_rate[70];
}
else if (strcmp(r->name, "AIR36") == 0) {
*rr = reaction_rate[71];
}
else if (strcmp(r->name, "AIR37") == 0) {
*rr = reaction_rate[72];
}
else if (strcmp(r->name, "AIR38") == 0) {
*rr = reaction_rate[73];
}
else if (strcmp(r->name, "AIR39") == 0) {
*rr = reaction_rate[74];
}
else if (strcmp(r->name, "AIR40") == 0) {
*rr = reaction_rate[75];
}
else if (strcmp(r->name, "AIR41") == 0) {
*rr = reaction_rate[76];
}
else if (strcmp(r->name, "AIR42") == 0) {
*rr = reaction_rate[77];
}
else {
*rr = 0;
}
}
Here is my python code
Code:
from tkinter import END
from IPython import get_ipython
import numpy as np
import cantera as ct
# import os
# import sys
# import time

# get_ipython().magic('reset -sf')

def calculate_reaction_rate_ATR(temperature, pressure, concentration):
gas = ct.Solution('methane_pox_on_pt.yaml','gas')
surf = ct.Interface('methane_pox_on_pt.yaml', 'Pt_surf',[gas])

surf.TP = temperature, pressure
# gas = surf.adjacent['gas']
gas.TPY = temperature, pressure, concentration

surf.advance_coverages(0.01)
# Get reaction rates
reaction_rates = surf.net_rates_of_progress
# def write_reaction_rates_to_file(reaction_rates, filename):
# with open(filename, 'w') as file:
# for rate in reaction_rates:
# file.write(str(rate) + '\n')
return reaction_rates


def calculate_reaction_rate_AIR(temperature, pressure, concentration):
gas = ct.Solution('sm_Ni.yaml','gas')
surf = ct.Interface('sm_Ni.yaml', 'surf',[gas])

surf.TP = temperature, pressure
# gas = surf.adjacent['gas']
gas.TPY = temperature, pressure, concentration

surf.advance_coverages(0.01)
# Get reaction rates
reaction_rates = surf.net_rates_of_progress
return reaction_rates

def calculate_reaction_rate_Other(temperature, pressure, concentration):
reaction_rates = [0]
return reaction_rates

with open("fluent_data.txt", "r") as f:
f_list = [float(i) for line in f for i in line.split(',') if i.strip()]
species_concentrations = f_list[0:7]
temperature = f_list[7]
pressure = f_list[8]
f.close()

test_array = [0,0,0,0,0,0,0]
if species_concentrations != test_array:
reaction_rate_ATR = calculate_reaction_rate_ATR(temperature, pressure, species_concentrations)
reaction_rate_AIR = calculate_reaction_rate_AIR(temperature, pressure, species_concentrations)
else:
reaction_rate_ATR = [0.5] * 36
reaction_rate_AIR = [0.5] * 42

with open("reaction_rate.txt", "w+") as f:
for rr in reaction_rate_ATR:
f.write(str(rr) + "\n")
for rr in reaction_rate_AIR:
f.write(str(rr) + "\n")
f.close()
When I compile the udf, the error is below:
PermissionError: [Errno 13] Permission denied: 'reaction_rate.txt'.
yifan998 is offline   Reply With Quote

Reply

Tags
python script, surface reaction udf, udf and programming


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
UDF Error: Error code 126 cilliant FLUENT 0 June 30, 2023 07:52
[swak4Foam] swak4foam openfoam 7 installation problem Andrea23 OpenFOAM Community Contributions 1 February 17, 2020 19:11
Compiling dynamicTopoFvMesh for OpenFOAM 2.1.x Saxwax OpenFOAM Installation 25 November 29, 2013 06:34
Ansys Fluent 13.0 UDF compilation problem in Window XP (32 bit) Yogini Fluent UDF and Scheme Programming 7 October 3, 2012 08:24
Installation OF1.5-dev ttdtud OpenFOAM Installation 46 May 5, 2009 03:32


All times are GMT -4. The time now is 14:04.