Ejemplo n.º 1
0
def test_sites(setup_cte, sites):
    '''Test a lattice with different unit cell parameters
    '''

    cte = setup_cte
    cte['lattice']['sites_pos'] = sites[0]
    cte['lattice']['sites_occ'] = sites[1]

    with pytest.raises(lattice.LatticeError):
        with temp_bin_filename() as temp_filename:
            (dist_array, ion_type, doped_lattice, initial_population,
             lattice_info, index_S_i, index_A_j, index_S_k, dist_S_k,
             index_S_l, dist_S_l, index_A_k, dist_A_k, index_A_l,
             dist_A_l) = lattice.generate(cte, full_path=temp_filename)
Ejemplo n.º 2
0
def test_unit_cell(setup_cte, cell_params):
    '''Test a lattice with different unit cell parameters
    '''

    cte = setup_cte
    for key, param in zip(['a', 'b', 'c', 'alpha', 'beta', 'gamma'],
                          cell_params):
        cte.lattice[key] = param

    with pytest.raises(lattice.LatticeError):
        with temp_bin_filename() as temp_filename:
            (dist_array, ion_type, doped_lattice, initial_population,
             lattice_info, index_S_i, index_A_j, index_S_k, dist_S_k,
             index_S_l, dist_S_l, index_A_k, dist_A_k, index_A_l,
             dist_A_l) = lattice.generate(cte, full_path=temp_filename)
Ejemplo n.º 3
0
def test_cte_wrong(setup_cte, params):
    '''Test a lattice with different unit cell parameters
    '''
    cte = setup_cte

    cte['lattice']['S_conc'] = params[0]
    cte['lattice']['A_conc'] = params[1]
    cte['lattice']['N_uc'] = params[2]
    cte['states']['sensitizer_states'] = params[3]
    cte['states']['activator_states'] = params[4]
    if params[5] is not None:
        cte['lattice']['radius'] = params[5]
        del cte['lattice']['N_uc']

    with pytest.raises(lattice.LatticeError):
        with temp_bin_filename() as temp_filename:
            (dist_array, ion_type, doped_lattice, initial_population,
             lattice_info, index_S_i, index_A_j, index_S_k, dist_S_k,
             index_S_l, dist_S_l, index_A_k, dist_A_k, index_A_l,
             dist_A_l) = lattice.generate(cte, full_path=temp_filename)
Ejemplo n.º 4
0
def test_single_atom(setup_cte, concs):
    '''Generate lattices with a single S or A'''

    cte = setup_cte
    cte['lattice']['N_uc'] = 1
    cte['states']['sensitizer_states'] = 2
    cte['states']['activator_states'] = 7

    # single A atom
    success = False
    cte['lattice']['S_conc'] = concs[0]
    cte['lattice']['A_conc'] = concs[1]
    while not success:
        try:
            with temp_bin_filename() as temp_filename:
                (dist_array, ion_type, doped_lattice, initial_population,
                 lattice_info, index_S_i, index_A_j, index_S_k, dist_S_k,
                 index_S_l, dist_S_l, index_A_k, dist_A_k, index_A_l,
                 dist_A_l) = lattice.generate(cte, full_path=temp_filename)
        except lattice.LatticeError:  # no ions were generated, repeat
            pass
        else:
            if len(ion_type) == 1:  # only one ion was generated
                success = True
Ejemplo n.º 5
0
def command_simulation(args: Dict) -> None:
    '''User invoked one of the simulation commands'''

    if args['--verbose']:
        no_console = False
    elif args['--quiet']:
        no_console = True
    else:
        no_console = False

    # show plots or not
    if args['--no-plot']:
        no_plot = True
    else:
        no_plot = False

    # load config file
    logger = logging.getLogger('simetuc')
    logger.info('Loading configuration...')
    cte = settings.load(args['<config_filename>'])
    cte['no_console'] = no_console
    cte['no_plot'] = no_plot
    N_samples = args.get('--N-samples', None)
    N_samples = int(N_samples) if N_samples else None
    cte['N_samples'] = N_samples

    # solution of the simulation
    solution: Union[simulations.Solution, simulations.SolutionList,
                    optimize.OptimSolution, None] = None

    # choose what to do
    if args['--lattice']:  # create lattice
        logger.info('Creating and plotting lattice...')
        lattice.generate(cte)

    elif args['--dynamics'] and not args[
            '--concentration-dependence']:  # simulate dynamics
        logger.info('Simulating dynamics...')
        sim = simulations.Simulations(cte)
        if args['--N-samples'] is not None:
            solution = sim.sample_simulation(sim.simulate_dynamics,
                                             N_samples=N_samples,
                                             average=args['--average'])
        else:
            solution = sim.simulate_dynamics(average=args['--average'])
        solution.log_errors()

    elif args['--steady-state']:  # simulate steady state
        logger.info('Simulating steady state...')
        sim = simulations.Simulations(cte)
        solution = sim.simulate_steady_state(average=args['--average'])
        solution.log_populations()

    elif args['--power-dependence']:  # simulate power dependence
        logger.info('Simulating power dependence...')
        sim = simulations.Simulations(cte)
        power_dens_list = cte.power_dependence
        solution = sim.simulate_power_dependence(power_dens_list,
                                                 average=args['--average'])
        print('')

    elif args['--concentration-dependence'] and not args[
            '--optimize']:  # simulate concentration dependence
        logger.info('Simulating concentration dependence...')
        sim = simulations.Simulations(cte)
        conc_list = cte.concentration_dependence['concentrations']
        N_uc_list = cte.concentration_dependence['N_uc_list']

        if args['--N-samples'] is not None:
            solution = sim.sample_simulation(
                sim.simulate_concentration_dependence,
                N_samples=N_samples,
                concentrations=conc_list,
                N_uc_list=N_uc_list,
                dynamics=args['--dynamics'],
                average=args['--average'])
        else:
            solution = sim.simulate_concentration_dependence(
                conc_list,
                N_uc_list,
                dynamics=args['--dynamics'],
                average=args['--average'])
        solution.log_errors()

    elif args['--optimize']:  # optimize
        logger.info('Optimizing parameters...')
        if args['--concentration'] or args['--concentration-dependence']:
            solution = optimize.optimize_concentrations(
                cte, average=args['--average'], N_samples=N_samples)
        else:
            solution = optimize.optimize_dynamics(cte,
                                                  average=args['--average'],
                                                  N_samples=N_samples)

    # save results to disk
    if solution is not None and not args['--no-save']:
        logger.info('Saving results to file.')
        solution.save()
        solution.save_txt(cmd=' '.join(sys.argv))

    logger.info('Program finished!')

    # show all plots
    # the user needs to close the window to exit the program
    if not args['--no-plot']:
        if solution is not None:
            solution.plot()
        logger.info('Close the plot window to exit.')
        plt.show()
Ejemplo n.º 6
0
def test_cte_ok(setup_cte, params):
    '''Test a lattice with different concentrations of S and A; different number of unit cells;
        and different number of S and A energy states
    '''
    cte = setup_cte
    cte['no_plot'] = False

    cte['lattice']['S_conc'] = params[0]
    cte['lattice']['A_conc'] = params[1]
    cte['lattice']['N_uc'] = params[2]
    cte['states']['sensitizer_states'] = params[3]
    cte['states']['activator_states'] = params[4]
    if params[5] is not None:
        cte['lattice']['radius'] = params[5]
        del cte['lattice']['N_uc']

    with temp_bin_filename() as temp_filename:
        (dist_array, ion_type, doped_lattice, initial_population, lattice_info,
         index_S_i, index_A_j, index_S_k, dist_S_k, index_S_l, dist_S_l,
         index_A_k, dist_A_k, index_A_l,
         dist_A_l) = lattice.generate(cte, full_path=temp_filename)

    num_ions = lattice_info['num_total']
    num_activators = lattice_info['num_activators']
    num_sensitizers = lattice_info['num_sensitizers']

    num_states = lattice_info['energy_states']
    num_S_states = lattice_info['sensitizer_states']
    num_A_states = lattice_info['activator_states']

    assert dist_array.shape == (num_ions, num_ions)
    # symmetric matrix
    assert np.allclose(dist_array, dist_array.T)
    # only positive distances
    assert np.alltrue(dist_array >= 0)

    assert ion_type.shape == (num_ions, )
    assert np.max(ion_type) == 1 or np.max(ion_type) == 0
    assert np.min(ion_type) == 1 or np.min(ion_type) == 0
    assert np.count_nonzero(ion_type) == num_activators
    assert ion_type.shape[0] - np.count_nonzero(ion_type) == num_sensitizers

    assert doped_lattice.shape == (num_ions, 3)

    assert initial_population.shape == (num_states, )
    assert np.max(initial_population) <= 1
    assert np.min(initial_population) >= 0

    assert len(index_S_i) == num_ions
    assert min(index_S_i) >= -1
    assert max(index_S_i) <= num_states - 1

    assert len(index_A_j) == num_ions
    assert min(index_A_j) >= -1
    assert max(index_A_j) <= num_states - 1

    if num_sensitizers > 0 and num_S_states > 0:
        assert len(index_S_k) == num_sensitizers
        assert all(
            len(list_elem) == num_sensitizers - 1 for list_elem in index_S_k)
        assert all(-1 <= max(list_elem) <= num_states
                   for list_elem in index_S_k if len(list_elem))
        assert len(dist_S_k) == num_sensitizers
        assert all(
            len(list_elem) == num_sensitizers - 1 for list_elem in dist_S_k)
        assert all(np.alltrue(list_elem >= 0) for list_elem in dist_S_k)

        if num_activators > 0 and num_A_states > 0:
            assert len(index_S_l) == num_sensitizers
            assert all(
                len(list_elem) == num_activators for list_elem in index_S_l)
            assert all(-1 <= max(list_elem) <= num_states
                       for list_elem in index_S_l if len(list_elem))
            assert len(dist_S_l) == num_sensitizers
            assert all(
                len(list_elem) == num_activators for list_elem in dist_S_l)
            assert all(np.alltrue(list_elem >= 0) for list_elem in dist_S_l)

    if num_activators > 0 and num_A_states > 0:
        assert len(index_A_l) == num_activators
        assert all(
            len(list_elem) == num_activators - 1 for list_elem in index_A_l)
        assert all(-1 <= max(list_elem) <= num_states
                   for list_elem in index_A_l if len(list_elem))
        assert len(dist_A_l) == num_activators
        assert all(
            len(list_elem) == num_activators - 1 for list_elem in dist_A_l)
        assert all(np.alltrue(list_elem >= 0) for list_elem in dist_A_l)

        if num_sensitizers > 0 and num_S_states > 0:
            assert len(index_A_k) == num_activators
            assert all(
                len(list_elem) == num_sensitizers for list_elem in index_A_k)
            assert all(-1 <= max(list_elem) <= num_states
                       for list_elem in index_A_k if len(list_elem))
            assert len(dist_A_k) == num_activators
            assert all(
                len(list_elem) == num_sensitizers for list_elem in dist_A_k)
            assert all(np.alltrue(list_elem >= 0) for list_elem in dist_A_k)