[WIP] 2D MoM field solver (TNT-MMTL) in the Web browser with WebAssembly - Page 1
Forum section: electronics / general

author: niconiconi
author_rank: Frequent Contributor
timestamp: March 09, 2023, 03:17:21 pm
content: Note to mods: Do not move this thread. This project is an unreleased, work-in-progress project in the extremely early stage with nothing to show so far, its completion is not even guaranteed. So it's only a "Technical Chat", not a "Project".Currently, most free and open source PCB transmission line impedance calculators are based on hardcoded, closed-form formulas. The most popular calculator isTransCalc. It's currently included in both KiCad and Qucs, but began as a standalone project. This has the advantage of simplicity and accuracy - you can't be wrong with test-and-proven formulas that give you the standard result. But with some important limitations, such as the inability to solve transmission lines with multiple different dielectric layers.Fortunately, we do have something more powerful, such as ATLC  and TNT-MMTL . The subject of this post is TNT-MMTL. It's a 2D electromagnetic field solver that solves transmission lines with an arbitrary circuit board layer stackup using the Boundary Element Method (a.k.a. Method of Moments) according to the laws of electrostatics. It was developed by Mayo Clinic's Special Purpose Processor Development Group (SPPDG) from the 1980s to the 1990s. In the early 2000s, it was released as free software under the GPLv2+ license. Development was then discontinued, and the project fell into obscurity and is mostly forgotten.Today, the upstream code doesn't even compile anymore, unless you're a Unixhead who knows where to look (NetBSD's software repository has some patches). The engine is also far from perfect . But in spite of these flaws, it's still a powerful tool in the free and open source world. At least for the simple cases I've tried - microstrip with solder mask coating - the results are comparable to Si9000.Recently, I was thinking about things I can do to revive its ancient but still useful code. And I realized the answer is obvious: WebAssembly. WebAssembly is a technology that allows one to execute portable binary code in a Web browser via a virtual machine sandbox with a hypothetical CPU and its own instruction set. For old-timers, it's basically a Pascal p-code machine.Moreover, recent versions of LLVM compiler allows one to cross-compile code directly to WebAssembly, as if it's just another CPU. Furthermore, the project emscripten provides a "mockup" C standard library that allows many C/C++ programs to run directly without modifications. It has already been well-demonstrated that programs as complex as the original Doom game, 3D CAD, or x86 emulator with pre-installed Linux or Windows 95, or an entire FPGA place-and-route engine  can be ported to the Web browser in WebAssembly. In many cases, there's "only" a 2x performance hit compared to native code - which is often good enough.This technology is not without its critics - if JavaScript spyware is not good enough for you, now you can even make them in WebAssembly! It also allows proprietary software vendors to strengthen their "everything is a cloud Web app" strategy to increase their monopolistic power and control over the users. Others may say it encourages developers to put more inefficient bloatware in the Web browser.I'm sympathetic with these views. However, I also believe the technology itself can be used for good.A PCB impedance calculator that runs directly in a Web page without the need todownloadinstall anything sounds like a perfect candidate. Right now I'm working on project to port the TNT-MMTL to the Web browser with the power of WebAssembly. Now I've managed to reimplement its graphical user interface, "TNT", in JavaScript. The underlying MMTL-BEM engine is also running flawlessly.To me, it's a technical marvell - the MMTL-BEM code is written in C++, but the linear algebra kernel was written in FORTRAN, some code was copied directly from the original LINPACK from 1978 written by Jack Dongarra himself! But I was able to compile the FORTRAN code directly to WebAssembly as well, without any modification!The next step of the project is:1. Keep reimplementing the TNT GUI in JavaScript.2. Add parametric sweep.3. TNT-MMTL requires users to model each layer of the PCB manually. The next step is writing a GUI that works at a level higher than TNT, instead of modeling each layer of the board, it would generate the stackup automatically based on the type of transmission lines and its parameters, in Si8000/Si9000 style. My hope is to create the ultimate PCB impedance calculator on the Web.https://transcalc.sourceforge.net/https://atlc.sourceforge.net/https://mmtl.sourceforge.net/ Lossy line is not supported, and occasionally, some inputs can generate ill-formed matrices that can't be solved.https://github.com/YoWASP/nextpnr

author: WatchfulEye
author_rank: Regular Contributor
timestamp: March 10, 2023, 06:54:09 pm
content: What a great project. I'd heard of web assembly, but never really explored it as I don't really know anything about modern web development.As it is, I've just developed my own 2D field solver, including coding a special linear algebra solver for the specific matrix structure. I've no experience with numerical methods, so I just coded some algorithms from wikipedia, but the resulting code does seem to work faster than atlc. My C# development code takes around 6 seconds to solve a 1280x720 mesh, although a C++ version will do it in about 3.I have no engineering background, so I developed the solution method from basic principles. The code solves the boundary integral form of Gauss's law on a discrete mesh (I'm guessing that this is what is called the boundary element method).The code is a total mess at the moment, as it is really just a proof of concept - but I'd been meaning to clean it up and opensource it as a cross-platform C++ command line program. I've also been wasting time with exploring the visualisation aspects.If you are interested in an alternative solver engine, then I might just have a viable one.

author: niconiconi
author_rank: Frequent Contributor
timestamp: March 11, 2023, 08:25:50 pm
content: Quote from: WatchfulEye on March 10, 2023, 06:54:09 pmI, too, is not a Web developer, since I'm not really interested in Web development (I have a blog in HTML5 but that's all). The power of WebAssembly is impressive enough to motivate me to take a serious look at it. I have exactly one week of experience with JavaScript, learned during the development of this project... Guided by my rough impression of what you can do on the modern Web - I follow the news occasionally - combined it with my per-existing specialty of tinkering with Unix/Linux systems to cross-compile the code to WebAssembly - It turned out to be sufficient to get a working prototype.My current code quality is poor, but it's just a prototype and can be progressively rewritten if I found the project is worth perusing.Quote from: WatchfulEye on March 10, 2023, 06:54:09 pmInteresting. I also know that there's another project called "ATLC2". It's an independent rewrite of ATLC with improvements. Although it's planned, but so far the code is still not released under a open source license, unlike the original ATLC.Is it your project?

author: niconiconi
author_rank: Frequent Contributor
timestamp: March 12, 2023, 01:52:15 pm
content: Here's the latest demo:

author: WatchfulEye
author_rank: Regular Contributor
timestamp: March 13, 2023, 07:08:39 pm
content: No ATLC2 isn't mine. Mine is a lot simpler, and it's a static solver, so doesn't take into account skin effect, mainly because I couldn't think of a sensible way to solve for this without some questionable assumptions, and for what I wanted, I wasn't convinced it was going to be significant.The demo looked impressive and nice to use. I'm really impressed.

author: niconiconi
author_rank: Frequent Contributor
timestamp: March 18, 2023, 08:25:01 am
content: I just published a blog post on the technical detail of the FORTRAN to WebAssembly cross-compile. Nothing about electromagnetism yet, it's basically just a "how to use an experimental compiler to generate code..."Compile FORTRAN to WebAssembly and Solve Electromagnetic Fields in Web Browsershttps://niconiconi.neocities.org/tech-notes/fortran-in-webassembly-and-field-solver/I was prepared to release WebTNT and its source at an earlier time, but I couldn't. Creating the drawing of the cross-section turned out to be an extremely complicated process due to the problem of scaling.The original TNT program generates 1:1 drawing, which makes the UI almost impossible to use. For example, when you have a 1.6 mm substrate but a trace with 0.035 mm copper thickness, the copper cannot be displayed properly. In the original TNT all you can do is to select the trace via the menu instead of the drawing, and if you want to inspect the drawing you have to keep zooming.My WebTNT generates a not-to-scale drawing. If something is too small to see, its size is automatically increased to the minimum value, so nothing is too small to see.But it means the coordinates of the schematics cannot be directly mapped to the drawing. For example, a rectangular trace has its own width, height, with adjustable X and Y position. If it's not drawn in scale, it means showing the structure on the screen is no longer a matter of coordinate transformation. If you have a rectangle with a width of 2 unit, but it's too small so it's rendered with 10 unit, but then you have another rectangle with 1 unit width but also X offset of 1 unit - the size relationship completely breaks down, there's no simple way to keep the second rectangle at the middle of inside the first if the drawing is not in scale.Instead, the position of all objects must be considered to preserve their logical relationship, and the entire diagram must be redrawn.I'm not even sure whether a general solution exists that works 100% of the time.I need a lot more time to find a solution.

author: niconiconi
author_rank: Frequent Contributor
timestamp: March 25, 2023, 03:14:54 pm
content: Quote from: niconiconi on March 18, 2023, 08:25:01 amYesterday, a helpful Reddit user told me that this GUI layout management problem is actually a real-world example of constrained optimization and linear programming, one of the most important problems in applied math and computer science. In the early 2000s, the Cassowary constraint-solving algorithm was developed specifically to solve this kind of GUI layout problems. It's uses a variant of the Simplex algorithm. There are many software libraries implementations of Cassowary.https://constraints.cs.washington.edu/solvers/cassowary-tochi.pdfI'm now trying to express the coordinates and relationship of objects as a set of equalities and inequalities, which can then be solved by the Cassowary engine. My initial attempt looked promising.