Commit b6a1fb75 authored by John Tourtellott's avatar John Tourtellott

Merge branch 'auto-generate-functions' into 'master'

Automatically generate AdH functions when needed

In many places, AdH requires function to be specified even when the
value is constant. Previously, users had to manually generate functions
in their attributes for this. Now they can enter constant values in
ModelBuilder, and the export logic will create functions ad hoc.
Also minor template updates.

See merge request !10
parents e5eff6d4 314152f7
......@@ -16,8 +16,7 @@
<ItemDefinitions>
<String Name="AnalysisTypes" Label="Analysis Types" AdvanceLevel="99" Version="0"
Extensible="true" NumberOfRequiredValues="0"/>
<MeshEntity Name="mesh" Label="Mesh" Version="0" NumberOfRequiredValues="1"
Optional="true" IsEnabledByDefault="false" />
<MeshEntity Name="mesh" Label="Mesh" Version="0" NumberOfRequiredValues="1" />
<Directory Name="OutputDirectory" Label="Output Directory" Version="1" NumberOfRequiredValues="1" />
<String Name="FileBase" Label="FileName Base" Version="1" NumberOfRequiredValues="1">
<BriefDescription>Common base name for output files (.2dm, .bc)</BriefDescription>
......
......@@ -261,6 +261,9 @@ def init_scope(spec):
# Define function dict only, initialized adhoc
scope.function_dict = dict()
# Second dictionary for constant values that must be specified as AdH function
scope.constant_function_dict = dict()
return scope
......@@ -554,6 +557,18 @@ def get_function_id(scope, exp_att):
return fcn_id
# ---------------------------------------------------------------------
def get_constant_function_id(scope, value):
# Check if function already created for this value
fcn_id = scope.constant_function_dict.get(value)
if fcn_id is None:
fcn_id = len(scope.function_dict) + 1
scope.constant_function_dict[value] = fcn_id
# Add placeholder in main function_dict
scope.function_dict[value] = fcn_id
#print 'Assign function id %d to value %s' % (fcn_id, value)
return fcn_id
# ---------------------------------------------------------------------
def write_functions(scope):
'''
......@@ -590,7 +605,7 @@ def write_functions(scope):
if fcn_id is None:
continue
scope.output.write('! function -- %s\n' % name)
scope.output.write('! function: %s\n' % name)
# Check if this is the TimeStepSize expression
# Compare ids since they are wrapped objects
......@@ -608,6 +623,29 @@ def write_functions(scope):
for i in range(num_vals):
scope.output.write('%f %f\n' % (x_item.value(i), val_item.value(i)))
# Write constant functions
if scope.constant_function_dict:
# Get the end time value and units
group_item = smtk.attribute.to_concrete(time_att.find('EndTime'))
value_item = smtk.attribute.to_concrete(group_item.find('Value'))
end_time = value_item.value(0)
units_item = smtk.attribute.to_concrete(group_item.find('Units'))
end_units = units_item.value(0)
#print 'end time', end_time, 'units', end_units
# Use 0 for start time, since it might be specified in different units
start_time = 0.0
# Write functions
for value,fcn_id in scope.constant_function_dict.items():
val_string = ('%f' % value).rstrip('0').rstrip('.')
scope.output.write('! constant-valued function: %s\n' % val_string)
scope.output.write('XY1 %d 2 %d %d\n' % \
(fcn_id, end_units, end_units))
# Use time = 0 for first row, since start time might be in different units
scope.output.write('%f %f\n' % (start_time, value))
scope.output.write('%f %f\n' % (end_time, value))
# ---------------------------------------------------------------------
def write_MTS_cards(scope):
......@@ -806,6 +844,12 @@ def get_values_as_strings(scope, item):
exp_att = item.expression(i)
exp_id = get_function_id(scope, exp_att)
output_list.append('%d' % exp_id)
elif item.type() == smtk.attribute.Item.DOUBLE and \
item.allowsExpressions():
# Create expression for constant-valued item
exp_id = get_constant_function_id(scope, item.value(i))
output_list.append('%d' % exp_id)
elif hasattr(item, 'isDiscrete') and item.isDiscrete():
index = item.discreteIndex(i)
output_list.append('%d' % index)
......
......@@ -633,7 +633,7 @@
<RangeInfo>
<Min Inclusive="false">0.0</Min>
</RangeInfo>
<DefaultValue>0.0</DefaultValue>
<DefaultValue>1.0</DefaultValue>
<Categories>
<Cat>General</Cat>
</Categories>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment