Ejemplo n.º 1
0
def test_cooling_pj():
    print("Job directory: " + jobdir)
    print("Temporary directory: " + tmpdir)

    # ---- CAMPAIGN INITIALISATION ---
    print("Initializing Campaign")
    # Set up a fresh campaign called "cooling"
    my_campaign = uq.Campaign(name='cooling', work_dir=tmpdir)

    # Define parameter space
    params = {
        "temp_init": {
            "type": "float",
            "min": 0.0,
            "max": 100.0,
            "default": 95.0
        },
        "kappa": {
            "type": "float",
            "min": 0.0,
            "max": 0.1,
            "default": 0.025
        },
        "t_env": {
            "type": "float",
            "min": 0.0,
            "max": 40.0,
            "default": 15.0
        },
        "out_file": {
            "type": "string",
            "default": "output.csv"
        }
    }

    output_filename = params["out_file"]["default"]
    output_columns = ["te", "ti"]

    # Create an encoder, decoder and collation element
    encoder = uq.encoders.GenericEncoder(template_fname=COOLING_APP_DIR +
                                         '/cooling.template',
                                         delimiter='$',
                                         target_filename=ENCODED_FILENAME)

    decoder = uq.decoders.SimpleCSV(target_filename=output_filename,
                                    output_columns=output_columns,
                                    header=0)

    collater = uq.collate.AggregateSamples(average=False)

    # Add the PCE app (automatically set as current app)
    my_campaign.add_app(name="cooling",
                        params=params,
                        encoder=encoder,
                        decoder=decoder,
                        collater=collater)

    vary = {"kappa": cp.Uniform(0.025, 0.075), "t_env": cp.Uniform(15, 25)}

    # Create the sampler
    if uqmethod == 'pce':
        my_sampler = uq.sampling.PCESampler(vary=vary, polynomial_order=2)
    if uqmethod == 'qmc':
        my_sampler = uq.sampling.QMCSampler(vary=vary, n_samples=10)

    # Associate the sampler with the campaign
    my_campaign.set_sampler(my_sampler)

    print("Generating samples")
    # Will draw all (of the finite set of) samples
    my_campaign.draw_samples()

    print("Initialising EasyPJ Executor")
    # Create EasyVVUQ-QCGPJ Executor that will process the execution
    qcgpjexec = Executor()

    # Create QCG PJ-Manager with 4 cores (if you want to use all available resources remove the resources parameter)
    # Refer to the documentation for customisation options.
    qcgpjexec.create_manager(dir=my_campaign.campaign_dir, resources='4')

    # Define ENCODING task that will be used for execution of encodings using encoders specified by EasyVVUQ.
    # The presented specification of 'TaskRequirements' assumes the execution of each of the tasks on 1 core.
    qcgpjexec.add_task(
        Task(TaskType.ENCODING, TaskRequirements(cores=Resources(exact=1))))

    # Define EXECUTION task that will be used for the actual execution of application.
    # The presented specification of 'TaskRequirements' assumes the execution of each of the tasks on 1 core,
    # but for more advanced parallel applications the resource requirements may be extended to use
    # many cores or even many resources.
    # Each task will execute the command provided in the 'application' parameter.
    qcgpjexec.add_task(
        Task(TaskType.EXECUTION,
             TaskRequirements(cores=Resources(exact=1)),
             application='python3 ' + APPLICATION + " " + ENCODED_FILENAME))

    print("Starting the execution of QCG Pilot Job tasks")
    # Execute encodings and executions for all generated samples
    qcgpjexec.run(campaign=my_campaign, submit_order=SubmitOrder.RUN_ORIENTED)

    # Terminate QCG PJ-Manager
    print("Completing the execution")
    qcgpjexec.terminate_manager()

    print("Collating results")
    my_campaign.collate()

    print("Making analysis")

    if uqmethod == 'pce':
        analysis = uq.analysis.PCEAnalysis(sampler=my_sampler,
                                           qoi_cols=output_columns)
    if uqmethod == 'qmc':
        analysis = uq.analysis.QMCAnalysis(sampler=my_sampler,
                                           qoi_cols=output_columns)

    my_campaign.apply_analysis(analysis)

    results = my_campaign.get_last_analysis()
    stats = results['statistical_moments']['te']
    per = results['percentiles']['te']
    dist_out = results['output_distributions']['te']

    print("Stats: ")
    print(stats)
    print("Percentiles: ")
    print(per)
    print("Output distribution: ")
    print(dist_out)

    print("Processing completed")
    return stats, per, dist_out
Ejemplo n.º 2
0
def test_cooling_pj():
    print("Job directory: " + jobdir)
    print("Temporary directory: " + tmpdir)

    # ---- CAMPAIGN INITIALISATION ---
    print("Initializing Campaign")
    # Set up a fresh campaign called "cooling"
    my_campaign = uq.Campaign(name='cooling', work_dir=tmpdir)

    # Define parameter space
    params = {
        "temp_init": {
            "type": "float",
            "min": 0.0,
            "max": 100.0,
            "default": 95.0
        },
        "kappa": {
            "type": "float",
            "min": 0.0,
            "max": 0.1,
            "default": 0.025
        },
        "t_env": {
            "type": "float",
            "min": 0.0,
            "max": 40.0,
            "default": 15.0
        },
        "out_file": {
            "type": "string",
            "default": "output.csv"
        }
    }

    output_filename = params["out_file"]["default"]
    output_columns = ["te", "ti"]

    # Create an encoder, decoder and collation element
    encoder = uq.encoders.GenericEncoder(template_fname=jobdir + '/' +
                                         TEMPLATE,
                                         delimiter='$',
                                         target_filename=ENCODED_FILENAME)

    decoder = uq.decoders.SimpleCSV(target_filename=output_filename,
                                    output_columns=output_columns,
                                    header=0)

    collater = uq.collate.AggregateSamples(average=False)

    # Add the PCE app (automatically set as current app)
    my_campaign.add_app(name="cooling",
                        params=params,
                        encoder=encoder,
                        decoder=decoder,
                        collater=collater)

    vary = {"kappa": cp.Uniform(0.025, 0.075), "t_env": cp.Uniform(15, 25)}

    # Create the sampler
    if uqmethod == 'pce':
        my_sampler = uq.sampling.PCESampler(vary=vary, polynomial_order=2)
    if uqmethod == 'qmc':
        my_sampler = uq.sampling.QMCSampler(vary=vary, n_samples=10)

    # Associate the sampler with the campaign
    my_campaign.set_sampler(my_sampler)

    # Will draw all (of the finite set of samples)
    my_campaign.draw_samples()

    print("Starting execution")
    qcgpjexec = Executor()

    # Create QCG PJ-Manager with 4 cores
    # (if you want to use all available resources remove resources parameter)
    qcgpjexec.create_manager(dir=my_campaign.campaign_dir,
                             resources='4',
                             log_level='debug')

    qcgpjexec.add_task(
        Task(TaskType.ENCODING, TaskRequirements(cores=Resources(exact=1))))

    qcgpjexec.add_task(
        Task(TaskType.EXECUTION,
             TaskRequirements(cores=Resources(exact=1)),
             application='python3 ' + jobdir + "/" + APPLICATION + " " +
             ENCODED_FILENAME))

    qcgpjexec.run(campaign=my_campaign, submit_order=SubmitOrder.RUN_ORIENTED)

    qcgpjexec.terminate_manager()

    print("Collating results")
    my_campaign.collate()

    print("Making analysis")

    if uqmethod == 'pce':
        analysis = uq.analysis.PCEAnalysis(sampler=my_sampler,
                                           qoi_cols=output_columns)
    if uqmethod == 'qmc':
        analysis = uq.analysis.QMCAnalysis(sampler=my_sampler,
                                           qoi_cols=output_columns)

    my_campaign.apply_analysis(analysis)

    results = my_campaign.get_last_analysis()
    stats = results['statistical_moments']['te']

    print("Processing completed")
    return stats
def urbanair_no2_pj(tmpdir):
    tmpdir = str(tmpdir)

    print("Job directory: " + jobdir)
    print("Temporary directory: " + tmpdir)

    # ---- CAMPAIGN INITIALISATION ---

    # Set up a UrbanAir campagin - NO2 modelling
    my_campaign = uq.Campaign(name='urbanair_no2', work_dir=tmpdir)

    home = os.environ['HOME']

    # Define parameter space
    params = {
        "cars": {
            "type": "integer",
            "min": 10,
            "max": 10000,
            "default": 860
        },
        "gas_engine": {
            "type": "float",
            "min": 0.1,
            "max": 1.0,
            "default": 0.72
        },
        "gas_usage": {
            "type": "float",
            "min": 4.0,
            "max": 13.0,
            "default": 8.0
        },
        "gas_density": {
            "type": "float",
            "min": 0.001,
            "max": 0.9,
            "default": 0.75
        },
        "gas_no2_index": {
            "type": "float",
            "min": 0.001,
            "max": 1.0,
            "default": 0.00855
        },
        "out_file": {
            "type": "string",
            "default": "output.csv"
        }
    }

    output_filename = params["out_file"]["default"]
    output_columns = ["NO2"]

    # Create encoders, decoder and collation element for UrbanAir
    encoder = uq.encoders.GenericEncoder(template_fname=jobdir + '/' +
                                         TEMPLATE,
                                         delimiter='$',
                                         target_filename=ENCODED_FILENAME)

    wind_encoder = uq.encoders.GenericEncoder(template_fname=jobdir + '/' +
                                              WIND_TEMPLATE,
                                              delimiter='$',
                                              target_filename='wind.dat')

    scalars_encoder = uq.encoders.GenericEncoder(template_fname=jobdir + '/' +
                                                 SCALARS_TEMPLATE,
                                                 delimiter='$',
                                                 target_filename='scalars.dat')

    fort13_encoder = uq.encoders.GenericEncoder(template_fname=jobdir + '/' +
                                                FORT13_TEMPLATE,
                                                delimiter='$',
                                                target_filename='fort.13')

    emis_encoder = EmisEncoder(template_fname=jobdir + '/' + EMIS_TEMPLATE,
                               delimiter='$',
                               target_filename='emis.dat')

    encoders = uq.encoders.MultiEncoder(encoder, wind_encoder, scalars_encoder,
                                        fort13_encoder, emis_encoder)

    decoder = uq.decoders.SimpleCSV(target_filename=output_filename,
                                    output_columns=output_columns,
                                    header=0)

    collater = uq.collate.AggregateSamples(average=False)

    # Add the app (automatically set as current app)
    my_campaign.add_app(name="urbanair_no2",
                        params=params,
                        encoder=encoders,
                        decoder=decoder,
                        collater=collater)

    vary = {
        "gas_engine": cp.Uniform(0.1, 0.9),
        "gas_usage": cp.Uniform(3.0, 13.0),
        "gas_density": cp.Uniform(0.001, 0.9),
        "gas_no2_index": cp.Uniform(0.01, 0.1),
    }

    # Create the sampler
    my_sampler = uq.sampling.SCSampler(vary=vary, polynomial_order=1)
    # Associate the sampler with the campaign
    my_campaign.set_sampler(my_sampler)

    # Will draw all (of the finite set of samples)
    my_campaign.draw_samples()

    # use integrator between EasyVVUQ and QCG PilotJob
    qcgpjexec = easypj.Executor()
    # allocate 32 nodes, 24 cores each
    qcgpjexec.create_manager(dir=my_campaign.campaign_dir, resources='32:24')

    # create task to sample parameters. Each task will use 1 core
    # the samples will be generated in parallel
    qcgpjexec.add_task(
        Task(TaskType.ENCODING, TaskRequirements(cores=Resources(exact=1))))

    # run the MPI application using 16 nodes, 24 cores each
    # Two ensembles should be run in parallel
    qcgpjexec.add_task(
        Task(TaskType.EXECUTION,
             TaskRequirements(nodes=Resources(exact=16),
                              cores=Resources(exact=24)),
             application=APPLICATION))

    # first generate samples, then run ensembles
    qcgpjexec.run(campaign=my_campaign,
                  submit_order=SubmitOrder.PHASE_ORIENTED)

    qcgpjexec.terminate_manager()

    my_campaign.collate()

    sc_analysis = uq.analysis.SCAnalysis(sampler=my_sampler,
                                         qoi_cols=output_columns)
    my_campaign.apply_analysis(sc_analysis)

    results = my_campaign.get_last_analysis()
    stats = results['statistical_moments']['NO2']

    return stats