Exemplo n.º 1
0
def _instantiate_element_and_dofmap(module, prefix):
    "Instantiate objects of the jit-compiled finite_element and dofmap."
    import dijitso
    fe_classname = make_classname(prefix, "finite_element", "main")
    dm_classname = make_classname(prefix, "dofmap", "main")
    fe = dijitso.extract_factory_function(module, "create_" + fe_classname)()
    dm = dijitso.extract_factory_function(module, "create_" + dm_classname)()
    return (fe, dm)
Exemplo n.º 2
0
def compile_class(cpp_data, mpi_comm=MPI.comm_world):
    """Compile a user C(++) string or set of statements to a Python object

    cpp_data is a dict containing:
      "name": must be "expression" or "subdomain"
      "statements": must be a string, or list/tuple of strings
      "properties": a dict of float properties
      "jit_generate": callable (generates cpp code with this dict as input)

    """

    # Set compiler/build options
    params = dijitso.params.default_params()
    params['build']['include_dirs'] = dolfin_pc["include_dirs"]
    params['build']['libs'] = dolfin_pc["libraries"]
    params['build']['lib_dirs'] = dolfin_pc["library_dirs"]

    name = cpp_data['name']
    if name not in ('subdomain', 'expression'):
        raise ValueError("DOLFIN JIT only for SubDomain and Expression")
    statements = cpp_data['statements']
    properties = cpp_data['properties']

    if not isinstance(statements, (str, tuple, list)):
        raise RuntimeError("Expression must be a string, or a list or tuple of strings")

    # Flatten tuple of tuples (2D array) and get value_shape
    statement_array = numpy.array(statements)
    cpp_data['statements'] = tuple(statement_array.flatten())
    cpp_data['value_shape'] = statement_array.shape

    # Make a string representing the properties (and distinguish float/GenericFunction)
    # by adding '*' for GenericFunction
    property_str = ''
    for k, v in properties.items():
        property_str += str(k)
        if hasattr(v, '_cpp_object') and isinstance(v._cpp_object, cpp.function.GenericFunction):
            property_str += '*'

    hash_str = str(statements) + str(property_str) + cpp.__version__
    module_hash = hashlib.md5(hash_str.encode('utf-8')).hexdigest()
    module_name = "dolfin_" + name + "_" + module_hash

    try:
        module, signature = dijitso_jit(cpp_data, module_name, params,
                                        generate=cpp_data['jit_generate'],
                                        mpi_comm=mpi_comm)
        submodule = dijitso.extract_factory_function(module, "create_" + module_name)()
    except Exception:
        raise RuntimeError("Unable to compile C++ code with dijitso")

    if name == 'expression':
        python_object = cpp.function.make_dolfin_expression(submodule)
    else:
        python_object = cpp.mesh.make_dolfin_subdomain(submodule)

    # Set properties to initial values
    # FIXME: maybe remove from here (do it in Expression and SubDomain instead)
    for k, v in properties.items():
        python_object.set_property(k, v)

    return python_object
Exemplo n.º 3
0
def compile_class(cpp_data):
    """Compile a user C(++) string or set of statements to a Python object

    cpp_data is a dict containing:
      "name": must be "expression"
      "statements": must be a string, or list/tuple of strings
      "properties": a dict of float properties
      "jit_generate": callable (generates cpp code with this dict as input)

    """

    # Set compiler/build options
    params = dijitso.params.default_params()
    params['build']['include_dirs'] = dolfin_pc["include_dirs"]
    params['build']['libs'] = dolfin_pc["libraries"]
    params['build']['lib_dirs'] = dolfin_pc["library_dirs"]

    name = cpp_data['name']
    if name not in ('expression'):
        raise ValueError("DOLFIN JIT only for Expression")
    statements = cpp_data['statements']
    properties = cpp_data['properties']

    if not isinstance(statements, (str, tuple, list)):
        raise RuntimeError(
            "Expression must be a string, or a list or tuple of strings")

    # Flatten tuple of tuples (2D array) and get value_shape
    statement_array = numpy.array(statements)
    cpp_data['statements'] = tuple(statement_array.flatten())
    cpp_data['value_shape'] = statement_array.shape

    # Make a string representing the properties (and distinguish float/GenericFunction)
    # by adding '*' for GenericFunction
    property_str = ''
    for k, v in properties.items():
        property_str += str(k)
        if hasattr(v, '_cpp_object') and isinstance(
                v._cpp_object, cpp.function.GenericFunction):
            property_str += '*'

    hash_str = str(statements) + str(property_str) + cpp.__version__
    module_hash = hashlib.md5(hash_str.encode('utf-8')).hexdigest()
    module_name = "dolfin_" + name + "_" + module_hash

    try:
        module, signature = dijitso_jit(cpp_data,
                                        module_name,
                                        params,
                                        generate=cpp_data['jit_generate'])
        submodule = dijitso.extract_factory_function(module,
                                                     "create_" + module_name)()
    except Exception:
        raise RuntimeError("Unable to compile C++ code with dijitso")

    python_object = cpp.function.make_dolfin_expression(submodule)

    # Set properties to initial values
    # FIXME: maybe remove from here (do it in Expression instead)
    for k, v in properties.items():
        python_object.set_property(k, v)

    return python_object
Exemplo n.º 4
0
def _instantiate_coordinate_mapping(module, prefix):
    "Instantiate an object of the jit-compiled coordinate_mapping."
    import dijitso
    classname = make_classname(prefix, "coordinate_mapping", "main")
    form = dijitso.extract_factory_function(module, "create_" + classname)()
    return form