Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memorize the C string representation. #495

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 33 additions & 19 deletions fipy/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,26 @@ def _getCIndexString(self, shape):
else:
return '[i + j * ni + k * ni * nj]'

def _getCvalue(self):
"""
Generate the value to be used in inline.
"""
v = self.value

if type(v) not in (type(numerix.array(1)),):
varray = numerix.array(v)
else:
varray = v

if len(varray.shape) == 0:
if varray.dtype in (numerix.array(1).dtype,):
return int(varray)
elif varray.dtype in (numerix.array(1.).dtype,):
return float(varray)
else:
return varray
else:
return varray

def _getCstring(self, argDict={}, id="", freshen=None):
"""
Expand Down Expand Up @@ -423,23 +443,7 @@ def _getCstring(self, argDict={}, id="", freshen=None):
"""

identifier = 'var%s' % (id)

v = self.value

if type(v) not in (type(numerix.array(1)),):
varray = numerix.array(v)
else:
varray = v

if len(varray.shape) == 0:
if varray.dtype in (numerix.array(1).dtype,):
argDict[identifier] = int(varray)
elif varray.dtype in (numerix.array(1.).dtype,):
argDict[identifier] = float(varray)
else:
argDict[identifier] = varray
else:
argDict[identifier] = varray
argDict[identifier] = self

try:
shape = self.opShape
Expand Down Expand Up @@ -892,8 +896,18 @@ def _execInline(self, comment=None):
"""

from fipy.tools import inline
argDict = {}
string = self._getCstring(argDict=argDict, freshen=True) + ';'
try:
argDict = self._c_argDict
string = self._c_string
except AttributeError:
argDict = {}
string = self._getCstring(argDict=argDict, freshen=True) + ';'
self._c_argDict = dict(argDict)
self._c_string = string

# _getCstring only returns variables,
# we need the values for the C code
argDict = {k: v._getCvalue() for k,v in argDict.iteritems()}

try:
shape = self.opShape
Expand Down