<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>SPHIN</title>
<link>https://thalesmaia.com/pages/blog/</link>
<atom:link href="https://thalesmaia.com/pages/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Thales Maia — researcher, engineer, and R&amp;D professional bridging academic research and industry innovation.</description>
<image>
<url>https://thalesmaia.com/files/icons/logo.png</url>
<title>SPHIN</title>
<link>https://thalesmaia.com/pages/blog/</link>
<height>144</height>
<width>144</width>
</image>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Mon, 04 May 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Designing a 2D Transformer Core: Post-processing with ParaView (Part 3)</title>
  <link>https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core post processing with paraview part3/</link>
  <description><![CDATA[ 




<p>We have arrived at the final part of this design series! Throughout this process, we followed some straightforward steps using exclusively Graphical User Interfaces (GUI) to perform a steady-state simulation. If you missed them, you can check out <a href="../20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/">Part 1</a> and <a href="../20260501 - Designing 2D transformer core steady state simulation elmerfem part2/">Part 2</a> of this series.</p>
<p>I will eventually address a more robust workflow for advanced analysis and tools. For now, however, it is essential for newcomers to understand these fundamental first steps.</p>
<section id="paraview" class="level2">
<h2 class="anchored" data-anchor-id="paraview">ParaView</h2>
<p>The first thing we need to do is open our <code>.vtu</code> result file.</p>
<ol type="1">
<li><p>Load the file. In the <em>Pipeline Browser</em> (the menu on the left), ParaView will show the file with a green box next to it, indicating it hasn’t been applied yet.</p></li>
<li><p>Below that, in the <em>Properties</em> panel, you will see the configuration options. This is where we adjust our results.</p></li>
<li><p>Click the <strong>Apply</strong> button, and the geometry will appear in the Render View.</p></li>
<li><p>Now, in the top toolbar (or the Properties window), change the visualization representation from <em>Solid Color</em> to <strong>magnetic flux density e</strong>.</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core post processing with paraview part3/images/image-13-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 1 - Initial ParaView setup showing the loaded <code>.vtu</code> file in the Pipeline Browser and the magnetic flux density distribution in the Render View.</p>
</figcaption>
</figure>
<p>You will notice that the magnetic flux is displayed in the form of elements (triangles) or nodes. The triangle view often looks a bit sharper because the data is self-contained within the boundaries of each element. The node view, on the other hand, averages the values of adjacent elements. If you play around with it, you will notice a blurring effect across the entire geometry.</p>
<section id="isolating-components-breaking-the-bodies" class="level3">
<h3 class="anchored" data-anchor-id="isolating-components-breaking-the-bodies">Isolating Components: Breaking the Bodies</h3>
<p>To visualize our results better, we need to separate the different materials. We will do this using the <em>Threshold</em> filter.</p>
<ol type="1">
<li><p>Click on your <code>case_t0001.vtu</code> file in the Pipeline Browser (feel free to rename it to something meaningful).</p></li>
<li><p>Go to the top menu: <strong>Filters</strong> &gt; <strong>Search…</strong> and type <strong>Threshold</strong>. Hit Enter.</p></li>
<li><p>We will now reference the body numbers we set up in Elmer (e.g., <code>1</code> for Air, <code>2</code> for Iron, etc.).</p></li>
<li><p>In the Threshold <em>Properties</em> panel, find the <strong>Scalars</strong> dropdown menu and select <strong>GeometryIds</strong>.</p></li>
<li><p>Set both the <strong>Lower Threshold</strong> and <strong>Upper Threshold</strong> to <code>1</code>, then click <strong>Apply</strong>.</p></li>
</ol>
<p>You will notice that only the air body is now visible. Rename this threshold in the Pipeline Browser to “Air”. Repeat this exact process for the other parts (Iron, Coils).</p>
<p><em>⚠️ <strong>Important note:</strong> In ParaView, filters are applied to the currently selected item in the pipeline. Make sure you select the original <code>.vtu</code> file (the parent element) before adding a new Threshold, otherwise, you will try to apply a Threshold to an existing Threshold!</em></p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core post processing with paraview part3/images/image-14-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 2 - Applying the Threshold filter to isolate specific components (e.g., the Air domain) by filtering their respective <code>GeometryIds</code>.</p>
</figcaption>
</figure>
</section>
<section id="smoothing-the-data-cell-data-to-point-data" class="level3">
<h3 class="anchored" data-anchor-id="smoothing-the-data-cell-data-to-point-data">Smoothing the Data: Cell Data to Point Data</h3>
<p>Now that we can see the individual bodies, we can improve their visual representation.</p>
<p>Select the “Iron” body in the pipeline and apply the filter <strong>Cell Data to Point Data</strong> (using the Search menu). This filter will interpolate the constant values within the elements to the nodes, resulting in a much smoother and more professional-looking color gradient.</p>
<p>From here, you can change the color map (the legend) and axis limits. For now, I will just leave the default options.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core post processing with paraview part3/images/image-15-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 3 - Visualization of the surface magnetic flux.</p>
</figcaption>
</figure>
</section>
<section id="drawing-flux-lines-the-contour-filter" class="level3">
<h3 class="anchored" data-anchor-id="drawing-flux-lines-the-contour-filter">Drawing Flux Lines: The Contour Filter</h3>
<p>A classic way to evaluate electromagnetic machines is by looking at the flux lines. We can achieve this using the Contour filter.</p>
<ol type="1">
<li><p>Select the smoothed “Iron” body in the pipeline.</p></li>
<li><p>Search for and apply the <strong>Contour</strong> filter.</p></li>
<li><p>In the Properties panel, change the Contour By field to <strong>potential</strong> (which represents the magnetic vector potential).</p></li>
<li><p>Look for the <em>Isosurfaces</em> section. Clear the default value, click on <strong>Add a range of values</strong>, choose the number of samples you want (e.g., 15 or 20), and generate them. Click <strong>Apply</strong>.</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core post processing with paraview part3/images/image-16-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 4 -Visualization of the magnetic flux lines across the iron core, generated by applying the Contour filter to the magnetic vector potential.</p>
</figcaption>
</figure>
</section>
<section id="extracting-data-plot-over-line" class="level3">
<h3 class="anchored" data-anchor-id="extracting-data-plot-over-line">Extracting Data: Plot Over Line</h3>
<p>The last post-processing technique we will cover is extracting the normal flux density over a specific line. This is crucial for evaluating core saturation.</p>
<ol type="1">
<li><p>Select the parent “Iron” body in the pipeline.</p></li>
<li><p>Search for and apply the <strong>Plot Over Line</strong> filter.</p></li>
<li><p>In the Properties panel, define the coordinates (<code>Point 1</code> and <code>Point 2</code> representing <code>&lt;x, y, z&gt;</code>) to draw a line across the section of the core you want to analyze. Click <strong>Apply</strong>.</p></li>
<li><p>A new line chart window will open. In the <em>Series Parameters</em> (Display properties), make sure you check only the variable of interest, such as <strong>magnetic flux density e_Y</strong> (if your line is horizontal).</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core post processing with paraview part3/images/image-17-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 5 - Evaluating the core’s saturation by extracting and plotting the magnetic flux density (<code>e_Y</code>) across a defined cross-section using the Plot Over Line filter.</p>
</figcaption>
</figure>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>There are far more powerful tools and filters inside ParaView, but these are the most relevant ones for a basic post-processing evaluation of our 2D model.</p>
<p>Now, we need to step back into Elmer and start playing serious, but that will be <a href="../20260504 - Designing 2D transformer core mastering sif file/">the topic for another discussion</a>!</p>
<!--Include social share buttons-->
<div class="social-share">
<p> <svg class="social-icon" viewbox="0 0 24 24"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 1 1 0-4.125 2.062 2.062 0 0 1 0 4.125zM7.114 20.452H3.558V9h3.556v11.452z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2.05 22l5.25-1.38a9.9 9.9 0 0 0 4.74 1.21h.01c5.46 0 9.91-4.45 9.91-9.91C21.96 6.45 17.5 2 12.04 2zm5.74 14.09c-.24.68-1.4 1.3-1.93 1.36-.51.06-1.03.24-3.45-.72-2.9-1.15-4.76-4.1-4.9-4.29-.14-.19-1.17-1.55-1.17-2.96 0-1.4.74-2.09 1-2.38.26-.29.57-.36.76-.36.19 0 .38 0 .55.01.18.01.41-.07.64.49.24.57.81 1.98.88 2.12.07.14.12.31.02.5-.1.19-.14.31-.28.48-.14.17-.29.38-.42.51-.14.14-.28.29-.12.57.16.29.72 1.19 1.55 1.93 1.06.95 1.96 1.24 2.24 1.38.29.14.46.12.63-.07.17-.19.72-.84.91-1.13.19-.29.38-.24.64-.14.26.1 1.64.77 1.92.91.29.14.48.21.55.33.07.12.07.69-.17 1.37z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M22 2 2 10.5l6 2 2 6.5 3-4 5 4 4-17zM8.5 13.5l9-7-7 8.5v3z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4-8 5-8-5V6l8 5 8-5v2z"></path></svg>  <button class="social-link copy-link-btn" data-share="copy" aria-label="Copiar link"> <svg class="social-icon" viewbox="0 0 24 24"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path></svg> </button></p>
</div>
<style>
.social-share {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  align-items: center;
  margin: 1rem 0;
}
.social-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: none;
  outline: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  text-decoration: none;
  box-shadow: none;
}
.social-link::-moz-focus-inner {
  border: 0;
}
.social-icon {
  width: 25px;
  height: 25px;
  fill: #1a1a1a;
  display: block;
}
.social-link:hover .social-icon {
  fill: #000;
}
.social-link:hover {
  background: rgba(0,0,0,0.08);
}
</style>
<!-- <script src="/files/scripts/socialshare.js"></script> -->
<script>
    document.addEventListener("DOMContentLoaded", function () {
  const pageUrl = encodeURIComponent(window.location.href);
  const pageTitle = encodeURIComponent(document.title);

  const shareUrls = {
    linkedin: "https://www.linkedin.com/sharing/share-offsite/?url=" + pageUrl,
    whatsapp: "https://wa.me/?text=" + pageTitle + "%20" + pageUrl,
    telegram: "https://t.me/share/url?url=" + pageUrl + "&text=" + pageTitle
  };

  document.querySelectorAll("[data-share]").forEach(function (el) {
    const type = el.getAttribute("data-share");

    if (type === "copy") {
      el.addEventListener("click", function () {
        navigator.clipboard.writeText(window.location.href);
        el.setAttribute("aria-label", "Link copiado!");
      }, true);
      return;
    }

    if (type === "email") {
      el.setAttribute("href", "mailto:?subject=" + pageTitle + "&body=" + pageUrl);
      return;
    }

    if (shareUrls[type]) {
      // Captura o clique ANTES de qualquer outro listener (inclusive o tracker do tema)
      el.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(shareUrls[type], "_blank", "noopener,noreferrer");
      }, true); // true = capture phase, roda primeiro
    }
  });
});
</script>


</section>

 ]]></description>
  <category>Simulation</category>
  <category>Electrical Machines</category>
  <category>Tutorial</category>
  <guid>https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core post processing with paraview part3/</guid>
  <pubDate>Mon, 04 May 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Designing a 2D Transformer Core: Mastering the Sif file</title>
  <link>https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core mastering sif file/</link>
  <description><![CDATA[ 




<p><a href="../20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/">In our last series</a>, we covered the step-by-step of how to model a simple EI core and simulate it using ElmerFEM. Due to the limitations of the ElmerGUI, we soon enough face some limitations.</p>
<p>In this post, we will follow some basics of the <code>.sif</code> file and, later, we will compare it with the ElmerGUI generated one.</p>
<p>Before we start, let’s consider that you already generated your <code>.unv</code> file and you are ready for a new model design. Our first suggestion was to open ElmerGUI and do everything from there. Now, we follow a different approach. Let’s try to do it using your preferable text editor. I will use VSCodium for that.</p>
<section id="elmergrid" class="level2">
<h2 class="anchored" data-anchor-id="elmergrid">ElmerGrid</h2>
<p>The first thing we need to do is convert the <code>.unv</code> file to Elmer format. To perform that, we use ElmerGrid.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> ElmerGrid 8 2 EIcore-FEMMeshGmsh.unv <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-autoclean</span></span></code></pre></div></div>
<p>It will generate a lot of files as follows:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> tree</span>
<span id="cb2-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">.</span></span>
<span id="cb2-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> EIcore-FEMMeshGmsh</span>
<span id="cb2-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── entities.sif</span>
<span id="cb2-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── mesh.boundary</span>
<span id="cb2-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── mesh.elements</span>
<span id="cb2-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── mesh.header</span>
<span id="cb2-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── mesh.names</span>
<span id="cb2-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── mesh.nodes</span>
<span id="cb2-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> EIcore-FEMMeshGmsh.unv</span></code></pre></div></div>
<p>If you look inside the <code>.sif</code> file, you will notice that now it uses the same name that you created in FreeCAD. That one will help us a lot.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb3-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">!------ Skeleton for body section -----</span></span>
<span id="cb3-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 1</span></span>
<span id="cb3-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    Name = Air</span></span>
<span id="cb3-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 2</span></span>
<span id="cb3-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = Iron_Faces</span></span>
<span id="cb3-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 3</span></span>
<span id="cb3-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = coil_a_minus_Faces</span></span>
<span id="cb3-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb3-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.</span></span>
<span id="cb3-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.</span></span></code></pre></div></div>
<p>Now, we fill out the rest.</p>
</section>
<section id="sif-editing" class="level2">
<h2 class="anchored" data-anchor-id="sif-editing">Sif Editing</h2>
<p>Let’s break the sif editing into different parts. The first one is the variables.</p>
<section id="matc" class="level3">
<h3 class="anchored" data-anchor-id="matc">MATC</h3>
<p>MATC is a library for the numerical evaluation of mathematical expressions. Here I first define the important variables for my simulation. It must appear as the very first thing in the .sif file.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb4-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ N = 217</span></span>
<span id="cb4-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ I = 0.74</span></span>
<span id="cb4-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ NI = N*I</span></span>
<span id="cb4-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ A_coil = 0.035 * 0.07 </span></span>
<span id="cb4-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ J = N * I / A_coil</span></span></code></pre></div></div>
</section>
<section id="headers-simulation-and-constants" class="level3">
<h3 class="anchored" data-anchor-id="headers-simulation-and-constants">Headers, Simulation and Constants</h3>
<p>For headers and simulation, just use the default of ElmerGUI. Don’t forget to add Coordinate Scaling if you need it.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb5-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Header</span></span>
<span id="cb5-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  CHECK KEYWORDS Warn</span></span>
<span id="cb5-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Mesh DB "." "."</span></span>
<span id="cb5-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Include Path ""</span></span>
<span id="cb5-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Results Directory ""</span></span>
<span id="cb5-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Simulation</span></span>
<span id="cb5-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Max Output Level = 5</span></span>
<span id="cb5-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Coordinate System = Cartesian</span></span>
<span id="cb5-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Coordinate Mapping(3) = 1 2 3</span></span>
<span id="cb5-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Simulation Type = Steady state</span></span>
<span id="cb5-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Steady State Max Iterations = 1</span></span>
<span id="cb5-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Output Intervals(1) = 1</span></span>
<span id="cb5-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! Solver Input File = case.sif</span></span>
<span id="cb5-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Coordinate Scaling = Real 0.001</span></span>
<span id="cb5-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb5-18"></span>
<span id="cb5-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Constants</span></span>
<span id="cb5-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Gravity(4) = 0 -1 0 9.82</span></span>
<span id="cb5-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Stefan Boltzmann = 5.670374419e-08</span></span>
<span id="cb5-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Permittivity of Vacuum = 8.85418781e-12</span></span>
<span id="cb5-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Permeability of Vacuum = 1.25663706e-6</span></span>
<span id="cb5-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Boltzmann Constant = 1.380649e-23</span></span>
<span id="cb5-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Unit Charge = 1.6021766e-19</span></span>
<span id="cb5-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
</section>
<section id="equation" class="level3">
<h3 class="anchored" data-anchor-id="equation">Equation</h3>
<p>Now we follow the order of Bodies, Solvers, Equation, Materials, Body Force and Boundary Condition.</p>
<p>We will skip the Bodies for now and go directly to Equation.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb6-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Equation 1</span></span>
<span id="cb6-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "MgDyn2D"</span></span>
<span id="cb6-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Active Solvers(3) = 1 2 3 ! &lt;-- Here you define the number of solvers and the execution order.</span></span>
<span id="cb6-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
<p>The solvers must be defined before the equation definition.</p>
<section id="solver-1" class="level4">
<h4 class="anchored" data-anchor-id="solver-1">Solver 1</h4>
<p>Those are default exported from ElmerGUI.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb7-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solver 1</span></span>
<span id="cb7-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = MgDyn2D</span></span>
<span id="cb7-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Variable = Potential</span></span>
<span id="cb7-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Procedure = "MagnetoDynamics2D" "MagnetoDynamics2D"</span></span>
<span id="cb7-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Exec Solver = Always</span></span>
<span id="cb7-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Optimize Bandwidth = True</span></span>
<span id="cb7-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Steady State Convergence Tolerance = 1.0e-5</span></span>
<span id="cb7-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Convergence Tolerance = 1.0e-7</span></span>
<span id="cb7-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Max Iterations = 20</span></span>
<span id="cb7-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Newton After Iterations = 3</span></span>
<span id="cb7-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Newton After Tolerance = 1.0e-3</span></span>
<span id="cb7-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Relaxation Factor = 1</span></span>
<span id="cb7-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Solver = Iterative</span></span>
<span id="cb7-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Iterative Method = BiCGStab</span></span>
<span id="cb7-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Max Iterations = 500</span></span>
<span id="cb7-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Convergence Tolerance = 1.0e-10</span></span>
<span id="cb7-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  BiCGstabl polynomial degree = 2</span></span>
<span id="cb7-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Preconditioning = ILU0</span></span>
<span id="cb7-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Abort Not Converged = False</span></span>
<span id="cb7-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Residual Output = 10</span></span>
<span id="cb7-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
</section>
<section id="solver-2" class="level4">
<h4 class="anchored" data-anchor-id="solver-2">Solver 2</h4>
<p>Those are also default, exported from ElmerGUI.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb8-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solver 2</span></span>
<span id="cb8-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = MgDynPost</span></span>
<span id="cb8-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Procedure = "MagnetoDynamics" "MagnetoDynamicsCalcFields"</span></span>
<span id="cb8-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Target Variable = Potential</span></span>
<span id="cb8-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Exec Solver = Always</span></span>
<span id="cb8-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Steady State Convergence Tolerance = 1.0e-5</span></span>
<span id="cb8-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Solver = Iterative</span></span>
<span id="cb8-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Iterative Method = BiCGStab</span></span>
<span id="cb8-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Max Iterations = 500</span></span>
<span id="cb8-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Convergence Tolerance = 1.0e-10</span></span>
<span id="cb8-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Preconditioning = ILU0</span></span>
<span id="cb8-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Calculate Magnetic Field Strength = Logical True</span></span>
<span id="cb8-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Calculate Magnetic Flux Density = Logical True</span></span>
<span id="cb8-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Calculate Current Density = Logical True</span></span>
<span id="cb8-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
</section>
<section id="solver-3" class="level4">
<h4 class="anchored" data-anchor-id="solver-3">Solver 3</h4>
<p>For solver 3, we just add more post-processing features. We use <code>Vtu Part Collection</code>. This will create separate results improving Paraview post-processing.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb9-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solver 3</span></span>
<span id="cb9-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Exec Solver = After saving</span></span>
<span id="cb9-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = "ResultOutput"</span></span>
<span id="cb9-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Procedure = "ResultOutputSolve" "ResultOutputSolver"</span></span>
<span id="cb9-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Output File Name = "results"</span></span>
<span id="cb9-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Vtu Format = Logical True</span></span>
<span id="cb9-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Vtu Part Collection = Logical True</span></span>
<span id="cb9-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Vtu Multiblock = Logical True</span></span>
<span id="cb9-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
</section>
</section>
<section id="materials" class="level3">
<h3 class="anchored" data-anchor-id="materials">Materials</h3>
<p>We add Air and Iron materials, however, we add non-linearity with an H-B Curve.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb10-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Material 1</span></span>
<span id="cb10-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Air (room temperature)"</span></span>
<span id="cb10-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat Conductivity = 0.0257</span></span>
<span id="cb10-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Relative Permeability = 1.00000037</span></span>
<span id="cb10-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Viscosity = 1.983e-5</span></span>
<span id="cb10-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat expansion Coefficient = 3.43e-3</span></span>
<span id="cb10-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Density = 1.205</span></span>
<span id="cb10-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Relative Permittivity = 1.00059</span></span>
<span id="cb10-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Sound speed = 343.0</span></span>
<span id="cb10-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat Capacity = 1005.0</span></span>
<span id="cb10-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb10-12"></span>
<span id="cb10-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Material 2</span></span>
<span id="cb10-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Iron (generic)"</span></span>
<span id="cb10-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Sound speed = 5000.0</span></span>
<span id="cb10-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! Relative Permeability = 4000</span></span>
<span id="cb10-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  H-B Curve = Variable "dummy"</span></span>
<span id="cb10-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    Real Cubic</span></span>
<span id="cb10-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      Include "hb_iron.txt"</span></span>
<span id="cb10-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    End</span></span>
<span id="cb10-21"></span>
<span id="cb10-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Poisson ratio = 0.29</span></span>
<span id="cb10-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Density = 7870.0</span></span>
<span id="cb10-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat Capacity = 449.0</span></span>
<span id="cb10-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat Conductivity = 80.2</span></span>
<span id="cb10-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Youngs modulus = 193.053e9</span></span>
<span id="cb10-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Electric Conductivity = 10.30e6</span></span>
<span id="cb10-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat expansion Coefficient = 11.8e-6</span></span>
<span id="cb10-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
<p>Please, notice that now I have included a <code>.txt</code> file with the H-B curve data. The correct path to the file must be addressed.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb11-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0 0</span></span>
<span id="cb11-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.05 19.96533</span></span>
<span id="cb11-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.1 29.906288</span></span>
<span id="cb11-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.15 36.398771</span></span>
<span id="cb11-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.2 41.371669</span></span>
<span id="cb11-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.25 45.590262</span></span>
<span id="cb11-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.3 49.424859</span></span>
<span id="cb11-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.35 53.08228</span></span>
<span id="cb11-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.4 56.692664</span></span>
<span id="cb11-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.45 60.347374</span></span>
<span id="cb11-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.5 64.117637</span></span>
<span id="cb11-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.55 68.064755</span></span>
<span id="cb11-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.6 72.246371</span></span>
<span id="cb11-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.65 76.720886</span></span>
<span id="cb11-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.7 81.551123</span></span>
<span id="cb11-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.75 86.807959</span></span>
<span id="cb11-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.8 92.574503</span></span>
<span id="cb11-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.85 98.95152</span></span>
<span id="cb11-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.9 106.065047</span></span>
<span id="cb11-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0.95 114.077777</span></span>
<span id="cb11-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1 123.206911</span></span>
<span id="cb11-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.05 133.753385</span></span>
<span id="cb11-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.1 146.151738</span></span>
<span id="cb11-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.15 161.058668</span></span>
<span id="cb11-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.2 179.516634</span></span>
<span id="cb11-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.25 203.267945</span></span>
<span id="cb11-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.3 235.379748</span></span>
<span id="cb11-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.35 281.524948</span></span>
<span id="cb11-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.4 352.648412</span></span>
<span id="cb11-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.45 470.426179</span></span>
<span id="cb11-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.5 677.451541</span></span>
<span id="cb11-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.55 1051.068036</span></span>
<span id="cb11-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.6 1703.275165</span></span>
<span id="cb11-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.65 2726.517959</span></span>
<span id="cb11-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.7 4137.981052</span></span>
<span id="cb11-36"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.75 5832.619704</span></span>
<span id="cb11-37"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.8 7939.55294</span></span>
<span id="cb11-38"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.85 10565.294335</span></span>
<span id="cb11-39"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.9 13843.912965</span></span>
<span id="cb11-40"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1.95 17970.359698</span></span>
<span id="cb11-41"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2 23423.776432</span></span>
<span id="cb11-42"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2.05 32234.32596</span></span>
<span id="cb11-43"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2.1 51366.778967</span></span>
<span id="cb11-44"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2.15 84577.843501</span></span>
<span id="cb11-45"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2.2 121162.493322</span></span>
<span id="cb11-46"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2.25 160127.812767</span></span>
<span id="cb11-47"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2.3 202470.282245</span></span></code></pre></div></div>
</section>
<section id="body-force" class="level3">
<h3 class="anchored" data-anchor-id="body-force">Body force</h3>
<p>Now we add current to the windings. We added the <code>-distribute</code> flag as presented in the <a href="https://github.com/ElmerCSC/elmerfem/blob/68e7613a4ea08101eaeb6a1f46d16be2f7d5ffbe/fem/src/ModelDescription.F90#L2303">source code</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb12-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body Force 1</span></span>
<span id="cb12-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Circuit_A"</span></span>
<span id="cb12-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Current Density = -distribute $ NI</span></span>
<span id="cb12-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! Current Density = $ J</span></span>
<span id="cb12-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb12-6"></span>
<span id="cb12-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body Force 2</span></span>
<span id="cb12-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Circuit_a"</span></span>
<span id="cb12-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Current Density = -distribute $ -NI</span></span>
<span id="cb12-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! Current Density = $ -J</span></span>
<span id="cb12-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb12-12"></span>
<span id="cb12-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body Force 3</span></span>
<span id="cb12-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Circuit_b"</span></span>
<span id="cb12-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Current Density = Real 0.0 ! $ NI</span></span>
<span id="cb12-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb12-17"></span>
<span id="cb12-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body Force 4</span></span>
<span id="cb12-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Circuit_B"</span></span>
<span id="cb12-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Current Density = Real 0.0 ! $ -NI</span></span>
<span id="cb12-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
</section>
<section id="zero-boundary" class="level3">
<h3 class="anchored" data-anchor-id="zero-boundary">Zero boundary</h3>
<p>Lastly, we add the <code>zero</code> boundary condition.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb13-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Boundary Condition 1</span></span>
<span id="cb13-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Target Boundaries(1) = 19 </span></span>
<span id="cb13-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Zero"</span></span>
<span id="cb13-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Potential = Real 0.0</span></span>
<span id="cb13-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
</section>
<section id="bodies" class="level3">
<h3 class="anchored" data-anchor-id="bodies">Bodies</h3>
<p>Now, just add equation, material and body forces to the target bodies.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb14-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 1</span></span>
<span id="cb14-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = Air</span></span>
<span id="cb14-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb14-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb14-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb14-6"></span>
<span id="cb14-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 2</span></span>
<span id="cb14-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = Iron_Faces</span></span>
<span id="cb14-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb14-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 2</span></span>
<span id="cb14-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb14-12"></span>
<span id="cb14-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 3</span></span>
<span id="cb14-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = coil_a_minus_Faces</span></span>
<span id="cb14-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb14-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb14-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Body Force = 1</span></span>
<span id="cb14-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb14-19"></span>
<span id="cb14-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 4</span></span>
<span id="cb14-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = coil_a_plus_Faces</span></span>
<span id="cb14-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb14-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb14-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Body Force = 2</span></span>
<span id="cb14-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb14-26"></span>
<span id="cb14-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 5</span></span>
<span id="cb14-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = coil_b_minus_Faces</span></span>
<span id="cb14-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb14-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb14-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Body Force = 3</span></span>
<span id="cb14-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb14-33"></span>
<span id="cb14-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 6</span></span>
<span id="cb14-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = coil_b_plus_Faces</span></span>
<span id="cb14-36"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb14-37"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb14-38"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Body Force = 4</span></span>
<span id="cb14-39"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
<p>The complete <code>.sif</code> file is presented bellow.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb15-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ N = 217</span></span>
<span id="cb15-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ I = 0.74</span></span>
<span id="cb15-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ NI = N*I</span></span>
<span id="cb15-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ A_coil = 0.035 * 0.07 </span></span>
<span id="cb15-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">$ J = N * I / A_coil</span></span>
<span id="cb15-6"></span>
<span id="cb15-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Header</span></span>
<span id="cb15-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  CHECK KEYWORDS Warn</span></span>
<span id="cb15-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Mesh DB "." "."</span></span>
<span id="cb15-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Include Path ""</span></span>
<span id="cb15-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Results Directory ""</span></span>
<span id="cb15-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-13"></span>
<span id="cb15-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Simulation</span></span>
<span id="cb15-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Max Output Level = 5</span></span>
<span id="cb15-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Coordinate System = Cartesian</span></span>
<span id="cb15-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Coordinate Mapping(3) = 1 2 3</span></span>
<span id="cb15-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Simulation Type = Steady state</span></span>
<span id="cb15-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Steady State Max Iterations = 1</span></span>
<span id="cb15-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Output Intervals(1) = 1</span></span>
<span id="cb15-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! Solver Input File = case.sif</span></span>
<span id="cb15-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Coordinate Scaling = Real 0.001</span></span>
<span id="cb15-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-24"></span>
<span id="cb15-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Constants</span></span>
<span id="cb15-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Gravity(4) = 0 -1 0 9.82</span></span>
<span id="cb15-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Stefan Boltzmann = 5.670374419e-08</span></span>
<span id="cb15-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Permittivity of Vacuum = 8.85418781e-12</span></span>
<span id="cb15-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Permeability of Vacuum = 1.25663706e-6</span></span>
<span id="cb15-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Boltzmann Constant = 1.380649e-23</span></span>
<span id="cb15-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Unit Charge = 1.6021766e-19</span></span>
<span id="cb15-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-33"></span>
<span id="cb15-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">!------ Skeleton for body section -----</span></span>
<span id="cb15-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 1</span></span>
<span id="cb15-36"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = Air</span></span>
<span id="cb15-37"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb15-38"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb15-39"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-40"></span>
<span id="cb15-41"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 2</span></span>
<span id="cb15-42"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = Iron_Faces</span></span>
<span id="cb15-43"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb15-44"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 2</span></span>
<span id="cb15-45"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-46"></span>
<span id="cb15-47"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 3</span></span>
<span id="cb15-48"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = coil_a_minus_Faces</span></span>
<span id="cb15-49"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb15-50"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb15-51"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Body Force = 1</span></span>
<span id="cb15-52"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-53"></span>
<span id="cb15-54"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 4</span></span>
<span id="cb15-55"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = coil_a_plus_Faces</span></span>
<span id="cb15-56"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb15-57"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb15-58"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Body Force = 2</span></span>
<span id="cb15-59"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-60"></span>
<span id="cb15-61"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 5</span></span>
<span id="cb15-62"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = coil_b_minus_Faces</span></span>
<span id="cb15-63"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb15-64"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb15-65"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Body Force = 3</span></span>
<span id="cb15-66"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-67"></span>
<span id="cb15-68"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body 6</span></span>
<span id="cb15-69"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = coil_b_plus_Faces</span></span>
<span id="cb15-70"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = 1</span></span>
<span id="cb15-71"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Material = 1</span></span>
<span id="cb15-72"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Body Force = 4</span></span>
<span id="cb15-73"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-74"></span>
<span id="cb15-75"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solver 1</span></span>
<span id="cb15-76"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = MgDyn2D</span></span>
<span id="cb15-77"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Variable = Potential</span></span>
<span id="cb15-78"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Procedure = "MagnetoDynamics2D" "MagnetoDynamics2D"</span></span>
<span id="cb15-79"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Exec Solver = Always</span></span>
<span id="cb15-80"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Optimize Bandwidth = True</span></span>
<span id="cb15-81"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Steady State Convergence Tolerance = 1.0e-5</span></span>
<span id="cb15-82"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Convergence Tolerance = 1.0e-7</span></span>
<span id="cb15-83"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Max Iterations = 20</span></span>
<span id="cb15-84"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Newton After Iterations = 3</span></span>
<span id="cb15-85"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Newton After Tolerance = 1.0e-3</span></span>
<span id="cb15-86"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Nonlinear System Relaxation Factor = 1</span></span>
<span id="cb15-87"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Solver = Iterative</span></span>
<span id="cb15-88"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Iterative Method = BiCGStab</span></span>
<span id="cb15-89"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Max Iterations = 500</span></span>
<span id="cb15-90"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Convergence Tolerance = 1.0e-10</span></span>
<span id="cb15-91"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  BiCGstabl polynomial degree = 2</span></span>
<span id="cb15-92"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Preconditioning = ILU0</span></span>
<span id="cb15-93"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Abort Not Converged = False</span></span>
<span id="cb15-94"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Residual Output = 10</span></span>
<span id="cb15-95"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-96"></span>
<span id="cb15-97"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solver 2</span></span>
<span id="cb15-98"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = MgDynPost</span></span>
<span id="cb15-99"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Procedure = "MagnetoDynamics" "MagnetoDynamicsCalcFields"</span></span>
<span id="cb15-100"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Target Variable = Potential</span></span>
<span id="cb15-101"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Exec Solver = Always</span></span>
<span id="cb15-102"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Steady State Convergence Tolerance = 1.0e-5</span></span>
<span id="cb15-103"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Solver = Iterative</span></span>
<span id="cb15-104"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Iterative Method = BiCGStab</span></span>
<span id="cb15-105"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Max Iterations = 500</span></span>
<span id="cb15-106"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Convergence Tolerance = 1.0e-10</span></span>
<span id="cb15-107"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Linear System Preconditioning = ILU0</span></span>
<span id="cb15-108"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Calculate Magnetic Field Strength = Logical True</span></span>
<span id="cb15-109"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Calculate Magnetic Flux Density = Logical True</span></span>
<span id="cb15-110"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Calculate Current Density = Logical True</span></span>
<span id="cb15-111"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-112"></span>
<span id="cb15-113"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solver 3</span></span>
<span id="cb15-114"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Exec Solver = After saving</span></span>
<span id="cb15-115"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = "ResultOutput"</span></span>
<span id="cb15-116"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Procedure = "ResultOutputSolve" "ResultOutputSolver"</span></span>
<span id="cb15-117"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Output File Name = "results"</span></span>
<span id="cb15-118"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Vtu Format = Logical True</span></span>
<span id="cb15-119"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Vtu Part Collection = Logical True</span></span>
<span id="cb15-120"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Vtu Multiblock = Logical True</span></span>
<span id="cb15-121"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-122"></span>
<span id="cb15-123"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Equation 1</span></span>
<span id="cb15-124"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "MgDyn2D"</span></span>
<span id="cb15-125"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Active Solvers(3) = 1 2 3</span></span>
<span id="cb15-126"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-127"></span>
<span id="cb15-128"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Material 1</span></span>
<span id="cb15-129"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Air (room temperature)"</span></span>
<span id="cb15-130"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat Conductivity = 0.0257</span></span>
<span id="cb15-131"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Relative Permeability = 1.00000037</span></span>
<span id="cb15-132"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Viscosity = 1.983e-5</span></span>
<span id="cb15-133"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat expansion Coefficient = 3.43e-3</span></span>
<span id="cb15-134"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Density = 1.205</span></span>
<span id="cb15-135"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Relative Permittivity = 1.00059</span></span>
<span id="cb15-136"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Sound speed = 343.0</span></span>
<span id="cb15-137"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat Capacity = 1005.0</span></span>
<span id="cb15-138"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-139"></span>
<span id="cb15-140"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Material 2</span></span>
<span id="cb15-141"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Iron (generic)"</span></span>
<span id="cb15-142"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Sound speed = 5000.0</span></span>
<span id="cb15-143"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! Relative Permeability = 4000</span></span>
<span id="cb15-144"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  H-B Curve = Variable "dummy"</span></span>
<span id="cb15-145"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    Real Cubic</span></span>
<span id="cb15-146"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      Include "bh_iron.txt"</span></span>
<span id="cb15-147"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    End</span></span>
<span id="cb15-148"></span>
<span id="cb15-149"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Poisson ratio = 0.29</span></span>
<span id="cb15-150"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Density = 7870.0</span></span>
<span id="cb15-151"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat Capacity = 449.0</span></span>
<span id="cb15-152"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat Conductivity = 80.2</span></span>
<span id="cb15-153"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Youngs modulus = 193.053e9</span></span>
<span id="cb15-154"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Electric Conductivity = 10.30e6</span></span>
<span id="cb15-155"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Heat expansion Coefficient = 11.8e-6</span></span>
<span id="cb15-156"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-157"></span>
<span id="cb15-158"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body Force 1</span></span>
<span id="cb15-159"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Circuit_A"</span></span>
<span id="cb15-160"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Current Density = -distribute $ NI</span></span>
<span id="cb15-161"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! Current Density = $ J</span></span>
<span id="cb15-162"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-163"></span>
<span id="cb15-164"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body Force 2</span></span>
<span id="cb15-165"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Circuit_a"</span></span>
<span id="cb15-166"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Current Density = -distribute $ -NI</span></span>
<span id="cb15-167"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! Current Density = $ -J</span></span>
<span id="cb15-168"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-169"></span>
<span id="cb15-170"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body Force 3</span></span>
<span id="cb15-171"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Circuit_b"</span></span>
<span id="cb15-172"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Current Density = Real 0.0 ! $ NI</span></span>
<span id="cb15-173"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-174"></span>
<span id="cb15-175"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body Force 4</span></span>
<span id="cb15-176"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Circuit_B"</span></span>
<span id="cb15-177"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Current Density = Real 0.0 ! $ -NI</span></span>
<span id="cb15-178"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span>
<span id="cb15-179"></span>
<span id="cb15-180"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Boundary Condition 1</span></span>
<span id="cb15-181"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Target Boundaries(1) = 19</span></span>
<span id="cb15-182"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = zerobound_Edges</span></span>
<span id="cb15-183"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Name = "Zero"</span></span>
<span id="cb15-184"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Potential = Real 0.0</span></span>
<span id="cb15-185"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
</section>
</section>
<section id="simulation" class="level2">
<h2 class="anchored" data-anchor-id="simulation">Simulation</h2>
<p>Now we run the simulation.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb16-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> ElmerSolver my_first_sif_file.sif </span></code></pre></div></div>
</section>
<section id="paraview" class="level2">
<h2 class="anchored" data-anchor-id="paraview">Paraview</h2>
<p>Now, inside paraview, you will notice a <code>.vtu</code> file. It has now different blocks that can be evaluated in paraview. It will be easier for post-processing. For now, let’s just see the expected result.</p>
<p>The total magnetic length is 420mm.</p>
<p><img src="https://latex.codecogs.com/png.latex?H%20=%20%5Cfrac%7BN%20%5Ccdot%20I%7D%7Bl_m%7D%20=%20%5Cfrac%7B160.6%7D%7B0.385%7D%20=%20417.17~%5Ctext%7BA/m%7D"></p>
<p>Lookig the HB curve, that gives us aroung 1.43T.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core mastering sif file/images/image-18-1024x575.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 1 - Elmer post-processing using ParaView.</p>
</figcaption>
</figure>
<p>The surface current integral shows 160.58 A.e and 1.48 T of normal flux density.</p>
</section>
<section id="post-processing" class="level2">
<h2 class="anchored" data-anchor-id="post-processing">Post-processing</h2>
<p>One last thing before we close this post, we can also perform post-processing natively using Elmer. Sometimes it is better to evaluate outside ParaView. For us, it is better just to compare both results. We are going to add plot over line flux, calculate the coil’s Area, and the Potential integral (Flux Linkage). With current and flux, we get inductance.</p>
<p>To achieve this, we need to add two internal solvers to our <code>.sif</code> file: <code>SaveLine</code> and <code>SaveScalars</code>.</p>
<section id="plot-over-line-saveline" class="level3">
<h3 class="anchored" data-anchor-id="plot-over-line-saveline">Plot Over Line (SaveLine)</h3>
<p>We add a 4th solver to extract data across a specific gap line:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb17-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solver 4</span></span>
<span id="cb17-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = SaveLine</span></span>
<span id="cb17-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Procedure = "SaveData" "SaveLine"</span></span>
<span id="cb17-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Filename = "linha_fluxo.dat"</span></span>
<span id="cb17-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  File Append = Logical False</span></span>
<span id="cb17-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Polyline Coordinates(2,2) = Real -0.14 0.0  0.14 0.0 </span></span>
<span id="cb17-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
<p><img src="https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core mastering sif file/images/teste.png" class="img-fluid"></p>
</section>
<section id="area-and-flux-linkage-savescalars" class="level3">
<h3 class="anchored" data-anchor-id="area-and-flux-linkage-savescalars">Area and Flux Linkage (SaveScalars)</h3>
<p>Instead of trying to integrate the current directly (which yields zero in a steady-state 2D model due to zero conductivity), we integrate the geometric Volume (Area in 2D) and the Magnetic Vector Potential (<img src="https://latex.codecogs.com/png.latex?A%5C_z">) over the coil surface:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb18-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solver 5</span></span>
<span id="cb18-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Equation = "SaveScalars"</span></span>
<span id="cb18-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Procedure = "SaveData" "SaveScalars"</span></span>
<span id="cb18-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Filename = "integrais_bobina.dat"</span></span>
<span id="cb18-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  File Append = Logical False</span></span>
<span id="cb18-6"></span>
<span id="cb18-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! 1. Area validation</span></span>
<span id="cb18-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Variable 1 = Coordinate 1</span></span>
<span id="cb18-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Operator 1 = volume</span></span>
<span id="cb18-10"></span>
<span id="cb18-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! 2. Potential Integral for Flux Linkage</span></span>
<span id="cb18-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Variable 2 = Potential</span></span>
<span id="cb18-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Operator 2 = int</span></span>
<span id="cb18-14"></span>
<span id="cb18-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  ! Apply exactly to the bodies marked with this tag</span></span>
<span id="cb18-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Mask Name 1 = "Integrar Corrente"</span></span>
<span id="cb18-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  Mask Name 2 = "Integrar Corrente"</span></span>
<span id="cb18-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">End</span></span></code></pre></div></div>
<p>Don’t forget to update your equation to include the new solvers (<code>Active Solvers(5) = 1 2 3 4 5</code>) and to add the mask <code>Integrar Corrente = Logical True</code> to the Body blocks representing your coils.The results are exported directly to standard text files (.dat), which are extremely friendly for pipelines using Python or Julia. With the integrated Potential (IAI_A) and the known coil Area (AcoilA_{coil}), the Inductance (LL) is easily calculated by:</p>
<p><img src="https://latex.codecogs.com/png.latex?L%20=%20%5Cfrac%7BN%7D%7BI%20%5Ccdot%20A_%7Bcoil%7D%7D%20%5Ciint%20A_z%20%5C,%20dA"></p>
<p>I’ve improved some parts. You can check the result <a href="https://github.com/thalesmaoa/Elmer-Examples/tree/main/Electrical_Machines/Static/Transformers/2D_Simulations/EICore">here</a>.</p>
<p><a href="https://github.com/thalesmaoa/Elmer-Examples/tree/main/Electrical_Machines/Static/Transformers/2D_Simulations/EICore">Get the Source Code o</a><a href="https://github.com/thalesmaoa/avr-3level-spwm">n GitHub</a></p>
<!--Include social share buttons-->
<div class="social-share">
<p> <svg class="social-icon" viewbox="0 0 24 24"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 1 1 0-4.125 2.062 2.062 0 0 1 0 4.125zM7.114 20.452H3.558V9h3.556v11.452z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2.05 22l5.25-1.38a9.9 9.9 0 0 0 4.74 1.21h.01c5.46 0 9.91-4.45 9.91-9.91C21.96 6.45 17.5 2 12.04 2zm5.74 14.09c-.24.68-1.4 1.3-1.93 1.36-.51.06-1.03.24-3.45-.72-2.9-1.15-4.76-4.1-4.9-4.29-.14-.19-1.17-1.55-1.17-2.96 0-1.4.74-2.09 1-2.38.26-.29.57-.36.76-.36.19 0 .38 0 .55.01.18.01.41-.07.64.49.24.57.81 1.98.88 2.12.07.14.12.31.02.5-.1.19-.14.31-.28.48-.14.17-.29.38-.42.51-.14.14-.28.29-.12.57.16.29.72 1.19 1.55 1.93 1.06.95 1.96 1.24 2.24 1.38.29.14.46.12.63-.07.17-.19.72-.84.91-1.13.19-.29.38-.24.64-.14.26.1 1.64.77 1.92.91.29.14.48.21.55.33.07.12.07.69-.17 1.37z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M22 2 2 10.5l6 2 2 6.5 3-4 5 4 4-17zM8.5 13.5l9-7-7 8.5v3z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4-8 5-8-5V6l8 5 8-5v2z"></path></svg>  <button class="social-link copy-link-btn" data-share="copy" aria-label="Copiar link"> <svg class="social-icon" viewbox="0 0 24 24"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path></svg> </button></p>
</div>
<style>
.social-share {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  align-items: center;
  margin: 1rem 0;
}
.social-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: none;
  outline: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  text-decoration: none;
  box-shadow: none;
}
.social-link::-moz-focus-inner {
  border: 0;
}
.social-icon {
  width: 25px;
  height: 25px;
  fill: #1a1a1a;
  display: block;
}
.social-link:hover .social-icon {
  fill: #000;
}
.social-link:hover {
  background: rgba(0,0,0,0.08);
}
</style>
<!-- <script src="/files/scripts/socialshare.js"></script> -->
<script>
    document.addEventListener("DOMContentLoaded", function () {
  const pageUrl = encodeURIComponent(window.location.href);
  const pageTitle = encodeURIComponent(document.title);

  const shareUrls = {
    linkedin: "https://www.linkedin.com/sharing/share-offsite/?url=" + pageUrl,
    whatsapp: "https://wa.me/?text=" + pageTitle + "%20" + pageUrl,
    telegram: "https://t.me/share/url?url=" + pageUrl + "&text=" + pageTitle
  };

  document.querySelectorAll("[data-share]").forEach(function (el) {
    const type = el.getAttribute("data-share");

    if (type === "copy") {
      el.addEventListener("click", function () {
        navigator.clipboard.writeText(window.location.href);
        el.setAttribute("aria-label", "Link copiado!");
      }, true);
      return;
    }

    if (type === "email") {
      el.setAttribute("href", "mailto:?subject=" + pageTitle + "&body=" + pageUrl);
      return;
    }

    if (shareUrls[type]) {
      // Captura o clique ANTES de qualquer outro listener (inclusive o tracker do tema)
      el.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(shareUrls[type], "_blank", "noopener,noreferrer");
      }, true); // true = capture phase, roda primeiro
    }
  });
});
</script>


</section>
</section>

 ]]></description>
  <category>Simulation</category>
  <category>Electrical Machines</category>
  <category>Tutorial</category>
  <guid>https://thalesmaia.com/pages/blog/20260504 - Designing 2D transformer core mastering sif file/</guid>
  <pubDate>Mon, 04 May 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Designing a 2D Transformer Core: Steady-State Simulation in ElmerFEM (Part 2)</title>
  <link>https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/</link>
  <description><![CDATA[ 




<p>In this second part of our series, we transition from geometry preparation in FreeCAD to the simulation environment of <strong>ElmerFEM</strong>. If you missed <a href="../20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/">Part 1, you can catch up via the link</a>.</p>
<p><strong>ElmerGUI</strong> streamlines the workflow by managing the Solver Input File (<code>.sif</code>) directly through its interface. In this stage, we will compute the static magnetic flux generated by the transformer’s primary winding and later apply a load to the secondary winding to validate our Finite Element Method (FEM) results against analytical calculations.</p>
<section id="understanding-elmergui" class="level2">
<h2 class="anchored" data-anchor-id="understanding-elmergui">Understanding ElmerGUI</h2>
<p>Before we start, it is important to understand that ElmerGUI is a great tool for those starting with Elmer. However, you will soon find that the core configuration eventually leads back to the <code>.sif</code> file, which we will learn to edit in this post.</p>
<section id="first-steps-importing-and-scaling" class="level3">
<h3 class="anchored" data-anchor-id="first-steps-importing-and-scaling">First Steps: Importing and Scaling</h3>
<p>The first task is to import the mesh we generated previously. Go to <strong>File &gt;&gt; Open</strong> and select your <code>.unv</code> file.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-1024x575.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 1 - The ElmerGUI interface showing the successfully imported transformer mesh, including the core and surrounding air domain.</p>
</figcaption>
</figure>
<p>For the model to work correctly, we need two specific solvers that aren’t active by default in the UI:</p>
<ol type="1">
<li><p>Go to <strong>File &gt;&gt; Definitions</strong>.</p></li>
<li><p>Click the <strong>Append</strong> button.</p></li>
<li><p>Navigate to the <code>edf-extra</code> folder and select <code>magnetodynamics2d.xml</code> and <code>magnetodynamics.xml</code>.</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-1-1024x575.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 2 - Appending the Magnetodynamics definitions to enable magnetic field simulation capabilities.</p>
</figcaption>
</figure>
<blockquote class="blockquote">
<p><strong>Important Note on Units:</strong> If you modeled your geometry in millimeters or inches, you must add a <strong>Coordinate Scaling</strong> factor. Go to <strong>Model &gt;&gt; Setup</strong> and set the scale to <code>0.001</code> (to convert mm to meters), as Elmer operates in SI units.</p>
</blockquote>
</section>
<section id="setting-up-the-equation" class="level3">
<h3 class="anchored" data-anchor-id="setting-up-the-equation">Setting Up the Equation</h3>
<p>We must inform Elmer what physics it needs to solve.</p>
<ol type="1">
<li><p>In the left sidebar, under <strong>Model</strong>, click <strong>Equation &gt;&gt; Add</strong>.</p></li>
<li><p>In the <strong>MgDyn2D</strong> tab, activate the solver and apply it to all bodies.</p></li>
<li><p>Set the <strong>Priority</strong> to <strong>2</strong> (higher numbers indicate higher priority).</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-10-1024x576.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 3 - Configuring the 2D Magnetodynamics equation and assigning execution priority 1.</p>
</figcaption>
</figure>
<ol type="1">
<li><p>Now look for <strong>MgDynPost</strong> (used for post-processing results). Activate it and set its <strong>Priority</strong> to <strong>1</strong>.</p></li>
<li><p>Name this set “MgDyn” before clicking OK.</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-11-1024x576.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 4 - Configuring the 2D Magnetodynamics equation and assigning execution priority 1.</p>
</figcaption>
</figure>
</section>
<section id="materials-and-body-identification" class="level3">
<h3 class="anchored" data-anchor-id="materials-and-body-identification">Materials and Body Identification</h3>
<p>Before assigning materials, it is best practice to rename your bodies for better organization. Double-click the numbered bodies in the left menu to rename them (e.g., Core, Coil_A, Air).</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-5-1024x575.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 5 - Renaming the mesh bodies to simplify the assignment of materials and forces.</p>
</figcaption>
</figure>
<p>Now, apply the materials via <strong>Model &gt;&gt; Material &gt;&gt; Add</strong>:</p>
<ul>
<li><p><strong>Air and Coils:</strong> Choose “Air” from the Material library and apply it to the Air and Coil bodies.</p></li>
<li><p><strong>Core (Iron):</strong> Select “Iron”. In the <strong>MgDyn2D</strong> tab, set the <strong>Relative Permeability</strong> to <strong>4000</strong>.Now we can go to Material and click in [Add..]. Click in the Material library. Chose Air and Apply to bodies Air and Coils. Follow the same process, but now, select the Iron material.</p></li>
</ul>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-6-1024x575.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 6 - Defining the magnetic properties for the transformer core material.</p>
</figcaption>
</figure>
</section>
<section id="body-force-adding-the-current-source" class="level3">
<h3 class="anchored" data-anchor-id="body-force-adding-the-current-source">Body Force: Adding the Current Source</h3>
<p>We must add the current source for the primary winding. In ElmerGUI, we enter the total surface current density (J). If we consider a total current of 60\ A.e, the surface current density is calculated as:</p>
<p><img src="https://latex.codecogs.com/png.latex?J%20=%20%5Cfrac%7BN%20%5Ccdot%20I%7D%7BA%7D%20=%20%5Cfrac%7B60~%5Ctext%7BA.e%7D%7D%7B70~%5Ctext%7Bmm%7D%20%5Ctimes%2017.5~%5Ctext%7Bmm%7D%7D%20=%2048%5C,979.6~%5Ctext%7BA/m%7D%5E2"></p>
<ol type="1">
<li><p>Go to <strong>Model &gt;&gt; Body Force &gt;&gt; Add</strong>.</p></li>
<li><p>In the <strong>MgDyn2D</strong> tab, check <strong>Calculate Potential</strong> and enter the calculated value above.</p></li>
<li><p>Create a second Body Force for the opposite coil using a <strong>negative</strong> value to represent the return path of the current.</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-7-1024x576.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 7 - Applying the calculated surface current density as a Body Force to the coils. Positive.</p>
</figcaption>
</figure>
<p>Create a new Body force, but now you should choose the opposite coil and put a negative surface current value.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-8-1024x576.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 8 - Applying the calculated surface current density as a Body Force to the coils. Negative.</p>
</figcaption>
</figure>
</section>
<section id="boundary-conditions" class="level3">
<h3 class="anchored" data-anchor-id="boundary-conditions">Boundary Conditions</h3>
<p>Finally, we define the simulation boundary.</p>
<ol type="1">
<li><p>Go to <strong>Model &gt;&gt; Boundary Condition &gt;&gt; Add</strong>.</p></li>
<li><p>Select the <strong>outer boundary</strong> of the air domain.</p></li>
<li><p>Set the <strong>Dirichlet Potential</strong> to <strong>0</strong>. This ensures the magnetic flux is contained within our simulation space.</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-9-1024x576.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 9 - Setting the Dirichlet boundary condition to zero at the outer edges of the model.</p>
</figcaption>
</figure>
</section>
<section id="the-sif-file-and-running-the-simulation" class="level3">
<h3 class="anchored" data-anchor-id="the-sif-file-and-running-the-simulation">The Sif File and Running the Simulation</h3>
<p>We are now ready for the first simulation run.</p>
<ol type="1">
<li><p>Click on <strong>Sif</strong> in the upper bar menu and select <strong>Generate</strong>.</p></li>
<li><p>Click <strong>Sif</strong> again and select <strong>Edit</strong>. You will be able to read every modification we’ve made in text format.</p></li>
<li><p>After checking the settings, click the <strong>Run</strong> icon to start the solver.</p></li>
</ol>
</section>
<section id="paraview-results" class="level3">
<h3 class="anchored" data-anchor-id="paraview-results">Paraview Results</h3>
<p>If everything went fine, the solver will finish, and you will be able to view the results in <strong>Paraview</strong>. We will discuss the visualization of flux lines and density <a href="../20260504 - Designing 2D transformer core post processing with paraview part3/">in our next topic</a>!</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/images/image-12-1024x576.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 10 - Paraview post-processing.</p>
</figcaption>
</figure>
<!--Include social share buttons-->
<div class="social-share">
<p> <svg class="social-icon" viewbox="0 0 24 24"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 1 1 0-4.125 2.062 2.062 0 0 1 0 4.125zM7.114 20.452H3.558V9h3.556v11.452z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2.05 22l5.25-1.38a9.9 9.9 0 0 0 4.74 1.21h.01c5.46 0 9.91-4.45 9.91-9.91C21.96 6.45 17.5 2 12.04 2zm5.74 14.09c-.24.68-1.4 1.3-1.93 1.36-.51.06-1.03.24-3.45-.72-2.9-1.15-4.76-4.1-4.9-4.29-.14-.19-1.17-1.55-1.17-2.96 0-1.4.74-2.09 1-2.38.26-.29.57-.36.76-.36.19 0 .38 0 .55.01.18.01.41-.07.64.49.24.57.81 1.98.88 2.12.07.14.12.31.02.5-.1.19-.14.31-.28.48-.14.17-.29.38-.42.51-.14.14-.28.29-.12.57.16.29.72 1.19 1.55 1.93 1.06.95 1.96 1.24 2.24 1.38.29.14.46.12.63-.07.17-.19.72-.84.91-1.13.19-.29.38-.24.64-.14.26.1 1.64.77 1.92.91.29.14.48.21.55.33.07.12.07.69-.17 1.37z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M22 2 2 10.5l6 2 2 6.5 3-4 5 4 4-17zM8.5 13.5l9-7-7 8.5v3z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4-8 5-8-5V6l8 5 8-5v2z"></path></svg>  <button class="social-link copy-link-btn" data-share="copy" aria-label="Copiar link"> <svg class="social-icon" viewbox="0 0 24 24"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path></svg> </button></p>
</div>
<style>
.social-share {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  align-items: center;
  margin: 1rem 0;
}
.social-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: none;
  outline: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  text-decoration: none;
  box-shadow: none;
}
.social-link::-moz-focus-inner {
  border: 0;
}
.social-icon {
  width: 25px;
  height: 25px;
  fill: #1a1a1a;
  display: block;
}
.social-link:hover .social-icon {
  fill: #000;
}
.social-link:hover {
  background: rgba(0,0,0,0.08);
}
</style>
<!-- <script src="/files/scripts/socialshare.js"></script> -->
<script>
    document.addEventListener("DOMContentLoaded", function () {
  const pageUrl = encodeURIComponent(window.location.href);
  const pageTitle = encodeURIComponent(document.title);

  const shareUrls = {
    linkedin: "https://www.linkedin.com/sharing/share-offsite/?url=" + pageUrl,
    whatsapp: "https://wa.me/?text=" + pageTitle + "%20" + pageUrl,
    telegram: "https://t.me/share/url?url=" + pageUrl + "&text=" + pageTitle
  };

  document.querySelectorAll("[data-share]").forEach(function (el) {
    const type = el.getAttribute("data-share");

    if (type === "copy") {
      el.addEventListener("click", function () {
        navigator.clipboard.writeText(window.location.href);
        el.setAttribute("aria-label", "Link copiado!");
      }, true);
      return;
    }

    if (type === "email") {
      el.setAttribute("href", "mailto:?subject=" + pageTitle + "&body=" + pageUrl);
      return;
    }

    if (shareUrls[type]) {
      // Captura o clique ANTES de qualquer outro listener (inclusive o tracker do tema)
      el.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(shareUrls[type], "_blank", "noopener,noreferrer");
      }, true); // true = capture phase, roda primeiro
    }
  });
});
</script>


</section>
</section>

 ]]></description>
  <category>Simulation</category>
  <category>Electrical Machines</category>
  <category>Tutorial</category>
  <guid>https://thalesmaia.com/pages/blog/20260501 - Designing 2D transformer core steady state simulation elmerfem part2/</guid>
  <pubDate>Fri, 01 May 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Designing a 2D Transformer Core: FreeCAD to ElmerFEM Integration (Part 1)</title>
  <link>https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/</link>
  <description><![CDATA[ 




<p>For an electrical machine designer, Finite Element Analysis (FEA) is an indispensable tool. Although a draft project usually begins with a base analytical design, high-performance machines demand flux path optimization, geometry tweaks, and deep material probing.</p>
<p>One of the best ways to achieve this is by creating a parametric drawing and coordinating it using a scripting language. For this, FreeCAD is my go-to choice. Its native Python API allows for complete automation and control over the design variables.</p>
<p>However, before diving into scripting, it is crucial to understand how to structure your first design properly. This post will guide you through that process. While the geometry may seem extremely simple, a few specific tricks will save you hours of rework and frustration.</p>
<p>We are starting with a standard 2D EI transformer core, inspired by the classic <a href="https://www.femm.info/wiki/ACForceExample">FEMM tutorials</a>. Please note that I am using the latest FreeCAD version (v1.1) for this guide.</p>
<section id="parameters" class="level2">
<h2 class="anchored" data-anchor-id="parameters">Parameters</h2>
<p>This step is not mandatory, and you can play with constraints for the parametric design as you wish. However, establishing parameters is a fundamental habit for me. I will draw an E-core with a center length of 70mm and lateral legs of 35mm. In total, the height is 140mm, and the coils are fully fitted within a 35mm window.</p>
<ol type="1">
<li><p>Create a new document.</p></li>
<li><p>Switch to the <strong>Spreadsheet</strong> workbench.</p></li>
<li><p>Create a <strong>New Spreadsheet</strong>. You can rename it (by pressing <code>F2</code>) as you like. Mine is named <code>vars</code>.</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-1-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 1 - Creating a new spreadsheet to host our parametric variables.</p>
</figcaption>
</figure>
<p>After filling out the spreadsheet, you should add aliases to the cells. This creates a global variable that is easy to access during the drawing phase. Just right-click on the value cell » <strong>Properties</strong> » <strong>Alias</strong> tab, and type the variable name.</p>
</section>
<section id="the-sketch" class="level2">
<h2 class="anchored" data-anchor-id="the-sketch">The Sketch</h2>
<ol type="1">
<li><p>Switch to the <strong>Part Design</strong> (or <strong>Sketcher</strong>) workbench.</p></li>
<li><p>Create a <strong>New Sketch</strong>.</p></li>
<li><p>Select any plane you prefer. You will likely see a window similar to the one below.</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-1024x574.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 3 - Selecting the sketch plane. For 2D FEM, the XY-plane is usually the standard choice.</p>
</figcaption>
</figure>
<p>After that, draw the model, apply the geometric constraints, and link your spreadsheet variables by clicking the small <code>f(x)</code> icon in the dimension input boxes or just type <code>=</code>.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-2-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 3 - The completed EI core geometry. Note the green lines indicating the sketch is fully constrained and tied to the variables.</p>
</figcaption>
</figure>
</section>
<section id="generating-the-faces-surface" class="level2">
<h2 class="anchored" data-anchor-id="generating-the-faces-surface">Generating the Faces (Surface)</h2>
<p>With the 2D geometry ready, it’s time to route it to Gmsh. Inside FreeCAD’s FEM workbench, meshing works reliably on solid bodies or faces, not directly on 2D wireframe sketches. Therefore, we must convert our sketch into 2D faces.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-3.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 4 - Using the Filling tool in the Surface workbench to create the 2D domains for each part.</p>
</figcaption>
</figure>
<ul>
<li>Select your sketch and use tools to generate flat faces. You will need distinct faces for the Air, the Core, and the Coils.</li>
</ul>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-4-1024x575.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 5 - Generating the primary surfaces using the Ruled Surface tool. Note that the domains for air, core, and coils are currently overlapping.</p>
</figcaption>
</figure>
<ul>
<li>If you end up with overlapping faces, they must be broken into their own specific, non-overlapping domains.</li>
</ul>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-5-1024x574.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 6 - Crucial step: Using ‘Slice Apart’ to ensure that overlapping surfaces (like coils inside the air window) are properly divided.</p>
</figcaption>
</figure>
<ul>
<li>This part can be tricky: using the <strong>Part</strong> workbench, you must select the surfaces (from the outermost to the innermost) and use the <strong>Slice Apart</strong> (or Boolean Fragments) tool.</li>
</ul>
<p>Once properly sliced, use the <strong>Connect</strong> tool to ensure all adjacent faces share the same boundary edges.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-6-1024x574.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 7 - The ‘Connect’ tool ensures that adjacent faces share the same nodes, which is vital for a continuous mesh.</p>
</figcaption>
</figure>
</section>
<section id="meshing-with-gmsh" class="level2">
<h2 class="anchored" data-anchor-id="meshing-with-gmsh">Meshing with Gmsh</h2>
<p>Now it’s time to mesh the model.</p>
<ol type="1">
<li><p>Switch to the <strong>FEM</strong> workbench.</p></li>
<li><p>Select your <code>Connect</code> (or <code>Fragment</code>) group in the tree view and click <strong>FEM mesh from shape by Gmsh</strong>.</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-7.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 8 - Initializing the FEM Mesh object using the Gmsh engine from the Part Design body.</p>
</figcaption>
</figure>
<p>In the configuration panel, you can choose the element dimension as <em>From Shape</em> or <em>2D</em>, select <em>2nd order elements</em>, and define the maximum and minimum mesh sizes based on your design requirements.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-8-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 9 - Adjusting the min/max element size and choosing 2nd-order elements for better numerical accuracy.</p>
</figcaption>
</figure>
<p>If you need to improve the mesh density in specific areas, use the <strong>Mesh Refinement</strong> tool, select the target surface, and adjust as needed. To apply the changes, double-click the Gmsh object in the tree and hit apply to re-mesh.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-9-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 10 - Applying local mesh refinement in areas where we expect high magnetic flux density gradients.</p>
</figcaption>
</figure>
<section id="materials-and-boundaries-the-mesh-groups-trick" class="level3">
<h3 class="anchored" data-anchor-id="materials-and-boundaries-the-mesh-groups-trick">Materials and Boundaries (The Mesh Groups Trick)</h3>
<p>FreeCAD’s visual material tools do not export properly to Elmer when generating <code>.unv</code> files. Instead, materials and boundaries are treated as <strong>Physical Groups</strong>.</p>
<p>To define them:</p>
<ol type="1">
<li><p>Click on <strong>FEM mesh region</strong> (Mesh Group).</p></li>
<li><p>Select the specific surface (e.g., the steel core) and assign a Label name (e.g., <code>Core</code>). The exact same logic applies to the boundaries (edges), which will later be defined as Magnetic Potentials inside Elmer.<br>
<em>Crucial Tip:</em> Always ensure you are selecting the edges/faces of your final <em>Connect/Face</em> object, not the original <em>Sketch</em>!</p></li>
</ol>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-10-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 11 - Creating Mesh Groups.</p>
</figcaption>
</figure>
<p>The final result is presented bellow.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-11-1024x577.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 12 - The complete FEM model in FreeCAD. Observe the tree view with mesh refinements and Mesh Groups (Physical Groups) properly assigned, ensuring a structured entity definition for the export.</p>
</figcaption>
</figure>
</section>
</section>
<section id="exporting-to-elmer" class="level2">
<h2 class="anchored" data-anchor-id="exporting-to-elmer">Exporting to Elmer</h2>
<p>The model must now be exported for Elmer to understand. Select your Gmsh mesh object in the tree, go to <strong>File » Export…</strong>, and save it as an I-DEAS Universal (<code>.unv</code>) file.</p>
<p>Open ElmerGUI, go to <strong>File » Open</strong>, select your <code>.unv</code> file, and check the final imported mesh. You should see your named physical groups translated perfectly into <em>Body Properties</em> and <em>Boundary Properties</em>.</p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/images/image-12-1024x575.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 12 - Import check: The .unv file is successfully parsed by Elmer, showing all bodies and boundaries ready for physics assignment.</p>
</figcaption>
</figure>
<p>Now it’s time to assign the electromagnetic equations, materials, and start the Elmer simulation— <a href="../20260501 - Designing 2D transformer core steady state simulation elmerfem part2/">but that is a topic for the next post</a>.</p>
<!--Include social share buttons-->
<div class="social-share">
<p> <svg class="social-icon" viewbox="0 0 24 24"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 1 1 0-4.125 2.062 2.062 0 0 1 0 4.125zM7.114 20.452H3.558V9h3.556v11.452z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2.05 22l5.25-1.38a9.9 9.9 0 0 0 4.74 1.21h.01c5.46 0 9.91-4.45 9.91-9.91C21.96 6.45 17.5 2 12.04 2zm5.74 14.09c-.24.68-1.4 1.3-1.93 1.36-.51.06-1.03.24-3.45-.72-2.9-1.15-4.76-4.1-4.9-4.29-.14-.19-1.17-1.55-1.17-2.96 0-1.4.74-2.09 1-2.38.26-.29.57-.36.76-.36.19 0 .38 0 .55.01.18.01.41-.07.64.49.24.57.81 1.98.88 2.12.07.14.12.31.02.5-.1.19-.14.31-.28.48-.14.17-.29.38-.42.51-.14.14-.28.29-.12.57.16.29.72 1.19 1.55 1.93 1.06.95 1.96 1.24 2.24 1.38.29.14.46.12.63-.07.17-.19.72-.84.91-1.13.19-.29.38-.24.64-.14.26.1 1.64.77 1.92.91.29.14.48.21.55.33.07.12.07.69-.17 1.37z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M22 2 2 10.5l6 2 2 6.5 3-4 5 4 4-17zM8.5 13.5l9-7-7 8.5v3z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4-8 5-8-5V6l8 5 8-5v2z"></path></svg>  <button class="social-link copy-link-btn" data-share="copy" aria-label="Copiar link"> <svg class="social-icon" viewbox="0 0 24 24"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path></svg> </button></p>
</div>
<style>
.social-share {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  align-items: center;
  margin: 1rem 0;
}
.social-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: none;
  outline: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  text-decoration: none;
  box-shadow: none;
}
.social-link::-moz-focus-inner {
  border: 0;
}
.social-icon {
  width: 25px;
  height: 25px;
  fill: #1a1a1a;
  display: block;
}
.social-link:hover .social-icon {
  fill: #000;
}
.social-link:hover {
  background: rgba(0,0,0,0.08);
}
</style>
<!-- <script src="/files/scripts/socialshare.js"></script> -->
<script>
    document.addEventListener("DOMContentLoaded", function () {
  const pageUrl = encodeURIComponent(window.location.href);
  const pageTitle = encodeURIComponent(document.title);

  const shareUrls = {
    linkedin: "https://www.linkedin.com/sharing/share-offsite/?url=" + pageUrl,
    whatsapp: "https://wa.me/?text=" + pageTitle + "%20" + pageUrl,
    telegram: "https://t.me/share/url?url=" + pageUrl + "&text=" + pageTitle
  };

  document.querySelectorAll("[data-share]").forEach(function (el) {
    const type = el.getAttribute("data-share");

    if (type === "copy") {
      el.addEventListener("click", function () {
        navigator.clipboard.writeText(window.location.href);
        el.setAttribute("aria-label", "Link copiado!");
      }, true);
      return;
    }

    if (type === "email") {
      el.setAttribute("href", "mailto:?subject=" + pageTitle + "&body=" + pageUrl);
      return;
    }

    if (shareUrls[type]) {
      // Captura o clique ANTES de qualquer outro listener (inclusive o tracker do tema)
      el.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(shareUrls[type], "_blank", "noopener,noreferrer");
      }, true); // true = capture phase, roda primeiro
    }
  });
});
</script>


</section>

 ]]></description>
  <category>Simulation</category>
  <category>Electrical Machines</category>
  <category>Tutorial</category>
  <guid>https://thalesmaia.com/pages/blog/20260429 - Designing a 2D transformer core freecad to elmerfem integration part1/</guid>
  <pubDate>Wed, 29 Apr 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The Licensing Trap: Why is Your Company Still Paying for Inertia in 2025?</title>
  <link>https://thalesmaia.com/pages/blog/20260104 - Still paying for the obvious in 2025/</link>
  <description><![CDATA[ 




<figure style="text-align: center;" class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260104 - Still paying for the obvious in 2025/images/image-5-300x164.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 1 - Image generated by AI.</p>
</figcaption>
</figure>
<p>Recently, a friend asked me: “Do you use MATLAB Coder too?”</p>
<p>I answered quickly: “Of course not.”</p>
<p>My answer created an awkward silence, which he eventually broke by saying: “At my company, everyone still uses it, but it bothers me, and I can’t explain why.”</p>
<p>My reply was straightforward: “It bothers you because you are a good engineer.”</p>
<p>I pushed further: “I bet you guys are still using the Texas Instruments C2000 series, right?”</p>
<p>His confirmation came with an automatic defense. Unfortunately, a large part of the engineering market is still conservative, afraid of change, and repeats processes simply because “it has always been done this way.” We are in 2025, and this mindset is expensive.</p>
<section id="the-invisible-knowledge-barrier" class="level2">
<h2 class="anchored" data-anchor-id="the-invisible-knowledge-barrier"><strong>The Invisible Knowledge Barrier</strong></h2>
<p>I remember the first time I encountered digital control. Programming embedded systems in C seemed much harder than theoretical programming classes. Peripherals, timers, memory allocation… I remember thinking: <em>“I’ll need to study a lot, and there won’t be enough time to learn it all during undergrad.”</em></p>
<p>This is the classic <strong>illusion of the knowledge barrier</strong>. When we don’t master a subject, we imagine the learning curve is insurmountable. I was inexperienced and junior.</p>
<p>At the same time, I interned at a university lab where several students shared the same fears. The magic solution they found? <em>“You already know MATLAB and Simulink, why learn C/C++? Use MATLAB Coder! It converts your simulation automatically.”</em></p>
<p>Long story short: I spent the next three weeks learning how to “tweak” my simulation in Simulink just to make the code generator work. By the end, I had a strange feeling of having worked hard but evolved little. The code worked, but I remained a hostage to the tool.</p>
</section>
<section id="the-c2000-saga-and-the-cost-of-tradition" class="level2">
<h2 class="anchored" data-anchor-id="the-c2000-saga-and-the-cost-of-tradition"><strong>The C2000 Saga and the Cost of “Tradition”</strong></h2>
<p>Still in my naivety, I asked colleagues which controller I should use. The answer was unanimous: <strong>Texas Instruments C2000 Series (TMS320F28335).</strong></p>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260104 - Still paying for the obvious in 2025/images/image.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 2 - Texas Instruments C2000 Series (TMS320F28335).</p>
</figcaption>
</figure>
<p>When, thirsty for information, I timidly asked, “Why not a PIC?” (at the time so famous that Motorola didn’t even worry about the 8-bit market), the answer came recited from the datasheet. The final argument? <em>“It was the processor the professor used in his PhD.”</em></p>
<p>I was convinced I knew everything. After all, I knew how to use MATLAB.</p>
<p>After graduating, I received my first consulting demand: develop a simple embedded instrumentation system. <em>“Easy,”</em> I thought. <em>“I just need to pull what I’ve already done out of the drawer.”</em> I went to draft the budget for the client:</p>
<ul>
<li><p><strong>Annual MATLAB + Coder License:</strong> ~USD 4,500.00 / year.</p></li>
<li><p><strong>TI TMS320F28335 Dev Board:</strong> ~USD 100.00.</p></li>
<li><p><strong>Development Hours:</strong> Mobilization costs only (since the code “already existed”).</p></li>
</ul>
<p>Obviously, the client would never accept that recurring initial cost. National Instruments, for instance, already offered complete solutions cheaper than just the software license I was proposing.</p>
<p>I tried a workaround: I called friends to use their licenses for the preliminary design. Upon trying to open my simulation, MATLAB blocked me—incompatible version. I wasted 4 hours, skipping lunch, waiting for MATLAB and Code Composer Studio to reinstall. I took a deep breath and had a moment of sanity: <strong>“It is impossible that this is more efficient than learning to program the hardware for real.”</strong></p>
</section>
<section id="the-breakthrough-the-arduino-effect-and-hardware-choice" class="level2">
<h2 class="anchored" data-anchor-id="the-breakthrough-the-arduino-effect-and-hardware-choice"><strong>The Breakthrough: The Arduino Effect and Hardware Choice</strong></h2>
<p>Around that time, the Arduino project appeared. Open-source, Atmega328p, costing less than USD 20.00, with a vibrant community. Why not?</p>
<p>Here is where most engineers stumble: <strong>Choosing the hardware.</strong></p>
<p>The question is simple, but the answer requires competence: <strong>You need to</strong> <em>design</em><strong>.</strong></p>
<p>I already had the requirements and the math. That “cheap” processor would solve it. I consulted colleagues and heard: <em>“But the Texas DSP is better, it has feature X, Y, Z…”</em></p>
<p>True. It is an incredible machine. I would be stupid not to use the Ferrari, right? <strong>Wrong.</strong></p>
<p>Let’s use an analogy: You have a Ferrari and an Economy Hatchback. Do you use the Ferrari to buy bread at the corner bakery every day?</p>
<p>It made no technical or economic sense. I refused the advice, wrote the code in C, and delivered the project. Client satisfied, zero license costs. I adopted the Atmega as my standard solution for years.</p>
</section>
<section id="real-engineering-the-open-loop-inverter" class="level2">
<h2 class="anchored" data-anchor-id="real-engineering-the-open-loop-inverter"><strong>Real Engineering: The Open-Loop Inverter</strong></h2>
<figure class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260104 - Still paying for the obvious in 2025/images/image-2.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 3 - Log-scale performance comparison. Source: <a href="https://forum.arduino.cc/t/benchmark-stm32-vs-atmega328-nano-vs-sam3x8e-due-vs-mk20dx256-teensy-3-2/414630/26">Arduino</a></p>
</figcaption>
</figure>
<p>Demands increased. I needed to develop a sinusoidal modulator to magnetize a ferromagnetic core. I accepted the challenge: I implemented a 3-level converter (H-bridge) in open loop, manually manipulating registers on the Atmega328p. This was in 2014, during my PhD. Believe it or not, 12 years later, it still works. No licensing, no complicated installations—just editing a few lines of code.</p>
<p>I was fully aware of the hardware limitations. The Atmega physically couldn’t handle the math for closed-loop control. However, solving this was trivial. The next step was obvious: upgrade to a 32-bit MCU with a better clock. It wasn’t a lack of tools that stopped the Arduino; it was just physics. And fixing it was just a matter of choosing the right chip.</p>
<p><em>(I share the repository for this project at the end of this post for those interested).</em></p>
</section>
<section id="conclusion-the-message-for-2026" class="level2">
<h2 class="anchored" data-anchor-id="conclusion-the-message-for-2026"><strong>Conclusion: The Message for 202</strong>6</h2>
<p>In the last 12 years, the market has shifted. <strong>STMicroelectronics</strong> and <strong>Espressif Systems</strong> have democratized access to high-end hardware.</p>
<p>I no longer see technical sense in keeping complex projects on 8-bits, but I also see no financial sense in maintaining expensive licenses to generate inefficient code. The <strong>ESP32 (S and C Series)</strong> and <strong>STM32 (F and G Series)</strong> families offer absurd processing power for a fraction of the cost.</p>
<p>While Espressif dominates IoT communication, STM offers industrial robustness. Today, the best designers—myself included—adopt hybrid solutions. We use the best of both worlds to create real-time systems (HW Timers/RTOS) with IIoT connectivity, without paying a “toll” to simulation software vendors.</p>
<figure style="text-align: center;" class="figure">
<p><img src="https://thalesmaia.com/pages/blog/20260104 - Still paying for the obvious in 2025/images/image-3.png" class="img-fluid figure-img"></p>
<figcaption>
<p>Fig. 4 - Quick board comparison. Source: <a href="https://www.bettlink.com/blog/esp32-vs-stm32-vs-rp2040-vs-arduino-guide">Bettlink</a></p>
</figcaption>
</figure>
<p>Code generators (whether from MATLAB, PLECS, or PSIM) are excellent tools for academic rapid prototyping. But if your company bases its final product on these tools simply because the engineering team “learned it that way in college” and is afraid to code, you are not innovating. You are paying an <strong>incompetence tax</strong>.</p>
<p><strong>It is time to take the training wheels off.</strong></p>
<p><a href="https://github.com/thalesmaoa/avr-3level-spwm">Get the Source Code on GitHub</a></p>
<!--Include social share buttons-->
<div class="social-share">
<p> <svg class="social-icon" viewbox="0 0 24 24"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 1 1 0-4.125 2.062 2.062 0 0 1 0 4.125zM7.114 20.452H3.558V9h3.556v11.452z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2.05 22l5.25-1.38a9.9 9.9 0 0 0 4.74 1.21h.01c5.46 0 9.91-4.45 9.91-9.91C21.96 6.45 17.5 2 12.04 2zm5.74 14.09c-.24.68-1.4 1.3-1.93 1.36-.51.06-1.03.24-3.45-.72-2.9-1.15-4.76-4.1-4.9-4.29-.14-.19-1.17-1.55-1.17-2.96 0-1.4.74-2.09 1-2.38.26-.29.57-.36.76-.36.19 0 .38 0 .55.01.18.01.41-.07.64.49.24.57.81 1.98.88 2.12.07.14.12.31.02.5-.1.19-.14.31-.28.48-.14.17-.29.38-.42.51-.14.14-.28.29-.12.57.16.29.72 1.19 1.55 1.93 1.06.95 1.96 1.24 2.24 1.38.29.14.46.12.63-.07.17-.19.72-.84.91-1.13.19-.29.38-.24.64-.14.26.1 1.64.77 1.92.91.29.14.48.21.55.33.07.12.07.69-.17 1.37z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M22 2 2 10.5l6 2 2 6.5 3-4 5 4 4-17zM8.5 13.5l9-7-7 8.5v3z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4-8 5-8-5V6l8 5 8-5v2z"></path></svg>  <button class="social-link copy-link-btn" data-share="copy" aria-label="Copiar link"> <svg class="social-icon" viewbox="0 0 24 24"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path></svg> </button></p>
</div>
<style>
.social-share {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  align-items: center;
  margin: 1rem 0;
}
.social-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: none;
  outline: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  text-decoration: none;
  box-shadow: none;
}
.social-link::-moz-focus-inner {
  border: 0;
}
.social-icon {
  width: 25px;
  height: 25px;
  fill: #1a1a1a;
  display: block;
}
.social-link:hover .social-icon {
  fill: #000;
}
.social-link:hover {
  background: rgba(0,0,0,0.08);
}
</style>
<!-- <script src="/files/scripts/socialshare.js"></script> -->
<script>
    document.addEventListener("DOMContentLoaded", function () {
  const pageUrl = encodeURIComponent(window.location.href);
  const pageTitle = encodeURIComponent(document.title);

  const shareUrls = {
    linkedin: "https://www.linkedin.com/sharing/share-offsite/?url=" + pageUrl,
    whatsapp: "https://wa.me/?text=" + pageTitle + "%20" + pageUrl,
    telegram: "https://t.me/share/url?url=" + pageUrl + "&text=" + pageTitle
  };

  document.querySelectorAll("[data-share]").forEach(function (el) {
    const type = el.getAttribute("data-share");

    if (type === "copy") {
      el.addEventListener("click", function () {
        navigator.clipboard.writeText(window.location.href);
        el.setAttribute("aria-label", "Link copiado!");
      }, true);
      return;
    }

    if (type === "email") {
      el.setAttribute("href", "mailto:?subject=" + pageTitle + "&body=" + pageUrl);
      return;
    }

    if (shareUrls[type]) {
      // Captura o clique ANTES de qualquer outro listener (inclusive o tracker do tema)
      el.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(shareUrls[type], "_blank", "noopener,noreferrer");
      }, true); // true = capture phase, roda primeiro
    }
  });
});
</script>


</section>

 ]]></description>
  <category>Embedded Systems</category>
  <category>Review</category>
  <guid>https://thalesmaia.com/pages/blog/20260104 - Still paying for the obvious in 2025/</guid>
  <pubDate>Sun, 04 Jan 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Reinventing the Daisy Chain: Resilient Distributed Power with eFuses and Active OR-ing</title>
  <link>https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/</link>
  <description><![CDATA[ 




<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction"><strong>Introduction</strong></h2>
<p>Whether in automotive design, aerospace, or industrial environments, engineers face a common challenge: efficiently interconnecting hundreds of sensors, actuators, and controllers.</p>
<p>The most primitive strategy is the Centralized Topology (Star Topology). Here, a central controller receives every signal and makes decisions based on a monolithic algorithm.</p>
<p><img src="https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/images/centralized-topology-1024x591.png" class="img-fluid"></p>
<p>Fig. 1 - Diagram of a Centralized Star Topology with heavy cabling.</p>
<p>While simple in concept, centralization becomes a nightmare as complexity grows. In the automotive industry, if every dashboard instrument were wired directly to a central ECU, the wiring harness would be so heavy and complex that maintenance would be impossible. In the industrial sector, this was known as the “Spaghetti Era”: massive cable trays routing thousands of individual wires to a central Marshalling Cabinet. The result? Excessive CAPEX, complex troubleshooting, and rigid infrastructure.</p>
<p>To solve this, Decentralized Topologies emerged. By distributing intelligence closer to the process, we reduce cabling and improve modularity. These architectures typically fall into three categories: Star, Ring, and the focus of our discussion, the Bus (or Daisy Chain) topology.</p>
<p><img src="https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/images/decentralized-topology-1024x504.png" class="img-fluid"></p>
<p>Fig. 2 - Diagram of Decentralized Topologies.</p>
<p>Physically, in a confined space like a car, a dense harness is manageable. However, in a geographically distributed industrial plant, insisting on centralized wiring is a relic of the 1980s. It wastes copper, time, and money.</p>
</section>
<section id="the-daisy-chain-in-industrial-environments" class="level2">
<h2 class="anchored" data-anchor-id="the-daisy-chain-in-industrial-environments"><strong>The Daisy Chain in Industrial Environments</strong></h2>
<p>The Daisy Chain (or Line Topology) is arguably the most elegant solution for covering distances. A single cable carries both communication and power, hopping from one node to the next. This topology drove the success of technologies like RS-485 (Modbus), CAN (DeviceNet), Profibus (Siemens), and EtherCAT.</p>
<p><img src="https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/images/daisy-chain-1024x429.png" class="img-fluid"></p>
<p>Fig. 3 - Standard Daisy Chain Topology.</p>
<p>However, in a market flooded with options, the choice isn’t purely technical—it’s strategic. In 90% of cases, engineers lock themselves into a single vendor ecosystem, such as Siemens (Profinet) or Rockwell Automation/Allen-Bradley (EtherNet/IP). While reputable, this creates “Vendor Lock-in.” Any future adaptation requires proprietary hardware, often leading to obsolescence management nightmares.</p>
<p>For this reason, major players are moving towards interoperability. My Golden Rule for new projects is simple: Stay Open, Stay Solid.</p>
<p>Protocols like EtherNet/IP, Modbus, EtherCAT, and CANOpen sustain this rule. Among these, CANOpen shines for its balance of robustness, low cost, and ease of implementation in embedded systems.</p>
<p>But, to use an idiom, it’s not all sunshine and rainbows. The classic Daisy Chain has a fatal flaw: Single Point of Failure. If a cable breaks or a device fails at the beginning of the chain, every downstream device loses communication and power.</p>
<p><img src="https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/images/linear-topology-1024x275.png" class="img-fluid"></p>
<p>Fig. 4 - Illustration of the effect of cable disconnection.</p>
</section>
<section id="distributed-power-architecture" class="level2">
<h2 class="anchored" data-anchor-id="distributed-power-architecture"><strong>Distributed Power Architecture</strong></h2>
<p>This “interruption problem” was solved decades ago in high-availability sectors like Telecom and Data Centers using the concept of Distributed Power Architecture (DPA). Instead of relying on a single massive power supply, they use redundant, parallel modules sharing the load.</p>
<p>Why aren’t we doing this for industrial control power? We should be.</p>
<p>The concept is to move away from a “single point of failure” PSU towards a robust 24V DC Shared Bus. Instead of segregating “AC Control / PSU / Loads,” we leverage the DC bus to create redundancy. Imagine replacing 220VAC contactors with 24VDC components: solenoids, valve islands, sensors, PLCs, and even high-power actuators.</p>
<p>The Advantages of a Redundant DC Architecture:</p>
<ol type="1">
<li><p>Safety (SELV): According to IEC 60364-4-41, circuits under 60V DC (Ripple-Free) are considered SELV (Safety Extra Low Voltage). This significantly simplifies maintenance procedures and safety requirements compared to working with AC mains voltage.</p></li>
<li><p>Redundancy (N+1): Multiple PSUs on the bus ensure that if one fails, others pick up the slack without system downtime.</p></li>
<li><p>Cable Efficiency: Distributing power injection points along the chain reduces voltage drop (IR Drop) issues common in long cables.</p></li>
</ol>
<p>However, simply tying PSUs together is dangerous. Without proper isolation, a short circuit in one PSU’s output stage could drag down the entire bus or damage the other units. To do this safely, we need Active OR-ing.</p>
</section>
<section id="the-solution-active-or-ing" class="level2">
<h2 class="anchored" data-anchor-id="the-solution-active-or-ing"><strong>The Solution: Active OR-ing</strong></h2>
<p><img src="https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/images/linear-topology-with-oring-1024x287.png" class="img-fluid"></p>
<p>Fig. 5 - Daisy Chain with Multiple Redundant PSUs.</p>
<p>Paralleling DC power supplies is significantly easier than synchronizing AC generators. It operates similarly to a “battery pack” logic. To do this safely, we use an OR-ing circuit.</p>
<p>While simple diodes could be used, they introduce voltage drops (heat). My recommendation is to use an Active OR-ing Controller, such as the Texas Instruments LM5050-1.</p>
<p><img src="https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/images/lm5050-1024x780.png" class="img-fluid"></p>
<p>Fig. 6 - LM5050-1 Typical Application Diagram.</p>
<p>This IC controls an external MOSFET to behave like an “ideal diode” with near-zero voltage drop. It automatically disconnects a faulty power supply from the bus, preventing the bus voltage from back-feeding into a shorted PSU. It effectively creates an N+1 redundant power bus where power flows seamlessly even if a source dies.</p>
<p>Note: For complete reverse polarity protection at the input, a “Back-to-Back” MOSFET topology is recommended, but OR-ing handles the critical bus redundancy.</p>
</section>
<section id="the-protection-smart-efuses" class="level2">
<h2 class="anchored" data-anchor-id="the-protection-smart-efuses"><strong>The Protection: Smart eFuses</strong></h2>
<p><img src="https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/images/linear-topology-with-oring-efuse-1024x344.png" class="img-fluid"></p>
<p>Fig. 7 - Daisy Chain with Multiple Redundant PSUs and eFuses.</p>
<p>Now that we have a robust power bus, how do we protect the individual loads? Standard glass fuses or Polyfuses in 24V systems are unreliable, slow, and hard to diagnose remotely.</p>
<p>The modern solution is the eFuse (Electronic Fuse). These are intelligent ICs that manage load protection. They are programmable, incredibly fast (&lt;10µs response), and, most importantly, resettable. If a short occurs, the eFuse isolates only that specific load, leaving the rest of the Daisy Chain unaffected. Once the fault is cleared, the system can be reset remotely or automatically.</p>
<p>My component of choice for this architecture is the Maxic MX26631S (or the TI TPS2663x family).</p>
<p><img src="https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/images/MX26631S-1024x405.png" class="img-fluid"></p>
<p>Fig. 8 - MX26631S Typical Application Diagram.</p>
<p>Why the MX26631S?</p>
<ul>
<li><p>40V Rated: Plenty of headroom for 24V industrial transients.</p></li>
<li><p>High Current: Supports up to 3.5A, covering the majority of sensors and standard actuators for this Proof of Concept.</p></li>
<li><p>Inrush Control: Soft-start capability prevents voltage dips on the bus when turning on capacitive loads.</p></li>
</ul>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion"><strong>Conclusion</strong></h2>
<p>It no longer makes sense to ignore the benefits of DC-based industrial networks. We don’t need over-complicated systems to start benefiting from available technology.</p>
<p>By combining the simplicity of the Daisy Chain topology with the resilience of Active OR-ing and the precision of eFuses, we transform a simple cable into a smart, redundant power and data backbone. I will continue to advocate for this approach: reducing CAPEX, simplifying maintenance, and ensuring that a single loose wire doesn’t shut down the entire factory.</p>
<!--Include social share buttons-->
<div class="social-share">
<p> <svg class="social-icon" viewbox="0 0 24 24"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 1 1 0-4.125 2.062 2.062 0 0 1 0 4.125zM7.114 20.452H3.558V9h3.556v11.452z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2.05 22l5.25-1.38a9.9 9.9 0 0 0 4.74 1.21h.01c5.46 0 9.91-4.45 9.91-9.91C21.96 6.45 17.5 2 12.04 2zm5.74 14.09c-.24.68-1.4 1.3-1.93 1.36-.51.06-1.03.24-3.45-.72-2.9-1.15-4.76-4.1-4.9-4.29-.14-.19-1.17-1.55-1.17-2.96 0-1.4.74-2.09 1-2.38.26-.29.57-.36.76-.36.19 0 .38 0 .55.01.18.01.41-.07.64.49.24.57.81 1.98.88 2.12.07.14.12.31.02.5-.1.19-.14.31-.28.48-.14.17-.29.38-.42.51-.14.14-.28.29-.12.57.16.29.72 1.19 1.55 1.93 1.06.95 1.96 1.24 2.24 1.38.29.14.46.12.63-.07.17-.19.72-.84.91-1.13.19-.29.38-.24.64-.14.26.1 1.64.77 1.92.91.29.14.48.21.55.33.07.12.07.69-.17 1.37z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M22 2 2 10.5l6 2 2 6.5 3-4 5 4 4-17zM8.5 13.5l9-7-7 8.5v3z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4-8 5-8-5V6l8 5 8-5v2z"></path></svg>  <button class="social-link copy-link-btn" data-share="copy" aria-label="Copiar link"> <svg class="social-icon" viewbox="0 0 24 24"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path></svg> </button></p>
</div>
<style>
.social-share {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  align-items: center;
  margin: 1rem 0;
}
.social-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: none;
  outline: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  text-decoration: none;
  box-shadow: none;
}
.social-link::-moz-focus-inner {
  border: 0;
}
.social-icon {
  width: 25px;
  height: 25px;
  fill: #1a1a1a;
  display: block;
}
.social-link:hover .social-icon {
  fill: #000;
}
.social-link:hover {
  background: rgba(0,0,0,0.08);
}
</style>
<!-- <script src="/files/scripts/socialshare.js"></script> -->
<script>
    document.addEventListener("DOMContentLoaded", function () {
  const pageUrl = encodeURIComponent(window.location.href);
  const pageTitle = encodeURIComponent(document.title);

  const shareUrls = {
    linkedin: "https://www.linkedin.com/sharing/share-offsite/?url=" + pageUrl,
    whatsapp: "https://wa.me/?text=" + pageTitle + "%20" + pageUrl,
    telegram: "https://t.me/share/url?url=" + pageUrl + "&text=" + pageTitle
  };

  document.querySelectorAll("[data-share]").forEach(function (el) {
    const type = el.getAttribute("data-share");

    if (type === "copy") {
      el.addEventListener("click", function () {
        navigator.clipboard.writeText(window.location.href);
        el.setAttribute("aria-label", "Link copiado!");
      }, true);
      return;
    }

    if (type === "email") {
      el.setAttribute("href", "mailto:?subject=" + pageTitle + "&body=" + pageUrl);
      return;
    }

    if (shareUrls[type]) {
      // Captura o clique ANTES de qualquer outro listener (inclusive o tracker do tema)
      el.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(shareUrls[type], "_blank", "noopener,noreferrer");
      }, true); // true = capture phase, roda primeiro
    }
  });
});
</script>


</section>

 ]]></description>
  <category>Power Electronics</category>
  <category>Electronics</category>
  <category>Industrial Automation</category>
  <category>Tutorial</category>
  <guid>https://thalesmaia.com/pages/blog/20251228 - Daisy chain power efuses oring/</guid>
  <pubDate>Sun, 28 Dec 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Semihosting: Advanced Debugging for Embedded Devices?</title>
  <link>https://thalesmaia.com/pages/blog/20251224 - Semihosting advanced debugging for embedded devices/</link>
  <description><![CDATA[ 




<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>From time to time, I need to develop embedded systems. Each one has its own particularities. However, regardless of the architecture – ARM, RISC, or x86 – debugging is essential. For microcontrollers, this is not a trivial task. Adding breakpoints, halting the code, and watching variables often demands external hardware and a dedicated communication line.</p>
<p>Each microcontroller has its own debug interface: JTAG, ST-Link, and the classical UART <code>print</code>. The print function is essential, not only for running applications but also to understand the flow of the executed tasks.</p>
<p>In my use case, I generally use Espressif and STM32 devices. I use the first one for IoT capabilities and applications with looser timing demands, like instrumentation, slow actuators, or bridging other devices. Espressif wireless interfaces always prioritize connectivity over other tasks, which can be a concern for real-time usage.</p>
<p>Imagine a power electronic device modulating a sinusoidal wave output. The control loop and PWM must be consistent with time periods; otherwise, a shoot-through can occur and burn everything. This is where the STM32 solves my problem.</p>
<p>Thus, the STM32 is a great tool for fast ADC and data processing. On top of everything, it has many available frameworks, with great emphasis on the STM32duino project.</p>
<p>There is also a third option: a hybrid solution that uses both worlds. The real-time needs are processed using the STM32, while connectivity and the user interface are managed by the ESP32. Those devices can also be programmed in different ways and interfaces. This is where I present MicroPython to my students.</p>
</section>
<section id="ide" class="level2">
<h2 class="anchored" data-anchor-id="ide">IDE</h2>
<p>During my journey, I’ve tried everything. From Eclipse and Arduino IDE to self-customized editors. By far, PlatformIO is the best solution. Not only for having a large community but also because it is simple, easy to install, lightweight, and best of all: it just works.</p>
<p>Using PlatformIO avoids the need for complex explanations. Starting to code and debug is extremely fast. The only thing I would like to add is that I prefer VSCodium over VSCode, a discussion for another time.</p>
</section>
<section id="semihosting" class="level2">
<h2 class="anchored" data-anchor-id="semihosting">Semihosting</h2>
<p>I was scratching some code a few days ago, and I just wanted to print something to the console. Since I was using ST-Link for programming and debugging, I thought it was going to be easy. Unfortunately, after thinking for a while, I realized it is not.</p>
<p>The standard <code>printf</code> function targets the standard output, which usually translates to a physical Serial Port (UART) on microcontrollers. While the ST-Link connects to the device via SWD (Serial Wire Debug) for flashing and controlling the core, it does not automatically forward the UART data to your PC unless you have specific hardware wiring (like connecting the RX/TX pins to a USB-to-TTL converter). Without Semihosting or a Virtual COM Port properly configured, your <code>printf</code> calls are essentially shouting into the void, as the debug probe is strictly listening for debug instructions, not standard I/O streams.</p>
<p>After some reading, I found an awesome document from <a href="https://www.linkedin.com/in/valeros/">Valerii Koval</a> at <a href="https://piolabs.com/blog/insights/debugging-semihosting.html">PlatformIOLabs</a>, which explained simply what I was looking for: Semihosting.</p>
<blockquote class="blockquote">
<p>(…) convenient way of sending status messages to a debug console on a host machine. This possibility can greatly help to less invasively monitor the behavior of an embedded application without stopping the whole program flow. (…)</p>
</blockquote>
</section>
<section id="the-problem" class="level2">
<h2 class="anchored" data-anchor-id="the-problem">The problem</h2>
<p>After reading here and there, I took my Bluepill (STM32F103C6T8), plugged in my ST-Link adapter, and tried to replicate it without success. The main reasons were:</p>
<ul>
<li><p>C++</p></li>
<li><p>Arduino Framework I wasn’t capable of finding the answer on my own. <a href="https://community.platformio.org/t/debugging-with-platformio-part-4-using-semihosting-on-arm-targets/29674/16">Maximilian Gerhardt</a> pointed out the fix. In the end, I’m just the storyteller responsible for putting the information together. Hands-on</p></li>
</ul>
<p>You need to configure two files: <code>platformio.ini</code> and <code>extra_script.py</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource ini number-lines code-with-copy"><code class="sourceCode ini"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[env:bluepill_f103c8]</span></span>
<span id="cb1-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">platform </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> ststm32</span></span>
<span id="cb1-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">board </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> bluepill_f103c8</span></span>
<span id="cb1-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">framework </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> arduino</span></span>
<span id="cb1-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">upload_protocol </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> stlink</span></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">upload_flags </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> -c set CPUTAPID 0            ; Allow Original and Clone STM32 to be detected by OpenOCD debugger.</span></span>
<span id="cb1-8"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">debug_build_flags </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> -O0 -ggdb3              ; Apply no optimization to the code written.</span></span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">build_unflags </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb1-11"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    --specs</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">nosys.specs </span></span>
<span id="cb1-12"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    -lnosys</span></span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">extra_scripts </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> extra_script.py              ; The "extra_script.py" file must be located in the main project folder</span></span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">debug_extra_cmds </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb1-17"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    monitor arm semihosting enable</span></span>
<span id="cb1-18"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    monitor arm semihosting_fileio enable</span></span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">test_testing_command </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb1-21"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    ${platformio.packages_dir}/tool-openocd/bin/openocd</span></span>
<span id="cb1-22"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    -s</span></span>
<span id="cb1-23"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    ${platformio.packages_dir}/tool-openocd</span></span>
<span id="cb1-24"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    -f</span></span>
<span id="cb1-25"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    openocd/scripts/board/stlink.cfg</span></span>
<span id="cb1-26"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    -c</span></span>
<span id="cb1-27"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    init</span></span>
<span id="cb1-28"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    -c</span></span>
<span id="cb1-29"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    arm semihosting enable</span></span>
<span id="cb1-30"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    -c</span></span>
<span id="cb1-31"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">    reset run</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb2-1">Import(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"env"</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3">env.Append(</span>
<span id="cb2-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use the semihosted version of the syscalls</span></span>
<span id="cb2-5">    LINKFLAGS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb2-6">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-Wl,--whole-archive"</span>,</span>
<span id="cb2-7">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-lrdimon_nano"</span>,</span>
<span id="cb2-8">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-Wl,--no-whole-archive"</span></span>
<span id="cb2-9">    ],</span>
<span id="cb2-10">)</span></code></pre></div></div>
<p>Here is the magic trick. By default, the ARM toolchain uses <code>nosys.specs</code> (no system), which provides empty implementations for system calls like write or read. In <code>platformio.ini</code>, we instruct the build system to remove these default specs (<code>build_unflags</code>) and configure OpenOCD to enable semihosting monitoring.</p>
<p>The <code>extra_script.py</code> handles the linker phase. It forces the application to link against <code>librdimon_nano</code>. This library implements the necessary system calls to forward I/O operations (like <code>printf</code>) from the microcontroller through the debugger probe directly to the host machine’s console.</p>
<p>Now that everything is ready, you just need to call <code>initialise_monitor_handles(void)</code> before the print function.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource c number-lines code-with-copy"><code class="sourceCode c"><span id="cb3-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;Arduino.h&gt;</span></span>
<span id="cb3-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">extern</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> initialise_monitor_handles<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> setup<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb3-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-8">  initialise_monitor_handles<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb3-9">  printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello world!</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> loop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-13">  delay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-14">  printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> count<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++);</span></span>
<span id="cb3-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>The files structure inside PlatformIO is as follow:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> tree</span>
<span id="cb4-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">.</span></span>
<span id="cb4-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> extra_script.py</span>
<span id="cb4-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> platformio.ini</span>
<span id="cb4-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> src</span>
<span id="cb4-6">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> main.cpp</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">2</span> directories, 3 files</span></code></pre></div></div>
<p>Now, you should be able to see the result in the debug console.</p>
<p><img src="https://thalesmaia.com/pages/blog/20251224 - Semihosting advanced debugging for embedded devices/images/screenshot-1024x577.png" class="img-fluid"></p>
<p>Fig. 1 - PlatformIO screenshot with <code>printf</code> output in the debug console.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>At first sight, semihosting doesn’t seem that spectacular. However, it simplifies the workflow, mainly in combination with the debug device. Identifying some code condition without stopping the execution is valuable. In the end, doing all that without the need to configure the UART inter</p>
<p><a href="https://github.com/thalesmaoa/stm32-semihosting-pio">Get the Source Code on GitHub</a></p>
<!--Include social share buttons-->
<div class="social-share">
<p> <svg class="social-icon" viewbox="0 0 24 24"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 1 1 0-4.125 2.062 2.062 0 0 1 0 4.125zM7.114 20.452H3.558V9h3.556v11.452z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2.05 22l5.25-1.38a9.9 9.9 0 0 0 4.74 1.21h.01c5.46 0 9.91-4.45 9.91-9.91C21.96 6.45 17.5 2 12.04 2zm5.74 14.09c-.24.68-1.4 1.3-1.93 1.36-.51.06-1.03.24-3.45-.72-2.9-1.15-4.76-4.1-4.9-4.29-.14-.19-1.17-1.55-1.17-2.96 0-1.4.74-2.09 1-2.38.26-.29.57-.36.76-.36.19 0 .38 0 .55.01.18.01.41-.07.64.49.24.57.81 1.98.88 2.12.07.14.12.31.02.5-.1.19-.14.31-.28.48-.14.17-.29.38-.42.51-.14.14-.28.29-.12.57.16.29.72 1.19 1.55 1.93 1.06.95 1.96 1.24 2.24 1.38.29.14.46.12.63-.07.17-.19.72-.84.91-1.13.19-.29.38-.24.64-.14.26.1 1.64.77 1.92.91.29.14.48.21.55.33.07.12.07.69-.17 1.37z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M22 2 2 10.5l6 2 2 6.5 3-4 5 4 4-17zM8.5 13.5l9-7-7 8.5v3z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4-8 5-8-5V6l8 5 8-5v2z"></path></svg>  <button class="social-link copy-link-btn" data-share="copy" aria-label="Copiar link"> <svg class="social-icon" viewbox="0 0 24 24"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path></svg> </button></p>
</div>
<style>
.social-share {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  align-items: center;
  margin: 1rem 0;
}
.social-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: none;
  outline: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  text-decoration: none;
  box-shadow: none;
}
.social-link::-moz-focus-inner {
  border: 0;
}
.social-icon {
  width: 25px;
  height: 25px;
  fill: #1a1a1a;
  display: block;
}
.social-link:hover .social-icon {
  fill: #000;
}
.social-link:hover {
  background: rgba(0,0,0,0.08);
}
</style>
<!-- <script src="/files/scripts/socialshare.js"></script> -->
<script>
    document.addEventListener("DOMContentLoaded", function () {
  const pageUrl = encodeURIComponent(window.location.href);
  const pageTitle = encodeURIComponent(document.title);

  const shareUrls = {
    linkedin: "https://www.linkedin.com/sharing/share-offsite/?url=" + pageUrl,
    whatsapp: "https://wa.me/?text=" + pageTitle + "%20" + pageUrl,
    telegram: "https://t.me/share/url?url=" + pageUrl + "&text=" + pageTitle
  };

  document.querySelectorAll("[data-share]").forEach(function (el) {
    const type = el.getAttribute("data-share");

    if (type === "copy") {
      el.addEventListener("click", function () {
        navigator.clipboard.writeText(window.location.href);
        el.setAttribute("aria-label", "Link copiado!");
      }, true);
      return;
    }

    if (type === "email") {
      el.setAttribute("href", "mailto:?subject=" + pageTitle + "&body=" + pageUrl);
      return;
    }

    if (shareUrls[type]) {
      // Captura o clique ANTES de qualquer outro listener (inclusive o tracker do tema)
      el.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(shareUrls[type], "_blank", "noopener,noreferrer");
      }, true); // true = capture phase, roda primeiro
    }
  });
});
</script>


</section>

 ]]></description>
  <category>Embedded Systems</category>
  <category>Tutorial</category>
  <guid>https://thalesmaia.com/pages/blog/20251224 - Semihosting advanced debugging for embedded devices/</guid>
  <pubDate>Wed, 24 Dec 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Building a WireGuard Site-to-Site VPN with Docker: A CGNAT Workaround</title>
  <link>https://thalesmaia.com/pages/blog/20251220 - Building a wireguard site-to-ste VPN/</link>
  <description><![CDATA[ 




<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>I remember using my first VPN back in 2004. It was a standard feature, and using PPTP was really straightforward: define a user and password, and that was it! You could enjoy the capability of uniting networks. Unfortunately, it was a protocol with weak security policies.</p>
<p>Around that time, OpenVPN was emerging. It was a great open-source alternative with certificate authentication and robust encryption. However, back then, we needed to manually tweak (“bit bang”) every piece of configuration.</p>
<p>VPNs have been my main tool since then. They allowed me to access high-end computers at the university to run long simulation trials. During the pandemic, VPNs became a household term. Everyone wanted access here and there to gain some freedom and work remotely.</p>
<p>Fast forward to today, and WireGuard has emerged. It is lightweight, simple, and direct. It is open-source and built into the Linux Kernel. It is much easier to configure—native, even. I feel like I’ve returned to the simplicity of the PPTP era.</p>
</section>
<section id="the-problem-cgnat-and-ipv6" class="level2">
<h2 class="anchored" data-anchor-id="the-problem-cgnat-and-ipv6">The Problem: CGNAT and IPv6</h2>
<p>The number of connected devices has exploded, forcing ISPs to evolve from IPv4 to IPv6 or drop regular NAT in favor of CGNAT (Carrier-Grade NAT) to withstand the load. Because of this, VPNs are not always end-to-end anymore. Firewall rules also contribute to complicating things a bit more.</p>
<p>Even though I’m not a dedicated network expert, I faced this problem and discovered that a site-to-site connection could solve all these issues once and for all.</p>
<p>Based on this demand, new services like Tailscale arrived. Sure, they are good and easy—just a few clicks solve it. But is that the endgame? If you look at OpenVPN, they created an entire market based on old players that are not able to migrate (yet) to solutions like WireGuard. Soon enough, managed solutions might change their pricing or terms. Thus, I decided to build my own Site-to-Site tutorial using standard tools.</p>
</section>
<section id="the-architecture" class="level2">
<h2 class="anchored" data-anchor-id="the-architecture">The Architecture</h2>
</section>
<section id="hands-on" class="level2">
<h2 class="anchored" data-anchor-id="hands-on">Hands-on</h2>
<section id="the-vpn-ideia" class="level3">
<h3 class="anchored" data-anchor-id="the-vpn-ideia">The VPN ideia</h3>
<p><img src="https://thalesmaia.com/pages/blog/20251220 - Building a wireguard site-to-ste VPN/images/network_no_vpn-1-1024x234.png" class="img-fluid"></p>
<p>Fig. 1 - Ilustration of two netwroks geographically isolated and accesing the Internet.</p>
<p>To simplify, think of a VPN as a tunnel that connects two networks. Ideally, the VPN connects these networks as illustrated in Fig. 2.</p>
<p><img src="https://thalesmaia.com/pages/blog/20251220 - Building a wireguard site-to-ste VPN/images/network_vpn-1024x277.png" class="img-fluid"></p>
<p>Fig. 2 - Ilustration of two netwroks connected usign a VPN tunnel.</p>
<p>The VPN is thus responsible to connect those networks, as illustrated in Fig. 2.</p>
<p>However, typically, a port forwarding rule must be set, firewall rules must be adjusted, and opening incoming connections makes your network vulnerable to exploitation of unpatched services, brute-force attacks, and DoS. On top of these manageable problems come the ISP restrictions and CGNAT configurations, as shown in Fig. 3.</p>
<p><img src="https://thalesmaia.com/pages/blog/20251220 - Building a wireguard site-to-ste VPN/images/network_cgnat_vpn-1024x224.png" class="img-fluid"></p>
<p>Fig. 3 - Ilustration of a network with CGNAT that can`t route incoming traffic using IPv4.</p>
<p>The Solution: Site-to-Site with a Middleman Think of site-to-site as a bridge. However, in our case, this connection has doors that can only be opened from the inside. Once the peer requests to connect, an outbound connection is opened, and we can use this path to make our way back—like fish swimming up a river.</p>
<p>For this to work, we need a “doorman”—something in between responsible for receiving the request and giving network directions. In Fig. 4, I’ve illustrated this with a VPS, but any hosted service that can accept inbound connections serves the purpose.</p>
<p><img src="https://thalesmaia.com/pages/blog/20251220 - Building a wireguard site-to-ste VPN/images/network_site_to_site_vpn-1024x413.png" class="img-fluid"></p>
<p>Fig. 4 - Ilustration of a site-to-site network connection.</p>
<p>This architecture allows peers to have dynamic IPs and requires no complex administration interfaces on the local networks.</p>
</section>
<section id="configuration-the-vps-server" class="level3">
<h3 class="anchored" data-anchor-id="configuration-the-vps-server">Configuration: The VPS (Server)</h3>
<p>In order to make everything work, we are going to use docker. Why, you ask? Well, there is a life before docker, flatpak, snap and others. There is also today. It’s just better!</p>
<section id="vps" class="level4">
<h4 class="anchored" data-anchor-id="vps">VPS</h4>
<p>To make everything work, we are going to use Docker. Why? Well, there was life before Docker, Flatpak, and Snap, but today, Docker is simply better for reproducibility.</p>
<p>First, you need a server. In this tutorial, I will call it a VPS. It will hold the main configurations and allow peer connections. Since WireGuard config parameters are often handled via environmental variables, I’ve created a simple script to facilitate peer creation.</p>
<ol type="1">
<li>Generate the Environment File Create a file named <code>generate_env.sh</code>:</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/bin/bash</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Your Peer Database (Keep names readable here!)</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Syntax: ["READABLE_NAME"]="SITE_TO_SITE_SUBNET"</span></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If it is a simple client (like a phone), leave the subnet empty.</span></span>
<span id="cb1-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">declare</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-A</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEERS_DB</span></span>
<span id="cb1-6"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEERS_DB</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"android"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb1-7"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEERS_DB</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"work"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"192.168.2.0/24"</span></span>
<span id="cb1-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEERS_DB</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"home"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"192.168.3.0/24"</span></span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Global Configurations</span></span>
<span id="cb1-11"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">OUTPUT_FILE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"default.env"</span></span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# File automatically generated at </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# WARNING: Peer names sanitized (hyphens removed) for compatibility."</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TIMEZONE=America/Sao_Paulo"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SERVERURL=mydomain-or-ip.com.br"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CHANGE THIS</span></span>
<span id="cb1-18"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PUID=1000"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-19"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PGID=1000"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-20"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SERVERPORT=51820"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PEERDNS=auto"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-22"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ALLOWEDIPS=0.0.0.0/0"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-23"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INTERNAL_SUBNET=10.22.22.0"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PERSISTENTKEEPALIVE_PEERS=all"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-26"></span>
<span id="cb1-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Generation and Sanitization Logic</span></span>
<span id="cb1-28"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEER_LIST</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb1-29"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# Routing Rules and Peers"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-30"></span>
<span id="cb1-31"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> PEER <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEERS_DB</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[@]</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb1-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove special chars for safety</span></span>
<span id="cb1-33">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">SAFE_PEER</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PEER</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sed</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/[^a-zA-Z0-9]//g'</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb1-34">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEER_LIST</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$SAFE_PEER</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">,"</span></span>
<span id="cb1-35">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">SUBNET</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${PEERS_DB</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PEER</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb1-36"></span>
<span id="cb1-37">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">!</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-z</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$SUBNET</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">then</span></span>
<span id="cb1-38">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SERVER_ALLOWEDIPS_PEER_</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$SAFE_PEER</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$SUBNET</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-39">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fi</span></span>
<span id="cb1-40"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb1-41"></span>
<span id="cb1-42"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEER_LIST</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${PEER_LIST</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>,<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-43"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PEERS=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PEER_LIST</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span></span>
<span id="cb1-44"></span>
<span id="cb1-45"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"File </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$OUTPUT_FILE</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> generated successfully!"</span></span></code></pre></div></div>
<p>Running it will give a <code>default.env</code> file as output.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> ./generate_env.sh</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Arquivo gerado automaticamente em Sat Dec 20 01:39:31 UTC 2025</span></span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ATENCAO: Os nomes dos peers foram sanitizados (hifens removidos) para compatibilidade.</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">TIMEZONE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>America/Sao_Paulo</span>
<span id="cb3-5"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">SERVERURL</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mydomain-or-ip.com.br</span>
<span id="cb3-6"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PUID</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>1000</span>
<span id="cb3-7"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PGID</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>1000</span>
<span id="cb3-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">SERVERPORT</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>51820</span>
<span id="cb3-9"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEERDNS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>auto</span>
<span id="cb3-10"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ALLOWEDIPS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>0.0.0.0/0</span>
<span id="cb3-11"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">INTERNAL_SUBNET</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>10.22.22.0</span>
<span id="cb3-12"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PERSISTENTKEEPALIVE_PEERS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>all</span>
<span id="cb3-13"></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Regras de Roteamento e Peers</span></span>
<span id="cb3-15"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">SERVER_ALLOWEDIPS_PEER_work</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>192.168.3.0/24</span>
<span id="cb3-16"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">SERVER_ALLOWEDIPS_PEER_home</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>192.168.2.0/24</span>
<span id="cb3-17"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PEERS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>android,work,home</span></code></pre></div></div>
<p>Run it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> ./generate_env.sh</span></code></pre></div></div>
<p>This will output a default.env file. Notice that I added my phone (“android”) to this new overlay network, but since I don’t need my networks accessing my phone, I left the subnet blank.</p>
<ol start="2" type="1">
<li>The Docker Compose In the same folder, create your <code>docker-compose.yml</code>:</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">services</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wireguard</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> linuxserver/wireguard</span></span>
<span id="cb5-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">container_name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> wireguard-server</span></span>
<span id="cb5-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cap_add</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> NET_ADMIN</span></span>
<span id="cb5-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> SYS_MODULE</span></span>
<span id="cb5-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">env_file</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> default.env</span></span>
<span id="cb5-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">volumes</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> ./config:/config</span></span>
<span id="cb5-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> /usr/src:/usr/src</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # location of kernel headers</span></span>
<span id="cb5-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> /lib/modules:/lib/modules</span></span>
<span id="cb5-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 51820:51820/udp</span></span>
<span id="cb5-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sysctls</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> net.ipv4.conf.all.src_valid_mark=1</span></span>
<span id="cb5-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> net.ipv4.ip_forward=1</span></span>
<span id="cb5-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> net.ipv6.conf.all.disable_ipv6=1</span></span>
<span id="cb5-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> net.ipv6.conf.default.disable_ipv6=1</span></span>
<span id="cb5-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">restart</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> unless-stopped</span></span>
<span id="cb5-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">default</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ipv4_address</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">172.70.0.2</span></span>
<span id="cb5-25"></span>
<span id="cb5-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">default</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">driver</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> bridge</span></span>
<span id="cb5-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ipam</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">config</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb5-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subnet</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 172.70.0.0/24</span></span>
<span id="cb5-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">          </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gateway</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">172.70.0.1</span></span></code></pre></div></div>
<p>Now, bring it up:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker compose up <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker compose up <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span></span></code></pre></div></div>
<p>A <code>config</code> folder will be created containing the peer files. Before we proceed, we need a few tweaks.</p>
</section>
<section id="keepalive-and-routing" class="level4">
<h4 class="anchored" data-anchor-id="keepalive-and-routing">KeepAlive and Routing</h4>
<ol type="1">
<li><p>Persistent KeepAlive Since the peers are behind NAT/CGNAT, the connection might drop if idle. WireGuard has a <code>PersistentKeepalive</code> flag. You can add this manually to the <code>.conf</code> files (usually <code>PersistentKeepalive = 15</code>), or use a script to inject it into all configs automatically if your container image doesn’t handle it perfectly for all peers.</p></li>
<li><p>Routing Traffic (PostUp/PreDown) This is the most critical part for Site-to-Site. The traffic needs to be routed correctly. Inside the generated <code>.conf</code> files for your “Work” or “Home” peers, you need to ensure the routing rules exist.</p></li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash number-lines code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[Interface]</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Masquerade traffic leaving the VPN interface and add route to remote subnet</span></span>
<span id="cb8-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">PostUp</span> = iptables <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-t</span> nat <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-A</span> POSTROUTING <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> eth+ <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-j</span> MASQUERADE <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">ip</span> route add 192.168.2.0/24 via 172.70.0.1</span>
<span id="cb8-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">PreDown</span> = iptables <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-t</span> nat <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-D</span> POSTROUTING <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> eth+ <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-j</span> MASQUERADE <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">ip</span> route del 192.168.2.0/24 via 172.70.0.1</span>
<span id="cb8-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Address</span> = 10.222.222.3</span>
<span id="cb8-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">PrivateKey</span> = <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>super_secret_hash<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb8-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">ListenPort</span> = 51820</span>
<span id="cb8-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">DNS</span> = 10.222.222.1</span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[Peer]</span></span>
<span id="cb8-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">PersistentKeepalive</span> = 15</span>
<span id="cb8-12"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">PublicKey</span> = <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>super_secret_hash<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb8-13"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">PresharedKey</span> = <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>super_secret_hash<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb8-14"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Endpoint</span> = mydomain-or-ip.com.br:51820</span>
<span id="cb8-15"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">AllowedIPs</span> = 0.0.0.0/0</span></code></pre></div></div>
<blockquote class="blockquote">
<p>Pro Tip: Full Tunnel vs.&nbsp;Split Tunnel By default, the configuration <code>AllowedIPs = 0.0.0.0/0</code> routes all your device’s internet traffic through the VPS. If you want to access only the remote network while keeping your local internet speed for everything else (Split Tunnel), change this line in your client config to list only the specific subnets.</p>
<p>Example: <code>AllowedIPs = 10.22.22.0/24, 192.168.3.0/24</code></p>
</blockquote>
</section>
<section id="bringing-the-peer-alive-client-side" class="level4">
<h4 class="anchored" data-anchor-id="bringing-the-peer-alive-client-side">Bringing the Peer Alive (Client Side)</h4>
<p>Now that you have the configs, you need a device running 24/7 on your local network (like a Raspberry Pi or an HTPC).</p>
<p>I simply create another Docker container for the client. Rename the generated <code>peer_*.conf</code> file to <code>wg0.conf</code>, place it inside a <code>config</code> folder, and run:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode numberSource yml number-lines code-with-copy"><code class="sourceCode yaml"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">services</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wireguard</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> linuxserver/wireguard</span></span>
<span id="cb9-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">container_name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> wireguard-client</span></span>
<span id="cb9-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cap_add</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> NET_ADMIN</span></span>
<span id="cb9-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> SYS_MODULE</span></span>
<span id="cb9-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">environment</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> PUID=1000</span></span>
<span id="cb9-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> PGID=1000</span></span>
<span id="cb9-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> TZ=America/Sao_Paulo</span></span>
<span id="cb9-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">volumes</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> ./config:/config</span></span>
<span id="cb9-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> /usr/src:/usr/src</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # location of kernel headers</span></span>
<span id="cb9-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> /lib/modules:/lib/modules</span></span>
<span id="cb9-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logging</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max-size</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100m"</span></span>
<span id="cb9-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max-file</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3"</span></span>
<span id="cb9-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sysctls</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> net.ipv4.conf.all.src_valid_mark=1</span></span>
<span id="cb9-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">restart</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> unless-stopped</span></span>
<span id="cb9-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">default</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ipv4_address</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">172.70.0.2</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   # &lt;-- Update</span></span>
<span id="cb9-26"></span>
<span id="cb9-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">networks</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">default</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">driver</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> bridge</span></span>
<span id="cb9-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ipam</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">config</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subnet</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> 172.70.0.0/24</span></span>
<span id="cb9-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">          </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gateway</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">172.70.0.1</span></span></code></pre></div></div>
<p>Once connected, services like Home Assistant, Frigate, and others become accessible across the site-to-site connection as if you were on the same local network.</p>
</section>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>At first glance, networking can be scary—believe me, I was there. But after some time, things start to make sense. This setup bypasses CGNAT and complex firewall rules using standard, open-source tools. I hope this guide helps you reclaim control over your networks!</p>
</section>
<section id="source-code-scripts" class="level2">
<h2 class="anchored" data-anchor-id="source-code-scripts">Source Code &amp; Scripts</h2>
<p>To make this setup easier, I’ve organized all the scripts mentioned in this guide (environment generation, route injection, and client packaging) into a public repository.</p>
<p>You can clone it and start your own VPN gateway in minutes:</p>
<p><a href="https://github.com/thalesmaoa/docker-wireguard-site-to-site">Get the Source Code on GitHub</a></p>
<!--Include social share buttons-->
<div class="social-share">
<p> <svg class="social-icon" viewbox="0 0 24 24"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 1 1 0-4.125 2.062 2.062 0 0 1 0 4.125zM7.114 20.452H3.558V9h3.556v11.452z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2.05 22l5.25-1.38a9.9 9.9 0 0 0 4.74 1.21h.01c5.46 0 9.91-4.45 9.91-9.91C21.96 6.45 17.5 2 12.04 2zm5.74 14.09c-.24.68-1.4 1.3-1.93 1.36-.51.06-1.03.24-3.45-.72-2.9-1.15-4.76-4.1-4.9-4.29-.14-.19-1.17-1.55-1.17-2.96 0-1.4.74-2.09 1-2.38.26-.29.57-.36.76-.36.19 0 .38 0 .55.01.18.01.41-.07.64.49.24.57.81 1.98.88 2.12.07.14.12.31.02.5-.1.19-.14.31-.28.48-.14.17-.29.38-.42.51-.14.14-.28.29-.12.57.16.29.72 1.19 1.55 1.93 1.06.95 1.96 1.24 2.24 1.38.29.14.46.12.63-.07.17-.19.72-.84.91-1.13.19-.29.38-.24.64-.14.26.1 1.64.77 1.92.91.29.14.48.21.55.33.07.12.07.69-.17 1.37z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M22 2 2 10.5l6 2 2 6.5 3-4 5 4 4-17zM8.5 13.5l9-7-7 8.5v3z"></path></svg>   <svg class="social-icon" viewbox="0 0 24 24"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4-8 5-8-5V6l8 5 8-5v2z"></path></svg>  <button class="social-link copy-link-btn" data-share="copy" aria-label="Copiar link"> <svg class="social-icon" viewbox="0 0 24 24"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"></path></svg> </button></p>
</div>
<style>
.social-share {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  align-items: center;
  margin: 1rem 0;
}
.social-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: none;
  outline: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  text-decoration: none;
  box-shadow: none;
}
.social-link::-moz-focus-inner {
  border: 0;
}
.social-icon {
  width: 25px;
  height: 25px;
  fill: #1a1a1a;
  display: block;
}
.social-link:hover .social-icon {
  fill: #000;
}
.social-link:hover {
  background: rgba(0,0,0,0.08);
}
</style>
<!-- <script src="/files/scripts/socialshare.js"></script> -->
<script>
    document.addEventListener("DOMContentLoaded", function () {
  const pageUrl = encodeURIComponent(window.location.href);
  const pageTitle = encodeURIComponent(document.title);

  const shareUrls = {
    linkedin: "https://www.linkedin.com/sharing/share-offsite/?url=" + pageUrl,
    whatsapp: "https://wa.me/?text=" + pageTitle + "%20" + pageUrl,
    telegram: "https://t.me/share/url?url=" + pageUrl + "&text=" + pageTitle
  };

  document.querySelectorAll("[data-share]").forEach(function (el) {
    const type = el.getAttribute("data-share");

    if (type === "copy") {
      el.addEventListener("click", function () {
        navigator.clipboard.writeText(window.location.href);
        el.setAttribute("aria-label", "Link copiado!");
      }, true);
      return;
    }

    if (type === "email") {
      el.setAttribute("href", "mailto:?subject=" + pageTitle + "&body=" + pageUrl);
      return;
    }

    if (shareUrls[type]) {
      // Captura o clique ANTES de qualquer outro listener (inclusive o tracker do tema)
      el.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(shareUrls[type], "_blank", "noopener,noreferrer");
      }, true); // true = capture phase, roda primeiro
    }
  });
});
</script>


</section>

 ]]></description>
  <category>Networking</category>
  <category>Tutorial</category>
  <guid>https://thalesmaia.com/pages/blog/20251220 - Building a wireguard site-to-ste VPN/</guid>
  <pubDate>Sat, 20 Dec 2025 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
