// File: /workspace/github_c_makefile_star2_O2/PoulinV[P]AxiCLASS/build/../source/background.c
// Function: background_derivs
int background_derivs(
                      double loga,
                      double* y, /* vector with argument y[index_bi] (must be already allocated with size pba->bi_size) */
                      double* dy, /* vector with argument dy[index_bi]
                                     (must be already allocated with
                                     size pba->bi_size) */
                      void * parameters_and_workspace,
                      ErrorMsg error_message
                      ) {

  /** Summary: */

  /** - define local variables */

  struct background_parameters_and_workspace * pbpaw;
  struct background * pba;
  double * pvecback, a, H, rho_M;
  double w_fld;
  pbpaw = parameters_and_workspace;
  pba =  pbpaw->pba;
  pvecback = pbpaw->pvecback;
  double cos_initial,sin_initial,n,Gac;
  /** - scale factor a (in fact, given our normalisation conventions, this stands for a/a_0) */
  a = exp(loga);

  /** - calculate functions of \f$ a \f$ with background_functions() */
  class_call(background_functions(pba, a, y, normal_info, pvecback),
             pba->error_message,
             error_message);

  /** - Short hand notation for Hubble */
  H = pvecback[pba->index_bg_H];

  /** - calculate derivative of cosmological time \f$ dt/dloga = 1/H \f$ */
  dy[pba->index_bi_time] = 1./H;

  /** - calculate derivative of conformal time \f$ d\tau/dloga = 1/aH \f$ */
  dy[pba->index_bi_tau] = 1./a/H;

  class_test(pvecback[pba->index_bg_rho_g] <= 0.,
             error_message,
             "rho_g = %e instead of strictly positive",pvecback[pba->index_bg_rho_g]);



   /* VP; in AxiCLASS we can switch from KG equation to fluid variables for the scalar field*/
   if(pba->has_scf == _TRUE_ && pba->scf_evolve_as_fluid == _TRUE_ ){
     if(pba->m_scf*pba->H0/H >= pba->threshold_scf_fluid_m_over_H){ //We switch for fluid equations at m > 3H by default.
       pba->scf_kg_eq = _FALSE_;
       // if(pba->scf_potential==axionquad &&  pba->a_c==1.0 ){
       if(pba->a_c==1.0 ){
         pba->a_c = a; //we defined a_c as the time at which the transition occurs. this is necessary for the perts.
         //for other potentials, a_c was already defined.
       }

       // printf("pba->a_c %e\n", pba->a_c);

     }
     else{
       pba->scf_kg_eq = _TRUE_;
     }
   }



  /** - calculate detivative of sound horizon \f$ drs/dloga = drs/dtau * dtau/dloga = c_s/aH \f$*/
  dy[pba->index_bi_rs] = 1./a/H/sqrt(3.*(1.+3.*pvecback[pba->index_bg_rho_b]/4./pvecback[pba->index_bg_rho_g]))*sqrt(1.-pba->K*y[pba->index_bi_rs]*y[pba->index_bi_rs]); // TBC: curvature correction

  /** - solve second order growth equation \f$ [D''(\tau)=-aHD'(\tau)+3/2 a^2 \rho_M D(\tau) \f$
      written as \f$ dD/dloga = D' / (aH) \f$ and \f$ dD'/dloga = -D' + (3/2) (a/H) \rho_M D \f$ */
  rho_M = pvecback[pba->index_bg_rho_b];
  if (pba->has_cdm == _TRUE_) {
    rho_M += pvecback[pba->index_bg_rho_cdm];
  }
  if (pba->has_idm == _TRUE_){
    rho_M += pvecback[pba->index_bg_rho_idm];
  }
  if (pba->has_scf == _TRUE_ && pba->include_scf_in_growth_factor == _TRUE_) {
    /*VP: add the scf contribution if the user wants to, e.g., for axion-like dark matter */
    if(pba->scf_potential==axionquad)
    rho_M += pvecback[pba->index_bg_rho_scf];
    else if(pba->scf_potential==axion && pba->n_axion ==1){
      rho_M += pvecback[pba->index_bg_rho_scf];
    }
    else{
      /*ignore contribution*/
    }
  }
  dy[pba->index_bi_D] = y[pba->index_bi_D_prime]/a/H;
  dy[pba->index_bi_D_prime] = -y[pba->index_bi_D_prime] + 1.5*a*rho_M*y[pba->index_bi_D]/H;

  if (pba->has_dcdm == _TRUE_) {
    /** - compute dcdm density \f$ d\rho/dloga = -3 \rho - \Gamma/H \rho \f$*/
    dy[pba->index_bi_rho_dcdm] = -3.*y[pba->index_bi_rho_dcdm] - pba->Gamma_dcdm/H*y[pba->index_bi_rho_dcdm];
  }

  if ((pba->has_dcdm == _TRUE_) && (pba->has_dr == _TRUE_)) {
    /** - Compute dr density \f$ d\rho/dloga = -4\rho - \Gamma/H \rho \f$ */
    dy[pba->index_bi_rho_dr] = -4.*y[pba->index_bi_rho_dr]+pba->Gamma_dcdm/H*y[pba->index_bi_rho_dcdm];
  }

  if (pba->has_fld == _TRUE_) {
    /** - Compute fld density \f$ d\rho/dloga = -3 (1+w_{fld}(a)) \rho \f$ */
    dy[pba->index_bi_rho_fld] = -3.*(1.+pvecback[pba->index_bg_w_fld])*y[pba->index_bi_rho_fld];
  }

  if (pba->has_scf == _TRUE_){
    /*<VP: Main modifications to SCF in AxiCLASS: we can seither solve using KG equations or fluid variables.*/
    /** - Scalar field equation: \f$ \phi'' + 2 a H \phi' + a^2 dV = 0 \f$  (note H is wrt cosmic time) */
    /*COComment - add if statement, dependent on flag, to either use KG equation or fluid equation  */
    // printf("inside SF evolution call\n");
    if (pba->scf_kg_eq == _TRUE_) {
    /* VP: OLD AXICLASS: derivative with respect to conformal time */
    // dy[pba->index_bi_phi_scf] = y[pba->index_bi_phi_prime_scf];
    // dy[pba->index_bi_phi_prime_scf] = - y[pba->index_bi_a]*
    //   (2*pvecback[pba->index_bg_H]*y[pba->index_bi_phi_prime_scf]
    //    + y[pba->index_bi_a]*dV_scf(pba,y[pba->index_bi_phi_scf])) ;

    /* VP: NEW AXICLASS: derivative with respect to log(a) */
    /** - Scalar field equation: \f$ \phi'' + 2 a H \phi' + a^2 dV = 0 \f$  (note H is wrt cosmological time)
        written as \f$ d\phi/dlna = phi' / (aH) \f$ and \f$ d\phi'/dlna = -2*phi' - (a/H) dV \f$ */
    dy[pba->index_bi_phi_scf] = y[pba->index_bi_phi_prime_scf]/a/H;
    dy[pba->index_bi_phi_prime_scf] = - 2*y[pba->index_bi_phi_prime_scf] - a*dV_scf(pba,y[pba->index_bi_phi_scf])/H ;

    dy[pba->index_bi_rho_scf] = 0; //Update the scf density until the fluid equation starts.
    // printf("aEvolving scalar field using KG equation. phi %e phi prime %e \n", y[pba->index_bi_phi_scf],y[pba->index_bi_phi_prime_scf]);
    // printf("dV %e \n", dV_scf(pba,y[pba->index_bi_phi_scf])  );
    // if(pba->background_verbose > 11) printf("Evolving scalar field using KG equation. phi %e phi prime %e \n", y[pba->index_bi_phi_scf],dy[pba->index_bi_phi_scf]  );
    }
    else if(pba->scf_kg_eq == _FALSE_) {
    dy[pba->index_bi_rho_scf] = -3.*y[pba->index_bi_rho_scf]*(1+pba->w_scf);
    dy[pba->index_bi_phi_scf] = 0;
    dy[pba->index_bi_phi_prime_scf] = 0;
    if(pba->background_verbose > 11) printf("Evolving scalar field using fluid equation, rho %e rho prime %e.\n",y[pba->index_bi_rho_scf],dy[pba->index_bi_rho_scf]);

    //
    }
    else if (pba->scf_evolve_as_fluid == _FALSE_ && pba->scf_kg_eq == _FALSE_) {
      /*COComment Throw an error code if neither KG nor fluid equations apply - this should never happen */
      class_stop(pba->error_message,"We are not evolving scalar field as KG nor fluid eq, something has gone wrong!\n");
    }
}



  return _SUCCESS_;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movq %rcx, %r13
movq %rsi, %r14
movq %rdi, %r15
movq (%rdx), %rbx
movq 0x8(%rdx), %r12
callq 0x86a0
pushq $0x1
popq %rbp
movq %rbx, %rdi
movsd %xmm0, 0x10(%rsp)
movq %r15, %rsi
movl %ebp, %edx
movq %r12, %rcx
callq 0x58c54
testl %eax, %eax
je 0x60657
addq $0xd1c, %rbx            # imm = 0xD1C
leaq 0x84dcd(%rip), %rsi     # 0xe53f0
leaq 0xa38f4(%rip), %rdx     # 0x103f1e
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x849f1(%rip), %rsi     # 0xe5034
leaq 0xa390b(%rip), %rdx     # 0x103f55
movq %r13, %rdi
movl $0xc69, %ecx            # imm = 0xC69
jmp 0x6074a
movslq 0x384(%rbx), %rax
movsd (%r12,%rax,8), %xmm3
movsd 0x84bf4(%rip), %xmm5    # 0xe5260
movapd %xmm5, %xmm0
divsd %xmm3, %xmm0
movslq 0x498(%rbx), %rax
movsd %xmm0, (%r14,%rax,8)
movapd %xmm5, %xmm4
movsd 0x10(%rsp), %xmm2
divsd %xmm2, %xmm4
divsd %xmm3, %xmm4
movslq 0x4a0(%rbx), %rax
movsd %xmm4, (%r14,%rax,8)
movslq 0x38c(%rbx), %rax
movsd (%r12,%rax,8), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x606fb
cmpw $0x1, 0x4bc(%rbx)
jne 0x60792
cmpw $0x1, 0x130(%rbx)
jne 0x60792
movsd 0x188(%rbx), %xmm1
mulsd (%rbx), %xmm1
divsd %xmm3, %xmm1
ucomisd 0x138(%rbx), %xmm1
jae 0x60768
movw $0x1, 0x230(%rbx)
jmp 0x60792
leaq 0xa3865(%rip), %rsi     # 0x103f67
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movb $0x1, %al
callq 0x2e014
leaq 0x8496a(%rip), %rsi     # 0xe5082
leaq 0xa3870(%rip), %rdx     # 0x103f8f
leaq 0x820(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x848f9(%rip), %rsi     # 0xe5034
leaq 0xa3813(%rip), %rdx     # 0x103f55
movq %r13, %rdi
movl $0xc76, %ecx            # imm = 0xC76
movq %r14, %r8
xorl %eax, %eax
callq 0x2e014
movl %ebp, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andw $0x0, 0x230(%rbx)
movsd 0x1c8(%rbx), %xmm1
ucomisd 0x84ae0(%rip), %xmm1    # 0xe5260
jne 0x60792
jp 0x60792
movsd %xmm2, 0x1c8(%rbx)
movsd (%r12,%rax,8), %xmm0
movslq 0x390(%rbx), %rax
movsd 0x85fa7(%rip), %xmm2    # 0xe6748
movsd (%r12,%rax,8), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x87d4d(%rip), %xmm1    # 0xe8500
divsd %xmm0, %xmm1
addsd 0x84aa1(%rip), %xmm1    # 0xe5260
mulsd %xmm2, %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
movsd %xmm3, 0x18(%rsp)
jb 0x607d9
sqrtsd %xmm1, %xmm0
jmp 0x607fa
movapd %xmm1, %xmm0
movsd %xmm4, 0x8(%rsp)
callq 0x82d0
xorpd %xmm2, %xmm2
movsd 0x84a6c(%rip), %xmm5    # 0xe5260
movsd 0x8(%rsp), %xmm4
divsd %xmm0, %xmm4
movslq 0x49c(%rbx), %rax
movsd (%r15,%rax,8), %xmm0
movsd 0x318(%rbx), %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
subsd %xmm1, %xmm5
ucomisd %xmm2, %xmm5
jb 0x6082b
sqrtsd %xmm5, %xmm0
jmp 0x60840
movapd %xmm5, %xmm0
movsd %xmm4, 0x8(%rsp)
callq 0x82d0
movsd 0x8(%rsp), %xmm4
movsd 0x10(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
mulsd %xmm0, %xmm4
movslq 0x49c(%rbx), %rax
movsd %xmm4, (%r14,%rax,8)
movslq 0x390(%rbx), %rax
movsd (%r12,%rax,8), %xmm0
cmpw $0x1, 0x4b4(%rbx)
jne 0x60881
movslq 0x394(%rbx), %rax
addsd (%r12,%rax,8), %xmm0
cmpw $0x1, 0x4b6(%rbx)
jne 0x60898
movslq 0x398(%rbx), %rax
addsd (%r12,%rax,8), %xmm0
movzwl 0x4bc(%rbx), %eax
cmpw $0x1, %ax
jne 0x608e0
cmpw $0x1, 0x23c(%rbx)
jne 0x608e0
movl 0x160(%rbx), %ecx
cmpl $0x4, %ecx
je 0x608d3
cmpl $0x2, %ecx
jne 0x608e0
movsd 0x210(%rbx), %xmm1
ucomisd 0x84991(%rip), %xmm1    # 0xe5260
jne 0x608e0
jp 0x608e0
movslq 0x3d0(%rbx), %rcx
addsd (%r12,%rcx,8), %xmm0
movslq 0x4a8(%rbx), %rcx
movsd (%r15,%rcx,8), %xmm1
divsd %xmm2, %xmm1
divsd %xmm3, %xmm1
movslq 0x4a4(%rbx), %rdx
movsd %xmm1, (%r14,%rdx,8)
movsd 0x8767e(%rip), %xmm1    # 0xe7f88
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
mulsd (%r15,%rdx,8), %xmm1
divsd %xmm3, %xmm1
subsd (%r15,%rcx,8), %xmm1
movsd %xmm1, (%r14,%rcx,8)
cmpw $0x1, 0x4b8(%rbx)
jne 0x6099c
movslq 0x480(%rbx), %rcx
movsd (%r15,%rcx,8), %xmm0
movsd 0x110(%rbx), %xmm1
divsd %xmm3, %xmm1
mulsd %xmm0, %xmm1
mulsd 0x87ae9(%rip), %xmm0    # 0xe8440
subsd %xmm1, %xmm0
movsd %xmm0, (%r14,%rcx,8)
cmpw $0x1, 0x4ba(%rbx)
jne 0x6099c
movslq 0x484(%rbx), %rdx
movsd (%r15,%rdx,8), %xmm0
movsd 0x110(%rbx), %xmm1
divsd %xmm3, %xmm1
mulsd (%r15,%rcx,8), %xmm1
mulsd 0x87a96(%rip), %xmm0    # 0xe8428
addsd %xmm1, %xmm0
movsd %xmm0, (%r14,%rdx,8)
cmpw $0x1, 0x4c2(%rbx)
jne 0x609d6
movslq 0x3a4(%rbx), %rcx
movsd (%r12,%rcx,8), %xmm0
addsd 0x848a5(%rip), %xmm0    # 0xe5260
mulsd 0x87a7d(%rip), %xmm0    # 0xe8440
movslq 0x488(%rbx), %rcx
mulsd (%r15,%rcx,8), %xmm0
movsd %xmm0, (%r14,%rcx,8)
xorl %ebp, %ebp
cmpw $0x1, %ax
jne 0x60754
movzwl 0x230(%rbx), %eax
testl %eax, %eax
je 0x60a72
cmpl $0x1, %eax
jne 0x60754
movslq 0x494(%rbx), %rax
movsd (%r15,%rax,8), %xmm0
divsd %xmm2, %xmm0
divsd %xmm3, %xmm0
movslq 0x490(%rbx), %rcx
movsd %xmm0, (%r14,%rcx,8)
movsd (%r15,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%r15,%rcx,8), %xmm0
movq %rbx, %rdi
callq 0x59bed
mulsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
mulsd 0x85cee(%rip), %xmm1    # 0xe6738
divsd 0x18(%rsp), %xmm0
subsd %xmm0, %xmm1
movslq 0x494(%rbx), %rax
movsd %xmm1, (%r14,%rax,8)
movslq 0x48c(%rbx), %rax
andq $0x0, (%r14,%rax,8)
jmp 0x60754
movslq 0x48c(%rbx), %rax
movsd (%r15,%rax,8), %xmm0
mulsd 0x879b9(%rip), %xmm0    # 0xe8440
movsd 0x218(%rbx), %xmm1
addsd 0x847c9(%rip), %xmm1    # 0xe5260
mulsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rax,8)
movslq 0x490(%rbx), %rcx
andq $0x0, (%r14,%rcx,8)
movslq 0x494(%rbx), %rcx
andq $0x0, (%r14,%rcx,8)
cmpw $0xc, 0xd1a(%rbx)
jl 0x60754
movsd (%r15,%rax,8), %xmm0
movsd (%r14,%rax,8), %xmm1
leaq 0xa34d9(%rip), %rdi     # 0x103fb3
movb $0x2, %al
callq 0x8030
jmp 0x60754