Skip to content

Commit 77b1dd1

Browse files
committed
Deploying to gh-pages from @ 25db310 🚀
1 parent ac63590 commit 77b1dd1

8 files changed

Lines changed: 428 additions & 3 deletions

File tree

‎404.html‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
<a href="/pages/software">Software</a>
5252
</li>
5353

54+
<li>
55+
<a href="/pages/blog">Blog</a>
56+
</li>
57+
5458
</div>
5559
<div id="hidden" class="mobile-nav"><li>
5660
<a href="/pages/aboutMe">About Me</a>
@@ -60,6 +64,10 @@
6064
<a href="/pages/software">Software</a>
6165
</li>
6266

67+
<li>
68+
<a href="/pages/blog">Blog</a>
69+
</li>
70+
6371
</div>
6472
</nav>
6573
</div>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "./"
5+
}
6+
],
7+
"settings": {
8+
"editor.rulers": [80, 100]
9+
}
10+
}

‎index.html‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
<a href="/pages/software">Software</a>
5454
</li>
5555

56+
<li>
57+
<a href="/pages/blog">Blog</a>
58+
</li>
59+
5660
</div>
5761
<div id="hidden" class="mobile-nav"><li>
5862
<a href="/pages/aboutMe">About Me</a>
@@ -62,6 +66,10 @@
6266
<a href="/pages/software">Software</a>
6367
</li>
6468

69+
<li>
70+
<a href="/pages/blog">Blog</a>
71+
</li>
72+
6573
</div>
6674
</nav>
6775
</div>

‎pages/aboutMe/index.html‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
<a href="/pages/software">Software</a>
5252
</li>
5353

54+
<li>
55+
<a href="/pages/blog">Blog</a>
56+
</li>
57+
5458
</div>
5559
<div id="hidden" class="mobile-nav"><li>
5660
<a href="/pages/aboutMe">About Me</a>
@@ -60,6 +64,10 @@
6064
<a href="/pages/software">Software</a>
6165
</li>
6266

67+
<li>
68+
<a href="/pages/blog">Blog</a>
69+
</li>
70+
6371
</div>
6472
</nav>
6573
</div>
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<!doctype html>
2+
<html lang="en" data-theme="light">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
7+
<link rel="stylesheet" href="/libs/katex/katex.min.css">
8+
<link rel="stylesheet" href="/libs/highlight/styles/github.min.css">
9+
10+
11+
<link rel="stylesheet" href="/css/franklin.css" />
12+
<link rel="stylesheet" href="/css/icons.css" />
13+
<link rel="icon" href="/assets/rndimg.jpg" />
14+
15+
<!-- Font Awesome Icons -->
16+
<link
17+
rel="stylesheet"
18+
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css"
19+
/>
20+
21+
22+
<link rel="stylesheet" href="/css/nav.css" />
23+
<link rel="stylesheet" href="/css/sidebar.css" />
24+
<link rel="stylesheet" href="/css/cards.css" />
25+
<link rel="stylesheet" href="/css/showcase.css" />
26+
<link rel="stylesheet" href="/css/software.css" />
27+
<link rel="stylesheet" href="/css/posts.css" />
28+
29+
30+
<title>Your Title</title>
31+
</head>
32+
<body>
33+
34+
35+
<div class="masthead">
36+
<nav>
37+
<div class="author-name-nav">
38+
<li><a href="/">Brian C. Ferrari</a></li>
39+
</div>
40+
<div class="theme-switch">
41+
<button id="theme-toggle" onclick="toggleTheme()">
42+
<i class="fas fa-moon"></i>
43+
</button>
44+
</div>
45+
<a class="burger-nav" onclick="myFunction()">
46+
<i class="fa-2x fa fa-bars"></i>
47+
</a>
48+
<div class="desktop-nav"><li>
49+
<a href="/pages/aboutMe">About Me</a>
50+
</li>
51+
52+
<li>
53+
<a href="/pages/software">Software</a>
54+
</li>
55+
56+
<li>
57+
<a href="/pages/blog">Blog</a>
58+
</li>
59+
60+
</div>
61+
<div id="hidden" class="mobile-nav"><li>
62+
<a href="/pages/aboutMe">About Me</a>
63+
</li>
64+
65+
<li>
66+
<a href="/pages/software">Software</a>
67+
</li>
68+
69+
<li>
70+
<a href="/pages/blog">Blog</a>
71+
</li>
72+
73+
</div>
74+
</nav>
75+
</div>
76+
77+
<div class="academic-sidebar">
78+
<div class="author_avatar"><img
79+
src="/assets/figs/me.jpg"
80+
class="author_avatar"
81+
alt="Brian C. Ferrari"
82+
/></div>
83+
84+
<h3>Brian C. Ferrari</h3>
85+
86+
<!-- Pronouns are a class -->
87+
<p class="pronouns">
88+
(he/him)
89+
</p>
90+
91+
<!-- Bio is just text -->
92+
Chemistry PhD Candidate at Leiden University
93+
<br /><br />
94+
95+
<br><br>
96+
<li>
97+
<a href="https://www.linkedin.com/in/brian-ferrari-001b8a163/" target=_blank>
98+
<i class="fa fa-brands fa-linkedin" aria-hidden="true"></i>
99+
</a>
100+
</li>
101+
<li>
102+
<a href="https://orcid.org/0000-0002-7416-8629" target=_blank>
103+
<i class="fa fa-brands fa-orcid" aria-hidden="true"></i>
104+
</a>
105+
</li>
106+
<li>
107+
<a href="https://www.researchgate.net/profile/Brian-Ferrari-2?ev=hdr_xprf" target=_blank>
108+
<i class="fa fa-brands fa-researchgate" aria-hidden="true"></i>
109+
</a>
110+
</li>
111+
<li>
112+
<a href="https://github.com/Cavenfish" target=_blank>
113+
<i class="fa fa-brands fa-github" aria-hidden="true"></i>
114+
</a>
115+
</li>
116+
<li>
117+
<a href="https://scholar.google.com/citations?user=col48NcAAAAJ&hl=en" target=_blank>
118+
<i class="fa fa-graduation-cap" aria-hidden="true"></i>
119+
</a>
120+
</li>
121+
122+
</div>
123+
124+
125+
<!-- Content appended here -->
126+
<div class="franklin-content">
127+
<h1 id="canonical_velocity_rescaling_thermostat"><a href="#canonical_velocity_rescaling_thermostat" class="header-anchor">Canonical Velocity Rescaling Thermostat</a></h1>
128+
<p>This thermostat is the most widely used thermostat for molecular dynamics &#40;MD&#41; simulations. The <a href="https://pubs.aip.org/aip/jcp/article-abstract/126/1/014101/186581/Canonical-sampling-through-velocity-rescaling?redirectedFrom&#61;fulltext">manuscript</a> it was published in is one of the highest cited papers related to MD simulations. Due to the massive success of this thermostat there is a wide array of resources explaining the thermostat and the math behind it in simple terms. However, none of these resources show how to implement the thermostat in a programming language. Making the source code of MD packages the only option, but they are often very difficult to follow. Since I&#39;ve implemented this thermostat in Julia for my MD package &#40;<a href="https://github.com/Cavenfish/YetAnotherSimulationSuite.jl">YASS</a>&#41;, I wanted to put out a short explanation of implementing it. </p>
129+
<h2 id="brief_math_explanation"><a href="#brief_math_explanation" class="header-anchor">Brief Math Explanation</a></h2>
130+
<p>The temperature of a system is related to the total kineteic energy of a system by,</p>
131+
\(
132+
K = \frac{1}{2} k_B N_f T
133+
\)
134+
<p>where \(K\) is the kinetic energy, \(k_B\) is Boltzmann constant, \(N_f\) is the degrees of freedom, and \(T\) is the temperature. A velocity rescaling thermostat takes advantage of this experssion by scaling the velocities to increase or decrease the temperature of the system. Typically through a scaling factor &#40;\(\alpha\)&#41;,</p>
135+
\(
136+
\alpha = \sqrt{\frac{K_\text{target}}{K\text{current}}}
137+
\)
138+
<p>where \(K_\text{target}\) is the target kinetic energy, and \(K\text{current}\) is the kinetic energy of the current timeframe of the simulation. The CVR thermostat enforces a canonical sampling for \(K_\text{target}\) to ensure proper thermodynamic proporties in the simulation. The explicit derivation of this can be found in the original manuscript, or even other online resources. For the sake of just learning to implement the thermostat only these two concepts are necessary.</p>
139+
<h2 id="coding_it_up"><a href="#coding_it_up" class="header-anchor">Coding it Up</a></h2>
140+
<p>We first need a function that calculates the temperature of our system from the kinetic energy. We can calculate this by taking \(\sum_i m_i v_i^2 = k_B N_f T\), where the \(\frac{1}{2}\) is dropped from both sides.</p>
141+
<pre><code class="language-julia">function getTemp&#40;v::AbstractVector, m::AbstractVector, kB::Float64&#41;
142+
# Leave out the 1/2 to get 2Ekin for T calc
143+
N &#61; length&#40;m&#41;
144+
Nf &#61; 3N - 3
145+
v2 &#61; &#91;i&#39;i for i in v&#93;
146+
Ekin &#61; sum&#40;m .* v2&#41;
147+
148+
Ekin / &#40;kB * Nf&#41;
149+
end</code></pre>
150+
<p>The next thing we need is a function for sampling from a normal and gamma distributions. You could implement this from scratch but its better to use the implementations in <a href="https://juliastats.org/Distributions.jl/stable/">Distributions.jl</a> since they are well tested and highly performant. These two functions are <code>Normal</code> and <code>Gamma</code> in the code blocks below.</p>
151+
<p>The CVR thermostat needs to have non-zero velocities to scale &#40;or else you still have zero after scaling&#41;, so if your system has all zero velocities &#40;or \(T=0\)&#41; then you skip apply the thermostat and let the system gain some amount of kinetic energy due to the potential. This means adding a conditional return in your CVR function to avoid wasteful computations. In this example I use a short circuit expression &#40;which I love but some hate&#41;, so I&#39;ll quickly explain them. These expressions take advantage of the and/or logic by using the <code>&amp;&amp;</code> and <code>||</code> operators to write compact if-then statements. </p>
152+
<pre><code class="language-julia"># This shows a typical if statment within a for loop
153+
for i &#61; 1:100
154+
if i &#37; 10 &#61;&#61; 0
155+
println&#40;i&#41;
156+
end
157+
end
158+
159+
# This does the same thing but using a short circuit expression
160+
for i &#61; 1:100
161+
i &#37; 10 &#61;&#61; 0 &amp;&amp; println&#40;i&#41;
162+
end
163+
164+
# Again, the same thing but using ||
165+
for i &#61; 1:100
166+
i &#37; 10 &#33;&#61; 0 || println&#40;i&#41;
167+
end</code></pre>
168+
<p>Getting back to the CVR thermostat, once we have a non-zero kinetic energy we can rescale the velocities to try to reach our desired temperature.</p>
169+
<pre><code class="language-julia">function CVR&#33;&#40;
170+
v::AbstractVector, m::AbstractVector,
171+
kB::Float64, tau::Float64, Ttar::Float64
172+
&#41;
173+
N &#61; length&#40;m&#41;
174+
Tsim &#61; getTemp&#40;v, m, kB&#41;
175+
176+
Tsim &#61;&#61; 0.0 &amp;&amp; return
177+
178+
Nf &#61; 3N - 3
179+
n &#61; Normal&#40;0.0,1.0&#41;
180+
R1 &#61; rand&#40;n&#41;
181+
182+
R2 &#61; if &#40;Nf-1&#41;&#37;2 &#61;&#61; 0
183+
alpha &#61; &#40;Nf - 1&#41;/2
184+
G &#61; Gamma&#40;alpha, 1&#41;
185+
186+
2 .* rand&#40;G&#41;
187+
else
188+
alpha &#61; &#40;Nf - 2&#41;/2
189+
G &#61; Gamma&#40;alpha, 1&#41;
190+
191+
2 .* rand&#40;G&#41; .&#43; &#40;rand&#40;n&#41; .^2&#41;
192+
end
193+
194+
tau &gt; 0.1 ? c1 &#61; exp&#40;-1/tau&#41; : c1 &#61; 0.0
195+
196+
K &#61; 0.5 * Nf * kB * Tsim
197+
sigma &#61; 0.5 * Nf * kB * Ttar
198+
199+
Knew &#61; @. &#40;
200+
K &#43; &#40;1-c1&#41; * &#40;sigma * &#40;R2 &#43; R1^2&#41; / Nf - K&#41; &#43;
201+
2 * R1 * sqrt&#40;K * sigma / Nf * &#40;1-c1&#41; * c1&#41;
202+
&#41;
203+
alpha2 &#61; @. sqrt&#40;Knew / K&#41;
204+
205+
@. v *&#61; alpha2
206+
end</code></pre>
207+
<div class="page-foot">
208+
Website built with <a href="https://franklinjl.org/">Franklin.jl</a>
209+
</div>
210+
</div><!-- CONTENT ENDS HERE -->
211+
212+
<script src="/libs/katex/katex.min.js"></script>
213+
<script src="/libs/katex/contrib/auto-render.min.js"></script>
214+
<script>renderMathInElement(document.body)</script>
215+
216+
217+
218+
<script src="/libs/highlight/highlight.min.js"></script>
219+
<script>hljs.highlightAll();hljs.configure({tabReplace: ' '});</script>
220+
221+
222+
<script src="/libs/buttons/nav.js"></script>
223+
<script src="/libs/buttons/theme.js"></script>
224+
</body>
225+
</html>

0 commit comments

Comments
 (0)