# Simulating the Spherical p=2 Spin Model with DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/simulating-the-spherical-p-2-spin-model-with-differentialequations-jl/127640>\
**Category:** Modelling & Simulations\
**Tags:** sde\
**Created:** [April 2, 2025, 5:08pm UTC](https://discourse.julialang.org/t/simulating-the-spherical-p-2-spin-model-with-differentialequations-jl/127640 "2025-04-02T17:08:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtarabolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtarabolo/32/214462_2.png) [@mtarabolo](https://discourse.julialang.org/u/mtarabolo)\
**Post date:** [April 2, 2025, 5:08pm UTC](https://discourse.julialang.org/t/simulating-the-spherical-p-2-spin-model-with-differentialequations-jl/127640/1 "2025-04-02T17:08:11Z")

</div>

### **Issue: Correctly Implementing the Spherical Constraint**

I am working on simulating the **spherical p=2 spin model** using the `DifferentialEquations.jl` package in Julia. The model consists of **N** coupled **stochastic differential equations (SDEs)**:

\dot{x}\_i(t) = -\lambda(t)x\_i(t) + \sum\_{j=1}^{N} J\_{ij}x\_j(t)+\sqrt{2 D}\eta\_i(t)

where:

- x\_i(t) is the state of the i-th spin,
- J\_{ij} is the sparse coupling matrix,
- \eta\_i(t) is a Gaussian white noise (zero mean, variance 1, independent across sites and times),
- \lambda(t) is a **Lagrange multiplier** that enforces the spherical constraint, given by:

\lambda(t) = \frac{1}{N} \sum\_{i=1}^N x\_i(t) \left( \eta\_i(t) + \sum\_{j \in \partial i} J\_{ij} x\_j(t) \right)

### **Current Approach**

I have implemented a simulation without enforcing the spherical constraint and am now trying to correctly **update \lambda(t) at each time step**. Initially, I considered using a **callback** , but I now prefer **directly modifying the integrator’s state** inside the solver.

One challenge I am facing is that the noise realization dW is generated internally by the `solve` function, so I cannot directly pass it to a function f! that updates the deterministic part of the equation in-place. What is the best way to enforce the spherical constraint while ensuring that the correct noise realization is used at each step? How can I compute \lambda(t) without interfering with the solver’s internal noise generation?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 5, 2025, 1:04pm UTC](https://discourse.julialang.org/t/simulating-the-spherical-p-2-spin-model-with-differentialequations-jl/127640/2 "2025-04-05T13:04:11Z")

</div>

> [@mtarabolo](#):
>
> I have implemented a simulation without enforcing the spherical constraint and am now trying to correctly **update \lambda(t) at each time step**. Initially, I considered using a **callback** , but I now prefer **directly modifying the integrator’s state** inside the solver.

Modifying the state in the solver is not good…

Have you tried defining it as a DAE or using ManifoldProjection callbacks?
