<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mustafa Bhotvawala on Medium]]></title>
        <description><![CDATA[Stories by Mustafa Bhotvawala on Medium]]></description>
        <link>https://medium.com/@mustafaabbs2?source=rss-7d332bb51b78------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*xsFHG_OIpTzi1I8-</url>
            <title>Stories by Mustafa Bhotvawala on Medium</title>
            <link>https://medium.com/@mustafaabbs2?source=rss-7d332bb51b78------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 14:28:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mustafaabbs2/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Underrelaxation in simpleFOAM]]></title>
            <link>https://medium.com/@mustafaabbs2/notes-on-underrelaxation-in-simple-69567eae864b?source=rss-7d332bb51b78------2</link>
            <guid isPermaLink="false">https://medium.com/p/69567eae864b</guid>
            <category><![CDATA[cfd]]></category>
            <category><![CDATA[openfoam]]></category>
            <dc:creator><![CDATA[Mustafa Bhotvawala]]></dc:creator>
            <pubDate>Mon, 27 Jul 2020 17:55:33 GMT</pubDate>
            <atom:updated>2020-07-28T04:31:19.139Z</atom:updated>
            <content:encoded><![CDATA[<p>Consider a simple time derivative at a point P:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*GIZ4NNmDecrH0OH06Ba6cQ.png" /></figure><p>The momentum equation, which then is used to construct the coefficient matrix of u_p is discretized using neighbouring cell velocities as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/363/1*seGUGsXl-zWQGTYUHgQlQg.png" /></figure><p>The large time steps Δt in a SIMPLE algorithm effectively reduce the time derivative to very small values. The time derivative contributes very directly to the diagonal of a matrix, since the time derivative does not require any neighbouring points, atleast in the way the discretization is shown above. The time derivative is responsible for the <strong>diagonal dominance</strong> of the matrix, which contributes heavily to the stability of the solution. How does SIMPLE get around not having significant time derivative? — <strong>underrelaxation</strong>.</p><p>You might have heard of iteration schemes with “over-relaxation”. Underrelaxation in SIMPLE is an unrelated concept. It is a way to artificially amplify the diagonal coefficient a_p to achieve diagonal dominance.</p><p>The following term (with 1- α) is added to both sides of the discretization equation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*JSH6Rdh8QuHgPqlEe6KsFQ.png" /></figure><p>You may ask, is it really the same? For a converged solution, the terms <strong>are</strong> indeed the same, and they cancel out. Underrelaxation needs to take effect for a non-converged solution. Grouping the terms:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*3VACUqDLnWZlqceSH8iSeg.png" /></figure><p>The diagonal coefficient is being effectively ampified via a 1/α multiplier, where α &lt; 1</p><p>Lets check the relax function invoked in <strong>UEqn.relax()</strong></p><pre>forAll(D, celli)</pre><pre>{<br>D[celli] = max(mag(D[celli ]), sumOff[celli ]);}</pre><pre>D /= alpha;</pre><p>D is the diagonal matrix, or the a_p coefficient. Diagonal dominance is first checked when compared to the off-diagonal elements sumOff. The central coefficient is then multiplied by the 1/α multiplier.</p><p>This mechanism for stability allows SIMPLE to use the “t=1 second” timestep that we are familiar with. Underrelaxation is not applied to PISO algorithms (with Courant Number &lt;1).</p><p>For reference, you set your underrelaxaton factors in fvSolutions</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DFdvIG8jZjY9YUeT__1qlw.png" /></figure><p><strong>References:</strong></p><ol><li>Jasak, Hrvoje. “Error analysis and estimation for the finite volume method with applications to fluid flows.” (1996)</li><li>Openfoamwiki</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69567eae864b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing Memory in OpenFOAM]]></title>
            <link>https://medium.com/@mustafaabbs2/managing-memory-in-openfoam-b5234232750b?source=rss-7d332bb51b78------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5234232750b</guid>
            <category><![CDATA[openfoam]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[cae]]></category>
            <category><![CDATA[cfd]]></category>
            <dc:creator><![CDATA[Mustafa Bhotvawala]]></dc:creator>
            <pubDate>Mon, 13 Jul 2020 13:25:20 GMT</pubDate>
            <atom:updated>2020-07-13T13:25:20.117Z</atom:updated>
            <content:encoded><![CDATA[<p>These are some notes on the components of the memory management system in OpenFOAM. I would expect to find these building blocks in any code that involves large chunks of data that you cannot possibly keep copying back and forth. The two components that OpenFOAM uses are variations of smart pointers, autoPtr and tmp. If you’ve programmed in C++ before, the OpenFOAM autoPtrhas a closest counterpart in std::unique_ptr , whereas tmp would be a variation of std::shared_ptr . This article deals with tmp, I’ll use the next one in this series for autoPtr.</p><p>This is where you might see tmp , on the other hand. In simpleFoam.C:</p><pre><strong>tmp</strong>&lt;fvVectorMatrix&gt; tUEqn</pre><pre>(<br>fvm::div(phi, U)<br>+ MRF.DDt(U)<br>+ turbulence-&gt;divDevReff(U)<br>==<br>fvOptions(U)<br>);</pre><p>These smart pointers are available in the folder:</p><pre>cd $FOAM_SRC/src/OpenFOAM/memory</pre><p>There are subfolders for autoPtr, refCount, and tmp.</p><p>The tmp class behaves like a std::shared_ptr . Shared pointers keep track of how many pointers are pointing to a block in memory. This is done through some kind of reference counting. To this end, the tmp class inherits from another class, refCount:</p><pre>typedef Foam::refCount refCount;</pre><p>If you use a std::shared_ptr to point to an object in memory, and if you copy the pointer once, there are now two pointers pointing to the same block of memory. If the original pointer goes out of scope, the reference count drops by one, but there is still one more pointer pointing to it, so the destructor for the object is not called just yet. This is an example of reference counting, which is laid out in refCount.H more explicitly.</p><p>tmp does it by incrementing the refCount variable as in when thetUEqn object is created. This is done via the operator++ from refCount.H.</p><pre>template&lt;class T&gt;<br>inline Foam::tmp&lt;T&gt;::tmp(const tmp&lt;T&gt;&amp; t):<br>ptr_(t.ptr_),<br>type_(t.type_)</pre><pre> {<br>     if (isTmp())<br>     {<br>        if (ptr_)<br>           {</pre><pre>               operator++();<br>           }</pre><pre>...</pre><p>When the pointer to tUEqn goes out of scope, it calls the destructor of tmp. The destructor does nothing but call the clear method in tmp</p><pre>template&lt;class T&gt;<br>inline void Foam::tmp&lt;T&gt;::clear() const noexcept<br>{<br>if (isTmp() &amp;&amp; ptr_)<br>   {<br>     if (ptr_-&gt;unique())<br>       {<br>          delete ptr_;  //deletes the pointer if it points uniquely<br>        }</pre><pre>     else<br>        {<br>           ptr_-&gt;operator--(); // decrements the ref counter<br>         }</pre><pre>ptr_ = nullptr;</pre><pre>    }<br>}</pre><p>The first real application comes when you see the following step in UEqn.H :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*5kqqFyX1uIW1dLUIGQyZDg.png" /><figcaption>UEqn.H</figcaption></figure><pre>fvVectorMatrix&amp; UEqn = tUEqn.ref();</pre><p>The tUEqn.ref() returns the pointer to tUEqn …</p><pre>template&lt;class T&gt;</pre><pre>inline T&amp; Foam::tmp&lt;T&gt;::ref() const</pre><pre>{ ...</pre><pre>return *ptr_; </pre><pre>}</pre><p>…and makes UEqn point to it. The “=” assignment operator here is meant to <strong>not make a copy</strong>. Copying is expensive, especially if your fvVectorMatrix is going to be generated for millions of mesh points. From how I understand it, it just copies the pointer to the original RHS (tUEqn) and sets the original pointer to null. Check out the Foam::tmp&lt;T&gt;::operator= method in tmpI.H .</p><pre>ptr_ = t.ptr_;</pre><pre>type_ = PTR;</pre><pre>t.ptr_ = nullptr;</pre><p>The second advantage is allowing the reduction in peak memory by clearing out large amounts of data. We saw the destructor of tmp using the clear() method. This can also be used at specific points in the top level solver and gives great flexibility in dropping peak memory loads instantly. Go to the pEqn.H file in the simpleFoam directory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*fcQEtoyU663KXqo2alVFlg.png" /><figcaption>pEqn.H</figcaption></figure><pre><strong>tUEqn.clear()</strong></pre><pre>while (simple.correctNonOrthogonal())</pre><pre>{<br>    fvScalarMatrix pEqn<br>       (<br>         fvm::laplacian(rAtU(), p) == fvc::div(phiHbyA)<br>        );</pre><pre>}</pre><p>This allows the pointer to the momentum matrix to be deleted right before the pressure equation, where the peak in simpleFoam is reached. This is partly based on the answer by Hrove Jasak <a href="https://www.cfd-online.com/Forums/openfoam-programming-development/71912-tmp-stands-true-macro-pain.html">here</a>.</p><p>If you’re looking for more information on this, this <a href="http://openfoamwiki.net/index.php/OpenFOAM_guide/tmp">wiki</a> covers a lot of stuff I haven’t.</p><p>References: <a href="http://openfoamwiki.net/index.php/OpenFOAM_guide/tmp">http://openfoamwiki.net/index.php/OpenFOAM_guide/tmp</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5234232750b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Wall Functions in OpenFOAM — Implementation in Code]]></title>
            <link>https://medium.com/@mustafaabbs2/wall-functions-in-openfoam-implementation-in-code-959eccc4e311?source=rss-7d332bb51b78------2</link>
            <guid isPermaLink="false">https://medium.com/p/959eccc4e311</guid>
            <category><![CDATA[openfoam]]></category>
            <category><![CDATA[cfd]]></category>
            <dc:creator><![CDATA[Mustafa Bhotvawala]]></dc:creator>
            <pubDate>Fri, 10 Jul 2020 11:04:56 GMT</pubDate>
            <atom:updated>2020-07-10T11:04:56.780Z</atom:updated>
            <content:encoded><![CDATA[<h3>Wall Functions in OpenFOAM — Implementation in Code</h3><p>There’s no way around turbulence modelling. When George Box famously said “<em>All turbulence models are wrong, some are useful</em>”, I’m sure notice was taken as to how absolutely <em>useless</em> CFD results are for setups that lack appropriate turbulence models and mesh resolution near a wall. It is indeed a <em>Garbage-In-Garbage-Out</em> business.</p><p>I will assume readers have a basic overview of turbulence and associated theory. For beginners, you can catch up <a href="https://www.simscale.com/blog/2017/12/turbulence-cfd-analysis/">here</a>. I’ll just dive into an explanation of wall functions and what the code in OpenFOAM looks like.</p><p>If you don’t have wall functions — you need to fully resolve your boundary layer (called using a <strong>Low Reynolds Number — LRN</strong> approach). This means, your first cell must be small enough to fit comfortably inside the viscous sublayer. This means your y⁺ value should be around 1. This is done most often using layers — but this is expensive business, in terms of mesh resolution. If you use a wall function, you can skip the tedious business of modelling the boundary layer velocity until y⁺ = 30 (this value varies across models, but generally this is where the buffer layer ends and the log layer starts )— and replace it with a profile that is already known. That’s a wall function — it is a defined velocity profile until a certain y⁺. This is called a <strong>High Reynolds Number (HRN) </strong>approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*oKZRiW0CxT_M8206ds3Fww.png" /><figcaption>From Menter [2003]</figcaption></figure><p>The plot below is logarithmic in y⁺. The curve you see for the viscous sublayer actually represents a linear relation between u⁺ and y⁺, where u⁺ is the velocity non-dimensionalized with a friction velocity uₜ. For a quick catch up, other turbulence quantities are described <a href="https://www.cfd-online.com/Wiki/RANS-based_turbulence_models">here</a>. All quantities are non-dimensionalized as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/116/1*-DLKniIzCbnLpS7G_PYUaA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/157/1*UgqOKMpziJb1aj7auXWUsw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/0*tVgZ5PumAHhPxXYh.png" /><figcaption>Non-Dimensional velocity versus y⁺ (Taken from <a href="https://www.simscale.com/">Simscale</a>)</figcaption></figure><p>OpenFOAM prescribes:</p><ol><li><strong>Viscous sublayer</strong>: u⁺ = y⁺</li><li><strong>Log layer</strong>: where κ =0.41 and E =9.8:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/189/1*7VQhOCFEMospJ5dncttZJA.png" /></figure><p>3. <strong>Buffer layer</strong>: Switch between viscous and log layer calculation depending on y⁺</p><p>Let’s look at this in the code. Move to the directory with the source code for wall functions:</p><pre>cd $FOAM_SRC/TurbulenceModels/turbulenceModels/derivedFvPatchFields/wallFunctions</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/261/1*m0FGQ2gP5O41JKPSdT5B7A.png" /><figcaption>Available wall functions. Use a “tree” command for this</figcaption></figure><p>The <a href="https://www.cfd-online.com/Wiki/SST_k-omega_model">k-ω SST model</a> has a blended approach to selecting the turbulence model, switching between k-ω and <a href="https://www.cfd-online.com/Wiki/K-epsilon_models">k-ϵ</a> based on a blending function depending on the distance from the wall. Moreover, the <strong>turbulence dissipation rate ω</strong> can handle both LRN and HRN approaches depending on the y⁺ value. Let’s look at the equation for <strong>turbulent kinetic energy, k:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*TAO21obahiidUukMCOSkjw.png" /></figure><p>This equation is first non-dimensionalized (based on <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.584.2507&amp;rep=rep1&amp;type=pdf">[1]</a>), yielding</p><ol><li><strong>Viscous sublayer</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/175/1*xTqU7l6PHvlLJcCuh1b_xg.png" /></figure><p>2.<strong> Log layer</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/249/1*Ft6onU1kOSNOMmPibBnQeQ.png" /></figure><p>The modelling of the buffer layer is done based on the y⁺ value. This needs the friction velocity, which is calculated as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/187/1*S_Xgu-W5ySCeZEhNsaqY3Q.png" /></figure><p>This can be compared to the code. Open the kLowReWallFunctionFvPatchScalarField.H file. The most important function in any wall function file in OpenFOAM is the updateCoeffs() function.</p><pre>const scalar Cmu25 = pow025(nutw.Cmu());<br>const scalar yPlus = Cmu25*y[facei]*sqrt(k[celli])/nuw[facei];</pre><p>The quantity yPlusLam marks where the switch happens. This value is about 11. If the value of y⁺&lt;yPlusLam , this is in the viscous sublayer region, where the corresponding formula is applied. If y⁺&gt;yPlusLam , the log-law region formula is used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*p05qLa3TSO_1oIQoif3HfA.png" /></figure><p>A similar equation is obtained for the near-wall region from the ω transport equation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*7a8ghE0-zM3lDs-0-jpQ_Q.png" /></figure><p>For ω, there are two solutions as well:</p><ol><li><strong>Viscous sublayer</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/157/1*m40YHQWgxDTQe5QHnP-ATA.png" /></figure><p>2. <strong>Log layer</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/208/1*EAEqyJM_dGUOu0ivNjDuNA.png" /></figure><p>The resultant omega is a magnitude quantity from both these omegas. For a lower y, the 1/y² quantity is dominant, and hence the treatment is that of a viscous sublayer. Compare this to the code in omegaWallFunctionsFvPatchScalarField.C.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*NGS8_GH9qFfwfeJMg51I9A.png" /></figure><p>Note that you see a w = cornerWeights[facei]; line there. The w is a weight that is multiplied to any resulting quantity. This can be seen in the function createAveragingWeights()</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*DjchKwJ6UigQgNTH56JXhA.png" /></figure><p>Why is this done? Because quantities like omega (ω) are calculated at the cell-centre. If more faces on a cell lie on a boundary patch (bf[patchi]), the cell-centre has to be a weighted average of the quantities on each face. For the turbulence kinetic energy k, this is not done as the cell face values are used.</p><p>There are other quantities like the turbulent viscosity μₜ (nut), which require wall functions to correctly calculate the wall shear stress. I could cover this in another article.</p><p><strong>Handy references:</strong></p><p>[1] Kalitzin, G., Medic, G., Iaccarino, G., &amp; Durbin, P. (2005). Near-wall behavior of RANS turbulence models and implications for wall functions. <em>Journal of Computational Physics</em>, <em>204</em>(1), 265–291</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=959eccc4e311" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up a Fluid Structure Interaction Workflow with OpenFOAM and deal.II]]></title>
            <link>https://medium.com/@mustafaabbs2/setting-up-a-fluid-structure-interaction-workflow-with-openfoam-and-deal-ii-80718f2e2e0e?source=rss-7d332bb51b78------2</link>
            <guid isPermaLink="false">https://medium.com/p/80718f2e2e0e</guid>
            <category><![CDATA[fsi]]></category>
            <category><![CDATA[cfd]]></category>
            <category><![CDATA[openfoam]]></category>
            <dc:creator><![CDATA[Mustafa Bhotvawala]]></dc:creator>
            <pubDate>Sat, 27 Jun 2020 09:33:11 GMT</pubDate>
            <atom:updated>2020-06-27T09:33:11.582Z</atom:updated>
            <content:encoded><![CDATA[<p>I spent a summer with a <a href="https://home.iitm.ac.in/sunetra/page4.html">research group</a> specializing in a variety of topics in non-linear dynamics, one of them being the <strong>Fluid Structure Interaction (FSI) </strong>of flapping wings. It was 2015 — I did not work with OpenFOAM then, but I’m sure the more recent FSI solvers with <a href="https://openfoamwiki.net/index.php/Extend-bazaar/Toolkits/Fluid-structure_interaction#fsiFoam_tutorial_cases">foam-extend</a> were only in their infancy, in terms of popularity.</p><p>foam-extend is getting a bit older for wear in 2020. Other development branches, including the ESI-OpenCFD (v1912 etc) that we’ve made use of on our WSL Ubuntu in previous articles may not be fully compatible with the other FSI utilities out there, including <a href="https://github.com/davidsblom/FOAM-FSI">FOAM-FSI</a> (Feel free to correct me on this though).</p><p>I’m going to keep it short and get to the workflow. I want users to be able to set up a common FSI tutorial case — a perpendicular flap in a 2-D channel. There are a multitude of options — especially with the choice of a FE solver, I hope this helps you avoid a very tedious path to just setting up an environment. I had to personally go through a ton of debugging to set this up correctly — this should help everyone save some time.</p><p>How are forces transmitted back and forth between fluid and solid solvers? Here’s where <a href="https://www.precice.org/"><strong>preCICE</strong></a> comes in. PreCICE can couple different types of solvers together in a multi-physics environment. For example, an <strong>adapter </strong>communicates data from the fluid solver, which is then mapped via PreCICE and lands at the adapter of the solid solver, and then the other way around. There are adapters for a range of different solvers, which to me, gives PreCICE its appeal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2jlsdJ8fMwGRKVz2qHBQvw.png" /><figcaption>(taken from precice.org)</figcaption></figure><p>You need <strong>five </strong>things:</p><ol><li>preCICE</li><li>A fluid solver — OpenFOAM (I use v19.12)</li><li>An OpenFOAM-preCICE adapter</li><li>A solid solver — deal.II</li><li>A deal.II-preCICE adapter</li></ol><p>There are some dependencies I recommend you have before you start. These are the ones I needed for <strong>Ubuntu-18.04</strong>. Copy and paste the following in your terminal.</p><pre>sudo apt update &amp;&amp; \<br>sudo apt install build-essential cmake libeigen3-dev libxml2-dev libboost-all-dev python3-dev python3-numpy</pre><p>As a note to organize your files better, create a directory titled FSI in your home folder that will house all the source code in the coming section. I’m going to assume you have a working OpenFOAM installation on your system. If not, check this out.</p><p><strong>A. Get preCICE</strong></p><p>I prefer having the source and building with CMake. Follow the instructions in this <a href="https://github.com/precice/precice/wiki/Building:-Using-CMake">wiki</a>. For people still drawing a blank, cmake generates your MakeFile on which you run the command make for the actual compilation. make install sends the installed files to their intended locations on the system.</p><p><strong>B</strong>. <strong>Get the OpenFOAM adapter for preCICE</strong></p><p>This is explained <a href="https://github.com/precice/openfoam-adapter/wiki/Building">here</a>. You will use the Allwmake script to create the adapter -which is actually just a shared library preciceAdapterFunctionObject , which needs to be referenced in <em>controlDict</em>. Don’t worry about this, the repository is equipped with working tutorials where you don’t need to change a thing!</p><p><strong>C. Get deal.II</strong></p><p>This is the solid solver we’ll use. Just like preCICE, the installation is again done from source — you’ll have to <a href="https://www.dealii.org/9.2.0/readme.html">download the source code</a> and compile it. This takes a while — allot a few hours for a compilation when done with a single processor.</p><p><strong>Note — </strong>I found it difficult to install deal.II with the OpenFOAM environment sourced because of clashing dependencies. Be sure to comment out sourcing the OpenFOAM .bashrc file in your system .bashrc.</p><p><strong>D</strong>. <strong>Get the deal.II adapter for preCICE</strong></p><p>Follow the instructions on this <a href="https://github.com/precice/dealii-adapter/wiki/Building">page</a>.</p><p>Ideally, you should now have a folder with the source code for <strong>A</strong>, <strong>B</strong>, <strong>C</strong>, and <strong>D</strong>. <strong>Keep the source code around</strong> — we may need to modify and recompile.</p><p>Now — download the <a href="https://github.com/precice/tutorials/tree/master/FSI">tutorials repository</a> in your <strong>$FOAM_RUN</strong> folder. These are the FSI cases we can test our environment with.</p><p>The tutorial we will test is the FSI/flap_perp_2D/OpenFOAM-deal.II in the tutorials folder.</p><p>Navigate to the <strong>deal.II adapter (D) </strong>source folder. The adapter has individual solvers which you can build with cmake and make . Do this in the linear_elasticity/ subfolder. Copy the executable you get to the FSI/flap_perp_2D/OpenFOAM-deal.II/Solid folder. This is your solid solver executable.</p><p><strong>Open two terminals</strong> — source one with OpenFOAM, leave the other one unsourced. On the unsourced terminal, after navigating to FSI/flap_perp_2D/OpenFOAM-deal.II/, run ./Solid/linear_elasticity Solid/linear_elasticity.prm . This starts the solid solver — preCICE will now wait for you to execute OpenFOAM on the other terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*ub-3jvm69WLyHUTRvTNAeg.png" /></figure><p>Go to the terminal sourced with OpenFOAM. Run the runFluid script to launch the OpenFOAM run script. <strong>This will start the solver on both terminals.</strong></p><p>It is important that you proof <em>controlDict </em>to make sure your fluid and solid results are being printed at the same time intervals. You will now have VTK folders in both Solid/ and Fluid/ folders which can be read into Paraview.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KqOszeSytheCB3K_edX_oQ.png" /><figcaption>Results from Fluid/ (time =5)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*thAE8HLpblpmyKsZ9g8PYg.png" /><figcaption>Results from Solid/ (time =5)</figcaption></figure><p>This is a <strong>dynamicMesh </strong>being employed. You can observe the changes in the structure with each time step. For instance, the meshes at the first and last time step look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FGktko4MWLVJuBQmUn4_tw.png" /><figcaption>Channel at time = 0</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DFcOz8fgv48HF1k7wFsPag.png" /><figcaption>Channel at time =5</figcaption></figure><p>This could be a good point for more complicated simulations. I’m no expert on deal.II, but there are other models, including non-linear elasticity that can be employed. 3-D cases, including the possibility of preparing cases for flapping airfoils — if you have other ideas on simple use cases in FSI, write to me!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80718f2e2e0e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Analyzing SIMPLEC — the “consistent” simpleFOAM control]]></title>
            <link>https://medium.com/@mustafaabbs2/analyzing-simplec-the-consistent-simplefoam-control-153015de10cf?source=rss-7d332bb51b78------2</link>
            <guid isPermaLink="false">https://medium.com/p/153015de10cf</guid>
            <category><![CDATA[fluid-mechanics]]></category>
            <category><![CDATA[openfoam]]></category>
            <category><![CDATA[cfd]]></category>
            <category><![CDATA[c]]></category>
            <dc:creator><![CDATA[Mustafa Bhotvawala]]></dc:creator>
            <pubDate>Wed, 24 Jun 2020 17:03:49 GMT</pubDate>
            <atom:updated>2020-06-24T17:03:49.219Z</atom:updated>
            <content:encoded><![CDATA[<h3>Analyzing SIMPLEC — the “consistent” simpleFOAM control</h3><p>The <a href="https://medium.com/@mustafaabbs2/the-simplefoam-implementation-explained-61fcc54ab24d">previous articles</a> in this series took on debugging the simpleFoam solver through a VSCode debugger. SIMPLEC, a “consistent” implementation of the SIMPLE algorithm can be toggled on and off via the “consistent” switch in your fvSolutions file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*U_rByI0s4seJJbA9Ja7Jsg.png" /><figcaption>system/fvSolution</figcaption></figure><p>The theory on SIMPLEC is a bit dense — so let’s go through it here.</p><p>When we derived our starting decomposed equation for SIMPLE:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/199/1*KSeNYZ5kgGW7ioLy0wAFrQ.png" /></figure><p>Just as a reminder — A has the diagonal coefficients of velocity U, while H has the non-diagonal coefficients.</p><p>The SIMPLE algorithm works on velocity and pressure corrections in an iterative method. Quantities like U*, p* are the predicted value in the first iteration of a loop, and U`, p` are the correction quantities that are used to correct the predicted values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/161/1*OUqCGhkTpel7pPP_4uGlFw.png" /></figure><p>A key assumption in the SIMPLE algorithm is that H(U`) = 0 — which means that all the non diagonal coefficients of the correction velocity are 0. This correction velocity is obtained from the Poisson equation for pressure.</p><p>This means that the contributions to the correction velocity from all neighbouring cells are dropped.</p><p>For <strong>SIMPLE</strong>, the correction velocity equation is derived as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*x8pK1OXExDe9-mhzf5ORSg.png" /></figure><p>The pressure correction term can be large and needs to be under-relaxed with each step for this reason. So the algorithm then ends up doing a step like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/122/1*Z4mkRRhyCD_dYPtDk_vUSA.png" /></figure><p>where α is the under-relaxation factor. This means that the pressure correction step is happening much slower than technically possible. How do we get rid of this? By using a consistent formulation: <strong>SIMPLEC</strong></p><p>SIMPLEC has the same basic formulation for correction velocity:</p><p>AU` = H1(U`)-∇ p`</p><p>Here, we are <strong>not</strong> going to ignore the neighbour velocities (the off-diagonal terms). Let’s rename H(U`) as H1(U`) to avoid confusion with H(U*).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/196/1*X7D_82eoEtpktqMkgygwEw.png" /></figure><p>Where At = A — H1. Deriving further</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*qxkszzkOCQ1A5lh7izeZpQ.png" /></figure><p>Compare this to the SIMPLE momentum corrector step to find U:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/165/1*_rt4hXktIbewk4jebtxblw.png" /></figure><p>You can see that the velocity is corrected by an equivalent of:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/153/1*_xYOcHyf18tfd_1X6Sr7jg.png" /></figure><p>How does this look like in OpenFOAM?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*16jQ5RMrVrAw2VqmldZG6Q.png" /><figcaption>SIMPLEC</figcaption></figure><p>This is represented by the term (rAU-rAtU())*fvc::grad(p). That’s the correction term for SIMPLEC! Observe the code to see if this matches with theory.</p><p>The velocity is then calculated by using the corrected HbyA. It’s that simple! There are no extra equations to be solved (like other SIMPLE variants like SIMPLER), and this may give you gains of 20–30% due to no under-relaxation being required. If you use this article to do a comparison between convergence in SIMPLE and SIMPLEC, do let me know how they fare against each other.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=153015de10cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The simpleFoam implementation — explained]]></title>
            <link>https://medium.com/@mustafaabbs2/the-simplefoam-implementation-explained-61fcc54ab24d?source=rss-7d332bb51b78------2</link>
            <guid isPermaLink="false">https://medium.com/p/61fcc54ab24d</guid>
            <category><![CDATA[openfoam]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[cfd]]></category>
            <category><![CDATA[c]]></category>
            <dc:creator><![CDATA[Mustafa Bhotvawala]]></dc:creator>
            <pubDate>Sat, 20 Jun 2020 18:23:24 GMT</pubDate>
            <atom:updated>2020-06-21T11:55:29.044Z</atom:updated>
            <content:encoded><![CDATA[<h3>The simpleFoam implementation — explained</h3><p>Tip — This is not meant to be a standalone article. It’s much more useful walking along this like a tutorial and not just reading through . So I highly recommend catching up to the point of entry in this article with the <a href="https://medium.com/@mustafaabbs2/stepping-through-simplefoam-with-vs-code-a-guide-to-debugging-solvers-39bfb1411372">previous one</a>.</p><p>Let’s look at the actual implementation of simpleFoam in this article. We left off by preparing the solver with all the mesh, field, and time objects that it would need. This is what you should see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*2ytEDfp2nkIn0IIDuwToNg.png" /><figcaption>simpleFoam.C</figcaption></figure><blockquote>Before you move ahead, I recommend you see this really good <a href="https://www.youtube.com/watch?v=OOILoJ1zuiw">video</a> (Aidan Wimshurst) on an introduction to the algorithm. (If you haven’t checked the whole series out, please do!)</blockquote><p>In an incompressible simulation, you have more variables (<strong><em>u,v,w,p</em></strong>) than equations. The system cannot be solved directly because there is no explicit equation for pressure. That’s why you need an algorithm to do this. As a refresher, the N-S equation is shown in vector form below:</p><p>ρ D<strong>U</strong>/Dt = -∇ p + ∇ . <strong>τ</strong> + ρ<strong> g</strong></p><p>Let’s convert this into the simplest matrix system we can think of, considering the stress term is a function of velocities too:</p><p><strong>MU</strong> = -∇ p …….. (1)</p><p>Here, <strong><em>M </em></strong>is just the coefficient matrix of the velocities. Now, what most algorithms do is convert this sparse matrix <strong><em>M </em></strong>into a diagonal and an off-diagonal matrix. The diagonal matrix is called <strong><em>A</em></strong> in OpenFOAM, while the off-diagonal one is called <strong><em>H</em></strong>. This will become clear in a bit once you see the source code. The decomposition is done as follows:</p><p><strong>AU</strong> -<strong>H</strong> =-∇ p …….. (2)</p><p>What the algorithm does is essentially the following.</p><ol><li>It solves directly for velocity U. This is called the momentum predictor step:</li></ol><p><strong>U</strong> =<strong> H</strong>/<strong>A</strong> -∇ p/<strong>A </strong>…….. (3)</p><p>Simple yes? Turns out however, this velocity was not giving a constraint of mass conservation. <strong>This velocity satisfies the momentum equations but not the continuity equations.</strong></p><p>∇.<strong>U</strong> = 0 …….. (4)</p><p>2. To satisfy this condition, the equation in (3) is inserted in the equation in (4). This yields a Poisonn equation for pressure — this is solved for pressure.</p><p>∇.(∇p/<strong>A</strong>) = ∇.(<strong>H</strong>/<strong>A</strong>)…….. (5)</p><p>3. The pressure correction is used to obtain the correct velocity that satisfies the continuity equation. <em>But wait, this no longer satisfies the momentum equation now! </em>This needs to be iterated in a loop until the solution converges.</p><p>Let’s observe how this is implemented. The two main solution components of the algorithm lie inside these two files:</p><pre>#include UEqn.H<br>#include pEqn.H</pre><p>Let’s step into<em> UEqn.H:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*-sDyjYOg1IZCYWqEn2n1UA.png" /><figcaption>UEqn.H</figcaption></figure><pre>tmp&lt;fvVectorMatrix&gt; tUEqn<br>(<br>fvm::div(phi, U)<br>+ MRF.DDt(U)<br>+ turbulence-&gt;divDevReff(U)<br>==<br>fvOptions(U)<br>);</pre><p>This creates a tUEqn object of type fvVectorMatrix. This is essentially the <strong>MU </strong>system discussed above. This includes contributions from Moving Reference Frame (MRF) and the stress tensor, both functions of velocity and hence part of the matrix.</p><pre>solve(UEqn == -fvc::grad(p))</pre><p>This is equivalent to the <strong>MU</strong> = -∇ p portion of the algorithm. Now, we have a UEqn object that contains the solution of this system of equations.</p><p>The diagonal matrix is extracted by<em> UEqn.A() </em>and the off-diagonal matrix by <em>UEqn.H()</em>. We see this when we enter <em>pEqn.H</em>:</p><pre>volScalarField <strong>rAU</strong>(1.0/UEqn.A());</pre><pre>volVectorField <strong>HbyA</strong>(constrainHbyA(rAU*UEqn.H(), U, p));</pre><pre>surfaceScalarField <strong>phiHbyA</strong>(“phiHbyA”, fvc::flux(HbyA));</pre><p><strong>rAU</strong> is simply 1/A,</p><p><strong>HbyA</strong> is unsurprisingly, H/A</p><p>and <strong>phiHbyA</strong> is the surfaceScalarField equivalent of HbyA, which is originally a volVectorField.</p><p>The Poisson equation that needs to be solved is <strong>∇.(∇p/A) = ∇.(H/A)</strong>, which calculates pressure. This is very direct translation into code:</p><pre>fvScalarMatrix pEqn</pre><pre>(<br>fvm::laplacian(rAtU(), p) == fvc::div(phiHbyA)<br>);</pre><pre>pEqn.solve();</pre><p>The last step of the algorithm is fairly straightforward too. The new pressure values are used to correct the velocity so that it satisfies the continuity equation. This equation — <strong>U</strong> =<strong> H</strong>/<strong>A</strong> -∇ p/<strong>A</strong></p><pre>U = HbyA — rAtU()*fvc::grad(p);</pre><pre>U.correctBoundaryConditions();</pre><p>That’s the end of one SIMPLE iteration. This will continue until convergence. There are a fair number of components this tutorial did not cover — mainly to avoid a tedious long read, relaxation factors, non-orthogonal correction etc to name a few. If this helped you gain some intuition into how essentially the most fundamental solver in OpenFOAM works, these could be covered in more advanced articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61fcc54ab24d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stepping through simpleFoam with VS Code — a guide to debugging solvers]]></title>
            <link>https://medium.com/@mustafaabbs2/stepping-through-simplefoam-with-vs-code-a-guide-to-debugging-solvers-39bfb1411372?source=rss-7d332bb51b78------2</link>
            <guid isPermaLink="false">https://medium.com/p/39bfb1411372</guid>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[openfoam]]></category>
            <category><![CDATA[cfd]]></category>
            <dc:creator><![CDATA[Mustafa Bhotvawala]]></dc:creator>
            <pubDate>Sat, 20 Jun 2020 09:29:43 GMT</pubDate>
            <atom:updated>2020-06-21T11:47:23.380Z</atom:updated>
            <content:encoded><![CDATA[<h3>Stepping through simpleFoam with VS Code — a guide to debugging solvers</h3><p>This is a the second article in a series exploring utilities of OpenFOAM for viewers beginning with CFD — geared towards OpenFOAM beginners. The toolbox has a steep learning curve, but a see-through box offers far more control than a black one. Though we set up an environment to code our own solvers in the<a href="https://medium.com/@mustafaabbs2/setting-up-my-openfoam-development-environment-on-windows-37b1575ecd4f"> last article</a>, we don’t have a debug environment to fix things when wrong, or to see how the code flows.</p><p><a href="https://www.google.com/search?q=Openfoam+debug+Opt&amp;rlz=1C1CHBF_enKW850KW850&amp;oq=Openfoam+debug+Opt&amp;aqs=chrome..69i57j35i39l3j69i60j69i58j69i60l2.4077j0j7&amp;sourceid=chrome&amp;ie=UTF-8">Debug mode</a> allows you to do just this. We unpacked OpenFOAM directly into the <em>/opt/</em> folder in our last article. This was the default “Opt” compilation. OpenFOAM needs to be compiled again in debug mode. Navigate to your .bashrc file in OpenFOAM’s<em> etc/</em> folder and switch the flag:</p><pre>WM_COMPILE_OPTION=<strong>Debug</strong></pre><p>Move back a directory and compile OpenFOAM:</p><pre>./Allwmake</pre><p>Debug mode is almost double the size of the normal compilation. This will take a while, atleast a few hours. Try running <em>Allwmake </em>in parallel to speed things up (for example, here with four processors):</p><pre>./Allwmake -j 4</pre><p>I recommend aliases to switch between your debug and Opt modes. Copy your OpenFOAM <em>.bashrc </em>file into<em> .bashrc_debug</em>. Source your <em>.bashrc_debug </em>file to activate debug mode.</p><pre><strong>alias</strong> of1912deb=”source /opt/OpenFOAM/OpenFOAM-v1912/etc/bashrc_debug”</pre><p>A quick check to see if you’re in debug mode -</p><pre><strong>which</strong> icoFoam</pre><pre>//should get--&gt; /opt/OpenFOAM/OpenFOAM-v1912/platforms/linux64Gcc63DPInt32<strong>Debug</strong>/bin/icoFoam</pre><p>Debug mode allows the <a href="http://www.gnu.org/software/gdb/">gdb</a> debugger to step through your code. You could do this in a terminal too, but I prefer working in an IDE, especially for code that’s as layered as OpenFOAM.</p><p>We’ll walk through<a href="https://www.openfoam.com/documentation/guides/latest/doc/guide-applications-solvers-incompressible-simpleFoam.html"> simpleFoam</a> in this example. simpleFoam uses the <a href="https://en.wikipedia.org/wiki/SIMPLE_algorithm">SIMPLE</a> algorithm to solve the N-S equations for incompressible fluid flow, for a steady state solution. It’s a great starting point for most explanation on OpenFOAM.</p><p>Copy simpleFoam into your $WM_PROJECT_USER_DIR.</p><pre>cp -r $FOAM_SOLVERS/incompressible/simpleFoam $WM_PROJECT_USER_DIR</pre><p>To debug the solver, you need to supply it with the input files it needs from the OpenFOAM case structure. I simply copy a <em>pitzDaily</em> tutorial into the same folder as the solver code:</p><pre>cp -r $FOAM_SOLVERS/incompressible/simpleFoam/pitzDaily/* $WM_PROJECT_USER_DIR/simpleFoam</pre><p>The contents of your folder should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/154/1*ir8AASrzIsdDAxqrdqpK9Q.png" /></figure><p>Start your VSCode environment:</p><pre>code .</pre><p>Navigate to the <em>simpleFoam.C </em>file. Place two breakpoints at the same locations as in the picture, after <em>argList:addNote</em> and at <em>turbulence -&gt; validate()</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*GfL7HeWOzxlwv6gDOJTLAQ.png" /></figure><p>For those not familiar with debuggers — find the command Run — Start Debugging to execute.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/213/1*liKX-zqCw6G8-ZOjRfb3gg.png" /><figcaption>Options to move along the code — Continue, Step Over, Step Into, Step Out</figcaption></figure><p>To move through the code, there are four options you need to use:</p><ol><li><strong>Continue</strong>: will move to the next breakpoint</li><li><strong>Step Over</strong>: will proceed to the next line of code without entering the function on that line. If you step over <em>turbulence-&gt;validate()</em>, it will move to the next line after executing it, but you will not move through the contents of the method itself</li><li><strong>Step Into</strong>: will enter the method</li><li><strong>Step Out</strong>: returns control to the preceding stack frame — i.e. if you’ve entered <em>turbulence-&gt;validate()</em> with Step Into, it will finish execution of the method and return control to simpleFoam.C</li></ol><p>Let’s run the debugger. If you want to work your way through the following header files too, I recommend adding a breakpoint at the <em>argList::addNote </em>line.</p><pre>argList::addNote</pre><pre>(</pre><pre>&quot;Steady-state solver for incompressible, turbulent flows.&quot;</pre><pre>);</pre><pre>#include &quot;postProcess.H&quot;<br>#include &quot;addCheckCaseOptions.H&quot;<br>#include &quot;setRootCaseLists.H&quot;<br>#include &quot;createTime.H&quot;<br>#include &quot;createMesh.H&quot;<br>#include &quot;createControl.H&quot;<br>#include &quot;createFields.H&quot;<br>#include &quot;initContinuityErrs.H&quot;</pre><p>We won’t enter the header files this way, but I can quickly desribe how the flow of code happens through these.</p><pre>#include &quot;postProcess.H&quot;<br>#include &quot;addCheckCaseOptions.H&quot;<br>#include &quot;setRootCaseLists.H&quot;</pre><p>The <em>postProcess.H</em> header file checks if the postProcess flag is issued and creates a list of all the function objects in the controlDict. <em>setRootCaseLists.H </em>parses through all the advanced switches that can be given to simpleFoam. For a list of these switches, see the <a href="https://www.openfoam.com/documentation/guides/latest/man/simpleFoam.html">documentation</a>.</p><p>Header files are entered as though they’re part of the code. You can observe which file the debugger is currently paused at by looking at the call stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*n_GFqwR0A_36kyeoatGqLQ.png" /></figure><p>Let’s work our way down the code:</p><pre>#include &quot;createTime.H&quot;<br>#include &quot;createMesh.H&quot;<br>#include &quot;createControl.H&quot;</pre><p><em>createTime.H</em> and <em>createMesh.H</em>, as their names suggests, create their respective<em> runTime</em> and <em>mesh</em> objects. <em>createTime.H</em> generates your first real output statement to your log file — “Create time”.</p><pre>Foam::Info&lt;&lt; “Create time\n” &lt;&lt; Foam::endl;</pre><pre>Foam::Time runTime(Foam::Time::controlDictName, args);</pre><p><em>createMesh.H</em> is simple — it creates a mesh object (class <em>fvMesh</em>) for the corresponding <em>runTime</em> object.</p><pre>Foam::autoPtr&lt;Foam::fvMesh&gt; meshPtr(nullptr);</pre><pre>Foam::fvMesh&amp; mesh = meshPtr();</pre><p><em>createFields.H </em>is present in your solver directory. The field objects <strong><em>p</em></strong> (pressure), <strong><em>U</em></strong> (velocity) are created here. Moreover, any new fields you want to evaluate — here’s where they need to be created for the solver to parse through.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*zJ46W2L9c0C7XgDPHxRa4A.png" /><figcaption>createFields.H</figcaption></figure><p>The face flux <strong><em>phi</em></strong> is also created here through a header file inside <em>createFields.H. </em>Additionally, the <strong>transport model</strong> (based on your viscosity) as well as the <strong>turbulence model</strong> objects are created here.</p><p>Keep checking your terminal to see your outputs too — this is a sanity check to see that the flow of output is just like what we’d expect:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*GVTQGg15lCugAW9UOwnuhw.png" /><figcaption>Your terminal</figcaption></figure><p>Voila, you’ve got through the setting up of the solver — your mesh, time, and fields, and models are now ready. The next article in the series will stick with this example, but move onto the actual code characteristic to <em>simpleFoam</em> — with a bit of math to explain how the SIMPLE algorithm is implemented.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39bfb1411372" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up my OpenFOAM Development Environment on Windows]]></title>
            <link>https://medium.com/@mustafaabbs2/setting-up-my-openfoam-development-environment-on-windows-37b1575ecd4f?source=rss-7d332bb51b78------2</link>
            <guid isPermaLink="false">https://medium.com/p/37b1575ecd4f</guid>
            <category><![CDATA[numerical-computation]]></category>
            <category><![CDATA[cfd]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[openfoam]]></category>
            <dc:creator><![CDATA[Mustafa Bhotvawala]]></dc:creator>
            <pubDate>Sun, 07 Jun 2020 14:11:51 GMT</pubDate>
            <atom:updated>2020-06-07T14:11:51.786Z</atom:updated>
            <content:encoded><![CDATA[<p>Well, if you’re this far, you’re no stranger to open source fluid simulation software. Or you’ve finally broken free from the binds that commercial CFD software pose — “I’m just going to do it myself”, you say all too confidently as you jostle out of your fifteenth meeting with your software support that month.</p><p>A full Linux development environment is often too intimidating for those just starting to make a switch from commercial tools to OpenFOAM. Windows until recently was, and to some extent still is, a poor solution for numerical computing. <a href="https://www.pcworld.com/article/230527/ubuntu_linux_day_16_ext4_vs_ntfs.html">The NTFS file system on Windows is slower than the ext4 for frequent I/O which is a mainstay of computing applications.</a> There were limited options to those looking to run OpenFOAM on Windows without a dual-boot.</p><ol><li>A <a href="https://openfoam.org/download/windows-vm/"><strong>VM install</strong></a> was the go-to solution, however I found that it significantly slowed my host device.</li><li>A native Windows build, <a href="http://bluecfd.github.io/Core/About/"><strong>blueCFD-Core</strong></a>, based on a <a href="https://www.msys2.org/">MSys2</a> build platform (built with on a Cygwin layer for the Linux “feel” and MinGw, for compilation on Windows.). The full software stack makes it quite bulky for use.</li><li><a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10"><strong>Windows Subsystem for Linux (WSL)</strong></a><strong>: </strong>The original WSL is a GNU environment for Windows without any virtualization, and with a very slow read/write ability to the Windows file system.</li></ol><p>My move to Windows was recent — circumstances around the pandemic made me switch to a new system which I did not want to have a dual boot on. Luckily, we live in times of the<strong> </strong><a href="https://docs.microsoft.com/en-us/windows/wsl/wsl2-index"><strong>WSL2</strong></a>. The new version of the architecture in WSL allows increased file system performance, perations like cmake and git clone can run up to 20x faster — count me in.</p><p>I will not describe setting up the WSL 2 VM in this article. There is a fairly comprehensive install guide <a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10">here</a>.</p><ol><li>Once you have WSL ready, download the OpenFOAM release (<a href="https://sourceforge.net/projects/openfoam/files/v1912/OpenFOAM-v1912-windows10.tgz">here</a>, 19.12).</li><li>Copy the .tgz to your folder of choice.</li></ol><pre>cp -ar /mnt/c/Users/&lt;USER&gt;/Downloads/OpenFOAM-v1912-windows10.tgz .</pre><p>3. Untar to the /opt folder and change permissions.</p><pre>sudo tar -xvzf  OpenFOAM-v1912-windows10.tgz -C /opt/<br>sudo chown -R $USER /opt/OpenFOAM</pre><p>4. Source your .bashrc file!</p><pre>echo &quot;source /opt/OpenFOAM/OpenFOAM-v1912/etc/bashrc&quot; &gt;&gt; ~/.bashrc<br>source $HOME/.bashrc</pre><p>And you’re good to go. Verify your source by running a tutorial case.</p><p>The purpose of this article is to set up a <strong>development</strong> environment for OpenFOAM — you need to create:</p><ol><li>A directory that houses your custom solver and library source code. This is preferably somewhere in your <strong>$WM_PROJECT_DIR</strong> folder.</li><li>Directories for the binaries generated from your custom solver and the .so files generated from your custom library. You’ll need to create a directory that is defined by your variables <strong>$FOAM_USER_APPBIN</strong> and <strong>$FOAM_USER_LIBBIN </strong>respectively.</li></ol><p>This is how my custom solver and library directory (as described in 1.) should look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/657/1*_qMiorNX0Qk96gHyHrhEig.png" /></figure><p>myReactingParcelFoam is my custom solver modifying reactingParcelFoam, and surfaceFilmModels is where I hold the libraries that the solver depends on.</p><p>Notice the vscode folder? The perfect pair to OpenFOAM programming, as I’ve discovered, is <a href="https://code.visualstudio.com/"><strong>Visual Studio Code</strong></a>. You can download this to run on your Windows system. Yes — you run it on Windows and keep the source in your WSL environment. You need to install the <a href="https://code.visualstudio.com/remote-tutorials/wsl/getting-started"><strong>remote WSL extension</strong></a> in VS Code to couple the two environments. Additionally, install the<strong> </strong><a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools"><strong>C/C++ extension</strong></a> too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*9k6bv6VmYFTM0VniBTynMQ.png" /><figcaption>What your extensions should look like</figcaption></figure><p>Voila! All you need to do is run the following command in the directory you want to create a VS Code directory for:</p><pre>code . </pre><p>This creates a .vscode folder in that directory. Additionally, it will open up VS Code with your project, for instance:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*jFosjbe2Wxj2Hqj0XUVMNg.png" /><figcaption>What your VS Code instance should look like (minus the files in .vscode, we’ll get to this)</figcaption></figure><p>The files in .vscode that are of relevance are: <em>launch.json</em>, <em>tasks.json </em>and <em>c_cpp_properties.json</em>. These will be created when you add a configuration, but you will have to modify them. These give VS Code inputs on what task to run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*wXqMwzDj5VuMw5JV8igGqQ.png" /><figcaption>A sample tasks.json file</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*HJZ4TXPlKAzGRgzHYAZhfA.png" /><figcaption>A sample launch.json file</figcaption></figure><p>If you’ve gone through these .json files, the command in <em>tasks.json</em> is <strong>wmake</strong>, which is a wrapper around make for OpenFOAM. This is given a label <strong>wmake-build </strong>which is referenced in the “preLaunchTask” field in <em>launch.json</em>.</p><p>“program” specifies where to deposit the binary from the executable. As discussed, this should be in <strong>$FOAM_USER_APPBIN.</strong></p><p>The final .json file, <em>c_cpp_properties.json</em>, contains the include paths for the compiler, as well as the settings for Intellisense.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*OgX1Oz6TLtc5vF_Wwrv-pw.png" /></figure><p>Edit the Make/files to make sure your binary ends up in the right place after wmake.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/373/1*8WSHlW8QLAz48vPortUHHA.png" /></figure><p>This process is very similar for compiling your own libraries. You will have to write in <strong>wmake libso </strong>in the <em>tasks.json</em> file instead to compile all the .so files.</p><p>And voila, you can compile OpenFOAM code! Complete with code completion and references to other code blocks — this allows for a seamless start into the freedom OpenFOAM development brings with it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*reDyQdIu4_p2RvhvQt64ag.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37b1575ecd4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>