NMR Sequences and Spin Transfer

NMR simulations to make your head spin...

C vs Python

Once I was hired to work on a project involving simulating NMR experiments. After a few years of wrenching on machines and dealing with deuterium solvents I decided it might be good to level up my computational experience further and start to do more physical chemistry computing work.

I worked in a small lab with just a PI - no postdocs or grad students. It was up to me just to figure it all out, go to the fourth floor sub-basement and dig through years of old work to find anything that would help. Our goal was to write code, understanding some matrix math, to simulate the behavior of a spin system in a magnetic field. At the time there was a really oddly written library called Gamma for doing this kind of work.

Question: Is the multiplication of two simulatenously diagonalizable matrices always commutative?

Answer: Yes - but not if you’re Gamma.

My job was two fold - take some old documents full of diagrams like this one but written in pencil:

Is this intelligible whatsoever?

And translate them into C language code that would run the experiment and reproduce the results in those old papers. The second part of my job was to take the Gamma library, update and fix the Python SWIG bindings, and write the same experiments in Python validating they worked the same.

I was able to do both of these things, it looked kind of like this:

Helpful Insights


I learned a tremendous amount about porting libraries between languages and what that would have taken. Many of the problems I encountered resulted from a simple fact - scientists write code for science and not necessarily according to the usual engineering practices. EG, the joke I made above about matrix multiplication being non-commutative where it should have been, was a direct result of receiving method arguments kind of oddly.

It really is true that two fields have different processes, concerns, and are really distinct jobs. It was actually really valuable to see how assumptions made for doing scientific demonstration probably could translate better into reusable experimental frameworks, if certain common engineering practices like proper interface and OOP design were more common knowledge in the physics and chemistry communities.