// File: /workspace/github_cpp_makefile_star2_O3/JRLamontagne[P]cdice_sa/CDICE_doeclim.cpp
// Function: doeclim_ts(int, int, int, double*, double, double, double, double, double, double, double, double, double, double*, double*, double, double, double*, double*, double*, double*, double*, double*, double*, double*)
void doeclim_ts(int tstep, int dt, int ns, double *forcing, 
            double flnd, double powtoheat, double cal, double cas,
            double taucfl, double taukls, double taucfs, double tauksl,
            double bsi, double *IB, double *A, double fso,
            double taudif, double *Ker,
            double *temp, double *temp_landair, double *temp_sst, 
            double *heat_mixed, double *heat_interior, 
            double *heatflux_mixed, double *heatflux_interior)
{
  // Initialize variables for time-stepping through the model
  double DQ1 = 0.0;
  double DQ2 = 0.0;
  double QC1 = 0.0;
  double QC2 = 0.0;
  double DelQL = 0.0;
  double DelQO = 0.0;
  double DPAST1 = 0.0;
  double DPAST2 = 0.0;
  double DTEAUX1 = 0.0;
  double DTEAUX2 = 0.0;

	// Reset the endogenous varibales for this time step
	temp[tstep] = 0.0;
	temp_landair[tstep] = 0.0;
	temp_sst[tstep] = 0.0;
	heat_mixed[tstep] = 0.0;
	heat_interior[tstep] = 0.0;
	heatflux_mixed[tstep] = 0.0;
	heatflux_interior[tstep] = 0.0;

  // Assume land and ocean forcings are equal to global forcing
  double *QL = forcing;
  double *QO = forcing;
  
  if (tstep > 0) {
    
    DelQL = QL[tstep] - QL[tstep - 1];
    DelQO = QO[tstep] - QO[tstep - 1];
    
    // Assume linear forcing change between tstep and tstep+1
    QC1 = (DelQL/cal*(1.0/taucfl+1.0/taukls)-bsi*DelQO/cas/taukls);
    QC2 = (DelQO/cas*(1.0/taucfs+bsi/tauksl)-DelQL/cal/tauksl);
    QC1 = QC1 * pow(double(dt), 2.0)/12.0;
    QC2 = QC2 * pow(double(dt), 2.0)/12.0;

    // ----------------- Initial Conditions --------------------
    // Initialization of temperature and forcing vector:
    // Factor 1/2 in front of Q in Equation A.27, EK05, and Equation 2.3.27, TK07 is a typo! 
    // Assumption: linear forcing change between n and n+1
    DQ1 = 0.5*double(dt)/cal*(QL[tstep]+QL[tstep-1]);
    DQ2 = 0.5*double(dt)/cas*(QO[tstep]+QO[tstep-1]);
    DQ1 = DQ1 + QC1;
    DQ2 = DQ2 + QC2;

    // ---------- SOLVE MODEL ------------------
    // Calculate temperatures
    for(int i = 0; i <= tstep; i++) {
      DPAST2 = DPAST2 + temp_sst[i] * Ker[ns-tstep+i-1];
    }
    DPAST2 = DPAST2 * fso * pow((double(dt)/taudif), 0.5);
    
    DTEAUX1 = A[0] * temp_landair[tstep-1] + A[1] * temp_sst[tstep-1];
    DTEAUX2 = A[2] * temp_landair[tstep-1] + A[3] * temp_sst[tstep-1];

    temp_landair[tstep] = IB[0] * (DQ1 + DPAST1 + DTEAUX1) + IB[1] * (DQ2 + DPAST2 + DTEAUX2);
    temp_sst[tstep] = IB[2] * (DQ1 + DPAST1 + DTEAUX1) + IB[3] * (DQ2 + DPAST2 + DTEAUX2);    
    
  }
  
  else {  // Handle the initial conditions
    
    temp_landair[0] = 0.0;
    temp_sst[0] = 0.0;

  }

  temp[tstep] = flnd * temp_landair[tstep] + (1.0 - flnd) * bsi * temp_sst[tstep];

  // Calculate ocean heat uptake [W/m^2]
  // heatflux[tstep] captures in the heat flux in the period between tstep-1 and tstep.
  // Numerical implementation of Equation 2.7, EK05, or Equation 2.3.13, TK07)
  // ------------------------------------------------------------------------
  
  if (tstep > 0) {
    
    heatflux_mixed[tstep] = cas*(temp_sst[tstep] - temp_sst[tstep-1]);
    
    for (int i=0; i < tstep; i++) { 
      heatflux_interior[tstep] = heatflux_interior[tstep] + temp_sst[i]*Ker[ns-tstep+i];
    }
    
    heatflux_interior[tstep] = cas*fso/pow((taudif*dt), 0.5)*(2.0*temp_sst[tstep] - heatflux_interior[tstep]);
    
    heat_mixed[tstep] = heat_mixed[tstep-1] + heatflux_mixed[tstep] * (powtoheat*dt);
    
    heat_interior[tstep] = heat_interior[tstep-1] + heatflux_interior[tstep] * (fso*powtoheat*dt);
    
  }
  
  else {   // Handle the initial conditions
    
    heatflux_mixed[0] = 0.0;
    heatflux_interior[0] = 0.0;
    heat_mixed[0] = 0.0;
    heat_interior[0] = 0.0;
    
  }


  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r15
movq %r8, 0x38(%rsp)
movsd %xmm1, 0x40(%rsp)
movq 0xd0(%rsp), %r13
movq 0xc8(%rsp), %r9
movq 0xc0(%rsp), %r10
movq 0xb8(%rsp), %r8
movq 0xb0(%rsp), %r12
movq 0xa8(%rsp), %r14
movq 0xa0(%rsp), %rbx
movq 0x98(%rsp), %r11
movsd 0x80(%rsp), %xmm13
movslq %edi, %rbp
xorl %eax, %eax
movq %rax, (%rbx,%rbp,8)
movq %rax, (%r14,%rbp,8)
movq %rax, (%r12,%rbp,8)
movq %rax, (%r8,%rbp,8)
movq %rax, (%r10,%rbp,8)
movq %rax, (%r9,%rbp,8)
movq %rax, (%r13,%rbp,8)
cvtsi2sd %esi, %xmm12
testl %ebp, %ebp
movsd %xmm12, 0x10(%rsp)
jle 0x6118
movsd %xmm0, 0x30(%rsp)
movsd (%rcx,%rbp,8), %xmm0
leal -0x1(%rbp), %ebx
movsd (%rcx,%rbx,8), %xmm11
movapd %xmm0, %xmm1
subsd %xmm11, %xmm1
movapd %xmm1, %xmm8
divsd %xmm2, %xmm8
movsd 0x20c3(%rip), %xmm9     # 0x8008
movapd %xmm9, %xmm10
divsd %xmm4, %xmm10
movapd %xmm9, %xmm4
divsd %xmm5, %xmm4
addsd %xmm10, %xmm4
movapd %xmm1, %xmm10
mulsd %xmm13, %xmm10
divsd %xmm3, %xmm10
divsd %xmm5, %xmm10
mulsd %xmm8, %xmm4
subsd %xmm10, %xmm4
divsd %xmm3, %xmm1
divsd %xmm6, %xmm9
movapd %xmm13, %xmm5
divsd %xmm7, %xmm5
addsd %xmm9, %xmm5
mulsd %xmm1, %xmm5
divsd %xmm7, %xmm8
subsd %xmm8, %xmm5
movapd %xmm12, %xmm1
mulsd %xmm12, %xmm1
mulsd %xmm1, %xmm4
divsd 0x219a(%rip), %xmm4     # 0x8150
mulsd %xmm5, %xmm1
movsd 0x2196(%rip), %xmm6     # 0x8158
mulsd %xmm12, %xmm6
movapd %xmm6, %xmm5
divsd %xmm2, %xmm5
addsd %xmm11, %xmm0
mulsd %xmm0, %xmm5
addsd %xmm4, %xmm5
movsd %xmm5, 0x28(%rsp)
movsd %xmm3, (%rsp)
divsd %xmm3, %xmm6
movl %edx, 0xc(%rsp)
movl %edx, %eax
subl %ebp, %eax
movslq %eax, %rcx
leal 0x1(%rbp), %eax
leaq (%r11,%rcx,8), %rcx
addq $-0x8, %rcx
xorpd %xmm3, %xmm3
xorl %edx, %edx
movsd (%r12,%rdx,8), %xmm2
mulsd (%rcx,%rdx,8), %xmm2
addsd %xmm2, %xmm3
incq %rdx
cmpq %rdx, %rax
jne 0x6007
divsd 0x212a(%rip), %xmm1     # 0x8150
mulsd %xmm0, %xmm6
addsd %xmm1, %xmm6
movsd %xmm6, 0x20(%rsp)
mulsd 0x88(%rsp), %xmm3
movsd %xmm3, 0x18(%rsp)
movapd %xmm12, %xmm0
divsd 0x90(%rsp), %xmm0
movsd 0x20ff(%rip), %xmm1     # 0x8158
callq 0x2340
mulsd 0x18(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movq 0xa8(%rsp), %rax
movsd (%rax,%rbx,8), %xmm1
movsd (%r12,%rbx,8), %xmm2
movsd 0x8(%r15), %xmm3
mulsd %xmm2, %xmm3
movsd (%r15), %xmm4
mulsd %xmm1, %xmm4
mulsd 0x18(%r15), %xmm2
addsd %xmm3, %xmm4
mulsd 0x10(%r15), %xmm1
addsd %xmm2, %xmm1
addsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
movsd 0x28(%rsp), %xmm5
addsd %xmm0, %xmm5
addsd %xmm4, %xmm5
movq 0x38(%rsp), %rcx
movsd 0x8(%rcx), %xmm0
mulsd %xmm1, %xmm0
movsd (%rcx), %xmm2
mulsd %xmm5, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, (%rax,%rbp,8)
mulsd 0x18(%rcx), %xmm1
mulsd 0x10(%rcx), %xmm5
addsd %xmm1, %xmm5
movsd %xmm5, (%r12,%rbp,8)
movsd (%rsp), %xmm3
movl 0xc(%rsp), %edx
movq 0x98(%rsp), %r11
movsd 0x30(%rsp), %xmm0
movq 0xa0(%rsp), %rbx
movsd 0x80(%rsp), %xmm13
jmp 0x612a
xorl %eax, %eax
movq %rax, (%r14)
movq %rax, (%r12)
movsd (%r12,%rbp,8), %xmm5
movq %r14, %rax
movsd 0x1ed6(%rip), %xmm1     # 0x8008
subsd %xmm0, %xmm1
mulsd %xmm13, %xmm1
mulsd (%rax,%rbp,8), %xmm0
mulsd %xmm5, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%rbp,8)
testl %ebp, %ebp
movq 0xc8(%rsp), %r14
jle 0x624f
movsd (%r12,%rbp,8), %xmm0
leal -0x1(%rbp), %ebx
subsd (%r12,%rbx,8), %xmm0
mulsd %xmm3, %xmm0
movsd %xmm0, (%r14,%rbp,8)
subl %ebp, %edx
movsd (%r13,%rbp,8), %xmm0
movslq %edx, %rcx
movl %ebp, %eax
leaq (%r11,%rcx,8), %rcx
xorl %edx, %edx
movsd (%r12,%rdx,8), %xmm1
mulsd (%rcx,%rdx,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%r13,%rbp,8)
incq %rdx
cmpq %rdx, %rax
jne 0x618a
movsd 0x88(%rsp), %xmm0
mulsd %xmm0, %xmm3
movsd %xmm3, (%rsp)
movsd 0x90(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
movsd 0x1f87(%rip), %xmm1     # 0x8158
callq 0x2340
movsd (%rsp), %xmm1
divsd %xmm0, %xmm1
movsd (%r12,%rbp,8), %xmm0
addsd %xmm0, %xmm0
subsd (%r13,%rbp,8), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, (%r13,%rbp,8)
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm0
movsd 0x40(%rsp), %xmm1
mulsd %xmm1, %xmm0
mulsd (%r14,%rbp,8), %xmm0
movq 0xb8(%rsp), %rax
addsd (%rax,%rbx,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
mulsd 0x88(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd (%r13,%rbp,8), %xmm1
movq 0xc0(%rsp), %rax
addsd (%rax,%rbx,8), %xmm1
movsd %xmm1, (%rax,%rbp,8)
jmp 0x626e
xorl %eax, %eax
movq %rax, (%r14)
movq %rax, (%r13)
movq 0xb8(%rsp), %rcx
movq %rax, (%rcx)
movq 0xc0(%rsp), %rcx
movq %rax, (%rcx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq