0

I’m trying to solve the following problem for around 2 days, but have not had success. I want to calculate the derivative using the sparse matrix, but the result doesn’t true. I think there is a mistake in the solution function, but I cannot find it.

class Cahn_Hillard(object):

    def __init__(self, n, X, T, dt, skip):
        self.n = n
        self.X = X
        self.dx = 1 / (n - 1)
        self.T = T
        self.dt = dt
        self.N = int(self.T / self.dt)

    def __call__(self):

        central = self.forward_diff_sparse()
        itr = int(self.T / self.dt)
        for i in range(0, itr + 1):
            if i == 0:
                c = np.random.uniform(0, 1, (self.n, 1))
            else:
                c_next = self.solution(c, central)
                c = c_next
                print(i)

        return c

    def forward_diff_sparse(self):

        sparse_mat = np.eye(self.n) - np.eye(self.n, k=-1)
        return sparse_mat

    def solution(self, c, central):
        # calculate derivative of concentration
        deriv = central.dot(c) / self.dx

        # calculate difusion coeffcient 
        D_eff = (1 - 2 * self.X * c * (1 - c)) * deriv
        Diff = central.dot(D_eff) / self.dx

        # Calculate the next step concentration
        next_c = c + self.dt * Diff
        return next_c

It would be great if you help me.

Ben
  • 20,038
  • 30
  • 112
  • 189
Javad
  • 1
  • The parameters are: n = 100 X = 0 T = 0.012 dt = 4e-6 skip = 100 Eq = Cahn_Hillard(n, X, T, dt, skip) simulation = Eq() print(np.max(simulation)) – Javad Feb 05 '22 at 02:06
  • Please provide actual and desired results. – Yuri Ginsburg Feb 05 '22 at 02:41
  • By considering X=0, This is a simple Poisson equation. – Javad Feb 05 '22 at 08:59
  • Do you mean you want to express `central` using `scipy.sparse`, possibly using `spdiags`? What is the PDE you are trying to solve? Are you sure that the transport, diffusion and reaction terms are correctly implemented? – Lutz Lehmann Feb 05 '22 at 12:18
  • I just want to calculate laplacian with two times using `sparse_mat = np.eye(self.n) - np.eye(self.n, k=-1)`. It is a forward difference. First I want to look at this equation as a diffusion Eq. (X=0). After solving the issue, I want to apply nonlinearity to this equation. – Javad Feb 05 '22 at 22:03

0 Answers0