utils.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
  6. <meta name="generator" content="pdoc 0.5.2" />
  7. <title>analysis.utils API documentation</title>
  8. <meta name="description" content="" />
  9. <link href='https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.0/normalize.min.css' rel='stylesheet'>
  10. <link href='https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/8.0.0/sanitize.min.css' rel='stylesheet'>
  11. <link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" rel="stylesheet">
  12. <style>.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{font-weight:bold}#index h4 + ul{margin-bottom:.6em}#index .two-column{column-count:2}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.name small{font-weight:normal}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase;cursor:pointer}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}.admonition{padding:.1em .5em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
  13. <style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
  14. <style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
  15. </head>
  16. <body>
  17. <main>
  18. <article id="content">
  19. <header>
  20. <h1 class="title"><code>analysis.utils</code> module</h1>
  21. </header>
  22. <section id="section-intro">
  23. <details class="source">
  24. <summary>Source code</summary>
  25. <pre><code class="python">import pickle
  26. import numpy as np
  27. def loadData(fileName, timestep, absolute=False):
  28. &#34;&#34;&#34;Load the data in data/fileName/ at the given timestep.
  29. Parameters:
  30. fileName (string): data is loaded from data/fileName/
  31. timestep (integer): timestep at which to load the data
  32. absolute (bool): true to load relative to parent folder.
  33. Used for interactive jupyter notebook.
  34. Returns:
  35. data instance that was saved by Simulation.save
  36. &#34;&#34;&#34;
  37. prepend = &#39;./..&#39; if absolute else &#39;./&#39;
  38. return pickle.load(open(prepend+&#39;/data/{}/data{}.pickle&#39;.format(fileName, timestep), &#34;rb&#34; ))
  39. def calculateEccentricity(M, ref_r, ref_v, r, v):
  40. &#34;&#34;&#34;Calculates the orbital eccentricity of a list of particles.
  41. Parameters:
  42. M (string): mass of the central object
  43. ref_r (array): reference position of the central object
  44. ref_v (array): reference velocity of the central object
  45. r (array): list of positions of all particles (n particles, 3)
  46. v (array): list of velocities of all particles (n particles, 3)
  47. Returns:
  48. Array of shape (n particles, 3) with the eccentricities of the orbits
  49. &#34;&#34;&#34;
  50. v1 = v - ref_v
  51. r1 = r - ref_r
  52. h_vec = np.cross(r1, v1)
  53. h = np.linalg.norm(h_vec, axis=-1)
  54. # Make use of $\vec{e} = (v \times (r \times v)) / M$, and e = |\vec{e}|
  55. e_vec = np.cross(v1, h_vec)/M - r1/np.linalg.norm(r1, axis=-1, keepdims=True)
  56. e = np.linalg.norm(e_vec, axis=-1)
  57. return e
  58. ############################# PLOTTING UTILS ########################
  59. # Matplotlib can be tedious at times. These short functions make the
  60. # repetitive parts simpler and ensure consistency.
  61. def plotCOM(ax):
  62. &#34;&#34;&#34;Plots cross at (0,0)&#34;&#34;&#34;
  63. ax.scatter([0],[0],marker=&#39;+&#39;, c=&#39;black&#39;, s=500,
  64. alpha=.5, linewidth=1, zorder=-1)
  65. def plotCenterMasses(ax, data):
  66. &#34;&#34;&#34;Plots stars at the position of the central masses&#34;&#34;&#34;
  67. ax.scatter(data[&#39;r_vec&#39;][data[&#39;type&#39;][:,0]==&#39;center&#39;][:,0],
  68. data[&#39;r_vec&#39;][data[&#39;type&#39;][:,0]==&#39;center&#39;][:,1],
  69. s=100, marker=&#34;*&#34;, c=&#39;black&#39;, alpha=.7)
  70. def plotTracks(ax, tracks):
  71. &#34;&#34;&#34;Plots the tracks of the central masses&#34;&#34;&#34;
  72. for track in tracks:
  73. ax.plot(track[:,0], track[:,1],
  74. c=&#39;black&#39;, alpha=1.0, linewidth=1)
  75. def setSize(ax, x=None, y=None, mode=None):
  76. &#34;&#34;&#34;Sets the size of the plot. If mode=&#39;square&#39;, the x and y axis
  77. will have the same scale.&#34;&#34;&#34;
  78. if mode==&#39;square&#39;: ax.axis(&#39;square&#39;)
  79. if x is not None: ax.set_xlim(x)
  80. if y is not None: ax.set_ylim(y)
  81. def setAxes(ax, x=None, y=None, xcoords=None, ycoords=None, mode=None):
  82. &#34;&#34;&#34;&#34;Sets the axis labels (x, y) and their position (None). The mode
  83. keyword can be used to hide all the axes (&#39;hide&#39;), only plot the bottom
  84. axis (&#39;bottom&#39;) or only plot the bottom and left axes (&#39;bottomleft&#39;)&#34;&#34;&#34;
  85. if x is not None: ax.set_xlabel(x)
  86. if y is not None: ax.set_ylabel(y)
  87. if mode==&#39;hide&#39;:
  88. ax.spines[&#39;right&#39;].set_visible(False)
  89. ax.spines[&#39;top&#39;].set_visible(False)
  90. ax.spines[&#39;left&#39;].set_visible(False)
  91. ax.spines[&#39;bottom&#39;].set_visible(False)
  92. ax.set_xticklabels([])
  93. ax.set_xticks([])
  94. ax.set_yticklabels([])
  95. ax.set_yticks([])
  96. elif mode==&#39;bottomleft&#39;:
  97. ax.spines[&#39;right&#39;].set_visible(False)
  98. ax.spines[&#39;top&#39;].set_visible(False)
  99. elif mode==&#39;bottom&#39;:
  100. ax.get_yaxis().set_ticks([])
  101. ax.set_ylabel(&#39;&#39;)
  102. ax.spines[&#39;left&#39;].set_visible(False)
  103. ax.spines[&#39;right&#39;].set_visible(False)
  104. ax.spines[&#39;top&#39;].set_visible(False)
  105. if xcoords is not None: ax.xaxis.set_label_coords(*xcoords)
  106. if ycoords is not None: ax.yaxis.set_label_coords(*ycoords)
  107. def stylizePlot(axs):
  108. &#34;&#34;&#34;Adds ticks (a la root style) for prettier plots&#34;&#34;&#34;
  109. for ax in axs:
  110. ax.tick_params(axis=&#39;both&#39;, which=&#39;both&#39;, direction=&#39;in&#39;, top=True, right=True)
  111. ax.minorticks_on()</code></pre>
  112. </details>
  113. </section>
  114. <section>
  115. </section>
  116. <section>
  117. </section>
  118. <section>
  119. <h2 class="section-title" id="header-functions">Functions</h2>
  120. <dl>
  121. <dt id="analysis.utils.calculateEccentricity"><code class="name flex">
  122. <span>def <span class="ident">calculateEccentricity</span></span>(<span>M, ref_r, ref_v, r, v)</span>
  123. </code></dt>
  124. <dd>
  125. <section class="desc"><p>Calculates the orbital eccentricity of a list of particles.</p>
  126. <h2 id="parameters">Parameters</h2>
  127. <dl>
  128. <dt><strong><code>M</code></strong> :&ensp;<code>string</code></dt>
  129. <dd>mass of the central object</dd>
  130. <dt><strong><code>ref_r</code></strong> :&ensp;<code>array</code></dt>
  131. <dd>reference position of the central object</dd>
  132. <dt><strong><code>ref_v</code></strong> :&ensp;<code>array</code></dt>
  133. <dd>reference velocity of the central object</dd>
  134. <dt><strong><code>r</code></strong> :&ensp;<code>array</code></dt>
  135. <dd>list of positions of all particles (n particles, 3)</dd>
  136. <dt><strong><code>v</code></strong> :&ensp;<code>array</code></dt>
  137. <dd>list of velocities of all particles (n particles, 3)</dd>
  138. </dl>
  139. <h2 id="returns">Returns</h2>
  140. <p>Array of shape (n particles, 3) with the eccentricities of the orbits</p></section>
  141. <details class="source">
  142. <summary>Source code</summary>
  143. <pre><code class="python">def calculateEccentricity(M, ref_r, ref_v, r, v):
  144. &#34;&#34;&#34;Calculates the orbital eccentricity of a list of particles.
  145. Parameters:
  146. M (string): mass of the central object
  147. ref_r (array): reference position of the central object
  148. ref_v (array): reference velocity of the central object
  149. r (array): list of positions of all particles (n particles, 3)
  150. v (array): list of velocities of all particles (n particles, 3)
  151. Returns:
  152. Array of shape (n particles, 3) with the eccentricities of the orbits
  153. &#34;&#34;&#34;
  154. v1 = v - ref_v
  155. r1 = r - ref_r
  156. h_vec = np.cross(r1, v1)
  157. h = np.linalg.norm(h_vec, axis=-1)
  158. # Make use of $\vec{e} = (v \times (r \times v)) / M$, and e = |\vec{e}|
  159. e_vec = np.cross(v1, h_vec)/M - r1/np.linalg.norm(r1, axis=-1, keepdims=True)
  160. e = np.linalg.norm(e_vec, axis=-1)
  161. return e</code></pre>
  162. </details>
  163. </dd>
  164. <dt id="analysis.utils.loadData"><code class="name flex">
  165. <span>def <span class="ident">loadData</span></span>(<span>fileName, timestep, absolute=False)</span>
  166. </code></dt>
  167. <dd>
  168. <section class="desc"><p>Load the data in data/fileName/ at the given timestep.</p>
  169. <h2 id="parameters">Parameters</h2>
  170. <dl>
  171. <dt><strong><code>fileName</code></strong> :&ensp;<code>string</code></dt>
  172. <dd>data is loaded from data/fileName/</dd>
  173. <dt><strong><code>timestep</code></strong> :&ensp;<code>integer</code></dt>
  174. <dd>timestep at which to load the data</dd>
  175. <dt><strong><code>absolute</code></strong> :&ensp;<code>bool</code></dt>
  176. <dd>true to load relative to parent folder.
  177. Used for interactive jupyter notebook.</dd>
  178. </dl>
  179. <h2 id="returns">Returns</h2>
  180. <p>data instance that was saved by Simulation.save</p></section>
  181. <details class="source">
  182. <summary>Source code</summary>
  183. <pre><code class="python">def loadData(fileName, timestep, absolute=False):
  184. &#34;&#34;&#34;Load the data in data/fileName/ at the given timestep.
  185. Parameters:
  186. fileName (string): data is loaded from data/fileName/
  187. timestep (integer): timestep at which to load the data
  188. absolute (bool): true to load relative to parent folder.
  189. Used for interactive jupyter notebook.
  190. Returns:
  191. data instance that was saved by Simulation.save
  192. &#34;&#34;&#34;
  193. prepend = &#39;./..&#39; if absolute else &#39;./&#39;
  194. return pickle.load(open(prepend+&#39;/data/{}/data{}.pickle&#39;.format(fileName, timestep), &#34;rb&#34; ))</code></pre>
  195. </details>
  196. </dd>
  197. <dt id="analysis.utils.plotCOM"><code class="name flex">
  198. <span>def <span class="ident">plotCOM</span></span>(<span>ax)</span>
  199. </code></dt>
  200. <dd>
  201. <section class="desc"><p>Plots cross at (0,0)</p></section>
  202. <details class="source">
  203. <summary>Source code</summary>
  204. <pre><code class="python">def plotCOM(ax):
  205. &#34;&#34;&#34;Plots cross at (0,0)&#34;&#34;&#34;
  206. ax.scatter([0],[0],marker=&#39;+&#39;, c=&#39;black&#39;, s=500,
  207. alpha=.5, linewidth=1, zorder=-1)</code></pre>
  208. </details>
  209. </dd>
  210. <dt id="analysis.utils.plotCenterMasses"><code class="name flex">
  211. <span>def <span class="ident">plotCenterMasses</span></span>(<span>ax, data)</span>
  212. </code></dt>
  213. <dd>
  214. <section class="desc"><p>Plots stars at the position of the central masses</p></section>
  215. <details class="source">
  216. <summary>Source code</summary>
  217. <pre><code class="python">def plotCenterMasses(ax, data):
  218. &#34;&#34;&#34;Plots stars at the position of the central masses&#34;&#34;&#34;
  219. ax.scatter(data[&#39;r_vec&#39;][data[&#39;type&#39;][:,0]==&#39;center&#39;][:,0],
  220. data[&#39;r_vec&#39;][data[&#39;type&#39;][:,0]==&#39;center&#39;][:,1],
  221. s=100, marker=&#34;*&#34;, c=&#39;black&#39;, alpha=.7)</code></pre>
  222. </details>
  223. </dd>
  224. <dt id="analysis.utils.plotTracks"><code class="name flex">
  225. <span>def <span class="ident">plotTracks</span></span>(<span>ax, tracks)</span>
  226. </code></dt>
  227. <dd>
  228. <section class="desc"><p>Plots the tracks of the central masses</p></section>
  229. <details class="source">
  230. <summary>Source code</summary>
  231. <pre><code class="python">def plotTracks(ax, tracks):
  232. &#34;&#34;&#34;Plots the tracks of the central masses&#34;&#34;&#34;
  233. for track in tracks:
  234. ax.plot(track[:,0], track[:,1],
  235. c=&#39;black&#39;, alpha=1.0, linewidth=1)</code></pre>
  236. </details>
  237. </dd>
  238. <dt id="analysis.utils.setAxes"><code class="name flex">
  239. <span>def <span class="ident">setAxes</span></span>(<span>ax, x=None, y=None, xcoords=None, ycoords=None, mode=None)</span>
  240. </code></dt>
  241. <dd>
  242. <section class="desc"><p>"Sets the axis labels (x, y) and their position (None). The mode
  243. keyword can be used to hide all the axes ('hide'), only plot the bottom
  244. axis ('bottom') or only plot the bottom and left axes ('bottomleft')</p></section>
  245. <details class="source">
  246. <summary>Source code</summary>
  247. <pre><code class="python">def setAxes(ax, x=None, y=None, xcoords=None, ycoords=None, mode=None):
  248. &#34;&#34;&#34;&#34;Sets the axis labels (x, y) and their position (None). The mode
  249. keyword can be used to hide all the axes (&#39;hide&#39;), only plot the bottom
  250. axis (&#39;bottom&#39;) or only plot the bottom and left axes (&#39;bottomleft&#39;)&#34;&#34;&#34;
  251. if x is not None: ax.set_xlabel(x)
  252. if y is not None: ax.set_ylabel(y)
  253. if mode==&#39;hide&#39;:
  254. ax.spines[&#39;right&#39;].set_visible(False)
  255. ax.spines[&#39;top&#39;].set_visible(False)
  256. ax.spines[&#39;left&#39;].set_visible(False)
  257. ax.spines[&#39;bottom&#39;].set_visible(False)
  258. ax.set_xticklabels([])
  259. ax.set_xticks([])
  260. ax.set_yticklabels([])
  261. ax.set_yticks([])
  262. elif mode==&#39;bottomleft&#39;:
  263. ax.spines[&#39;right&#39;].set_visible(False)
  264. ax.spines[&#39;top&#39;].set_visible(False)
  265. elif mode==&#39;bottom&#39;:
  266. ax.get_yaxis().set_ticks([])
  267. ax.set_ylabel(&#39;&#39;)
  268. ax.spines[&#39;left&#39;].set_visible(False)
  269. ax.spines[&#39;right&#39;].set_visible(False)
  270. ax.spines[&#39;top&#39;].set_visible(False)
  271. if xcoords is not None: ax.xaxis.set_label_coords(*xcoords)
  272. if ycoords is not None: ax.yaxis.set_label_coords(*ycoords)</code></pre>
  273. </details>
  274. </dd>
  275. <dt id="analysis.utils.setSize"><code class="name flex">
  276. <span>def <span class="ident">setSize</span></span>(<span>ax, x=None, y=None, mode=None)</span>
  277. </code></dt>
  278. <dd>
  279. <section class="desc"><p>Sets the size of the plot. If mode='square', the x and y axis
  280. will have the same scale.</p></section>
  281. <details class="source">
  282. <summary>Source code</summary>
  283. <pre><code class="python">def setSize(ax, x=None, y=None, mode=None):
  284. &#34;&#34;&#34;Sets the size of the plot. If mode=&#39;square&#39;, the x and y axis
  285. will have the same scale.&#34;&#34;&#34;
  286. if mode==&#39;square&#39;: ax.axis(&#39;square&#39;)
  287. if x is not None: ax.set_xlim(x)
  288. if y is not None: ax.set_ylim(y)</code></pre>
  289. </details>
  290. </dd>
  291. <dt id="analysis.utils.stylizePlot"><code class="name flex">
  292. <span>def <span class="ident">stylizePlot</span></span>(<span>axs)</span>
  293. </code></dt>
  294. <dd>
  295. <section class="desc"><p>Adds ticks (a la root style) for prettier plots</p></section>
  296. <details class="source">
  297. <summary>Source code</summary>
  298. <pre><code class="python">def stylizePlot(axs):
  299. &#34;&#34;&#34;Adds ticks (a la root style) for prettier plots&#34;&#34;&#34;
  300. for ax in axs:
  301. ax.tick_params(axis=&#39;both&#39;, which=&#39;both&#39;, direction=&#39;in&#39;, top=True, right=True)
  302. ax.minorticks_on()</code></pre>
  303. </details>
  304. </dd>
  305. </dl>
  306. </section>
  307. <section>
  308. </section>
  309. </article>
  310. <nav id="sidebar">
  311. <h1>Index</h1>
  312. <div class="toc">
  313. <ul></ul>
  314. </div>
  315. <ul id="index">
  316. <li><h3><a href="#header-functions">Functions</a></h3>
  317. <ul class="">
  318. <li><code><a title="analysis.utils.calculateEccentricity" href="#analysis.utils.calculateEccentricity">calculateEccentricity</a></code></li>
  319. <li><code><a title="analysis.utils.loadData" href="#analysis.utils.loadData">loadData</a></code></li>
  320. <li><code><a title="analysis.utils.plotCOM" href="#analysis.utils.plotCOM">plotCOM</a></code></li>
  321. <li><code><a title="analysis.utils.plotCenterMasses" href="#analysis.utils.plotCenterMasses">plotCenterMasses</a></code></li>
  322. <li><code><a title="analysis.utils.plotTracks" href="#analysis.utils.plotTracks">plotTracks</a></code></li>
  323. <li><code><a title="analysis.utils.setAxes" href="#analysis.utils.setAxes">setAxes</a></code></li>
  324. <li><code><a title="analysis.utils.setSize" href="#analysis.utils.setSize">setSize</a></code></li>
  325. <li><code><a title="analysis.utils.stylizePlot" href="#analysis.utils.stylizePlot">stylizePlot</a></code></li>
  326. </ul>
  327. </li>
  328. </ul>
  329. </nav>
  330. </main>
  331. <footer id="footer">
  332. <p>Generated by <a href="https://pdoc3.github.io/pdoc"><cite>pdoc</cite> 0.5.2</a>.</p>
  333. </footer>
  334. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  335. <script>hljs.initHighlightingOnLoad()</script>
  336. </body>
  337. </html>