<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.3.4">Jekyll</generator><link href="https://leiapple.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://leiapple.github.io/" rel="alternate" type="text/html" /><updated>2026-04-18T17:09:47+02:00</updated><id>https://leiapple.github.io/feed.xml</id><title type="html">Lei’s Blog</title><subtitle>Lei Zhang</subtitle><author><name>Lei Zhang</name></author><entry><title type="html">Interstitial energy in LAMMPS</title><link href="https://leiapple.github.io/2024/10/03/lammps-inters/" rel="alternate" type="text/html" title="Interstitial energy in LAMMPS" /><published>2024-10-03T00:00:00+02:00</published><updated>2024-10-03T00:00:00+02:00</updated><id>https://leiapple.github.io/2024/10/03/lammps-inters</id><content type="html" xml:base="https://leiapple.github.io/2024/10/03/lammps-inters/"><![CDATA[<h1 id="calculating-interstitial-energy-of-carbon-in-iron-using-lammps">Calculating Interstitial Energy of Carbon in Iron Using LAMMPS</h1>

<p><strong>Interstitial energy</strong> refers to the energy required to insert an atom into the interstitial site of a crystal lattice. In this guide, we will calculate the interstitial energy of carbon in a body-centered cubic (BCC) iron lattice using the LAMMPS molecular dynamics software.</p>

<p>We will:</p>
<ol>
  <li>Set up a perfect BCC iron lattice and calculate its potential energy.</li>
  <li>Insert a carbon atom into an octahedral interstitial site, relax the system and calculate its potential energy.</li>
  <li>Calculate the potential energy of a carbon atom in diamond structure.</li>
  <li>Compute the interstitial energy of carbon.</li>
</ol>

<hr />

<h2 id="step-1-setting-up-the-simulation">Step 1: Setting Up the Simulation</h2>

<p>First, install LAMMPS if you haven’t already. You can download it from the <a href="https://www.lammps.org">official LAMMPS website</a>.</p>

<p>We will use an embedded-atom method (EAM) potential for the iron and carbon interaction. Make sure you have the potential files in your working directory. You can download the Fe-C EAM potential file from <a href="https://openkim.org">OpenKIM</a> or other sources.</p>

<h3 id="lattice-setup">Lattice Setup</h3>

<p>We begin by defining a perfect BCC iron lattice. Below is the LAMMPS input script to set up the simulation box and create atoms for the BCC lattice. Here is the lammps command:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># Interstitial Energy of Carbon in BCC Iron

units metal
dimension 3
boundary p p p
atom_style atomic

# Define the BCC lattice with lattice constant 2.87 Angstroms (typical for Fe, but changes according to the potential)
lattice bcc 2.87
region box block 0 10 0 10 0 10
create_box 2 box

# Create Fe atoms in the BCC structure
create_atoms 1 box

# Define the potential (Fe-C alloy EAM potential)
pair_style eam
pair_coeff * * Fe_C.eam.alloy Fe C

# Assign masses to the atom types
mass 1 55.845  # Iron
mass 2 12.011  # Carbon

# Set up neighbor lists and thermodynamics output
neighbor 2.0 bin
neigh_modify delay 5

thermo 100
thermo_style custom step temp pe

# Minimize the system to get the energy of pure BCC iron
minimize 1.0e-6 1.0e-8 1000 10000

# Write the minimized configuration to a file
write_data bcc_iron.data

</code></pre></div></div>

<h2 id="step-2-insert-carbon-atom-into-octahedral-interstitial-site">Step 2: Insert Carbon Atom into Octahedral Interstitial Site</h2>

<p>Once you’ve generated the BCC iron lattice and minimized the system, the next step is to insert a carbon atom into an octahedral interstitial site.</p>

<p>The octahedral interstitial site in a BCC unit cell is located at fractional coordinates such as (0.5, 0.5, 0), (0.5, 0, 0.5), and (0, 0.5, 0.5). We will insert a carbon atom at (0.5, 0.5, 0), which corresponds to one of the octahedral sites in the BCC lattice.</p>

<p>Add the following lines to the LAMMPS script:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># Insert a carbon atom at an octahedral site (0.5, 0.5, 0)
create_atoms 2 single 1.435 1.435 0.0 unit box # Position in Angstroms based on lattice constant 2.87 Å

# Minimize the system again after inserting the carbon atom
minimize 1.0e-6 1.0e-8 1000 10000

# Write the minimized configuration with the carbon interstitial
write_data fe_c_interstitial.data
</code></pre></div></div>

<p>To extract these energies, you can modify the LAMMPS script to output the potential energies at each stage:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># Output the total energy of the system
variable E_Fe equal pe  # Energy of pure BCC iron
print "Energy of pure BCC iron: ${E_Fe} eV"

# After inserting the carbon atom
variable E_Fe_C equal pe  # Energy of BCC iron with carbon interstitial
print "Energy of BCC iron with carbon interstitial: ${E_Fe_C} eV"
</code></pre></div></div>

<h2 id="step-3-calculate-the-potential-energy-of-a-carbon-atom-in-diamond-structure">Step 3: Calculate the potential energy of a carbon atom in diamond structure</h2>

<p>The carbon energy can be obtained from a separate calculation in LAMMPS of carbon atomic energy in diamond structure.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># -----------------------------------------------------------
# LAMMPS script to find the equilibrium lattice constant
# of carbon in a diamond structure and save results to file
# -----------------------------------------------------------

log             eos_diamond.log           
label           loop_start

variable        a equal 3.5                      # Initial guess for the lattice constant
variable        da equal 0.005                    # Increment for the lattice constant
variable        steps equal 20                   # Number of steps for varying the lattice constant

variable        i loop 1 ${steps}
variable        latparam equal ${a}+${da}*${i}

clear
units           metal
dimension       3
boundary        p p p
atom_style      atomic
atom_modify     map yes

#-------Define geometry  (2d X 2d) ---------------------
lattice         diamond ${latparam}
region          box block 0 1 0 1 0 1 units lattice
create_box      2 box
create_atoms    2 box

#-------Define interatomic potential--------------------
pair_style      eam
pair_coeff      * * Fe_C.eam.alloy Fe C
neighbor        2.0 bin
neigh_modify    every 1 delay 0 check yes

#-------Define atom mass (no needed for moelcular statics) 
mass            1 55.845
mass            2 12.011

#-------Compute------------------------------------------
compute 	eng all pe/atom
compute 	new all temp
compute 	csym all centro/atom bcc
compute 	poten all pe
compute 	stress all stress/atom NULL

#------Relaxation----------------------------------------
thermo          100
thermo_style    custom step pe lx ly lz pxx pyy pzz pxy pxz pyz press

min_style       cg
minimize        1e-30 10e-12 100000000 1000000000

#-----Calculate the lattice parameter--------------------
variable        tmp equal "lx"
variable        LX equal ${tmp}
variable        tmp equal "ly"
variable        LY equal ${tmp}
variable        tmp equal "lz"
variable        LZ equal ${tmp}
variable        tmp equal "atoms"
variable        N equal ${tmp}
variable        V equal (${LX}*${LY}*${LZ})
variable        alat equal (${LX}+${LY}+${LZ})/3
variable        tmp equal "pe"
variable        pe0 equal ${tmp}
variable        teng equal ${pe0}/${N}
variable        vol  equal ${V}/${N}

#-----output to datafolder-------------------------------- 
print           "${vol} ${teng} ${alat}" append ./volume_diamond.dat

#-----jump to next loop-----------------------------------
next            i
jump            in.eos_C_dia loop_start
</code></pre></div></div>

<h2 id="step-4-compute-the-interstitial-energy-of-carbon">Step 4: Compute the interstitial energy of carbon.</h2>

<p>Calculating Interstitial Energy
Now that we have the system both with and without the carbon atom, we can calculate the interstitial energy.
The interstitial energy is given by:
[
    E_{inter} = E_{Fe+C} - E_{Fe} - E_{C}
]
Where:</p>
<ul>
  <li>$ E_{Fe+C} $ is the energy of the iron lattice with the carbon atom.</li>
  <li>$ E_{Fe} $ is the energy of the pure iron lattice.</li>
  <li>$ E_{C} $ is the energy of an isolated carbon atom.</li>
</ul>

<p>By following this process, you can calculate the interstitial energy of carbon in iron using LAMMPS. This value is useful in understanding how carbon atoms interact with the iron lattice, which is important for studying phenomena like carbon diffusion in steels.
Feel free to modify the script to use different lattice sizes or to insert multiple interstitial atoms. The same approach can be extended to calculate interstitial energies for other elements in different metals.</p>

<p>Happy simulating!</p>]]></content><author><name>Lei Zhang</name></author><category term="Atomistic" /><summary type="html"><![CDATA[interstitial energy]]></summary></entry><entry><title type="html">Comprehensive Introduction to Bash Scripting</title><link href="https://leiapple.github.io/2024/09/30/bash-start/" rel="alternate" type="text/html" title="Comprehensive Introduction to Bash Scripting" /><published>2024-09-30T00:00:00+02:00</published><updated>2024-09-30T00:00:00+02:00</updated><id>https://leiapple.github.io/2024/09/30/bash-start</id><content type="html" xml:base="https://leiapple.github.io/2024/09/30/bash-start/"><![CDATA[<h1 id="comprehensive-introduction-to-bash-scripting">Comprehensive Introduction to Bash Scripting</h1>

<p>Bash (short for <strong>Bourne-Again SHell</strong>) is a powerful command-line shell and scripting language commonly used on Unix-like systems such as Linux and macOS. Bash scripts enable you to automate repetitive tasks, manage system operations, and create complex workflows in an efficient way.</p>

<h2 id="table-of-contents">Table of Contents</h2>
<ol>
  <li><a href="#what-is-bash">What Is Bash?</a></li>
  <li><a href="#creating-and-running-a-bash-script">Creating and Running a Bash Script</a></li>
  <li><a href="#variables-and-command-substitution">Variables and Command Substitution</a></li>
  <li><a href="#user-input-and-command-line-arguments">User Input and Command-Line Arguments</a></li>
  <li><a href="#control-structures">Control Structures</a>
    <ul>
      <li><a href="#if-else-statement">If-Else Statement</a></li>
      <li><a href="#for-loops">For Loops</a></li>
      <li><a href="#while-loops">While Loops</a></li>
      <li><a href="#case-statements">Case Statements</a></li>
    </ul>
  </li>
  <li><a href="#functions">Functions</a></li>
  <li><a href="#working-with-files">Working with Files</a></li>
  <li><a href="#useful-built-in-commands">Useful Built-in Commands</a></li>
  <li><a href="#error-handling-and-debugging">Error Handling and Debugging</a></li>
  <li><a href="#practical-examples">Practical Examples</a></li>
  <li><a href="#best-practices">Best Practices</a></li>
  <li><a href="#further-reading-and-resources">Further Reading and Resources</a></li>
</ol>

<hr />

<h2 id="what-is-bash">What Is Bash?</h2>

<p>Bash is both a command interpreter and a scripting language:</p>
<ul>
  <li><strong>Command interpreter (shell)</strong>: It lets you type and execute commands interactively in a terminal.</li>
  <li><strong>Scripting language</strong>: You can create files (scripts) containing a sequence of commands, which are then run automatically, saving time and reducing errors.</li>
</ul>

<hr />

<h2 id="creating-and-running-a-bash-script">Creating and Running a Bash Script</h2>

<p>1). <strong>Create a new file</strong> (e.g., <code class="language-plaintext highlighter-rouge">myscript.sh</code>) with any text editor (<code class="language-plaintext highlighter-rouge">vi</code>, <code class="language-plaintext highlighter-rouge">nano</code>, <code class="language-plaintext highlighter-rouge">gedit</code>, etc.).
2). <strong>Add the shebang line</strong> at the top of the file to specify the interpreter:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   <span class="c">#!/bin/bash</span>
</code></pre></div></div>
<p>3). Write your script by adding commands you would normally run in the terminal:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c">#!/bin/bash</span>
<span class="nb">echo</span> <span class="s2">"Hello, world!"</span>
</code></pre></div></div>
<p>4). Make the script executable by running:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c">#!/bin/bash</span>
<span class="nb">chmod</span> +x myscript.sh
</code></pre></div></div>
<p>5). Execute the script:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./myscript.sh
</code></pre></div></div>
<p>or</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>bash myscript.sh
</code></pre></div></div>

<h2 id="variables-and-command-substitution">Variables and Command Substitution</h2>

<p>In Bash, you can create and use variables to store information, such as strings, numbers, or the output of commands.</p>

<h3 id="creating-variables">Creating Variables</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
name="Alice"
number=42

echo "My name is $name and my favorite number is $number."
</code></pre></div></div>

<h3 id="command-substitution">Command Substitution</h3>

<p>Command substitution lets you assign the output of a command to a variable:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
current_date=$(date)
echo "Today's date is $current_date."
</code></pre></div></div>
<p>Alternatively, older syntax uses backticks:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>current_date=`date`
</code></pre></div></div>

<h2 id="user-input-and-command-line-arguments">User Input and Command-Line Arguments</h2>

<h3 id="reading-user-input-with-read">Reading User Input with read</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
echo "Please enter your name:"
read user_name
echo "Hello, $user_name!"
</code></pre></div></div>

<h3 id="command-line-arguments">Command-Line Arguments</h3>
<p>$1, $2, $3, etc. represent the arguments passed to the script:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
echo "The first argument is $1"
echo "The second argument is $2"
</code></pre></div></div>
<p>To handle many arguments, you can use $@ or $* to represent all arguments.</p>

<h2 id="control-structures">Control Structures</h2>

<p>Control structures allow you to alter the flow of your Bash scripts.</p>

<h3 id="if-else-statement">If-Else Statement</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
aa=3.14
bb=3.14
c=$(echo "$aa == $bb" | bc)  # compares numerical values

if [[ "$c" -eq 1 ]]; then
  echo "true"
else
  echo "false"
fi
</code></pre></div></div>
<p>String Comparison Example</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
flag_0="DISLOCATION"

if [ "$flag_0" = "DISLOCATION" ]; then
  echo "equal 000"
else
  echo "not equal 000"
fi
</code></pre></div></div>
<p>Common Operators:</p>

<ul>
  <li>-eq, -ne, -gt, -ge, -lt, -le: numeric comparisons</li>
  <li>-z, -n: check if a string is empty or not</li>
  <li>=, !=: string equality or inequality</li>
</ul>

<h3 id="for-loops">For Loops</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
for ((i=1; i&lt;=200; i=i+5))
do
  echo $i
  echo loop.$i.dump
done
</code></pre></div></div>

<p>Another form (iterating over a list):</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
for fruit in apple banana cherry
do
  echo "I like $fruit"
done
</code></pre></div></div>

<h3 id="while-loops">While Loops</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
count=1
while [ $count -le 5 ]
do
  echo "Count is: $count"
  ((count++))
done
</code></pre></div></div>

<h3 id="case-statements">Case Statements</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
echo "Enter a number between 1 and 3:"
read number

case $number in
  1)
    echo "You chose one."
    ;;
  2)
    echo "You chose two."
    ;;
  3)
    echo "You chose three."
    ;;
  *)
    echo "Invalid choice!"
    ;;
esac
</code></pre></div></div>

<h2 id="functions">Functions</h2>

<p>Functions allow you to group related commands for easier reuse and better structure:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash

function greet {
  echo "Hello, $1!"
}

greet "Alice"
greet "Bob"
</code></pre></div></div>
<ul>
  <li>$1, $2, etc., in functions refer to the function’s arguments.</li>
  <li>The return command can return an integer status code, and echo can be used to produce function output.</li>
</ul>

<h2 id="working-with-files">Working with Files</h2>

<h3 id="check-if-a-file-exists-and-is-not-empty">Check if a File Exists and Is Not Empty</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
if [ -s pidfile ]; then
  echo "hi"
else
  echo "empty"
fi
</code></pre></div></div>

<h3 id="creating-appending-and-reading-files">Creating, Appending, and Reading Files</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
# Creating or overwriting a file
echo "Hello" &gt; myfile.txt

# Appending to a file
echo "World" &gt;&gt; myfile.txt

# Reading a file line by line
while IFS= read -r line
do
  echo "Line: $line"
done &lt; myfile.txt
</code></pre></div></div>

<h2 id="useful-built-in-commands">Useful Built-in Commands</h2>
<ul>
  <li>echo: Prints arguments to standard output.</li>
  <li>read: Reads a line of input from standard input.</li>
  <li>cd: Changes the current directory.</li>
  <li>pwd: Prints the current working directory.</li>
  <li>ls, cp, mv, rm: Basic file and directory management commands.</li>
  <li>test or [ ]: Evaluates conditional expressions (used in if statements).</li>
  <li>exit: Exits the script with an optional status code (0 for success).</li>
</ul>

<h2 id="error-handling-and-debugging">Error Handling and Debugging</h2>

<p><code class="language-plaintext highlighter-rouge">set</code>  Options</p>
<ul>
  <li><code class="language-plaintext highlighter-rouge">set -e</code>: Exit the script when any command fails.</li>
  <li><code class="language-plaintext highlighter-rouge">set -u</code>: Treat unset variables as errors.</li>
  <li><code class="language-plaintext highlighter-rouge">set -o pipefail</code>: Return exit code of the last command that failed in a pipeline.
Include them near the top of your script:
    <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
set -euo pipefail
</code></pre></div>    </div>
  </li>
</ul>

<h3 id="using-trap">Using trap</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#!/bin/bash
trap 'echo "An error occurred. Exiting..."' ERR

some_command_that_might_fail
echo "This won't run if the above command fails."
</code></pre></div></div>
<h3 id="debug-mode">Debug Mode</h3>
<ul>
  <li><em>Verbose</em> mode: <code class="language-plaintext highlighter-rouge">bash -v myscript.sh</code></li>
  <li><em>Debug</em> mode: <code class="language-plaintext highlighter-rouge">bash -x myscript.sh</code> (prints each command with its expanded arguments)</li>
</ul>

<h2 id="practical-examples">Practical Examples</h2>

<ol>
  <li>Monitoring Disk Usage
```
#!/bin/bash
threshold=80</li>
</ol>

<p>current_usage=$(df -h / | tail -1 | awk ‘{print $5}’ | sed ‘s/%//’)
if [ $current_usage -gt $threshold ]; then
  echo “Warning: Disk usage is above $threshold%!”
else
  echo “Disk usage is under control at $current_usage%.”
fi</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
2. Backup Script
</code></pre></div></div>
<p>#!/bin/bash
source_dir=”/home/user/documents”
backup_dir=”/home/user/backup”
timestamp=$(date +%Y%m%d%H%M)</p>

<p>mkdir -p “$backup_dir”
cp -r “$source_dir” “${backup_dir}/documents_${timestamp}”</p>

<p>echo “Backup of $source_dir completed at ${timestamp}.”
```</p>
<h2 id="best-practices">Best Practices</h2>

<ol>
  <li>Use Meaningful Names: Give variables and functions descriptive names (count, backup_files, greet_user).</li>
  <li>Comment Your Code: Explain why certain commands are used, especially if they’re not obvious.</li>
  <li>Validate Inputs: Check that arguments and user inputs are valid before using them.</li>
  <li>Quote Variables: Put variables in quotes (“$var”) to prevent globbing and word splitting issues.</li>
  <li>Prefer $() for Command Substitution: It’s more readable than backticks.</li>
  <li>Use set -euo pipefail: Helps catch errors early and makes your script more robust.</li>
  <li>Test Incrementally: Run sections of your script as you build it to catch errors before they spread.</li>
</ol>

<h2 id="further-reading-and-resources">Further Reading and Resources</h2>

<ul>
  <li>Official Bash Manual: <a href="https://www.gnu.org/software/bash/manual/">https://www.gnu.org/software/bash/manual/</a></li>
  <li>Advanced Bash-Scripting Guide: <a href="https://tldp.org/LDP/abs/html/">https://tldp.org/LDP/abs/html/</a></li>
  <li>Linux Command Tutorials: <a href="https://linuxcommand.org/">https://linuxcommand.org/</a></li>
</ul>]]></content><author><name>Lei Zhang</name></author><category term="Linux," /><category term="Bash" /><summary type="html"><![CDATA[A deeper dive into creating and using Bash scripts effectively]]></summary></entry><entry><title type="html">Displacement &amp;amp; stress field of a sharp crack</title><link href="https://leiapple.github.io/2024/09/30/plot-crack-field/" rel="alternate" type="text/html" title="Displacement &amp;amp; stress field of a sharp crack" /><published>2024-09-30T00:00:00+02:00</published><updated>2024-09-30T00:00:00+02:00</updated><id>https://leiapple.github.io/2024/09/30/plot-crack-field</id><content type="html" xml:base="https://leiapple.github.io/2024/09/30/plot-crack-field/"><![CDATA[<h1 id="plotting-the-displacement--stress-field-of-a-sharp-crack-according-to-linear-elastic-fracture-mechanics">Plotting the displacement &amp; stress field of a sharp crack according to linear elastic fracture mechanics</h1>

<p>This blog is purely for educational purposes.
Linear elastic fracture mechanics is of vital importance in engineering applications, especially predicting material failure. 
The readers are redireted to the book of <code class="language-plaintext highlighter-rouge">Fracture : an advanced treatise. Vol. 2, Mathematical fundamentals</code> by H. Liebowitz <em>et al.</em> for the detailed derivation of the elastic fields in anisotropic medium.</p>

<h2 id="the-code">The code</h2>]]></content><author><name>Lei Zhang</name></author><category term="Fracture-Mechanics" /><summary type="html"><![CDATA[Linear ealstic fracture mechanics]]></summary></entry></feed>