3"""Copyright (c) 2005-2016, University of Oxford.
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
10This file is part of Chaste.
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions
in binary form must reproduce the above copyright notice,
17 this list of conditions
and the following disclaimer
in the documentation
18 and/
or other materials provided
with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse
or promote products derived
from this
21 software without specific prior written permission.
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36This part of PyCml deals
with converting CellML models into programming language code.
37It
is a thin executable wrapper around translators.py.
40import os #importing functions related to operating system
41import sys #importing functions related to system (e.g. path directory etc)
43# Make sure PyCml is on sys.path
44pycml_path = os.path.dirname(os.path.realpath(__file__))
45sys.path[0:0] = [pycml_path]
47import translators #importing the main translator class
48from translators import CellMLTranslator #again, just to make sure
49import CellMLToNektarTranslator #importing the nektar sub-class
51#This part actually runs the code
53if __name__ == '__main__': #this part of the code is only run when CellMLToNektar.py is run directly, not imported
54 CellMLTranslator.register(CellMLTranslator, 'C++')
56 if '--profile' in sys.argv:
58 profile_name =
'/tmp/pycml-profile-%f-%d' % (time.time(), os.getpid())
59 cProfile.run(
'modified_translators.run()', profile_name)
66 def euler(doc, t, nsteps=1000, dt=0.01):
67 global tvar, state_vars, exprs
69 state_vars = t.state_vars
70 for var
in state_vars:
71 var.set_value(float(var.initial_value))
73 exprs = [e
for e
in doc.model.get_assignments()
74 if isinstance(e, modified_translators.mathml_apply)]
75 for _
in range(nsteps):
78 tvar.set_value(tvar.get_value() + dt)
79 for var
in state_vars:
80 var.set_value(var.get_value() +
81 dt * var.get_value(ode=tvar))
86 st = modified_translators.open_output_stream(outfn)
87 doc.xml(indent=1, stream=st)
92 for comp
in doc.model.component:
93 for var
in comp.variable:
94 print var.fullname(), var._cml_usage_count
99 Several models have equations of a form that may give rise to
100 a divide by zero error on simulation, especially when lookup
101 tables are used. The general form is:
103 (a * (V - v0)) / (exp(b * (V - v0)) - 1)
105 When V = v0 this
is undefined, however the limit of the
106 function
as V approaches v0
from either side
is well-defined,
107 and each limit
is the same. We approximate the limit by
108 linear interpolation between values of the expression
for
111 divides = [d.xml_parent
112 for d
in doc.xml_xpath(
u'//m:apply/m:divide')]
113 for divide
in divides:
def euler(doc, t, nsteps=1000, dt=0.01)
def writefile(doc, outfn='test.cml')
def fix_divide_by_zero(doc)