Question sur un exemple de script en python
Fermé
khampoulaye
Messages postés
4
Date d'inscription
lundi 18 mars 2019
Statut
Membre
Dernière intervention
18 mars 2019
-
18 mars 2019 à 15:17
khampoulaye Messages postés 4 Date d'inscription lundi 18 mars 2019 Statut Membre Dernière intervention 18 mars 2019 - 18 mars 2019 à 16:55
khampoulaye Messages postés 4 Date d'inscription lundi 18 mars 2019 Statut Membre Dernière intervention 18 mars 2019 - 18 mars 2019 à 16:55
A voir également:
- Question sur un exemple de script en python
- Script vidéo youtube - Guide
- Url exemple - Guide
- Exemple de mot de passe sécurisé - Guide
- Planning en 12h exemple - Télécharger - Outils professionnels
- Sommaire word exemple - Guide
1 réponse
Chris 94
Messages postés
50978
Date d'inscription
mardi 8 janvier 2008
Statut
Modérateur
Dernière intervention
17 février 2023
7 325
18 mars 2019 à 15:32
18 mars 2019 à 15:32
18 mars 2019 à 15:38
s0 = [[],[]]
f=open('compute_biochem.in')
for line in f.readlines():
if line[0]!='#':
if ":" in line:
li=line.split(":")
if li[0].split()[0] == 'SPECIES':
lis=li[1].split(';')
s0[0].append(lis[0].split()[0])
s0[1].append(lis[1].split('\n')[0].split()[0])
elif li[0].split()[0] == 'XMLFILE':
INPUTFILE = li[1].split('\n')[0].split()[0]
f.close()
print('Biochemistry read from ',INPUTFILE)
print(' ')
#
# Compute source from chemistry
#
def source_biochem(concentration,time, reac_rate, index_in, index_out):
number_of_species = len(concentration)
number_of_reactions = len(reac_rate)
source = [0 for s in range(number_of_species)]
for r in range(number_of_reactions):
ind_in = index_in[r]
ind_out = index_out[r]
for spec in ind_in:
source[spec] = source[spec] - reac_rate[r] * reduce(mul,[concentration[s] for s in ind_in])
for spec in ind_out:
source[spec] = source[spec] + reac_rate[r] * reduce(mul,[concentration[s] for s in ind_in])
return source
#
# Read an .xml file for biochemistry
#
tree = etree.parse(INPUTFILE)
#
# Read the species from .xml and check consistency with initial conditions in compute_biochem.in
#
for spec in tree.xpath("species"):
rr=spec.findall("name")
spec_name = [rr[n].text for n in range(len(rr))]
number_of_species = len(spec_name)
#
# Check if the list of species is correct
#
if spec_name != sorted(s0[0]):
print('Species list not valid')
print('From .xml file: ',spec_name)
print('From .in file: ',sorted(s0[0]))
quit()
#
# if list of species is correct, reorder the initial concentrations and generate y0
# y0 contains the initial conditions of the species in the same order as in spec_name
#
s0t=[[x[i] for x in s0] for i in range(len(s0[0]))]
s0tsorted=sorted(s0t,key=lambda col: col[0])
s0sorted=[[x[i] for x in s0tsorted] for i in range(len(s0tsorted[0]))]
y0 = [float(data) for data in s0sorted[1]]
#
# Read the reactions and deduce species indices for computing the source terms
#
for reac in tree.xpath("reactions"):
rr=reac.findall("reaction")
reac_name = [rr[n].text for n in range(len(rr))]
rr=reac.findall("reaction_rate")
reac_rate = [rr[n].text for n in range(len(rr))]
number_of_reactions = len(reac_name)
specs_in=[[] for r in range(number_of_reactions)]
specs_out=[[] for r in range(number_of_reactions)]
index_in=[[] for r in range(number_of_reactions)]
index_out=[[] for r in range(number_of_reactions)]
for reac in reac_name:
all_in = reac[:reac.index("--")].split("+")
specs_in[reac_name.index(reac)] = [spec.split() for spec in all_in][0]
index_in[reac_name.index(reac)] = [spec_name.index(spec.split()[0]) for spec in all_in]
all_out = reac[reac.index("--")+2:].split("+")
specs_out[reac_name.index(reac)] = [spec.split() for spec in all_out][0]
index_out[reac_name.index(reac)] = [spec_name.index(spec.split()[0]) for spec in all_out]
fichier = open("reactions2.in", "r")
line=fichier.readline()
while line:
print(line)
print(("reac_rate"))
line=fichier.readline()
fichier.close()
#
# Print case
#
print("Species :")
print(spec_name)
print(" ")
print('Initial conditions: ',y0)
print(" ")
print("Reactions :")
print(reac_name)
print(" ")
print("Reactions rates:")
print(reac_rate)
print(" ")
print("Indices in / out:")
print(index_in)
print(index_out)
j'ai voulu ouvrir un fichier "reactions2.in" pour que le programme puisse aller là bas car c'est là bas où se trouvent les fichiers.in et xml des reactions biochimiques. Du coup le programme me dit :"I/O operation on closed file"
18 mars 2019 à 15:58
Par exemple si c'est du basic :
Ou, si c'est du html :
18 mars 2019 à 16:45
Merci.
18 mars 2019 à 16:52
Ceci dit... Je ne peux pas faire grand chose d'autre pour toi. Si un spécialiste ne passe pas d'ici quelques heures, relance le sujet (si je suis dans le coin, je le verrais et je pourrais essayer de te trouver un spécialiste).
18 mars 2019 à 16:55
Merci pour les conseils, je tâcherais de relancer le sujet s'il n'y a pas de spécialistes d'ici quelques heures.