Difference between revisions of "Team:EPF Lausanne/Project/Modelling/Code"
Line 20: | Line 20: | ||
max-height:50%; | max-height:50%; | ||
} | } | ||
− | + | .highlight > pre { background-color: grey;} | |
.highlight .hll { background-color: #49483e } | .highlight .hll { background-color: #49483e } | ||
.highlight { background: #272822; color: #f8f8f2 } | .highlight { background: #272822; color: #f8f8f2 } |
Revision as of 10:44, 30 July 2015
Code
ODE Generator
class Reaction:
def __init__(self, reaction_, cst_name_, cst_value_, description_):
self.reaction = reaction_
self.cst_name = cst_name_
self.cst_value = cst_value_
self.description = description_
def __str__(self):
return "{}\n{} = {}\n{}".format(self.reaction,
self.cst_name,
str(self.cst_value),
self.description)
class ReactionParser:
def __init__(self, input_, sep_=":", comment_="#"):
self.sep = sep_
self.comment = comment_
self.file = open(input_, 'r') # TODO: OPEN SAFELY
self.reactions = []
self._load()
def __del__(self):
self.file.close()
def _load(self):
for line in self.file:
if line.strip() == "\n" or line.strip() == "": # Check for empty lines
continue
if len(line.strip()) != 0 and line.strip()[0] == self.comment: # Check for comments
continue
splitted = line.split(":")
if len(splitted) != 4:
raise ValueError('ReactionParser: problem with the input file (not enough arguments)')
reaction = Reaction(splitted[0].strip(),
splitted[1].strip(),
float(splitted[2].strip()),
splitted[3].strip())
self.reactions.append(reaction)
def print_reactions(self):
for reaction in self.reactions:
print(reaction)
print()
def reaction_table(self):
kin = []
for reaction in self.reactions:
splitted = reaction.reaction.split("->")
left = splitted[0].strip()
right = splitted[-1].strip()
kin.append( ([left, right], (reaction.cst_name, reaction.description, reaction.cst_value) ) )
return kin
if __name__ == "__main__":
myParser = ReactionParser("reactions.dat")
#myParser.load()
myParser.print_reactions()
kin = myParser.reaction_table()
print(kin)