Neuroinflab / kESI

GNU General Public License v3.0
1 stars 5 forks source link

update kESI engine/solver #17

Open mdovgialo opened 1 week ago

mdovgialo commented 1 week ago

Start by recreating sphere solution from tutorial

mdovgialo commented 1 week ago

17 trying out mfem

Managed to load our 4 spheres model, without ground place, experimenting with boundary conditions and poin charges. Right now only boundary conditions seem to work, not sure how:

image

image

Making only the outside sphere have 0 potential results in zero potential everywhere, despite a point charge present...

image

Next: try adding ground plane like in FENICS solver

mdovgialo commented 1 week ago

MFEM seems to have predefined (but flexible) integrators for different classes of differential equations. It might be an issue to write a custom equation to solve in python

mdovgialo commented 1 week ago

Solved a bug which didn't notice yesterday. Now 3 spheres with a point in it can look like this:

COefficients all equall 0.33

image

coeeficients equal: [0.33, 1.65, 0.0165, 0.33])

image

and coefficients which are reciprocity of above:

image

Unity Point source on the side. Outside sphere has a boundary condition of being 0. Can't figure out how to add a grounding plane yet...

mdovgialo commented 2 days ago

Implemented solver, which seems to do the correct thing. Implemented an experimental voxel based resampling of a point cloud of the solution, might not be needed for equally sampled volumetric solutions in spirit of "MRI as a hex mesh" . For now allows to decently sample any mesh at any resolution, smoothed with boxcar kernel of a size of biggest mesh element

We put an electrode on top of the brain, just below the skull/CSF.

First, let's look at analytic solution of assuming

V_kcsd = 1.0 / (4 np.pi sigma_base * distance_to_electrode)

Which we define on vertices of vertices of the mesh we are using, and then sampled using voxel based resampling (kinda sanity check of a MFEM solution postprocessing methods):

image

Contrast is set that at 10 Volts it's exactly white, so we can follow 10 Volt isolines. Cross is put on an isoline

sigma = 0.33.

Lets compare it with MFEM simulations of a point charge at the electrode positions, using 3 meshes:

  1. 4 spheres in an air box, with grounding plate being a bottom surface of the box, just below the 4 shperes
  2. 4 spheres in an air box, with grounding electrode in brain tissue
  3. 4 spheres with a grounding electrode in brain
  4. 4 spheres with a grounding electrode in skin

To simulate point charges a DiffusionIntegrator, which solves poisson equation was used. Physical Volumes had assigned coefficients:

conductivities_vector = mfem.Vector(list(1.0 / (4 np.pi np.array([0.33, 1.65, 0.0165, 0.33, 1e-10])))) (conductances, of brain matter, CSF, bone, skin, air (when model had air box))

Additionally a domain integrator was used to place a point charge using DeltaCoefficient.

4 spheres in an air box, with grounding plate

image

Contrast is set that at 10 Volts it's exactly white, so we can follow 10 Volt isolines. Cross is put on an isoline of analytical solution

image

Contrast set to 1V so we can see the the effect of ground

4 spheres in an air box, with grounding electrode in brain tissue

image

Contrast is set that at 10 Volts it's exactly white, so we can follow 10 Volt isolines. Cross is put on an isoline of analytical solution

image

Contrast set to 1V so we can see the the effect of ground electrode

4 spheres with a grounding electrode in brain

image

Contrast is set that at 10 Volts it's exactly white, so we can follow 10 Volt isolines. Cross is put on an isoline of analytical solution

image

Contrast set to 400V so we can see the the effect of ground electrode

We can see that without surrounding air, the potential inside the spheres is a lot higher

4 spheres with a grounding electrode in skin

image

Contrast is set that at 10 Volts it's exactly white, so we can follow 10 Volt isolines. Cross is put on an isoline of analytical solution

image

Contrast set to 400V so we can see the the effect of ground electrode

The overall potential in the spheres is lower compared to electrode in the brain tissue, but still is way higher compared to free space analytical solution.

Old FENICS/KESI leadfield correction potential

image

This image is a difference between FEM solution and free space solution. The mesh is 4 spheres without air, but with a forced infinte grounding plane touching the bottom of the spheres.

Let's try to have a more decent comparisson, by addign the free space solution to the leadfield:

image

Contrast is set that at 10 Volts it's exactly white, so we can follow 10 Volt isolines. Cross is put on an isoline of analytical solution. We can see that the whole sphere is way above 10 Volt line, except the part, where the sphere meets the grounding plate.

image

Contrast set at 400 volts, we can see the weird interpolation, slighly triangular in shape, which gets visible even better, if we just look at the difference between FEM solution and free space solution, calculated by the FENICS zoomed in

image We can observe triangular, hexagonal artifacts with protrusions on connecting edges, which means, this image is sampled in a way higher resolution than the size of FEM elements (this was calculated using "fine" preset of KESI 4 spheres template)

Conclusions?

As we can see We can recreate similar point source potential distribution, on way higher resolution meshes, than FENICS, with calculations taking a fraction of time. Mmesh with maximum element size of 0.005 meters (around 5 milion elements after refining) takes a dozen of seconds to simulate, and 30 to sample using KDTrees, and MFEM is not even compiled with CUDA or multithreading support.

There doesn't seem to be problems with the electrode position singularity. But lack of air volume in legacy KESI models, seems to elevate potential inside the sphere by orders of magnitude. Adding air box, makes the solution similar in shape and orders of magnitude to free space analytical (in the parts of isotropic, even material), and seem to have similar isolines at V=10, 20, but different at low voltages (1, 2 5). The isolines on the FEM solution are deformed by the mesh boundaries, which is what we wanted to see.

Lack of air volume might have introduced elevated potential, and changed the profiles

Todo

@danek8317 What do you think? I guess we'll discuss it on monday.

mdovgialo commented 2 days ago

image

comparison of profiles going vertically through 4 spheres through the electrode