// File: 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:
subq $0xf8, %rsp
movq 0x150(%rsp), %rax
movq 0x148(%rsp), %rax
movq 0x140(%rsp), %rax
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rax
movq 0x128(%rsp), %rax
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rax
movsd 0x110(%rsp), %xmm8
movsd 0x108(%rsp), %xmm8
movsd 0x100(%rsp), %xmm8
movl %edi, 0xf4(%rsp)
movl %esi, 0xf0(%rsp)
movl %edx, 0xec(%rsp)
movq %rcx, 0xe0(%rsp)
movsd %xmm0, 0xd8(%rsp)
movsd %xmm1, 0xd0(%rsp)
movsd %xmm2, 0xc8(%rsp)
movsd %xmm3, 0xc0(%rsp)
movsd %xmm4, 0xb8(%rsp)
movsd %xmm5, 0xb0(%rsp)
movsd %xmm6, 0xa8(%rsp)
movsd %xmm7, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
movq %r9, 0x90(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x78(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x120(%rsp), %rax
movslq 0xf4(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x128(%rsp), %rax
movslq 0xf4(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x130(%rsp), %rax
movslq 0xf4(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x138(%rsp), %rax
movslq 0xf4(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x140(%rsp), %rax
movslq 0xf4(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x148(%rsp), %rax
movslq 0xf4(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x150(%rsp), %rax
movslq 0xf4(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0xe0(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpl $0x0, 0xf4(%rsp)
jle 0x18916
movq 0x38(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x38(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x30(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x30(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x68(%rsp), %xmm0
divsd 0xc8(%rsp), %xmm0
movsd 0x3b59(%rip), %xmm2     # 0x1c008
divsd 0xb8(%rsp), %xmm2
movsd 0x3b48(%rip), %xmm1     # 0x1c008
divsd 0xb0(%rsp), %xmm1
addsd %xmm1, %xmm2
movsd 0x100(%rsp), %xmm1
mulsd 0x60(%rsp), %xmm1
divsd 0xc0(%rsp), %xmm1
divsd 0xb0(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x60(%rsp), %xmm0
divsd 0xc0(%rsp), %xmm0
movsd 0x3ade(%rip), %xmm2     # 0x1c008
divsd 0xa8(%rsp), %xmm2
movsd 0x100(%rsp), %xmm1
divsd 0xa0(%rsp), %xmm1
addsd %xmm1, %xmm2
movsd 0x68(%rsp), %xmm1
divsd 0xc8(%rsp), %xmm1
divsd 0xa0(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
cvtsi2sdl 0xf0(%rsp), %xmm0
movsd 0x3be5(%rip), %xmm1     # 0x1c188
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd 0x3c4b(%rip), %xmm1     # 0x1c208
divsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
cvtsi2sdl 0xf0(%rsp), %xmm0
movsd 0x3ba4(%rip), %xmm1     # 0x1c188
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd 0x3c0a(%rip), %xmm1     # 0x1c208
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
cvtsi2sdl 0xf0(%rsp), %xmm1
movsd 0x3be7(%rip), %xmm0     # 0x1c200
mulsd %xmm1, %xmm0
divsd 0xc8(%rsp), %xmm0
movq 0x38(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x38(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addsd (%rax,%rcx,8), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
cvtsi2sdl 0xf0(%rsp), %xmm1
movsd 0x3b93(%rip), %xmm0     # 0x1c200
mulsd %xmm1, %xmm0
divsd 0xc0(%rsp), %xmm0
movq 0x30(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x30(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addsd (%rax,%rcx,8), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x88(%rsp), %xmm0
addsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x80(%rsp), %xmm0
addsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0xf4(%rsp), %eax
jg 0x1874d
movsd 0x50(%rsp), %xmm1
movq 0x130(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x118(%rsp), %rax
movl 0xec(%rsp), %ecx
subl 0xf4(%rsp), %ecx
addl 0x2c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x186e8
movsd 0x50(%rsp), %xmm0
mulsd 0x108(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
cvtsi2sdl 0xf0(%rsp), %xmm0
divsd 0x110(%rsp), %xmm0
movsd 0x3a84(%rip), %xmm1     # 0x1c200
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x90(%rsp), %rax
movsd (%rax), %xmm0
movq 0x128(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
movq 0x90(%rsp), %rax
movsd 0x8(%rax), %xmm1
movq 0x130(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
mulsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x90(%rsp), %rax
movsd 0x10(%rax), %xmm0
movq 0x128(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
movq 0x90(%rsp), %rax
movsd 0x18(%rax), %xmm1
movq 0x130(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
mulsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x98(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x88(%rsp), %xmm2
addsd 0x58(%rsp), %xmm2
addsd 0x48(%rsp), %xmm2
movq 0x98(%rsp), %rax
movsd 0x8(%rax), %xmm1
movsd 0x80(%rsp), %xmm3
addsd 0x50(%rsp), %xmm3
addsd 0x40(%rsp), %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x128(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x98(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd 0x88(%rsp), %xmm2
addsd 0x58(%rsp), %xmm2
addsd 0x48(%rsp), %xmm2
movq 0x98(%rsp), %rax
movsd 0x18(%rax), %xmm1
movsd 0x80(%rsp), %xmm3
addsd 0x50(%rsp), %xmm3
addsd 0x40(%rsp), %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x130(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x18934
movq 0x128(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x130(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movsd 0xd8(%rsp), %xmm0
movq 0x128(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd 0x36ae(%rip), %xmm1     # 0x1c008
subsd 0xd8(%rsp), %xmm1
mulsd 0x100(%rsp), %xmm1
movq 0x130(%rsp), %rax
movslq 0xf4(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x120(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpl $0x0, 0xf4(%rsp)
jle 0x18bfe
movsd 0xc0(%rsp), %xmm0
movq 0x130(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x130(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
subsd (%rax,%rcx,8), %xmm1
mulsd %xmm1, %xmm0
movq 0x148(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0xf4(%rsp), %eax
jge 0x18a85
movq 0x150(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x130(%rsp), %rax
movslq 0x28(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x118(%rsp), %rax
movl 0xec(%rsp), %ecx
subl 0xf4(%rsp), %ecx
addl 0x28(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x150(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x18a05
movsd 0xc0(%rsp), %xmm0
mulsd 0x108(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x110(%rsp), %xmm0
cvtsi2sdl 0xf0(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x3745(%rip), %xmm1     # 0x1c200
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
movq 0x130(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x150(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq %xmm2, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm2
addsd %xmm1, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movq 0x150(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x138(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x148(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0xd0(%rsp), %xmm2
cvtsi2sdl 0xf0(%rsp), %xmm3
mulsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x138(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x140(%rsp), %rax
movl 0xf4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x150(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x108(%rsp), %xmm2
mulsd 0xd0(%rsp), %xmm2
cvtsi2sdl 0xf0(%rsp), %xmm3
mulsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x140(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x18c3a
movq 0x148(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x150(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x138(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x140(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
addq $0xf8, %rsp
retq
nopw %cs:(%rax,%rax)