<commit_before>/*===- Cloud.cpp - libSimulation -==============================================
*
*                                  DEMON
*
* This file is distributed under the BSD Open Source License. See LICENSE.TXT 
* for details.
*
*===-----------------------------------------------------------------------===*/

#include "Cloud.h"
#include <cmath>
#include <sstream>

const double Cloud::interParticleSpacing = 0.0003;
const double Cloud::electronCharge = -1.602E-19;
const double Cloud::epsilon0 = 8.8541878E-12;
const double Cloud::dustParticleMassDensity = 2200.0;

Cloud::Cloud(const cloud_index numPar) 
: n(numPar),
x(new double[n]), y(new double[n]), Vx(new double[n]), Vy(new double[n]), 
charge(new double[n]), mass(new double[n]),
k1(new double[n]), k2(new double[n]), k3(new double[n]), k4(new double[n]),
l1(new double[n]), l2(new double[n]), l3(new double[n]), l4(new double[n]),
m1(new double[n]), m2(new double[n]), m3(new double[n]), m4(new double[n]),
n1(new double[n]), n2(new double[n]), n3(new double[n]), n4(new double[n]),
forceX(new double[n]), forceY(new double[n]),
xCache(new doubleV[n/DOUBLE_STRIDE]), yCache(new doubleV[n/DOUBLE_STRIDE]), 
VxCache(new doubleV[n/DOUBLE_STRIDE]), VyCache(new doubleV[n/DOUBLE_STRIDE]) {
#ifdef _OPENMP
	omp_set_num_threads(omp_get_num_procs());
#endif
}

Cloud::~Cloud() {
	delete[] x; delete[] y; delete[] Vx; delete[] Vy;
	delete[] charge; delete[] mass; 
	delete[] k1; delete[] k2; delete[] k3; delete[] k4;
	delete[] l1; delete[] l2; delete[] l3; delete[] l4;
	delete[] m1; delete[] m2; delete[] m3; delete[] m4;
	delete[] n1; delete[] n2; delete[] n3; delete[] n4;
	delete[] forceX; delete[] forceY;
	delete[] xCache; delete[] yCache; 
	delete[] VxCache; delete[] VyCache;
}

// Particle charges are set as a guassian distribution. To set a uniform 
// distribution the charge sigma should be set to zero.
inline void Cloud::initCharge(const double qMean, const double qSigma) {
	std::normal_distribution<double> dist(qMean, qSigma);
	for (cloud_index i = 0; i < n; i++)
		charge[i] = rands.guassian(dist)*electronCharge;
}

// Particle sizes are set as a guassian distribution. To set a uniform 
// distribution the radius sigma should be set to zero.
inline void Cloud::initMass(const double rMean, const double rSigma) {
	const double particleMassConsant = (4.0/3.0)*M_PI*dustParticleMassDensity;
	std::normal_distribution<double> dist(rMean, rSigma);
	for (cloud_index i = 0; i < n; i++) {
		const double r = rands.guassian(dist);
		mass[i] = particleMassConsant*r*r*r;
	}
}

// Generates a cloud on a square grid with zero inital velocity.
Cloud * const Cloud::initializeGrid(const cloud_index numParticles, 
									const double rMean, const double rSigma,
                                    const double qMean, const double qSigma) {
	Cloud * const cloud = new Cloud(numParticles);

	const cloud_index sqrtNumPar = (cloud_index)floor(sqrt(numParticles));
	
	// For even numbers of partciles on a row center the row over the origin.
	const double cloudHalfSize = (double)sqrtNumPar/2.0*interParticleSpacing 
		- ((sqrtNumPar%2) ? 0.0 : interParticleSpacing/2.0);

	cloud->initCharge(qMean, qSigma);
	cloud->initMass(rMean, rSigma);
    BEGIN_PARALLEL_FOR(i, e, numParticles, 1, static)
    	cloud->x[i] = cloudHalfSize - (double)(i%sqrtNumPar)*interParticleSpacing;
	    cloud->y[i] = cloudHalfSize - (double)(i%sqrtNumPar)*interParticleSpacing;
		cloud->Vx[i] = cloud->Vy[i] = 0.0;
    END_PARALLEL_FOR
	return cloud;
}

// Generates a cloud using the last time step of the specified file.
Cloud * const Cloud::initializeFromFile(fitsfile * const file, int &error, 
										double * const currentTime) {
	int anyNull = 0;
	long numParticles = 0;
	long numTimeSteps = 0;

	// move to CLOUD HDU:
	if (!error)
		fits_movnam_hdu(file, BINARY_TBL, const_cast<char *> ("CLOUD"), 0, &error);

	// get number of particles:
	if (!error)
		fits_get_num_rows(file, &numParticles, &error);

	// create cloud:
	Cloud * const cloud = new Cloud((cloud_index)numParticles);

	// read mass information:
	if (!error) {<commit_msg>Fix but introduced by a careless copy and past.<commit_after>/*===- Cloud.cpp - libSimulation -==============================================
*
*                                  DEMON
*
* This file is distributed under the BSD Open Source License. See LICENSE.TXT 
* for details.
*
*===-----------------------------------------------------------------------===*/

#include "Cloud.h"
#include <cmath>
#include <sstream>

const double Cloud::interParticleSpacing = 0.0003;
const double Cloud::electronCharge = -1.602E-19;
const double Cloud::epsilon0 = 8.8541878E-12;
const double Cloud::dustParticleMassDensity = 2200.0;

Cloud::Cloud(const cloud_index numPar) 
: n(numPar),
x(new double[n]), y(new double[n]), Vx(new double[n]), Vy(new double[n]), 
charge(new double[n]), mass(new double[n]),
k1(new double[n]), k2(new double[n]), k3(new double[n]), k4(new double[n]),
l1(new double[n]), l2(new double[n]), l3(new double[n]), l4(new double[n]),
m1(new double[n]), m2(new double[n]), m3(new double[n]), m4(new double[n]),
n1(new double[n]), n2(new double[n]), n3(new double[n]), n4(new double[n]),
forceX(new double[n]), forceY(new double[n]),
xCache(new doubleV[n/DOUBLE_STRIDE]), yCache(new doubleV[n/DOUBLE_STRIDE]), 
VxCache(new doubleV[n/DOUBLE_STRIDE]), VyCache(new doubleV[n/DOUBLE_STRIDE]) {
#ifdef _OPENMP
	omp_set_num_threads(omp_get_num_procs());
#endif
}

Cloud::~Cloud() {
	delete[] x; delete[] y; delete[] Vx; delete[] Vy;
	delete[] charge; delete[] mass; 
	delete[] k1; delete[] k2; delete[] k3; delete[] k4;
	delete[] l1; delete[] l2; delete[] l3; delete[] l4;
	delete[] m1; delete[] m2; delete[] m3; delete[] m4;
	delete[] n1; delete[] n2; delete[] n3; delete[] n4;
	delete[] forceX; delete[] forceY;
	delete[] xCache; delete[] yCache; 
	delete[] VxCache; delete[] VyCache;
}

// Particle charges are set as a guassian distribution. To set a uniform 
// distribution the charge sigma should be set to zero.
inline void Cloud::initCharge(const double qMean, const double qSigma) {
	std::normal_distribution<double> dist(qMean, qSigma);
	for (cloud_index i = 0; i < n; i++)
		charge[i] = rands.guassian(dist)*electronCharge;
}

// Particle sizes are set as a guassian distribution. To set a uniform 
// distribution the radius sigma should be set to zero.
inline void Cloud::initMass(const double rMean, const double rSigma) {
	const double particleMassConsant = (4.0/3.0)*M_PI*dustParticleMassDensity;
	std::normal_distribution<double> dist(rMean, rSigma);
	for (cloud_index i = 0; i < n; i++) {
		const double r = rands.guassian(dist);
		mass[i] = particleMassConsant*r*r*r;
	}
}

// Generates a cloud on a square grid with zero inital velocity.
Cloud * const Cloud::initializeGrid(const cloud_index numParticles, 
									const double rMean, const double rSigma,
                                    const double qMean, const double qSigma) {
	Cloud * const cloud = new Cloud(numParticles);

	const cloud_index sqrtNumPar = (cloud_index)floor(sqrt(numParticles));
	
	// For even numbers of partciles on a row center the row over the origin.
	const double cloudHalfSize = (double)sqrtNumPar/2.0*interParticleSpacing 
		- ((sqrtNumPar%2) ? 0.0 : interParticleSpacing/2.0);

	cloud->initCharge(qMean, qSigma);
	cloud->initMass(rMean, rSigma);
    BEGIN_PARALLEL_FOR(i, e, numParticles, 1, static)
    	cloud->x[i] = cloudHalfSize - (double)(i%sqrtNumPar)*interParticleSpacing;
	    cloud->y[i] = cloudHalfSize - (double)(i/sqrtNumPar)*interParticleSpacing;
		cloud->Vx[i] = cloud->Vy[i] = 0.0;
    END_PARALLEL_FOR
	return cloud;
}

// Generates a cloud using the last time step of the specified file.
Cloud * const Cloud::initializeFromFile(fitsfile * const file, int &error, 
										double * const currentTime) {
	int anyNull = 0;
	long numParticles = 0;
	long numTimeSteps = 0;

	// move to CLOUD HDU:
	if (!error)
		fits_movnam_hdu(file, BINARY_TBL, const_cast<char *> ("CLOUD"), 0, &error);

	// get number of particles:
	if (!error)
		fits_get_num_rows(file, &numParticles, &error);

	// create cloud:
	Cloud * const cloud = new Cloud((cloud_index)numParticles);

	// read mass information:
	if (!error) {
{'fraction_non_alphanumeric': 0.16938229571984437, 'fraction_numerical': 0.01921206225680934, 'mean_word_length': 4.202403542061986, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '993768', 'n_tokens_mistral': 2990, 'n_tokens_neox': 2822, 'n_words': 796}