// File: /workspace/github_cpp_makefile_star2_O2/richardposner[P]BioNetFit2/src/code/Particle.cpp
// Function: Particle::finalizeSim(unsigned int)
void Particle::finalizeSim(unsigned int mid) {
	unsigned int subParID = calcsubParID(mid);
	if ((mid<0)|| (mid >= swarm_->getNumModels()) || (!getSubParticle(mid)) || (!models.at(mid)) || (dataFiles_.at(mid).empty())){
		outputError("finalizeSim Failed. Invalid SubParticle.  PID:"+ toString(id_) + "  model id:" + toString(mid)+ "  subParID:" + toString(subParID)+". Quitting !!!");
	}
	if (swarm_->options.verbosity >= 3) {cout << "Finalizing simulation. Started" << endl;}

	// Calculate our fit
	calculateFit(false, mid);  //razi: was calculateFit(), check if local should be false


	// Put our simulation params into the message vector
	if ((!this->getSubParticle(mid)) || (fitCalcs.at(mid).empty())){
		cout<<"can  not finalize simulation for subParticle with model id:"<<mid<<endl;
		return;
	}

		// Put our fit calc into the message vector
	swarm_->swarmComm->univMessageSender.push_back(toString(fitCalcs.at(mid).at(currentGeneration_)));
		//cout << "stored fit calc of " << fitCalcs.at(currentGeneration_) << " as " << swarm_->swarmComm->univMessageSender[0] << endl;


	//razi: here the slave fills in the values for free parameters [the list and order may be different for each model, should be taken care of by Master]
	for (map<string,double>::iterator i = simParams_.at(mid).begin(); i != simParams_.at(mid).end(); ++i){
		//cout << "stored param of " << i->second << endl;
		swarm_->swarmComm->univMessageSender.push_back(toString(i->second));
	}

	// Tell the swarm master that we're finished
	if (swarm_->options.verbosity >= 3) {
		cout << "Telling master that my simulation is finished" << endl;
	}

	swarm_->swarmComm->sendToSwarm(subParID, 0, SIMULATION_END, false, swarm_->swarmComm->univMessageSender);
	// Reset the message vector
	swarm_->swarmComm->univMessageSender.clear();
	if (swarm_->options.verbosity >= 3) {cout << "Finalizing simulation. Completed" << endl;}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %esi, %r15d
movq %rdi, %rbx
callq 0x5645e
movq 0xa8(%rbx), %rcx
movq 0x15c8(%rcx), %rdx
subq 0x15c0(%rcx), %rdx
movl %eax, %ebp
shrq $0x3, %rdx
movl %r15d, %r14d
cmpl %r15d, %edx
jbe 0x56501
movq %rbx, %rdi
movl %r15d, %esi
callq 0x59f90
testq %rax, %rax
je 0x56501
leaq 0x78(%rbx), %rdi
movq %r14, %rsi
callq 0x3374c
cmpq $0x0, (%rax)
je 0x56501
leaq 0x90(%rbx), %rdi
movq %r14, %rsi
callq 0x5a176
cmpq $0x0, 0x28(%rax)
jne 0x56636
movl 0x40(%rbx), %esi
leaq 0x108(%rsp), %r12
movq %r12, %rdi
callq 0x9f56b
leaq 0x81158(%rip), %rsi     # 0xd7673
leaq 0x88(%rsp), %rdi
movq %r12, %rdx
callq 0x4933b
leaq 0x806a5(%rip), %rdx     # 0xd6bd7
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3336c
leaq 0x68(%rsp), %rdi
movl %r15d, %esi
callq 0x9f56b
leaq 0xc8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x4e6ed
leaq 0x8010f(%rip), %rdx     # 0xd6684
leaq 0x28(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x3336c
leaq 0x48(%rsp), %rdi
movl %ebp, %esi
callq 0x9f56b
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x4e6ed
leaq 0x80744(%rip), %rdx     # 0xd6cf2
leaq 0xe8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3336c
leaq 0xe8(%rsp), %rdi
callq 0x9f54b
leaq 0xe8(%rsp), %rdi
callq 0x295a0
leaq 0x8(%rsp), %rdi
callq 0x295a0
leaq 0x48(%rsp), %rdi
callq 0x295a0
leaq 0x28(%rsp), %rdi
callq 0x295a0
leaq 0xc8(%rsp), %rdi
callq 0x295a0
leaq 0x68(%rsp), %rdi
callq 0x295a0
leaq 0xa8(%rsp), %rdi
callq 0x295a0
leaq 0x88(%rsp), %rdi
callq 0x295a0
leaq 0x108(%rsp), %rdi
callq 0x295a0
movq 0xa8(%rbx), %rax
cmpl $0x3, 0x1614(%rax)
jb 0x56661
movq 0x10f94b(%rip), %rdi    # 0x165f98
leaq 0x8104e(%rip), %rsi     # 0xd76a2
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movq %rbx, %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x544c2
movq %rbx, %rdi
movl %r15d, %esi
callq 0x59f90
testq %rax, %rax
je 0x5674b
leaq 0x18(%rbx), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x5a0a2
cmpq $0x0, 0x28(%rax)
je 0x5674b
movq 0xa8(%rbx), %rax
movq (%rax), %r12
movq %r15, %rdi
movq %r14, %rsi
callq 0x5a0a2
movl 0xb0(%rbx), %ecx
leaq 0x28(%rsp), %rsi
movl %ecx, (%rsi)
movq %rax, %rdi
callq 0x5a51a
movsd (%rax), %xmm0
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x9f57b
movq %r12, %rdi
movq %r15, %rsi
callq 0x5ec8c
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x295a0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x59f72
movq 0x18(%rax), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x59f72
addq $0x8, %rax
movq 0xa8(%rbx), %rcx
cmpq %rax, %r12
je 0x5676b
movq (%rcx), %r13
movsd 0x40(%r12), %xmm0
movq %r15, %rdi
callq 0x9f57b
movq %r13, %rdi
movq %r15, %rsi
callq 0x5ec8c
movq %r15, %rdi
callq 0x295a0
movq %r12, %rdi
callq 0x28700
movq %rax, %r12
jmp 0x566fe
movq 0x10f846(%rip), %rdi    # 0x165f98
leaq 0x80f68(%rip), %rsi     # 0xd76c1
callq 0x289f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x286e0
jmp 0x567e1
cmpl $0x2, 0x1614(%rcx)
jbe 0x56796
movq 0x10f81d(%rip), %rdi    # 0x165f98
leaq 0x80f7b(%rip), %rsi     # 0xd76fd
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movq 0xa8(%rbx), %rcx
movq (%rcx), %rdi
orl $-0x1, (%rsp)
pushq $0xd
popq %rcx
movl %ebp, %esi
xorl %edx, %edx
xorl %r8d, %r8d
movq %rdi, %r9
callq 0x600c8
movq 0xa8(%rbx), %rax
movq (%rax), %rdi
callq 0x3376e
movq 0xa8(%rbx), %rax
cmpl $0x3, 0x1614(%rax)
jb 0x567e9
movq 0x10f7c3(%rip), %rdi    # 0x165f98
leaq 0x80f4f(%rip), %rsi     # 0xd772b
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x56891
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x295a0
jmp 0x56815
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x295a0
jmp 0x56824
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x295a0
jmp 0x56833
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x295a0
jmp 0x56842
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x295a0
leaq 0x68(%rsp), %rdi
callq 0x295a0
leaq 0xa8(%rsp), %rdi
callq 0x295a0
leaq 0x88(%rsp), %rdi
callq 0x295a0
leaq 0x108(%rsp), %rdi
jmp 0x56899
movq %rax, %rbx
jmp 0x5684f
movq %rax, %rbx
jmp 0x56859
movq %rax, %rbx
jmp 0x56866
movq %rax, %rbx
jmp 0x56873
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x295a0
movq %rbx, %rdi
callq 0x292f0