Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make random number generator more standards conforming and hopefully threadsafe. #585

Merged
merged 14 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ jobs:
- name: Checkout source
uses: actions/checkout@v3
- name: Make clean
run: make clean
run: VLASIATOR_ARCH=carrington_gcc_openmpi make clean
- uses: ursg/gcc-problem-matcher@master
- name: Compile vlasiator (Testpackage build)
run: |
Expand Down Expand Up @@ -147,7 +147,7 @@ jobs:
uses: actions/checkout@v3
- uses: ursg/gcc-problem-matcher@master
- name: Make clean
run: make clean
run: VLASIATOR_ARCH=carrington_gcc_openmpi make clean
- name: Compile tools
run: |
export VLASIATOR_ARCH=carrington_gcc_openmpi
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ OBJS = version.o memoryallocation.o backgroundfield.o quadr.o dipole.o linedipo
project.o projectTriAxisSearch.o read_gaussian_population.o\
Alfven.o Diffusion.o Dispersion.o Distributions.o Firehose.o\
Flowthrough.o Fluctuations.o Harris.o KHB.o Larmor.o Magnetosphere.o MultiPeak.o\
VelocityBox.o Riemann1.o Shock.o Template.o test_fp.o testAmr.o testHall.o test_trans.o\
VelocityBox.o Riemann1.o Shock.o Template.o test_fp.o testHall.o test_trans.o\
IPShock.o object_wrapper.o\
verificationLarmor.o Shocktest.o grid.o ioread.o iowrite.o vlasiator.o logger.o\
common.o parameters.o readparameters.o spatial_cell.o\
Expand Down
4 changes: 2 additions & 2 deletions projects/Alfven/Alfven.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ namespace projects {
RP::add(pop + "_Alfven.rho", "Number density (m^-3)", 1.0e8);
RP::add(pop + "_Alfven.Temperature", "Temperature (K)", 0.86456498092);
RP::add(pop + "_Alfven.A_vel", "Amplitude of the velocity perturbation", 0.1);
RP::add(pop + "_Alfven.nSpaceSamples", "Number of sampling points per spatial dimension", 2);
RP::add(pop + "_Alfven.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add(pop + "_Alfven.nSpaceSamples", "Number of sampling points per spatial dimension", 1);
RP::add(pop + "_Alfven.nVelocitySamples", "Number of sampling points per velocity dimension", 1);

}
}
Expand Down
4 changes: 2 additions & 2 deletions projects/Diffusion/Diffusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ namespace projects {
RP::add(pop + "_Diffusion.Temperature", "Temperature (K)", 2.0e6);
RP::add(pop + "_Diffusion.Scale_x", "Scale length in x (m)", 100000.0);
RP::add(pop + "_Diffusion.Scale_y", "Scale length in y (m)", 100000.0);
RP::add(pop + "_Diffusion.nSpaceSamples", "Number of sampling points per spatial dimension", 2);
RP::add(pop + "_Diffusion.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add(pop + "_Diffusion.nSpaceSamples", "Number of sampling points per spatial dimension", 1);
RP::add(pop + "_Diffusion.nVelocitySamples", "Number of sampling points per velocity dimension", 1);
}
}

Expand Down
28 changes: 13 additions & 15 deletions projects/Dispersion/Dispersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ namespace projects {
RP::add(pop + "_Dispersion.Temperature", "Temperature (K)", 2.0e6);
RP::add(pop + "_Dispersion.densityPertRelAmp", "Relative amplitude of the density perturbation", 0.1);
RP::add(pop + "_Dispersion.velocityPertAbsAmp", "Absolute amplitude of the velocity perturbation", 1.0e6);
RP::add(pop + "_Dispersion.nSpaceSamples", "Number of sampling points per spatial dimension", 2);
RP::add(pop + "_Dispersion.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add(pop + "_Dispersion.nSpaceSamples", "Number of sampling points per spatial dimension", 1);
RP::add(pop + "_Dispersion.nVelocitySamples", "Number of sampling points per velocity dimension", 1);
}
}

Expand Down Expand Up @@ -216,18 +216,15 @@ namespace projects {
creal dy = cellParams[CellParams::DY];
creal z = cellParams[CellParams::ZCRD];
creal dz = cellParams[CellParams::DZ];

CellID cellID = (int) ((x - Parameters::xmin) / dx) +
(int) ((y - Parameters::ymin) / dy) * Parameters::xcells_ini +
(int) ((z - Parameters::zmin) / dz) * Parameters::xcells_ini * Parameters::ycells_ini;

setRandomSeed(cellID);
std::default_random_engine rndState;
ykempf marked this conversation as resolved.
Show resolved Hide resolved
setRandomCellSeed(cell,rndState);

this->rndRho=getRandomNumber();
this->rndRho=getRandomNumber(rndState);

this->rndVel[0]=getRandomNumber();
this->rndVel[1]=getRandomNumber();
this->rndVel[2]=getRandomNumber();
this->rndVel[0]=getRandomNumber(rndState);
this->rndVel[1]=getRandomNumber(rndState);
this->rndVel[2]=getRandomNumber(rndState);
}

void Dispersion::setProjectBField(
Expand All @@ -252,12 +249,13 @@ namespace projects {
std::array<Real, fsgrids::bfield::N_BFIELD>* cell = perBGrid.get(x, y, z);
const int64_t cellid = perBGrid.GlobalIDForCoords(x, y, z);

setRandomSeed(cellid);
std::default_random_engine rndState;
setRandomSeed(cellid,rndState);

Real rndBuffer[3];
rndBuffer[0]=getRandomNumber();
rndBuffer[1]=getRandomNumber();
rndBuffer[2]=getRandomNumber();
rndBuffer[0]=getRandomNumber(rndState);
rndBuffer[1]=getRandomNumber(rndState);
rndBuffer[2]=getRandomNumber(rndState);

cell->at(fsgrids::bfield::PERBX) = this->magXPertAbsAmp * (0.5 - rndBuffer[0]);
cell->at(fsgrids::bfield::PERBY) = this->magYPertAbsAmp * (0.5 - rndBuffer[1]);
Expand Down
1 change: 0 additions & 1 deletion projects/Dispersion/Dispersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ namespace projects {
uint seed;

char rngStateBuffer[256];
random_data rngDataBuffer;

static Real rndRho, rndVel[3];
#pragma omp threadprivate(rndRho,rndVel)
Expand Down
14 changes: 8 additions & 6 deletions projects/Distributions/Distributions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ namespace projects {
}

void Distributions::calcCellParameters(spatial_cell::SpatialCell* cell,creal& t) {
setRandomCellSeed(cell);
std::default_random_engine rndState;
setRandomCellSeed(cell,rndState);
for (uint i=0; i<2; i++) {
this->rhoRnd[i] = this->rho[i] + this->rhoPertAbsAmp[i] * (0.5 - getRandomNumber());
this->rhoRnd[i] = this->rho[i] + this->rhoPertAbsAmp[i] * (0.5 - getRandomNumber(rndState));
}
}

Expand All @@ -179,17 +180,18 @@ namespace projects {
const int64_t cellid = perBGrid.GlobalIDForCoords(x, y, z);
const std::array<Real, 3> xyz = perBGrid.getPhysicalCoords(x, y, z);

setRandomSeed(cellid);
std::default_random_engine rndState;
setRandomSeed(cellid,rndState);

if (this->lambda != 0.0) {
cell->at(fsgrids::bfield::PERBX) = this->dBx*cos(2.0 * M_PI * xyz[0] / this->lambda);
cell->at(fsgrids::bfield::PERBY) = this->dBy*sin(2.0 * M_PI * xyz[0] / this->lambda);
cell->at(fsgrids::bfield::PERBZ) = this->dBz*cos(2.0 * M_PI * xyz[0] / this->lambda);
}

cell->at(fsgrids::bfield::PERBX) += this->magXPertAbsAmp * (0.5 - getRandomNumber());
cell->at(fsgrids::bfield::PERBY) += this->magYPertAbsAmp * (0.5 - getRandomNumber());
cell->at(fsgrids::bfield::PERBZ) += this->magZPertAbsAmp * (0.5 - getRandomNumber());
cell->at(fsgrids::bfield::PERBX) += this->magXPertAbsAmp * (0.5 - getRandomNumber(rndState));
cell->at(fsgrids::bfield::PERBY) += this->magYPertAbsAmp * (0.5 - getRandomNumber(rndState));
cell->at(fsgrids::bfield::PERBZ) += this->magZPertAbsAmp * (0.5 - getRandomNumber(rndState));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions projects/Firehose/Firehose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ namespace projects {
RP::add(pop + "_Firehose.Vy2", "Bulk velocity y component, second peak (m/s)", 0.0);
RP::add(pop + "_Firehose.Vz1", "Bulk velocity z component, first peak (m/s)", 0.0);
RP::add(pop + "_Firehose.Vz2", "Bulk velocity z component, second peak (m/s)", 0.0);
RP::add(pop + "_Firehose.nSpaceSamples", "Number of sampling points per spatial dimension", 2);
RP::add(pop + "_Firehose.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add(pop + "_Firehose.nSpaceSamples", "Number of sampling points per spatial dimension", 1);
RP::add(pop + "_Firehose.nVelocitySamples", "Number of sampling points per velocity dimension", 1);
}
}

Expand Down
2 changes: 1 addition & 1 deletion projects/Firehose/Firehose_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool addProjectParameters(){
RP::add("Firehose.Bx", "Magnetic field x component (T)", 0.0);
RP::add("Firehose.By", "Magnetic field y component (T)", 0.0);
RP::add("Firehose.Bz", "Magnetic field z component (T)", 0.0);
RP::add("Firehose.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add("Firehose.nVelocitySamples", "Number of sampling points per velocity dimension", 1);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions projects/Flowthrough/Flowthrough.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ namespace projects {
RP::add(pop + "_Flowthrough.VX0", "Initial bulk velocity in x-direction", 0.0);
RP::add(pop + "_Flowthrough.VY0", "Initial bulk velocity in y-direction", 0.0);
RP::add(pop + "_Flowthrough.VZ0", "Initial bulk velocity in z-direction", 0.0);
RP::add(pop + "_Flowthrough.nSpaceSamples", "Number of sampling points per spatial dimension", 2);
RP::add(pop + "_Flowthrough.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add(pop + "_Flowthrough.nSpaceSamples", "Number of sampling points per spatial dimension", 1);
RP::add(pop + "_Flowthrough.nVelocitySamples", "Number of sampling points per velocity dimension", 1);
}
}

Expand Down
24 changes: 13 additions & 11 deletions projects/Fluctuations/Fluctuations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ namespace projects {
RP::add(pop + "_Fluctuations.Temperature", "Temperature (K)", 2.0e6);
RP::add(pop + "_Fluctuations.densityPertRelAmp", "Amplitude factor of the density perturbation", 0.1);
RP::add(pop + "_Fluctuations.velocityPertAbsAmp", "Amplitude of the velocity perturbation", 1.0e6);
RP::add(pop + "_Fluctuations.nSpaceSamples", "Number of sampling points per spatial dimension", 2);
RP::add(pop + "_Fluctuations.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add(pop + "_Fluctuations.nSpaceSamples", "Number of sampling points per spatial dimension", 1);
RP::add(pop + "_Fluctuations.nVelocitySamples", "Number of sampling points per velocity dimension", 1);
RP::add(pop + "_Fluctuations.maxwCutoff", "Cutoff for the maxwellian distribution", 1e-12);
}
}
Expand Down Expand Up @@ -162,12 +162,13 @@ namespace projects {
(int) ((y - Parameters::ymin) / dy) * Parameters::xcells_ini +
(int) ((z - Parameters::zmin) / dz) * Parameters::xcells_ini * Parameters::ycells_ini;

setRandomSeed(cellID);
std::default_random_engine rndState;
setRandomCellSeed(cell,rndState);

this->rndRho=getRandomNumber();
this->rndVel[0]=getRandomNumber();
this->rndVel[1]=getRandomNumber();
this->rndVel[2]=getRandomNumber();
this->rndRho=getRandomNumber(rndState);
this->rndVel[0]=getRandomNumber(rndState);
this->rndVel[1]=getRandomNumber(rndState);
this->rndVel[2]=getRandomNumber(rndState);
}

void Fluctuations::setProjectBField(
Expand All @@ -192,11 +193,12 @@ namespace projects {
std::array<Real, fsgrids::bfield::N_BFIELD>* cell = perBGrid.get(x, y, z);
const int64_t cellid = perBGrid.GlobalIDForCoords(x, y, z);

setRandomSeed(cellid);
std::default_random_engine rndState;
setRandomSeed(cellid,rndState);

cell->at(fsgrids::bfield::PERBX) = this->magXPertAbsAmp * (0.5 - getRandomNumber());
cell->at(fsgrids::bfield::PERBY) = this->magYPertAbsAmp * (0.5 - getRandomNumber());
cell->at(fsgrids::bfield::PERBZ) = this->magZPertAbsAmp * (0.5 - getRandomNumber());
cell->at(fsgrids::bfield::PERBX) = this->magXPertAbsAmp * (0.5 - getRandomNumber(rndState));
cell->at(fsgrids::bfield::PERBY) = this->magYPertAbsAmp * (0.5 - getRandomNumber(rndState));
cell->at(fsgrids::bfield::PERBZ) = this->magZPertAbsAmp * (0.5 - getRandomNumber(rndState));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions projects/Harris/Harris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ namespace projects {

RP::add(pop + "_Harris.Temperature", "Temperature (K)", 2.0e6);
RP::add(pop + "_Harris.rho", "Number density at infinity (m^-3)", 1.0e7);
RP::add(pop + "_Harris.nSpaceSamples", "Number of sampling points per spatial dimension.", 2);
RP::add(pop + "_Harris.nVelocitySamples", "Number of sampling points per velocity dimension.", 2);
RP::add(pop + "_Harris.nSpaceSamples", "Number of sampling points per spatial dimension.", 1);
RP::add(pop + "_Harris.nVelocitySamples", "Number of sampling points per velocity dimension.", 1);
}
}

Expand Down
4 changes: 2 additions & 2 deletions projects/IPShock/IPShock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ namespace projects {
RP::add(pop + "_IPShock.rhod", "Downstream Number density (m^-3)", 1.0e7);
RP::add(pop + "_IPShock.Temperatured", "Downstream Temperature (K)", 2.0e6);

RP::add(pop + "_IPShock.nSpaceSamples", "Number of sampling points per spatial dimension", 2);
RP::add(pop + "_IPShock.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add(pop + "_IPShock.nSpaceSamples", "Number of sampling points per spatial dimension", 1);
RP::add(pop + "_IPShock.nVelocitySamples", "Number of sampling points per velocity dimension", 1);
RP::add(pop + "_IPShock.maxwCutoff", "Cutoff for the maxwellian distribution", 1e-12);
}

Expand Down
4 changes: 2 additions & 2 deletions projects/KHB/KHB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ namespace projects {
RP::add("KHB.amp", "Initial perturbation amplitude (m)", 0.0);
RP::add("KHB.offset", "Boundaries offset from 0 (m)", 0.0);
RP::add("KHB.transitionWidth", "Width of tanh transition for all changing values", 0.0);
RP::add("KHB.nSpaceSamples", "Number of sampling points per spatial dimension", 2);
RP::add("KHB.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add("KHB.nSpaceSamples", "Number of sampling points per spatial dimension", 1);
RP::add("KHB.nVelocitySamples", "Number of sampling points per velocity dimension", 1);
}

void KHB::getParameters() {
Expand Down
4 changes: 2 additions & 2 deletions projects/Magnetosphere/Magnetosphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ namespace projects {
RP::add(pop + "_Magnetosphere.VX0", "Initial bulk velocity in x-direction", 0.0);
RP::add(pop + "_Magnetosphere.VY0", "Initial bulk velocity in y-direction", 0.0);
RP::add(pop + "_Magnetosphere.VZ0", "Initial bulk velocity in z-direction", 0.0);
RP::add(pop + "_Magnetosphere.nSpaceSamples", "Number of sampling points per spatial dimension", 2);
RP::add(pop + "_Magnetosphere.nVelocitySamples", "Number of sampling points per velocity dimension", 5);
RP::add(pop + "_Magnetosphere.nSpaceSamples", "Number of sampling points per spatial dimension", 1);
RP::add(pop + "_Magnetosphere.nVelocitySamples", "Number of sampling points per velocity dimension", 1);
RP::add(pop + "_Magnetosphere.taperInnerRadius", "Inner radius of the zone with a density tapering from the ionospheric value to the background (m)", 0.0);
RP::add(pop + "_Magnetosphere.taperOuterRadius", "Outer radius of the zone with a density tapering from the ionospheric value to the background (m)", 0.0);
}
Expand Down
Loading