Ejemplo n.º 1
0
def gotran2cuda(filename, params):
    """
    Create a CUDA file from a gotran model
    """

    # Load Gotran model
    ode = load_ode(filename)

    # Create a C code generator
    gen = CUDACodeGenerator(params)

    output = params.output

    if output:
        if not output.endswith(".cu"):
            output += ".cu"
    else:
        output = filename.replace(".ode", "") + ".cu"

    info("")
    info(f"Generating CUDA code for the {ode.name} ode...")
    code = gen.module_code(ode)
    info("  done.")
    with open(output, "w") as f:
        if params.system_headers:
            f.write("#include <math.h>\n")
            f.write("#include <string.h>\n")

        f.write(code)

    if params.list_timings:
        list_timings()
Ejemplo n.º 2
0
def gotran2py(filename, params):
    """
    Create a c header file from a gotran model
    """
    timer = Timer(f"Generate Python code from {filename}")

    # Load Gotran model
    ode = load_ode(filename)

    # Collect monitored
    if params.functions.monitored.generate:
        monitored = [
            expr.name for expr in ode.intermediates + ode.state_expressions
        ]
    else:
        monitored = None

    # Create a Python code generator
    gen = PythonCodeGenerator(
        params,
        ns=params.namespace,
        import_inside_functions=params.import_inside_functions,
    )

    output = params.output

    if output:
        if not output.endswith(".py"):
            output += ".py"
    else:
        output = filename.replace(".ode", "") + ".py"

    info("")
    info(f"Generating Python code for the {ode.name} ode...")
    if params.class_code:
        code = gen.class_code(ode, monitored=monitored)
    else:
        code = gen.module_code(ode, monitored=monitored)
    info("  done.")
    with open(output, "w") as f:
        f.write(code)

    del timer

    if params.list_timings:
        list_timings()
Ejemplo n.º 3
0
def gotran2cpp(filename, params):
    """
    Create a C++ header file from a gotran model
    """

    timer = Timer(f"Generate C++ code from {filename}")

    # Load Gotran model
    ode = load_ode(filename)

    # Create a C code generator
    gen = CppCodeGenerator(params)

    output = params.output

    if output:
        if not (output.endswith(".cpp") or output.endswith(".h")):
            output += ".h"
    else:
        output = filename.replace(".ode", "") + ".h"

    info("")
    info(f"Generating C++ code for the {ode.name} ode...")
    if params.class_code:
        code = gen.class_code(ode)
    else:
        code = gen.module_code(ode)
    info("  done.")

    with open(output, "w") as f:
        if params.system_headers:
            f.write("#include <cmath>\n")
            f.write("#include <cstring>\n")
            f.write("#include <stdexcept>\n")

        f.write(code)

    del timer

    if params.list_timings:
        list_timings()
Ejemplo n.º 4
0
def gotran2julia(filename, params):
    """
    Create a c header file from a gotran model
    """
    timer = Timer(f"Generate Julia code from {filename}")

    # Load Gotran model
    ode = load_ode(filename)

    # Collect monitored
    if params.functions.monitored.generate:
        monitored = [
            expr.name for expr in ode.intermediates + ode.state_expressions
        ]
    else:
        monitored = None

    # Create a C code generator
    gen = JuliaCodeGenerator(params)

    output = params.output

    if output:
        if not output.endswith(".jl"):
            output += ".jl"
    else:
        output = filename.replace(".ode", "") + ".jl"

    info("")
    info(f"Generating Julia code for the {ode.name} ode...")
    code = gen.module_code(ode, monitored=monitored)
    info("  done.")
    with open(output, "w") as f:
        f.write(code)

    del timer

    if params.list_timings:
        list_timings()
Ejemplo n.º 5
0
# parser = CellMLParser(model, params=params)
# open(parser.name+".ode", "w").write(parser.to_gotran())
# ode = load_ode(parser.name+".ode")

for f in glob.glob("*.cellml"):
    # print
    # print f
    params = CellMLParser.default_parameters()
    parser = CellMLParser(f, params=params)
    open(parser.name + ".ode", "w").write(parser.to_gotran())
    try:
        ode = load_ode(parser.name + ".ode")
    except Exception as e:
        print("***Error: Could not load gotran model", parser.name, e)
        print()
    list_timings()
    clear_timings()
    print()

mathmlparser = parser.mathmlparser
parsed_components = parser.components
cellml = parser.cellml

cellml_namespace = cellml.tag.split("}")[0] + "}"

# Grab encapsulation grouping
encapsulations = {}


def get_encapsulation(elements):