# Molly for Emulsion Rheology

**URL:** <https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386>\
**Category:** Modelling & Simulations\
**Created:** [October 23, 2025, 1:40pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386 "2025-10-23T13:40:02Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [October 23, 2025, 1:40pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/1 "2025-10-23T13:40:02Z")

</div>

Greetings, How does one implement on imposed shear force in MD simulations in general and Molly in particular? For context, see e.g. [https://github.com/ziolai/finite\_element\_electrical\_engineering/tree/main/project-based-assignment/making\_mayonnaise](https://github.com/ziolai/finite_element_electrical_engineering/tree/main/project-based-assignment/making_mayonnaise)

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [October 23, 2025, 1:49pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/2 "2025-10-23T13:49:47Z")

</div>

Could you point to a particular interaction equation you want to implement?

Molly only works with particle simulations, so finite element or fluid dynamics methods may not be applicable.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [October 23, 2025, 3:35pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/3 "2025-10-23T15:35:16Z")

</div>

Dear Joe,

We want to implement two interactions.

The first interaction is the random motion of oil droplets in water. This interaction can be modeled be the minimization of the entropic free energy. This term dominates in case of low volume fraction of oil droplets in water.

The second interaction in the deformation of the interface between the oil droplets and water. This interaction can be modeled by the minimization of the interfacial energy. This term dominates in case of high volume fraction of oil droplets in water.

Both interaction depend on the magnitude of external force (dilation or shear) applied to the sample. The entropic interaction depends on the amount of external force via the partition function. The interfacial interaction depends on the amount of external force via the amount of deviation from perfect spheres.

Both interactions have closed-form expressions for the free energy as a function of the volume fraction in case of mono-disperse oil droplets (all oil droplets having the same diameter). We are happy to start MD assuming mono-disperse simulations.

Does Molly allow to derive interaction potentials from free energy potential expressed in terms of the volume fraction of the droplets?

Thank you. Domenico.

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [October 23, 2025, 4:30pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/4 "2025-10-23T16:30:10Z")

</div>

> [@ziolai](#):
>
> Does Molly allow to derive interaction potentials from free energy potential expressed in terms of the volume fraction of the droplets?

I don’t have much experience of what this would look like. If the potential can be calculated as a function of the droplet positions and properties then yes, you should be able to implement it in Molly.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [October 23, 2025, 5:23pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/5 "2025-10-23T17:23:57Z")

</div>

Thanks!

Are there examples of Molly.jl simulations with post-processing for local values of the mixture fraction of particles or the [partition function](https://en.wikipedia.org/wiki/Partition_function_(statistical_mechanics))?

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [October 23, 2025, 5:48pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/6 "2025-10-23T17:48:33Z")

</div>

Not that I know of, sorry.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [October 23, 2025, 6:04pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/7 "2025-10-23T18:04:30Z")

</div>

Many thanks! Good to know that my queries are not naive.

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [October 23, 2025, 10:24pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/8 "2025-10-23T22:24:41Z")

</div>

Not at all, and if you do find something useful then consider contributing it as an example to the documentation.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [October 24, 2025, 7:29am UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/9 "2025-10-24T07:29:58Z")

</div>

Correct!

Our current path of flight is therefore as outlined below.

## Build Your Own Particle Simulator from Scratch

The code listed below is taken from [this Discourse Post](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/42).

With 15 lines, Leandro Martinez (@lmiq) writes a code to perform a particle simulation with periodic boundary conditions, a langevin thermostat, a quadratic potential between the particles, and produce an animation (see below).

The code below uses the function wrap that in turn uses the ternary operator a?b:c explained at [control flow](https://docs.julialang.org/en/v1/manual/control-flow/#man-conditional-evaluation).

**Exercise** Extend this code with:

1. monitor the potential, kinetic and total energy on each particle. Verify in which conditions (implicit vs. explicit time stepping, order of time stepping method, fixed vs. adaptive time step, time step size) the total energy remains preserved (i.e. constant in time);
2. assume a uniform mesh on the domain of computation. Compute the mixture fraction on the mesh;
3. assume a computed mixture fraction on a uniform mesh. Compute the partion function on this mesh;
4. differentiate the total energy wrt the position vector \mathbf{x}\_i of particle i to obtain the force vector \mathbf{F}\_i on particle i. Possibly use [ForwardDiff](https://juliadiff.org/ForwardDiff.jl/stable/) or any for this differentiation;
5. differentiate the force vector \mathbf{F}\_i on particle i wrt its position vector \mathbf{x}\_i (thus obtaining the second derivative of the energy) to obtain the coefficients in the constitutive force-strain relation;
6. repeat above for alternative choices of the potential between particles. Choices include adding a cubic term to the potential (Duffing oscillators), Langevin and Van der Waals potentials. Verify whether the later allow to take particle sizes into account;
7. taylor the above framework to emulsions formed by vegetable oil droplets in water by considering the entropic and interfacial free energy of the emulsion undergoing a shear force;
8. verify to what extend the package [Molly](https://juliamolsim.github.io/Molly.jl/stable/) allows to automate and/or extend any of the above steps. Please note that the Molly package allows for differentiable simulations. This property motivates our choice for this package;

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [December 17, 2025, 8:58am UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/10 "2025-12-17T08:58:56Z")

</div>

Here is a [Video](https://tud365-my.sharepoint.com/personal/dsnik_tudelft_nl/_layouts/15/stream.aspx?id=%252Fpersonal%252Fdsnik_tudelft_nl%252FDocuments%252FBijlagen%252FWhatsApp%20Video%202025-12-05%20at%2022.52.05.mp4&ct=1765960395924&or=OWA-NT-Mail&cid=8318ab74-4857-46b0-30ba-96a1c5f0246b&ga=1&referrer=StreamWebApp.Web&referrerScenario=AddressBarCopied.view.b4465468-95e7-41f1-8f30-f48313a8326e) showing how students are taking advantage of Molly to make progress in their project. I am happy to discuss this further.

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [December 17, 2025, 12:03pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/11 "2025-12-17T12:03:51Z")

</div>

Thanks for sharing. The link seems to require a login though?

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [December 17, 2025, 7:42pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/12 "2025-12-17T19:42:58Z")

</div>

Apologies for this glitch. I asked students to prepare more results. Hope to be back here soon. Thx.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [December 18, 2025, 7:02pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/13 "2025-12-18T19:02:30Z")

</div>

The video is now available at [video](https://drive.google.com/file/d/1T3wJDfLSW_WMIQhvoUiB72riKvNXzrTk/view)

The animation shows two kinds of particles. The first and second kind are inside and outside the disk, respectively.

In a next step, we would like to gain a better understanding of these simulations.

We plan to do so by

1. considering one kind of spherical particles only (oil droplets);
2. lowering the numbering of particles, thus allowing faster computations on smaller problem size;
3. considering an [entropic force](https://en.wikipedia.org/wiki/Entropic_force) as surface force between the particles. We plan to take advantage of user-defined pairwise interactions in Molly to implement such a force. Aside from the entropic force, particles will also be subjected to the wall shear force;
4. run simulations at a fixed temperature (using a thermostat to fix the T-value) and fixed angular frequency of the wall shearing force until the entropic energy reaches a steady-state value;
5. computing the average shear stress on the particles (output of the simulations) hoping to recover a sinusoidal signal at a single dominant frequency;
6. computing the auto-correlation of the shear stress (Green-Kubo) hoping again to recover a sinusoidal signal at a single dominant frequency;
7. repeat above for a set of frequencies of the wall shear hoping to recover a plateau zone for the real part of the dynamic modulus (the elastic modulus);
8. repeat above for various temperature hoping to recover published results for the elastic modulus;

Details are missing and need to be filled in.

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [December 18, 2025, 10:14pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/14 "2025-12-18T22:14:32Z")

</div>

Very nice, I’d be interested to hear how you get on.

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [December 29, 2025, 6:41pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/15 "2025-12-29T18:41:28Z")

</div>

Greetings.

Is it obvious how to extend [Comparing-forces-to-AD](https://juliamolsim.github.io/Molly.jl/stable/examples/#Comparing-forces-to-AD) to a system of N particles in 2D?

Thank you.

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [December 29, 2025, 10:54pm UTC](https://discourse.julialang.org/t/molly-for-emulsion-rheology/133386/16 "2025-12-29T22:54:46Z")

</div>

For the more general case I would recommend Enzyme. You can compare `forces` to `autodiff` of the `potential_energy` call, hopefully for most cases on CPU.

See for example [Molly.jl/test/gradients.jl at 5c0ef7507d24ad7e69ce1a9af7019f52f9fe831c · JuliaMolSim/Molly.jl · GitHub](https://github.com/JuliaMolSim/Molly.jl/blob/5c0ef7507d24ad7e69ce1a9af7019f52f9fe831c/test/gradients.jl#L1-L35).
