Ejemplo n.º 1
0
def setup(argv=None,
          do_features=True,
          do_logging=True,
          do_welcome=None,
          log_to_console_only=None,
          warn_about_py_ext=True):
    """Carry out the various stages of the setup of Nsim.

    do_cmdline: the command line is parsed using the OptionParser object stored
    inside cmdline_parser variable. If it has not been set by the user, then
    it is automatically generated in this function.
    """

    # If the setup has been marked as completed then exit immediately
    if task_done['completed']:
        return

    # Check once for all what really needs to be done
    do_features = do_features and not task_done['features']
    do_cmdline = (argv != None) and not task_done['cmdline']
    do_logging = do_logging and not task_done['logging']
    do_welcome = (do_welcome == True
                  or (do_welcome == None and not task_done['welcome']))

    # We first parse the command line
    (options, arguments) = (None, None)
    if do_cmdline:
        global cmdline_parser
        if cmdline_parser == None:
            cmdline_parser = generate_cmdline_parser()
        (options, arguments) = cmdline_parser.parse_args(argv)
        is_interactive = (len(arguments) == 0)

        # Deal here with some of the command line args
        if options.forcelog == None:
            log_to_console_only = is_interactive

        elif options.forcelog == True:
            log_to_console_only = False

        else:
            log_to_console_only = True

        task_done['cmdline'] = True

    # We would like now to setup the ocaml and python feature objects.
    # First, however, we need to determine the name of the logging file, since
    # it is required in order to construct correctly the pyfeature object.
    savedir = '.'
    runid = get_nmag_runid(arguments)
    if options and options.logfilename:
        logfilename = options.logfilename
        log_to_console_only = False
    else:
        logfilename = runid + '_log.log'
    logfilepath = os.path.join(savedir, logfilename)

    # We now find out where we should read the configuration for logging
    logconfigfile = None
    if options != None and options.logconfigfile != None:
        logconfigfile = options.logconfigfile

    # We can now construct the feature objects
    global pyfeatures, ocamlfeatures
    if do_features:
        pyfeatures = \
          nsim.features.Features(defaults={'logfilepath':logfilepath})
        ocamlfeatures = nsim.features.OcamlFeatures()

        # We now determine the exact name of the config file which we should
        # load in order to setup the logger
        nmaglibrarypath = nsim.snippets.get_absolute_librarypath(__file__)[0]

        if logconfigfile == None:
            # The user has not provided his own configuration file for logging
            # we then have to use one of the default files.
            if log_to_console_only:
                logconfigfile = "logging-console.conf"

            else:
                logconfigfile = "logging.conf"

            # We need to prepend the path where this files can be found
            # (which doesn't make sense if the user has provided his own file)
            logconfigfile = os.path.join(nmaglibrarypath, logconfigfile)

        else:
            # The option log_to_console_only does not make sense in this case.
            # We may 'assert log_to_console_only == None', but we don't.
            # We just ignore the issue, this is the best thing to do!
            pass

        # We finally fill the pyfeatures object with default values (to be
        # overridden by command line options)
        ocamlconfigfile = os.path.join(nmaglibrarypath, 'ocaml.conf')
        pyfeatures.set('nmag', 'ocamlconfigfile', ocamlconfigfile)
        pyfeatures.set('nmag', 'logconfigfile', logconfigfile)
        pyfeatures.set('nmag', 'loglevel', 'info')
        pyfeatures.set('etc', 'runid', runid)
        pyfeatures.set('etc', 'mainprogram', 'nmag')
        pyfeatures.set('etc', 'savedir', savedir)  # Location of output files

        # Move any settings from pyfeatures to OCaml's features living in
        # 'snippets'
        pyfeatures.to_ocaml_features()
        task_done['features'] = True

    # Interpret the command line arguments and override settings in pyfeatures
    if do_cmdline:
        cmdline_to_pyfeatures(options, arguments)

        # We rename the files here for two reasons:
        # - we have to do it before the log file is created
        #   (before the 'do_logging' section)
        # - we have to do it after the command line has been parsed
        #   (and only if the command line has been parsed?), after the config
        #   file has been read and we hence know what is the name of the log
        #   file.
        if pyfeatures.get('nmag', 'clean', raw=True):
            nsim.snippets.rename_old_files([logfilename])

    # We are now ready to setup the logger
    global log
    log = logging.getLogger('nsim')

    if do_logging:
        # Setup logging from default logging configuration file
        nsim.logtools.setup_loggers(logconfigfile, logfilepath)

        # Last, we need to set the global log level
        nsim.logtools.set_global_loglevel(pyfeatures.get('nmag', 'loglevel'))

        # All loggers are set up.
        setup_ocaml_loggers()

        log.debug('current logging status is \n%s' %
                  nsim.logtools.logging_status_str())
        task_done['logging'] = True

    # Just a warning message about the extension of the file
    if warn_about_py_ext and do_cmdline and len(argv) > 0:
        _, script_extension = os.path.splitext(os.path.split(argv[0])[1])
        if script_extension.lower() != ".py":
            msg = ("Nmag scripts need to have the .py extension. Will wait "
                   "2 seconds before continuing...")
            log.warn(msg)
            nsim.snippets.funky_wait(2)

    if do_cmdline:
        setup_things(options, arguments)

    if do_welcome:
        import ocaml
        log.debug("Sundials library path ='%s'" %
                  ocaml.get_nsim_sundials_library_path())

        nr_cpus = ocaml.petsc_mpi_nr_nodes()
        log.info("Nsim %s" % get_version_string())
        log.info("Runid is '%s'" % (pyfeatures.get('etc', 'runid')))
        log.info("Using %d CPUs" % (nr_cpus))

        if nr_cpus > 1:
            log.info("Waiting 1 seconds for messages from slaves to arrive "
                     "(experimental)")
            ocaml.mpi_hello()
            time.sleep(1)

        task_done['welcome'] = True

    return (options, arguments)
Ejemplo n.º 2
0
#raw_mesh=ocaml.mesh_readfile("periodic/periodic.nmesh",False)
#mesh=nmesh.load_ascii("periodic/periodic.nmesh")
#mesh=nmesh.load_ascii("periodic/periodic2.nmesh")
#mesh=nmesh.load_ascii("periodic/simple2d.nmesh")
mesh=nmesh.load_ascii("periodic/simple2d-split.nmesh")
raw_mesh=mesh.raw_mesh

print "MESH: ",raw_mesh
sys.stdout.flush()

if ocaml.petsc_is_mpi():
    print "*** PARALLEL EXECUTION *** (not yet supported here!)"
    sys.exit(0) # DDD add parallel support!

    nr_nodes=ocaml.petsc_mpi_nr_nodes()
    nr_points=ocaml.mesh_nr_points(raw_mesh)
    z=nr_points/nr_nodes
    distrib = [int(round(z*(i+1)))-int(round(z*i)) for i in range(0,nr_nodes)]
    slack=nr_points-reduce(lambda x,y:x+y,distrib)
    distrib[0] = distrib[0] + slack
    print "*** RAW MESH %s *** DISTRIB %s ***" %(repr(raw_mesh),repr(distrib))
    ocaml.mesh_set_vertex_distribution(raw_mesh,distrib)

elem_V=ocaml.make_element("V",[3],2,1) # vector
elem_S=ocaml.make_element("S",[],2,1) # scalar element

mwe_V=ocaml.make_mwe("V",raw_mesh,[(0,ocaml.empty_element),(1,elem_V)],[]) 
mwe_S=ocaml.make_mwe("S",raw_mesh,[(0,ocaml.empty_element),(1,elem_S)],[])

Ejemplo n.º 3
0
def setup(argv=None, do_features=True, do_logging=True,
          do_welcome=None, log_to_console_only=None,
          warn_about_py_ext=True):
    """Carry out the various stages of the setup of Nsim.

    do_cmdline: the command line is parsed using the OptionParser object stored
    inside cmdline_parser variable. If it has not been set by the user, then
    it is automatically generated in this function.
    """

    # If the setup has been marked as completed then exit immediately
    if task_done['completed']:
        return

    # Check once for all what really needs to be done
    do_features = do_features and not task_done['features']
    do_cmdline =  (argv != None) and not task_done['cmdline']
    do_logging =  do_logging and not task_done['logging']
    do_welcome = (do_welcome == True
                  or (do_welcome == None and not task_done['welcome']))

    # We first parse the command line
    (options, arguments) = (None, None)
    if do_cmdline:
        global cmdline_parser
        if cmdline_parser == None:
            cmdline_parser = generate_cmdline_parser()
        (options, arguments) = cmdline_parser.parse_args(argv)
        is_interactive = (len(arguments) == 0)

        # Deal here with some of the command line args
        if options.forcelog == None:
            log_to_console_only = is_interactive

        elif options.forcelog == True:
            log_to_console_only = False

        else:
            log_to_console_only = True

        task_done['cmdline'] = True

    # We would like now to setup the ocaml and python feature objects.
    # First, however, we need to determine the name of the logging file, since
    # it is required in order to construct correctly the pyfeature object.
    savedir = '.'
    runid = get_nmag_runid(arguments)
    if options and options.logfilename:
        logfilename = options.logfilename
        log_to_console_only = False
    else:
        logfilename = runid + '_log.log'
    logfilepath = os.path.join(savedir, logfilename)

    # We now find out where we should read the configuration for logging
    logconfigfile = None
    if options != None and options.logconfigfile != None:
        logconfigfile = options.logconfigfile

    # We can now construct the feature objects
    global pyfeatures, ocamlfeatures
    if do_features:
        pyfeatures = \
          nsim.features.Features(defaults={'logfilepath':logfilepath})
        ocamlfeatures = nsim.features.OcamlFeatures()

        # We now determine the exact name of the config file which we should
        # load in order to setup the logger
        nmaglibrarypath = nsim.snippets.get_absolute_librarypath(__file__)[0]

        if logconfigfile == None:
            # The user has not provided his own configuration file for logging
            # we then have to use one of the default files.
            if log_to_console_only:
                logconfigfile = "logging-console.conf"

            else:
                logconfigfile = "logging.conf"

            # We need to prepend the path where this files can be found
            # (which doesn't make sense if the user has provided his own file)
            logconfigfile = os.path.join(nmaglibrarypath, logconfigfile)

        else:
            # The option log_to_console_only does not make sense in this case.
            # We may 'assert log_to_console_only == None', but we don't.
            # We just ignore the issue, this is the best thing to do!
            pass


        # We finally fill the pyfeatures object with default values (to be
        # overridden by command line options)
        ocamlconfigfile = os.path.join(nmaglibrarypath, 'ocaml.conf')
        pyfeatures.set('nmag', 'ocamlconfigfile', ocamlconfigfile)
        pyfeatures.set('nmag', 'logconfigfile', logconfigfile)
        pyfeatures.set('nmag', 'loglevel', 'info')
        pyfeatures.set('etc', 'runid', runid)
        pyfeatures.set('etc', 'mainprogram', 'nmag')
        pyfeatures.set('etc', 'savedir', savedir)   # Location of output files

        # Move any settings from pyfeatures to OCaml's features living in
        # 'snippets'
        pyfeatures.to_ocaml_features()
        task_done['features'] = True

    # Interpret the command line arguments and override settings in pyfeatures
    if do_cmdline:
        cmdline_to_pyfeatures(options, arguments)

        # We rename the files here for two reasons:
        # - we have to do it before the log file is created
        #   (before the 'do_logging' section)
        # - we have to do it after the command line has been parsed
        #   (and only if the command line has been parsed?), after the config
        #   file has been read and we hence know what is the name of the log
        #   file.
        if pyfeatures.get('nmag', 'clean', raw=True):
            nsim.snippets.rename_old_files([logfilename])

    # We are now ready to setup the logger
    global log
    log = logging.getLogger('nsim')

    if do_logging:
        # Setup logging from default logging configuration file
        nsim.logtools.setup_loggers(logconfigfile, logfilepath)

        # Last, we need to set the global log level
        nsim.logtools.set_global_loglevel(pyfeatures.get('nmag', 'loglevel'))

        # All loggers are set up.
        setup_ocaml_loggers()

        log.debug('current logging status is \n%s'
                  % nsim.logtools.logging_status_str())
        task_done['logging'] = True

    # Just a warning message about the extension of the file
    if warn_about_py_ext and do_cmdline and len(argv) > 0:
        _, script_extension = os.path.splitext(os.path.split(argv[0])[1])
        if script_extension.lower() != ".py":
            msg = ("Nmag scripts need to have the .py extension. Will wait "
                   "2 seconds before continuing...")
            log.warn(msg)
            nsim.snippets.funky_wait(2)

    if do_cmdline:
        setup_things(options, arguments)

    if do_welcome:
        import ocaml
        log.debug("Sundials library path ='%s'"
                  % ocaml.get_nsim_sundials_library_path())

        nr_cpus = ocaml.petsc_mpi_nr_nodes()
        log.info("Nsim %s" % get_version_string())
        log.info("Runid is '%s'" % (pyfeatures.get('etc', 'runid')))
        log.info("Using %d CPUs" % (nr_cpus))

        if nr_cpus > 1:
            log.info("Waiting 1 seconds for messages from slaves to arrive "
                     "(experimental)")
            ocaml.mpi_hello()
            time.sleep(1)

        task_done['welcome'] = True

    return (options, arguments)