<jupyter_start><jupyter_text>Exercise 2: Simulation Codes **Authors:  <NAME>, <NAME>, <NAME> ([Max-Planck-Institut für Eisenforschung](https://www.mpie.de))**The scope of this second exercise is to become familar with:* Interatomic potential calculation, * the pyiron job management and* the aggregation of multiple calculations.  ReminderIn the first session we learned how to create a pyiron project object and then use this pyiron project object to create atomistic structure objects.<jupyter_code># Import the Project object
from pyiron import ____
# Create a Project object instance for a project named atomistics
pr = ____("simulations")
# Create a cubic aluminum fcc structure and repeat it 3 times in each direction
al_fcc = pr.create_ase_bulk(____, _____=True)
al_fcc_repeated = al_fcc.repeat(____)
# Confirm the final structure has 108 atoms by calculating the length of the structure object
____(al_fcc_repeated) == 108<jupyter_output><empty_output><jupyter_text>LAMMPS Calculation The Large-scale Atomic/Molecular Massively Parallel Simulator (LAMMPS) code is used inside pyiron for atomistic simualtion with interatomic potentials. These interatomic potentials approximate the interaction of atoms and can be either fitted to density functional theory (DFT) or experimental results. Still in contrast to density functional theory which scales cubically with the number of atoms interatomic potentials scale linearly with the number of atoms. Therefore we are going to use primarly interatomic potentials in this workshop but most calculations could be executed with a DFT codes as well.   Molecular dynamics calculationWe start with a first molecular dynamic calculation at an ensemble with constant number of atoms, constant volume and contant fixed temperature.<jupyter_code># Create a LAMMPS job object with the job name lmp
job_md = pr.create_job(
    job_type=pr.job_type.Lammps, 
    job_name=____
)
# Assign the fcc aluminium structure to the LAMMPS job object
job_md.structure = _________
# Define an ensemble with constant number of atoms, 
# constant volume and a constant temperature of 500K
# and simulate 10000 molecular dynamics steps
job_md.calc_md(temperature=______, n_ionic_steps=______)
# Execute the calculation - You get a warning that 
# not potential was set and the default potential was
# used instead. The selection of interatomic potentials 
# is discussed below.
job_md.run()
# Animate the molecular dynamics trajectory
job_md.animate_structure()<jupyter_output><empty_output><jupyter_text>Reminder: Job Management in pyironAfter the successful execution of the calculation it is listed in the project database and can be reloaded using either the job name or the job id, therefore theese have to be unique for a given project.<jupyter_code># list all calculations in the current project using the project job table
______.job_table()<jupyter_output><empty_output><jupyter_text>As discussed in the previous section by default pyiron calculations are reloaded from the database when a calculation with the same name already exists in a given project. Therefore to overwrite the calculation parameters we use `delete_existing_job` parameter in the `run()` function.<jupyter_code># Change the temperature to 800K and calculate 20000 steps
# You get a warning message that the job is already finished. 
job_md.calc_md(temperature=_____, n_ionic_steps=_______)
# Execute the LAMMPS calculation by calling the run function 
# with the delete_existing parameter set to true: 
job_md.run(delete_existing_job=______)<jupyter_output><empty_output><jupyter_text>Reminder: Plot calculation results using matplotlib In the same way we can again use matplotlib to analyse the results of our calculation. For example we can use the matplotlib library to plot the temperature over simulation steps.<jupyter_code># Import the matplotlib library for plotting. 
import matplotlib.pyplot as plt

# for the LAMMPS job object plot the temperature over simualation steps
plt.plot(________.output.steps, _____.output.temperature)
plt.xlabel("timesteps")
plt.ylabel("Temperature");<jupyter_output><empty_output><jupyter_text>In the beginning the potential energy is close to the 0K equilibrium, therefore to accelerate the equilibration the kinetic energy is set to twice the expected kinetic energy. The additional kinetic energy is transfered to the potential energy resulting in an equal distribution of potential and kinetic energy. With this trick the equilibration is accelerated. The large temperature fluctuations are related to the small number of atoms in the simulation cell.  Advanced input optionsBesides the general functions `calc_static()`, `calc_minimize()` and `calc_md()`. pyiron also has the option to modify the input of the simulation code directly.<jupyter_code># display the LAMMPS input file 
job_md.input.control
# change the number of simulation steps to 2000 
# by manually modifying the run command.
# You again get a warning message that the job 
# is already finished. 
job_md.input.control["run"] = ____<jupyter_output><empty_output><jupyter_text>Advanced output options Besides the output properties it is also possible to access the output of a calculation directly from the data interface which is based om the hierachical file format (HDF5) pyiron is using to store the simulation data.<jupyter_code># print content of the job object 
job_md
# print content of the output of the job object
# use strings to specify the path for the data interface 
job_md[_______]
# print content of the generic group 
# of the output of the job object 
job_md[______/______]
# plot the temperature over simulation steps directly from the HDF5 file 
plt.plot(job_md[___________], job_md[___________])
plt.xlabel("timesteps")
plt.ylabel("Temperature");<jupyter_output><empty_output><jupyter_text>Accessing the original output files of the LAMMPS codeWhile pyiron parses most of the output of the simulation codes some users might have the need to access addtional output parameters.<jupyter_code># Decompress the LAMMPS job
_______.decompress()
# Read the LAMMPS output file of the LAMMPS job
_______["log.lammps"]<jupyter_output><empty_output><jupyter_text>Beyond a single LAMMPS calculationWhile for individual LAMMPS calculation an integrated solution like pyiron is not required, pyiron really shines when it comes to combining multiple calculations. So in the following we iterate over a database of existing interatomic potentials and determine the lattice structure by minimizing the simulation cell.     Filter Interatomic Potential DatabaseWe start by identifying suitable interatomic potentials. By default pyiron already filters the interatomic potentials to only list those which include the required elements. Still the user can further filter the list of available potentials for a given project.<jupyter_code># List interatomic potentials from the NIST repository: 
# https://www.ctcms.nist.gov/potentials/
# which include interactions for aluminium by calling view_potentials() on the LAMMPS job object
potential_df = ______.view_potentials()
potential_df[potential_df.Model == "NISTiprpy"]<jupyter_output><empty_output><jupyter_text>Choose an interatmoic potentialInteratomic potentials can be fitted for specific applications, therefore before selecting a given interatomic potential it is recommended to test basic properties of the potential. In this example we calculate the 0K equilibrium lattice constant by optimizing the atmoic supercell.  Job TemplateTo develop a scalable simulation protocol, we first define a job template and then apply this template to the available interatomic potentials.<jupyter_code># To compare different potentials we start by creating a template job named lmp_template
job_template = pr.create_job(
    job_type=pr.job_type.Lammps, 
    job_name=______
)
# We assign a cubic fcc aluminium supercell 
job_template.structure = pr.create_ase_bulk(_____, _____)
# Enable volume minimization by specifying the pressure as zero
job_template.calc_minimize(pressure=_____)<jupyter_output><empty_output><jupyter_text>Iterate over interatomic potentialsAfter the template is constructed we can iterate over the database of existing interatomic potentials. In this example we limit the total umber of potentials to three to accelerate the calculations.<jupyter_code># Select the first three potentials from the NIST database
potential_df[potential_df.Model == "NISTiprpy"].Name.values[_____]
# We then iterate over the first three potentials of the NIST database
for p in potential_df[potential_df.Model == "NISTiprpy"].Name.values[_____]:
    # create a copy for each of the template job for each of the potentials
    # without creating a new database entry by setting the new_database_entry 
    # parameter to False 
    job_minimize = job_template.copy_to(
        new_job_name="lmp_" + p.replace("-", "_"), 
        new_database_entry=_____
    )
    
    # We then assign the potential 
    job_minimize.potential = p
    
    # Execute the calculation and set the delete_existing_job parameter to True
    job_minimize.run(delete_existing_job=____)
# list all calculations in the current project using the project job table
pr.job_table()<jupyter_output><empty_output><jupyter_text>Validate calculationsWe validate the simulation results by confirming the calculations have been executed successfully. In this example one calculation failed.<jupyter_code># We use the pyiron job table of the project object 
# to validate all jobs finished successfully with 
# the status "finished"
df = ____.job_table()
len(df[df.status == _______]) == 4<jupyter_output><empty_output><jupyter_text>Analyse resultsIterate over the successful calculation and compare the calculated lattice constants.<jupyter_code># load a job object by the job name in the column job 
# of the pyiron job table 
job = pr.load(______)
# print the final lattice constant 
job["output/generic/cells"][______]
# We iterate over the jobs in a the current project 
for job in pr.iter_jobs():
    # Filter job using only those who have the job status 
    # finished and "lmp_" in the job_name
    if job.status == _____ and ____ in job.job_name: 
        # Print the job name and the lattice constant 
        print(job.job_name, job["output/generic/cells"][_____])<jupyter_output><empty_output><jupyter_text>pyiron table To automate the collection of data from individual calculations pyiron includes the pyiron table object.<jupyter_code># Create a pyiron table object
table = pr.create_table()
# Implement a filter function, which returns true 
# for finished jobs and jobs with "lmp_" in the job_name
def filter_jobs(job):
    return job.status == ______ and ______ in job.job_name
# Implement an analysis functions, which takes a job object
# as an input and returns the lattice constant. Based on the
# previous cell above to print the lattice constant.
def get_lattice_constant(job): 
    return _________________
# Implement a second analysis functions, which takes a 
# job object as an input and returns the job_name.
def get_job_name(job):
    return _________________
# Assigne the filter functions and the analysis functions 
# to the pyiron table object
table.filter_function = filter_jobs
table.add["job_name"] = get_job_name
table.add["lattice_constant"] = get_lattice_constant
# Execute the pyiron table just like a pyiron job object
table.run()
# Return a pandas DataFrame with the collected results 
table.get_dataframe()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.05184091698506426, 'fraction_numerical': 0.002865578325807572, 'mean_word_length': 5.525212464589235, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 7, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '196471', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2949, 'n_words': 1372, 'path': 'Exercise2.ipynb', 'repo_name': 'pyiron/phasediagram-workshop-2020', 'size': '11538'}