def fenicsxml2pool(xmlfile, pool=None): """Return a Pool tree from an XML file with FEniCS parameters.""" if pool is None: from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main menu') # else: add menus wherever we are in a pool tree import xml.etree.ElementTree as ET tree = ET.parse(xmlfile) root = tree.getroot() def iterate(element): if element.tag == 'parameters': # New subpool of parameters pool.subpool(element.attrib['name']) elif element.tag == 'parameter': # Add data item value = element.attrib['value'] if value == '': value = 'emptystring' # just a magic code, the value gets transformed back to '' in set_fenics_prm widget = 'textline' if element.attrib['type'] == 'double': str2type = float value = str2type(value) widget = 'float' elif element.attrib['type'] == 'int': str2type = int value = str2type(value) widget = 'integer' elif element.attrib['type'] == 'string': str2type = str value = str2type(value) elif element.attrib['type'] == 'bool': value = value.capitalize() # True/False, not true/false # Short call to make sure bools are correctly handled pool.add_data_item( name=element.attrib['key'], default=value, widget='checkbox') else: raise ValueError('Not impl element.attrib["type"]=%s' % element.attrib['type']) if element.attrib['type'] != 'bool': pool.add_data_item( name=element.attrib['key'], default=value, str2type=str2type, widget=widget) for child in element: iterate(child) if element.tag == 'parameters' and \ pool.get_current_subpool().name != 'Main menu': pool.change_subpool('..') iterate(root) return pool
def fenicsxml2pool(xmlfile, pool=None): """Return a Pool tree from an XML file with FEniCS parameters.""" if pool is None: from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main menu') # else: add menus wherever we are in a pool tree import xml.etree.ElementTree as ET tree = ET.parse(xmlfile) root = tree.getroot() def iterate(element): if element.tag == 'parameters': # New subpool of parameters pool.subpool(element.attrib['name']) elif element.tag == 'parameter': # Add data item value = element.attrib['value'] if value == '': value = 'emptystring' # just a magic code, the value gets transformed back to '' in set_fenics_prm widget = 'textline' if element.attrib['type'] == 'double': str2type = float value = str2type(value) widget = 'float' elif element.attrib['type'] == 'int': str2type = int value = str2type(value) widget = 'integer' elif element.attrib['type'] == 'string': str2type = str value = str2type(value) elif element.attrib['type'] == 'bool': value = value.capitalize() # True/False, not true/false # Short call to make sure bools are correctly handled pool.add_data_item(name=element.attrib['key'], default=value, widget='checkbox') else: raise ValueError('Not impl element.attrib["type"]=%s' % element.attrib['type']) if element.attrib['type'] != 'bool': pool.add_data_item(name=element.attrib['key'], default=value, str2type=str2type, widget=widget) for child in element: iterate(child) if element.tag == 'parameters' and \ pool.get_current_subpool().name != 'Main menu': pool.change_subpool('..') iterate(root) return pool
def listtree2Pool(pool_tree): """ Transform pool_tree, a nested list of strings and dicts, to Pool representation. Example:: >>> pool_tree = [ 'main', [ dict(name='print intermediate results', default=False), dict(name='U', default=120, unit='km/h', help='velocity of body', str2type=eval), 'fluid properties', [ dict(name='rho', default=1.2, unit='kg/m**3', help='density'), dict(name='mu', default=2E-5, help='viscosity'), ], 'body properties', [ dict(name='m', default=0.43, unit='kg', help='mass'), dict(name='V', default=pi*0.11**3, help='volume'), dict(name='A', default=pi*0.11**2, unit='m**2', help='cross section area'), dict(name='d', default=2*0.11, unit='m', help='diameter'), dict(name='C_D', default=0.2, minmax=[0,1], help='drag coefficient'), ], ], ] """ def make_data_item( pool_path, level, data_item, pool): pool.add_data_item(**data_item) def make_subpool( pool_path, level, subpool, pool): path = TreePath(pool_path).to_str() pool.subpool(path) from parampool.tree import list_tree pool = Pool() list_tree.traverse_list_tree( pool_tree, callback_leaf=make_data_item, callback_subtree_start=make_subpool, user_data=pool) pool.update() return pool
def test_CommandLineOptions(): import parampool.pool.Pool as Pool pool = Pool.make_test_pool_drag() clo = CommandLineOptions(pool) clargs = '--/main/U 1.2 --rho 2.6 --mu 5.5E-5 '\ '--/main/body_properties/d 0.2 --C_D 0.7'.split() clo.set_values(clargs) print dump(pool)
def listtree2Pool(pool_tree): """ Transform pool_tree, a nested list of strings and dicts, to Pool representation. Example:: >>> pool_tree = [ 'main', [ dict(name='print intermediate results', default=False), dict(name='U', default=120, unit='km/h', help='velocity of body', str2type=eval), 'fluid properties', [ dict(name='rho', default=1.2, unit='kg/m**3', help='density'), dict(name='mu', default=2E-5, help='viscosity'), ], 'body properties', [ dict(name='m', default=0.43, unit='kg', help='mass'), dict(name='V', default=pi*0.11**3, help='volume'), dict(name='A', default=pi*0.11**2, unit='m**2', help='cross section area'), dict(name='d', default=2*0.11, unit='m', help='diameter'), dict(name='C_D', default=0.2, minmax=[0,1], help='drag coefficient'), ], ], ] """ def make_data_item(pool_path, level, data_item, pool): pool.add_data_item(**data_item) def make_subpool(pool_path, level, subpool, pool): path = TreePath(pool_path).to_str() pool.subpool(path) from parampool.tree import list_tree pool = Pool() list_tree.traverse_list_tree(pool_tree, callback_leaf=make_data_item, callback_subtree_start=make_subpool, user_data=pool) pool.update() return pool
def pool_def_api(): from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main pool') pool.add_data_item(name='Expression to be integrated', default='sin(x)', widget='textline') pool.add_data_item(name='a', default=0.0, symbol='a') pool.add_data_item(name='b', default=1.0, symbol='b') return pool
def load_from_file(filename): """ Create a new Pool object from a file definition. A typical definition of a data item is:: name1 = value ! unit # help widget=... Example:: subpool Main pool rho = 1.2 ! kg/m**3 # Density. widget=float subpool Numerical parameters dt = 0.1 ! s # Time step (None: automatically set, otherwise float value). widget=textline end end """ pool = Pool() return read_poolfile(filename, pool, task='create')
def define_pool(): from parampool.utils import fenicsxml2pool from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main menu') pool.add_data_item(name='element degree', default=1, str2type=int) pool.add_data_item(name='Nx', default=10, str2type=int) pool.add_data_item(name='Ny', default=10, str2type=int) pool.add_data_item(name='f', default='-6.0', str2type=str) pool.add_data_item(name='u0', default='1 + x[0]*x[0] + 2*x[1]*x[1]', str2type=str) # Subpool with built-in FEniCS parameters pool = fenicsxml2pool('prm.xml', pool) pool.update() return pool
def pool_definition_api_with_separate_subpools(): """ Create and return a pool by calling up other functions for defining the subpools. Also demonstrate customization of pool properties and inserting default values from file or the command line. """ from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main pool') pool = motion_pool(pool) pool.change_subpool('..') pool = body_and_envir_pool(pool) pool.change_subpool('..') pool = numerics_pool(pool) pool.change_subpool('..') pool = plot_pool(pool) pool.update() # finalize pool construction from parampool.pool.UI import ( set_data_item_attribute, set_defaults_from_file, set_defaults_from_command_line, set_defaults_in_model_file, write_poolfile, ) # Change default values in the web GUI import parampool.pool.DataItem parampool.pool.DataItem.DataItem.defaults['minmax'] = [0, 100] parampool.pool.DataItem.DataItem.defaults['range_steps'] = 500 # Can also change 'number_step' for the step in float fields # and 'widget_size' for the width of widgets # Let all widget sizes be 6, except for Time step pool = set_data_item_attribute(pool, 'widget_size', 6) pool.get('Time step').data['widget_size'] = 4 pool = set_defaults_from_file(pool, command_line_option='--poolfile') pool = set_defaults_from_command_line(pool) flask_modelfile = 'model.py' print 'XXX', os.getcwd() django_modelfile = os.path.join('motion_and_forces_with_pool', 'app', 'models.py') if os.path.isfile(flask_modelfile): set_defaults_in_model_file(flask_modelfile, pool) elif os.path.isfile(django_modelfile): set_defaults_in_model_file(django_modelfile, pool) poolfile = open('pool.dat', 'w') poolfile.write(write_poolfile(pool)) poolfile.close() return pool
def pool_def_api(): from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main pool') pool.add_data_item(name='Filename', widget='file') return pool
def pool_definition_api_with_separate_subpools(): """ Create and return a pool by calling up other functions for defining the subpools. Also demonstrate customization of pool properties and inserting default values from file or the command line. """ from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main pool') pool = motion_pool(pool) pool.change_subpool('..') pool = body_and_envir_pool(pool) pool.change_subpool('..') pool = numerics_pool(pool) pool.change_subpool('..') pool = plot_pool(pool) pool.update() # finalize pool construction from parampool.pool.UI import set_data_item_attribute # Change default values in the web GUI import parampool.pool.DataItem parampool.pool.DataItem.DataItem.defaults['minmax'] = [0, 100] parampool.pool.DataItem.DataItem.defaults['range_steps'] = 500 # Can also change 'number_step' for the step in float fields # and 'widget_size' for the width of widgets # Let all widget sizes be 6, except for Time step pool = set_data_item_attribute(pool, 'widget_size', 6) pool.get('Time step').data['widget_size'] = 4 # Example on editing hardcoded defaults in the model files # (not necessary, but a possible technique along with # setting defaults in the pool, in a file, or on the command line) from parampool.pool.UI import set_defaults_in_model_file flask_modelfile = 'model.py' django_modelfile = os.path.join('motion_and_forces_with_pool', 'app', 'models.py') if os.path.isfile(flask_modelfile): set_defaults_in_model_file(flask_modelfile, pool) elif os.path.isfile(django_modelfile): set_defaults_in_model_file(django_modelfile, pool) return pool
def pool_definition_api(): """Create and return pool using the parampool.pool API.""" from parampool.pool.Pool import Pool pool = Pool() # Go to a subpool, but create it if it does not exist pool.subpool('Main pool') pool.subpool('Initial motion data') # Define data items for the current subpool pool.add_data_item(name='Initial velocity', default=5.0) pool.add_data_item(name='Initial angle', default=45, widget='range', minmax=[0, 90]) pool.add_data_item(name='Spinrate', default=50, widget='float', unit='1/s') # Move to (and create) another subpool, as in a file tree pool.subpool('../Body and environment data') # Add data items for the current subpool pool.add_data_item(name='Wind velocity', default=0.0, help='Wind velocity in positive x direction.', minmax=[-50, 50], number_step=0.5, widget='float', str2type=float) pool.add_data_item(name='Mass', default=0.1, unit='kg', validate=lambda data_item, value: value > 0) pool.add_data_item(name='Radius', default=0.11, unit='m') pool.subpool('../Numerical parameters') pool.add_data_item(name='Method', default='RK4', widget='select', options=['RK4', 'RK2', 'ForwardEuler'], help='Numerical solution method.') pool.add_data_item(name='Time step', default=None, widget='textline', unit='s', str2type=convert_time_step) pool.subpool('../Plot parameters') pool.add_data_item(name='Plot simplified motion', default=True) pool.add_data_item(name='New plot', default=True) pool.update() return pool
def pool_definition_api_with_separate_subpools(): """ Create and return a pool by calling up other functions for defining the subpools. Also demonstrate customization of pool properties and inserting default values from file or the command line. """ from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main pool') pool = motion_pool(pool) pool.change_subpool('..') pool = body_and_envir_pool(pool) pool.change_subpool('..') pool = numerics_pool(pool) pool.change_subpool('..') pool = plot_pool(pool) pool.update() # finalize pool construction from parampool.pool.UI import set_data_item_attribute # Change default values in the web GUI import parampool.pool.DataItem parampool.pool.DataItem.DataItem.defaults['minmax'] = [0, 100] parampool.pool.DataItem.DataItem.defaults['range_steps'] = 500 # Can also change 'number_step' for the step in float fields # and 'widget_size' for the width of widgets # Let all widget sizes be 6, except for Time step pool = set_data_item_attribute(pool, 'widget_size', 6) pool.get('Time step').data['widget_size'] = 4 # Example on editing hardcoded defaults in the model files # (not necessary, but a possible technique along with # setting defaults in the pool, in a file, or on the command line) from parampool.pool.UI import set_defaults_in_model_file flask_modelfile = 'model.py' django_modelfile = os.path.join( 'motion_and_forces_with_pool', 'app', 'models.py') if os.path.isfile(flask_modelfile): set_defaults_in_model_file(flask_modelfile, pool) elif os.path.isfile(django_modelfile): set_defaults_in_model_file(django_modelfile, pool) return pool
def pool_definition_api(): """Create and return pool using the parampool.pool API.""" from parampool.pool.Pool import Pool pool = Pool() # Go to a subpool, but create it if it does not exist pool.subpool('Main pool') pool.subpool('Initial motion data') # Define data items for the current subpool pool.add_data_item( name='Initial velocity', default=5.0) pool.add_data_item( name='Initial angle', default=45, widget='range', minmax=[0,90]) pool.add_data_item( name='Spinrate', default=50, widget='float', unit='1/s') # Move to (and create) another subpool, as in a file tree pool.subpool('../Body and environment data') # Add data items for the current subpool pool.add_data_item( name='Wind velocity', default=0.0, help='Wind velocity in positive x direction.', minmax=[-50, 50], number_step=0.5, widget='float', str2type=float) pool.add_data_item( name='Mass', default=0.1, unit='kg', validate=lambda data_item, value: value > 0, help='Mass of body.') pool.add_data_item( name='Radius', default=0.11, unit='m', help='Radius of spherical body.') pool.subpool('../Numerical parameters') pool.add_data_item( name='Method', default='RK4', widget='select', options=['RK4', 'RK2', 'ForwardEuler'], help='Numerical solution method.') pool.add_data_item( name='Time step', default=None, widget='textline', unit='s', str2type=convert_time_step) pool.subpool('../Plot parameters') pool.add_data_item( name='Plot simplified motion', default=True, help='Plot motion without drag+lift forces.') pool.add_data_item( name='New plot', default=True, help='Erase all old curves.') pool.update() return pool
def pool_definition_api(): from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main pool') pool.subpool('Initial motion data') pool.add_data_item( name='Initial velocity', default=5.0) pool.add_data_item( name='Initial angle', default=45, widget='range', minmax=[0,90], unit='deg') pool.add_data_item( name='Spinrate', default=50, widget='float', str2type=float, unit='1/s') pool.subpool('../Body and environment data') pool.add_data_item( name='Wind velocity', default=0.0, help='Wind velocity in positive x direction.') pool.add_data_item( name='Mass', default=0.1, help='Mass of body.', unit='kg') pool.add_data_item( name='Radius', default=0.11, help='Radius of spherical body.', unit='m') pool.subpool('../Numerical parameters') pool.add_data_item( name='Method', default='RK4', widget='select', options=['RK4', 'RK2', 'ForwardEuler'], help='Numerical solution method.') pool.add_data_item( name='Time step', default=None, widget='float', unit='s') pool.subpool('../Plot parameters') pool.add_data_item( name='Plot simplified motion', default=True, help='Plot motion without drag and lift forces.') pool.add_data_item( name='New plot', default=True, help='Erase all old curves.') pool.update() print pool return pool
def pool_definition_api_with_separate_subpools(): from parampool.pool.Pool import Pool pool = Pool() pool.subpool('Main pool') pool = motion_pool(pool) pool.change_subpool('..') pool = body_and_envir_pool(pool) pool.change_subpool('..') pool = numerics_pool(pool) pool.change_subpool('..') pool = plot_pool(pool) pool.update() print pool return pool