# Section: Mean field
from pyscf import scf
mean_field = scf.{{ mean_field.method }}(structure)
{% if mean_field %}
    {% for key, value in mean_field.items() %}
        {% if key not in ['method', 'checkpoint'] %}
mean_field.{{ key }} = {{ value|render_python }}
        {% endif %}
    {% endfor %}
    {% if 'checkpoint' in mean_field %}
density_matrix = mean_field.from_chk({{ mean_field.checkpoint|render_python }})
    {% else %}
density_matrix = None
    {% endif %}
{% endif %}
mean_field_start = time.perf_counter()
mean_field_run = mean_field.run(density_matrix)

{% if localize_orbitals %}
from pyscf import lo
mean_field_run.mo_coeff = lo.orth_ao(mean_field_run, {{ localize_orbitals.method|render_python }})
{% endif -%}

results['timings']['mean_field'] = time.perf_counter() - mean_field_start
results['mean_field'] = {}
results['mean_field']['is_converged'] = mean_field_run.converged
results['mean_field']['total_energy'] = mean_field_run.e_tot
results['mean_field']['forces'] = (- mean_field_run.nuc_grad_method().kernel()).tolist()

if mean_field_run.converged:
    results['mean_field']['molecular_orbitals'] = {}
    results['mean_field']['molecular_orbitals']['energies'] = mean_field_run.mo_energy.tolist()
    results['mean_field']['molecular_orbitals']['labels'] = structure.ao_labels()
    results['mean_field']['molecular_orbitals']['occupations'] = mean_field_run.mo_occ.tolist()
else:
    write_results_and_exit(results)
