Author Topic: [WIP] 2D MoM field solver (TNT-MMTL) in the Web browser with WebAssembly  (Read 1041 times)

0 Members and 1 Guest are viewing this topic.

Offline niconiconiTopic starter

  • Frequent Contributor
  • **
  • Posts: 362
  • Country: cn
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 is TransCalc [1]. 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 [2] and TNT-MMTL [3]. 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 [4]. 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 [5] 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 to download install 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.

[1] https://transcalc.sourceforge.net/

[2] https://atlc.sourceforge.net/

[3] https://mmtl.sourceforge.net/

[4] Lossy line is not supported, and occasionally, some inputs can generate ill-formed matrices that can't be solved.

[5] https://github.com/YoWASP/nextpnr
« Last Edit: March 11, 2023, 08:10:25 pm by niconiconi »
 
The following users thanked this post: pardo-bsso, jpanhalt

Offline WatchfulEye

  • Regular Contributor
  • *
  • Posts: 123
  • Country: gb
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.
 
The following users thanked this post: niconiconi

Offline niconiconiTopic starter

  • Frequent Contributor
  • **
  • Posts: 362
  • Country: cn
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.

I, 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.

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.

Interesting. 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?
« Last Edit: March 11, 2023, 08:27:36 pm by niconiconi »
 

Offline niconiconiTopic starter

  • Frequent Contributor
  • **
  • Posts: 362
  • Country: cn
Here's the latest demo:


« Last Edit: March 12, 2023, 01:55:44 pm by niconiconi »
 

Offline WatchfulEye

  • Regular Contributor
  • *
  • Posts: 123
  • Country: gb
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.
 

Offline niconiconiTopic starter

  • Frequent Contributor
  • **
  • Posts: 362
  • Country: cn
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 Browsers
https://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.  |O Instead, the position of all objects must be considered to preserve their logical relationship, and the entire diagram must be redrawn.  |O

I'm not even sure whether a general solution exists that works 100% of the time.  |O

I need a lot more time to find a solution.
« Last Edit: March 18, 2023, 08:26:43 am by niconiconi »
 

Offline niconiconiTopic starter

  • Frequent Contributor
  • **
  • Posts: 362
  • Country: cn
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.
[...]
I need a lot more time to find a solution.

Yesterday, 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.pdf

I'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.
« Last Edit: March 25, 2023, 03:18:32 pm by niconiconi »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf