// File: TBody[P]atomicpp/Prad.cpp
// Function: computeIonisationDistribution(atomicpp::ImpuritySpecies&, double, double, double, double)
std::vector<double> computeIonisationDistribution(atomicpp::ImpuritySpecies& impurity, double Te, double Ne, double Nz, double Nn){

	int Z = impurity.get_atomic_number();
	std::vector<double> iz_stage_distribution(Z+1);

	// std::set GS density equal to 1 (arbitrary)
	iz_stage_distribution[0] = 1;
	double sum_iz = 1;

	// Loop over 0, 1, ..., Z-1
	// Each charge state is std::set in terms of the density of the previous
	for(int k=0; k<Z; ++k){
		// Ionisation
		// Get the atomicpp::RateCoefficient from the rate_coefficient std::map (atrribute of impurity)
		std::shared_ptr<atomicpp::RateCoefficient> iz_rate_coefficient = impurity.get_rate_coefficient("ionisation");
		// Evaluate the atomicpp::RateCoefficient at the point
		double k_iz_evaluated = iz_rate_coefficient->call0D(k, Te, Ne);

		// Recombination
		// Get the atomicpp::RateCoefficient from the rate_coefficient std::map (atrribute of impurity)
		std::shared_ptr<atomicpp::RateCoefficient> rec_rate_coefficient = impurity.get_rate_coefficient("recombination");
		// Evaluate the atomicpp::RateCoefficient at the point
		double k_rec_evaluated = rec_rate_coefficient->call0D(k, Te, Ne);

		// The ratio of ionisation from the (k)th stage and recombination from the (k+1)th std::sets the equilibrium densities
		// of the (k+1)th stage in terms of the (k)th (since R = Nz * Ne * rate_coefficient) N.b. Since there is no
		// ionisation from the bare nucleus, and no recombination onto the neutral (ignoring anion formation) the 'k'
		// value of ionisation coeffs is shifted down  by one relative to the recombination coeffs - therefore this
		// evaluation actually gives the balance

		iz_stage_distribution[k+1] = iz_stage_distribution[k] * (k_iz_evaluated/k_rec_evaluated);
		sum_iz += iz_stage_distribution[k+1];
	}

	// # Normalise such that the sum over all ionisation stages is '1' at all points
	for(int k=0; k<=Z; ++k){
		iz_stage_distribution[k] = iz_stage_distribution[k] / sum_iz;
	}
	return iz_stage_distribution;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0xf8(%rbp)
movq %rdi, %rax
movq %rax, -0x100(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movsd %xmm1, -0x20(%rbp)
movsd %xmm2, -0x28(%rbp)
movsd %xmm3, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x11f10
movl %eax, -0x34(%rbp)
movb $0x0, -0x35(%rbp)
movl -0x34(%rbp), %eax
incl %eax
cltq
movq %rax, -0xf0(%rbp)
leaq -0x36(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x58a0
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
callq 0x58c0
jmp 0x4b02
leaq -0x36(%rbp), %rdi
callq 0x5930
movq -0xf8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5950
movsd 0x314e5(%rip), %xmm0    # 0x36008
movsd %xmm0, (%rax)
movsd 0x314d9(%rip), %xmm0    # 0x36008
movsd %xmm0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x4de3
movq -0x10(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x45f0
movq -0x108(%rbp), %rdx
leaq 0x314bd(%rip), %rsi     # 0x36030
leaq -0x88(%rbp), %rdi
callq 0x44d0
jmp 0x4b81
movq -0x110(%rbp), %rsi
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rdx
callq 0x11fd0
jmp 0x4b9a
leaq -0x88(%rbp), %rdi
callq 0x4220
leaq -0x89(%rbp), %rdi
callq 0x4410
leaq -0x68(%rbp), %rdi
callq 0x5970
movq %rax, %rdi
movl -0x54(%rbp), %esi
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
callq 0x1aa40
movsd %xmm0, -0x118(%rbp)
jmp 0x4bda
movsd -0x118(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x45f0
movq -0x120(%rbp), %rdx
leaq 0x31425(%rip), %rsi     # 0x3603b
leaq -0xc8(%rbp), %rdi
callq 0x44d0
jmp 0x4c24
movq -0x128(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
callq 0x11fd0
jmp 0x4c40
leaq -0xc8(%rbp), %rdi
callq 0x4220
leaq -0xc9(%rbp), %rdi
callq 0x4410
leaq -0xa8(%rbp), %rdi
callq 0x5970
movq %rax, %rdi
movl -0x54(%rbp), %esi
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
callq 0x1aa40
movsd %xmm0, -0x130(%rbp)
jmp 0x4c83
movq -0xf8(%rbp), %rdi
movsd -0x130(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movslq -0x54(%rbp), %rsi
callq 0x5950
movq -0xf8(%rbp), %rdi
movsd (%rax), %xmm0
movsd -0x98(%rbp), %xmm1
divsd -0xd8(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x138(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x5950
movsd -0x138(%rbp), %xmm0
movq -0xf8(%rbp), %rdi
movsd %xmm0, (%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x5950
movsd (%rax), %xmm0
addsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x5990
leaq -0x68(%rbp), %rdi
callq 0x5990
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4b3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x36(%rbp), %rdi
callq 0x5930
jmp 0x4e7e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4d6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x4220
leaq -0x89(%rbp), %rdi
callq 0x4410
jmp 0x4e72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4dd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4daf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x4220
leaq -0xc9(%rbp), %rdi
callq 0x4410
jmp 0x4dd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x5990
leaq -0x68(%rbp), %rdi
callq 0x5990
jmp 0x4e72
movl $0x0, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
cmpl -0x34(%rbp), %eax
jg 0x4e4c
movq -0xf8(%rbp), %rdi
movslq -0xdc(%rbp), %rsi
callq 0x5950
movq -0xf8(%rbp), %rdi
movsd (%rax), %xmm0
divsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x140(%rbp)
movslq -0xdc(%rbp), %rsi
callq 0x5950
movsd -0x140(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x4ded
movb $0x1, -0x35(%rbp)
testb $0x1, -0x35(%rbp)
jne 0x4e62
movq -0xf8(%rbp), %rdi
callq 0x59b0
movq -0x100(%rbp), %rax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0xf8(%rbp), %rdi
callq 0x59b0
movq -0x40(%rbp), %rdi
callq 0x45e0
nopw (%rax,%rax)