// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
movsd %xmm1, 0x40(%rsp)
movq 0xd0(%rsp), %rbx
movq 0xc8(%rsp), %r10
movq 0xc0(%rsp), %rax
movq 0xa0(%rsp), %r9
movslq %edi, %rbp
andq $0x0, (%r9,%rbp,8)
movq 0xa8(%rsp), %r11
andq $0x0, (%r11,%rbp,8)
movq 0xb0(%rsp), %r12
andq $0x0, (%r12,%rbp,8)
movq 0xb8(%rsp), %rdi
andq $0x0, (%rdi,%rbp,8)
movq 0x98(%rsp), %rdi
andq $0x0, (%rax,%rbp,8)
andq $0x0, (%r10,%rbp,8)
andq $0x0, (%rbx,%rbp,8)
movsd 0x80(%rsp), %xmm13
cvtsi2sd %esi, %xmm12
testl %ebp, %ebp
movsd %xmm12, 0x10(%rsp)
jle 0x5c8e
movsd %xmm0, 0x38(%rsp)
movsd (%rcx,%rbp,8), %xmm0
leal -0x1(%rbp), %eax
movq %rax, 0x28(%rsp)
movsd (%rcx,%rax,8), %xmm11
movapd %xmm0, %xmm1
subsd %xmm11, %xmm1
movapd %xmm1, %xmm8
divsd %xmm2, %xmm8
movsd 0x2559(%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 0x2630(%rip), %xmm4     # 0x8150
mulsd %xmm5, %xmm1
movsd 0x262c(%rip), %xmm6     # 0x8158
mulsd %xmm12, %xmm6
movapd %xmm6, %xmm5
divsd %xmm2, %xmm5
addsd %xmm11, %xmm0
mulsd %xmm0, %xmm5
addsd %xmm4, %xmm5
divsd %xmm3, %xmm6
movl %edx, %eax
subl %ebp, %eax
movslq %eax, %rcx
leal 0x1(%rbp), %eax
leaq (%rdi,%rcx,8), %rcx
addq $-0x8, %rcx
xorpd %xmm4, %xmm4
xorl %esi, %esi
cmpq %rsi, %rax
je 0x5b7b
movsd (%r12,%rsi,8), %xmm2
mulsd (%rcx,%rsi,8), %xmm2
addsd %xmm2, %xmm4
incq %rsi
jmp 0x5b62
divsd 0x25cd(%rip), %xmm1     # 0x8150
mulsd %xmm0, %xmm6
addsd %xmm1, %xmm6
movsd %xmm6, 0x20(%rsp)
mulsd 0x88(%rsp), %xmm4
movsd %xmm4, 0x18(%rsp)
movapd %xmm12, %xmm0
divsd 0x90(%rsp), %xmm0
movsd 0x25a2(%rip), %xmm1     # 0x8158
movsd %xmm3, 0x8(%rsp)
movl %edx, %r14d
movq %r8, %r15
movsd %xmm5, 0x30(%rsp)
callq 0x2330
movsd 0x80(%rsp), %xmm13
movq 0xa8(%rsp), %r11
movq 0xa0(%rsp), %r9
movq 0x98(%rsp), %rdi
movl %r14d, %edx
movsd 0x8(%rsp), %xmm3
mulsd 0x18(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movq 0x28(%rsp), %rax
movsd (%r11,%rax,8), %xmm1
movsd (%r12,%rax,8), %xmm2
movsd 0x8(%r13), %xmm5
mulsd %xmm2, %xmm5
movsd (%r13), %xmm4
mulsd %xmm1, %xmm4
mulsd 0x18(%r13), %xmm2
addsd %xmm5, %xmm4
movsd 0x30(%rsp), %xmm5
mulsd 0x10(%r13), %xmm1
addsd %xmm2, %xmm1
addsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
addsd %xmm0, %xmm5
addsd %xmm4, %xmm5
movsd 0x8(%r15), %xmm0
mulsd %xmm1, %xmm0
movsd (%r15), %xmm2
mulsd %xmm5, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, (%r11,%rbp,8)
mulsd 0x18(%r15), %xmm1
mulsd 0x10(%r15), %xmm5
addsd %xmm1, %xmm5
movsd %xmm5, (%r12,%rbp,8)
movsd 0x38(%rsp), %xmm0
jmp 0x5c9d
andq $0x0, (%r11)
andq $0x0, (%r12)
movsd (%r12,%rbp,8), %xmm5
movsd 0x2363(%rip), %xmm1     # 0x8008
subsd %xmm0, %xmm1
mulsd %xmm13, %xmm1
mulsd (%r11,%rbp,8), %xmm0
mulsd %xmm5, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%r9,%rbp,8)
testl %ebp, %ebp
movq 0xc8(%rsp), %r14
jle 0x5dc9
movsd (%r12,%rbp,8), %xmm0
leal -0x1(%rbp), %r15d
subsd (%r12,%r15,8), %xmm0
mulsd %xmm3, %xmm0
movsd %xmm0, (%r14,%rbp,8)
subl %ebp, %edx
movslq %edx, %rcx
movl %ebp, %eax
leaq (%rdi,%rcx,8), %rcx
movsd (%rbx,%rbp,8), %xmm0
xorl %edx, %edx
movsd 0x10(%rsp), %xmm2
movsd 0x90(%rsp), %xmm4
cmpq %rdx, %rax
je 0x5d2b
movsd (%r12,%rdx,8), %xmm1
mulsd (%rcx,%rdx,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%rbp,8)
incq %rdx
jmp 0x5d0d
movsd 0x88(%rsp), %xmm0
mulsd %xmm0, %xmm3
movsd %xmm3, 0x8(%rsp)
mulsd %xmm2, %xmm4
movsd 0x240e(%rip), %xmm1     # 0x8158
movapd %xmm4, %xmm0
callq 0x2330
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd (%r12,%rbp,8), %xmm0
addsd %xmm0, %xmm0
subsd (%rbx,%rbp,8), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%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,%r15,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
mulsd 0x88(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd (%rbx,%rbp,8), %xmm1
movq 0xc0(%rsp), %rax
addsd (%rax,%r15,8), %xmm1
movsd %xmm1, (%rax,%rbp,8)
jmp 0x5de9
andq $0x0, (%r14)
andq $0x0, (%rbx)
movq 0xb8(%rsp), %rax
andq $0x0, (%rax)
movq 0xc0(%rsp), %rax
andq $0x0, (%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq