|
|
f4e088e |
--- moose-core-3.1.5/python/moose/utils.py.orig 2021-08-16 19:05:38.269473061 -0600
|
|
|
f4e088e |
+++ moose-core-3.1.5/python/moose/utils.py 2021-08-16 19:08:56.592968592 -0600
|
|
|
f4e088e |
@@ -14,9 +14,7 @@
|
|
|
f4e088e |
__license__ = "GPLv3"
|
|
|
f4e088e |
|
|
|
f4e088e |
import types
|
|
|
f4e088e |
-import parser
|
|
|
f4e088e |
import token
|
|
|
f4e088e |
-import symbol
|
|
|
f4e088e |
import os
|
|
|
f4e088e |
import math
|
|
|
f4e088e |
import warnings
|
|
|
f4e088e |
@@ -205,44 +204,6 @@
|
|
|
f4e088e |
raise TypeError('Second argument must be a string specifying a field to assign to when third argument is a value')
|
|
|
f4e088e |
|
|
|
f4e088e |
|
|
|
f4e088e |
-def tweak_field(moose_wildcard, field, assignment_string):
|
|
|
f4e088e |
- """Tweak a specified field of all objects that match the
|
|
|
f4e088e |
- moose_wildcard using assignment string. All identifiers in
|
|
|
f4e088e |
- assignment string must be fields of the target object.
|
|
|
f4e088e |
-
|
|
|
f4e088e |
- Example:
|
|
|
f4e088e |
-
|
|
|
f4e088e |
- tweak_field('/mycell/##[Class=Compartment]', 'Rm', '1.5 / (3.1416 * diameter * length')
|
|
|
f4e088e |
-
|
|
|
f4e088e |
- will assign Rm to every compartment in mycell such that the
|
|
|
f4e088e |
- specific membrane resistance is 1.5 Ohm-m2.
|
|
|
f4e088e |
- """
|
|
|
f4e088e |
- if not isinstance(moose_wildcard, str):
|
|
|
f4e088e |
- raise TypeError('moose_wildcard must be a string.')
|
|
|
f4e088e |
- id_list = moose.getWildcardList(moose_wildcard, True)
|
|
|
f4e088e |
- expression = parser.expr(assignment_string)
|
|
|
f4e088e |
- expr_list = expression.tolist()
|
|
|
f4e088e |
- # This is a hack: I just tried out some possible syntax trees and
|
|
|
f4e088e |
- # hand coded the replacement such that any identifier is replaced
|
|
|
f4e088e |
- # by moose_obj.identifier
|
|
|
f4e088e |
- def replace_fields_with_value(x):
|
|
|
f4e088e |
- if len(x)>1:
|
|
|
f4e088e |
- if x[0] == symbol.power and x[1][0] == symbol.atom and x[1][1][0] == token.NAME:
|
|
|
f4e088e |
- field = x[1][1][1]
|
|
|
f4e088e |
- x[1] = [symbol.atom, [token.NAME, 'moose_obj']]
|
|
|
f4e088e |
- x.append([symbol.trailer, [token.DOT, '.'], [token.NAME, field]])
|
|
|
f4e088e |
- for item in x:
|
|
|
f4e088e |
- if isinstance(item, list):
|
|
|
f4e088e |
- replace_fields_with_value(item)
|
|
|
f4e088e |
- return x
|
|
|
f4e088e |
- tmp = replace_fields_with_value(expr_list)
|
|
|
f4e088e |
- new_expr = parser.sequence2st(tmp)
|
|
|
f4e088e |
- code = new_expr.compile()
|
|
|
f4e088e |
- for moose_id in id_list:
|
|
|
f4e088e |
- moose_obj = eval('moose.%s(moose_id)' % (moose.Neutral(moose_id).className))
|
|
|
f4e088e |
- value = eval(code)
|
|
|
f4e088e |
- moose.setField(moose_id, field, str(value))
|
|
|
f4e088e |
-
|
|
|
f4e088e |
# 2012-01-11 19:20:39 (+0530) Subha: checked for compatibility with dh_branch
|
|
|
f4e088e |
def printtree(root, vchar='|', hchar='__', vcount=1, depth=0, prefix='', is_last=False):
|
|
|
f4e088e |
"""Pretty-print a MOOSE tree.
|