# Make a list to hold the configurations.
configurations = []

# Loop over a list of distances between 0.3 and 5.0 Angstrom.
distances = numpy.linspace(0.3, 5.0, 20)
for distance in distances:
    # Define elements
    elements = [Hydrogen, Hydrogen]

    # Define coordinates
    cartesian_coordinates = [[ 0.0, 0.0, 0.0 ],
                             [ distance, 0.0, 0.0 ]]*Angstrom

    # Set up configuration
    molecule_configuration = MoleculeConfiguration(
        elements=elements,
        cartesian_coordinates=cartesian_coordinates
        )

    # Define a calculator
    molecule_configuration.setCalculator(LCAOCalculator())

    # Add the configuration to the list of configurations.
    configurations.append(molecule_configuration)

def task_function(configuration, index):
    # Compute the total energy.
    total_energy = TotalEnergy(configuration)
    # Save the result to a file.
    nlsave('total_energy_%i.hdf5'%index, configuration)
    # Return the calculated total energy.
    return total_energy.evaluate()

# Define a list of filenames to save the logging output from each calculation to.
filenames = [ 'total_energy_%i.log' % i for i in range(len(configurations)) ]

# Calculate the energy of each configuration. Each calculation will use 2 MPI processes.
energies = ParallelMapConfigurations(
    task_function,
    configurations,
    processes_per_configuration=2,
    filenames=filenames,
)

# Only print on the master process. This prevents the table from being printed multiple times.
if processIsMaster():
    print('%10s %12s' % ('distance', 'energy'))
    for i in range(len(configurations)):
        print('%10.4f %12.3e' % (distances[i], energies[i].inUnitsOf(eV)))


