How to use the pymars.reduce_model.ReducedModel function in pymars

To help you get started, we’ve selected a few pymars examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Niemeyer-Research-Group / pyMARS / pymars / pfa.py View on Github external
raise SystemExit(
                    'Threshold value dropped below 1e-5 without producing viable reduced model'
                    )
            logging.info('Threshold value too high, reducing by factor of 10')
            continue
        
        logging.info(f'{threshold:^9.2e} | {num_species:^17} | {error_current:^.2f}')

        threshold += threshold_increment
        first = False

        # cleanup files
        if previous_model.model.n_species != reduced_model.model.n_species:
            os.remove(reduced_model.filename)
        
        previous_model = ReducedModel(
            model=reduced_model.model, filename=reduced_model.filename, 
            error=reduced_model.error, limbo_species=reduced_model.limbo_species
            )
    
    if reduced_model.error > error_limit:
        threshold -= (2 * threshold_increment)
        reduced_model = reduce_pfa(
            model_file, species_targets, species_safe, threshold, matrices, 
            ignition_conditions, sampled_metrics, 
            threshold_upper=threshold_upper, num_threads=num_threads, path=path
            )
    else:
        soln2cti.write(reduced_model, f'reduced_{reduced_model.model.n_species}.cti', path=path)
    
    logging.info(45 * '-')
    logging.info('PFA reduction complete.')
github Niemeyer-Research-Group / pyMARS / pymars / drg.py View on Github external
raise SystemExit(
                    'Threshold value dropped below 1e-5 without producing viable reduced model'
                    )
            logging.info('Threshold value too high, reducing by factor of 10')
            continue
        
        logging.info(f'{threshold:^9.2e} | {num_species:^17} | {error_current:^.2f}')

        threshold += threshold_increment
        first = False

        # cleanup files
        if previous_model.model.n_species != reduced_model.model.n_species:
            os.remove(reduced_model.filename)
        
        previous_model = ReducedModel(
            model=reduced_model.model, filename=reduced_model.filename, 
            error=reduced_model.error, limbo_species=reduced_model.limbo_species
            )
    
    if error_current > error_limit:
        threshold -= (2 * threshold_increment)
        reduced_model = reduce_drg(
            model_file, species_targets, species_safe, threshold, matrices, 
            ignition_conditions, sampled_metrics, 
            threshold_upper=threshold_upper, num_threads=num_threads, path=path
            )
    else:
        soln2cti.write(reduced_model, f'reduced_{reduced_model.model.n_species}.cti', path=path)
    
    logging.info(45 * '-')
    logging.info('DRG reduction complete.')
github Niemeyer-Research-Group / pyMARS / pymars / pfa.py View on Github external
reduced_model_filename, ignition_conditions, num_threads=num_threads, path=path
        )
    error = calculate_error(sampled_metrics, reduced_model_metrics)
    
    # If desired, now identify limbo species for future sensitivity analysis
    limbo_species = []
    if threshold_upper:
        species_retained += trim_pfa(
            matrix, solution.species_names, species_targets, threshold_upper
            )
        limbo_species = [
            sp for sp in solution.species_names
            if sp not in (species_retained + species_safe + species_removed)
            ]

    return ReducedModel(
        model=reduced_model, filename=reduced_model_filename, 
        error=error, limbo_species=limbo_species
        )
github Niemeyer-Research-Group / pyMARS / pymars / sensitivity_analysis.py View on Github external
if error > error_limit:
                break
            else:
                current_model = ReducedModel(model=test_model, filename=test_model_file, error=error)

            # If using the greedy algorithm, now need to reevaluate all species errors
            if algorithm_type == 'greedy':
                species_errors = evaluate_species_errors(
                    current_model, ignition_conditions, initial_metrics, species_limbo, 
                    phase_name=phase_name, num_threads=num_threads
                    )
                if min(species_errors) > error_limit:
                    break
    
    # Final model; may need to rewrite
    reduced_model = ReducedModel(
        model=current_model.model, filename=f'reduced_{current_model.model.n_species}.cti', 
        error=current_model.error
        )
    soln2cti.write(reduced_model.model, reduced_model.filename, path=path)

    logging.info(53 * '-')
    logging.info('Sensitivity analysis stage complete.')
    logging.info(f'Skeletal model: {reduced_model.model.n_species} species and '
                 f'{reduced_model.model.n_reactions} reactions.'
                 )
    logging.info(f'Maximum error: {reduced_model.error:.2f}%')
    return reduced_model
github Niemeyer-Research-Group / pyMARS / pymars / sensitivity_analysis.py View on Github external
List of species to consider; if empty, consider all not in ``species_safe``
    num_threads : int, optional
        Number of CPU threads to use for performing simulations in parallel.
        Optional; default = 1, in which the multiprocessing module is not used.
        If 0, then use the available number of cores minus one. Otherwise,
        use the specified number of threads.
    path : str, optional
        Optional path for writing files
    
    Returns
    -------
    ReducedModel
        Return reduced model and associated metadata

    """
    current_model = ReducedModel(
        model=ct.Solution(model_file, phase_name), error=starting_error, filename=model_file
        )
    
    logging.info(f'Beginning sensitivity analysis stage, using {algorithm_type} approach.')

    # The metrics for the starting model need to be determined or read
    initial_metrics = sample_metrics(
        model_file, ignition_conditions, reuse_saved=True, phase_name=phase_name,
        num_threads=num_threads, path=path
        )

    if not species_limbo:
        species_limbo = [
            sp for sp in current_model.model.species_names if sp not in species_safe
            ]
github Niemeyer-Research-Group / pyMARS / pymars / drg.py View on Github external
reduced_model_filename, ignition_conditions, num_threads=num_threads, path=path
        )
    error = calculate_error(sampled_metrics, reduced_model_metrics)
    
    # If desired, now identify limbo species for future sensitivity analysis
    limbo_species = []
    if threshold_upper:
        species_retained += trim_drg(
            matrix, solution.species_names, species_targets, threshold_upper
            )
        limbo_species = [
            sp for sp in solution.species_names
            if sp not in (species_retained + species_safe + species_removed)
            ]

    return ReducedModel(
        model=reduced_model, filename=reduced_model_filename, 
        error=error, limbo_species=limbo_species
        )
github Niemeyer-Research-Group / pyMARS / pymars / drg.py View on Github external
# will be used to produce graphs for any threshold value.
    sampled_metrics, sampled_data = sample(
        model_file, ignition_conditions, num_threads=num_threads, path=path
        )

    matrices = []
    for state in sampled_data:
        matrices.append(create_drg_matrix((state[0], state[1], state[2:]), solution))

    # begin reduction iterations
    logging.info('Beginning DRG reduction loop')
    logging.info(45 * '-')
    logging.info('Threshold | Number of species | Max error (%)')

    # start with detailed (starting) model
    previous_model = ReducedModel(model=solution, filename=model_file, error=0.0)

    first = True
    error_current = 0.0
    threshold = 0.01
    threshold_increment = 0.01
    while error_current <= error_limit:
        reduced_model = reduce_drg(
            model_file, species_targets, species_safe, threshold, matrices, 
            ignition_conditions, sampled_metrics, previous_model=previous_model, 
            threshold_upper=threshold_upper, num_threads=num_threads, path=path
            )
        error_current = reduced_model.error
        num_species = reduced_model.model.n_species

        # reduce threshold if past error limit on first iteration
        if first and error_current > error_limit:
github Niemeyer-Research-Group / pyMARS / pymars / sensitivity_analysis.py View on Github external
test_model, output_filename=f'reduced_model_{species_remove}.cti', path=temp_dir
                )

            reduced_model_metrics = sample_metrics(
                test_model_file, ignition_conditions, phase_name=phase_name, 
                num_threads=num_threads, path=path
                )
            error = calculate_error(initial_metrics, reduced_model_metrics)

            logging.info(f'{test_model.n_species:^17} | {species_remove:^17} | {error:^.2f}')

            # Ensure new error isn't too high
            if error > error_limit:
                break
            else:
                current_model = ReducedModel(model=test_model, filename=test_model_file, error=error)

            # If using the greedy algorithm, now need to reevaluate all species errors
            if algorithm_type == 'greedy':
                species_errors = evaluate_species_errors(
                    current_model, ignition_conditions, initial_metrics, species_limbo, 
                    phase_name=phase_name, num_threads=num_threads
                    )
                if min(species_errors) > error_limit:
                    break
    
    # Final model; may need to rewrite
    reduced_model = ReducedModel(
        model=current_model.model, filename=f'reduced_{current_model.model.n_species}.cti', 
        error=current_model.error
        )
    soln2cti.write(reduced_model.model, reduced_model.filename, path=path)
github Niemeyer-Research-Group / pyMARS / pymars / drgep.py View on Github external
# Cut the exclusion list from the model.
    reduced_model = trim(
        model_file, species_removed, f'reduced_{model_file}', phase_name=phase_name
        )
    reduced_model_filename = soln2cti.write(
        reduced_model, f'reduced_{reduced_model.n_species}.cti', path=path
        )

    reduced_model_metrics = sample_metrics(
        reduced_model_filename, ignition_conditions, phase_name=phase_name, 
        num_threads=num_threads, path=path
        )
    error = calculate_error(sampled_metrics, reduced_model_metrics)

    return ReducedModel(
        model=reduced_model, filename=reduced_model_filename, error=error
        )