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

Nan check #134

Merged
merged 9 commits into from
Aug 13, 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
3 changes: 3 additions & 0 deletions include/aether.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
/// This is for generating random numbers:
#include <random>

//This is to look into bits for flagging nans and infs
#include <cstdint>

// Types
// Precision compile-time aliasing
#ifdef AETHER_USE_PRECISION_DOUBLE
Expand Down
4 changes: 4 additions & 0 deletions include/inputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class Inputs {
bool get_do_lat_dependent_radius();
bool get_do_J2();

bool get_check_for_nans();
bool get_nan_test();
std::string get_nan_test_variable();

bool get_is_cubesphere();

bool get_NO_cooling();
Expand Down
3 changes: 2 additions & 1 deletion include/ions.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class Ions {
Grid grid);
void calc_ion_temperature(Neutrals neutrals, Grid grid, Times time);
void calc_electron_temperature(Neutrals neutrals, Grid grid);

bool check_for_nonfinites();
void nan_test(std::string variable);
bool restart_file(std::string dir, bool DoRead);
};
#endif // INCLUDE_IONS_H_
10 changes: 10 additions & 0 deletions include/neutrals.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ class Neutrals {
**/
int get_species_id(std::string name);

/*****************************************************************************
\brief Checks for nans and +/- infinities in density, temp, and velocity
**/
bool check_for_nonfinites();

/**********************************************************************
\brief Checks for nans in the specified variable
**/
void nan_test(std::string variable);

/**********************************************************************
\brief Read / Write restart files for the neutral variables
\param dir directory to write restart files
Expand Down
128 changes: 108 additions & 20 deletions include/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,114 @@
#ifndef INCLUDE_TOOLS_H_
#define INCLUDE_TOOLS_H_

// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------
// Display an armadillo vector
// ----------------------------------------------------------------------

void display_vector(arma_vec vec);

// -----------------------------------------------------------------------------
// synchronize a variable across all processors
// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------
// synchronize a (boolean) variable across all processors
// ----------------------------------------------------------------------

bool sync_across_all_procs(bool value);

// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------
// Calculate the average value across all processors
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------

precision_t sync_mean_across_all_procs(precision_t value);

// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------
// Generate a vector of normally distributed random doubles
// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------

std::vector<double> get_normal_random_vect(double mean,
double std,
int64_t nValues,
int seed);

// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------
// Generate a vector of uniformly distributed random unsigned ints
// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------

std::vector<unsigned int> get_random_unsigned_vect(int64_t nValues,
int seed);

// ----------------------------------------------------------------------
// Make a vector of arma cubes:
// ----------------------------------------------------------------------

std::vector<arma_cube> make_cube_vector(int64_t nLons,
int64_t nLats,
int64_t nAlts,
int64_t nComps);
int64_t nLats,
int64_t nAlts,
int64_t nComps);

// ----------------------------------------------------------------------
// Take the dot product between two armadilo cubes
// ----------------------------------------------------------------------

arma_cube dot_product(std::vector<arma_cube> vec1,
std::vector<arma_cube> vec2);

// ----------------------------------------------------------------------
// Take the cross product between two arma cubes
// ----------------------------------------------------------------------

std::vector<arma_cube> cross_product(std::vector<arma_cube> vec1,
std::vector<arma_cube> vec2);

// ----------------------------------------------------------------------
// Convert an armadillo vector to a c++ vector
// ----------------------------------------------------------------------

std::vector<precision_t> make_vector_from_fvec(arma_vec in_fvec);

// ----------------------------------------------------------------------
// Convert a c++ vector to an armadillo vector
// ----------------------------------------------------------------------

arma_vec make_fvec_from_vector(std::vector<precision_t> in_vector);

// ----------------------------------------------------------------------
// Convert an integer to a 0-padded string
// ----------------------------------------------------------------------

std::string tostr(int64_t num_to_convert, int64_t zero_padding_len);

// ----------------------------------------------------------------------
// Convert a string to a number
// ----------------------------------------------------------------------

precision_t str_to_num(std::string input);

// ----------------------------------------------------------------------
// Read a json from a file
// ----------------------------------------------------------------------

json read_json(std::string json_file);

// ----------------------------------------------------------------------
// Write a json to a file
// ----------------------------------------------------------------------

bool write_json(std::string json_file, json json_output);

// ----------------------------------------------------------------------
// Compare two numbers and fail if the difference is too large
// ----------------------------------------------------------------------

bool compare(precision_t value1, precision_t value2);

// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------
// calculate mean of vector
// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------

precision_t mean(std::vector<precision_t> values);

// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------
// calculate standard deviation of vector
// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------

precision_t standard_deviation(std::vector<precision_t> values);

Expand All @@ -80,7 +122,8 @@ precision_t standard_deviation(std::vector<precision_t> values);
std::vector<precision_t> get_min_mean_max(const arma_cube &value);

//-------------------------------------------------------------
// Find the name of given species in neutrals and ions. Throw exception if not found
// Find the name of given species in neutrals and ions. Throw
// exception if not found
//-------------------------------------------------------------

const arma_cube& find_species_density(const std::string &name,
Expand All @@ -95,4 +138,49 @@ std::vector<precision_t> get_min_mean_max_density(const std::string &name,
Neutrals &neutrals,
Ions &ions);

//-----------------------------------------------------------------------
// Checks if armacube(s) has all finite values, if not, adds them to
// errors in report class
//-----------------------------------------------------------------------

bool all_finite(arma_cube cube, std::string name);
bool all_finite(std::vector<arma_cube> cubes, std::string name);


//-----------------------------------------------------------------------
// Takes an index of an armacube and converts it to latitude,
// longitude, and altitude
//-----------------------------------------------------------------------

std::vector<int> index_to_ijk(arma_cube cube, int index);

//-----------------------------------------------------------------------
// Inserts 3 nans and 3 inf into the specified armacube(s)
//-----------------------------------------------------------------------

std::vector<int> insert_indefinites(arma_cube &cube);

//-----------------------------------------------------------------------
// Returns a vector of ints as a string with spaces in between
//-----------------------------------------------------------------------
std::string print_nan_vector(std::vector<int> input, arma_cube cube);

//-----------------------------------------------------------------------
// Returns whether a given arma cube has all finite values
//-----------------------------------------------------------------------

bool is_finite(arma_cube &cube);

//-----------------------------------------------------------------------
//Returns whether the double/float value is a nan or infinity
//-----------------------------------------------------------------------

bool is_nan_inf(double value);

//-----------------------------------------------------------------------
// Returns vector of indefinite values
//-----------------------------------------------------------------------

std::vector<int> indef_vector(arma_cube cube);

#endif // INCLUDE_TOOLS_H_
9 changes: 8 additions & 1 deletion share/run/UA/inputs/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
"dt" : 60.0,
"TimingPercent" : 1.0,
"iTimingDepth" : 5,
"iProc" : 0},
"iProc" : 0,
"iFunctionVerbose" : {
"Grid::create_altitudes": 0},
"check_for_nans" : false,
"nan_test" : {
"insert" : false,
"variable" : "temperature_scgc"}
},

"InitialConditions" : {
"type" : "Planet"},
Expand Down
9 changes: 5 additions & 4 deletions share/run/aether.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

{

"Ensembles" : {
"nMembers" : 1},
"nMembers" : 1},

"Debug" : {
"Debug" : {
"iVerbose" : 0,
"iFunctionVerbose" : {
"Grid::create_altitudes": 0},
"dt" : 10.0},
"dt" : 10.0,
"check_for_nans" : false
},

"EndTime" : [2011, 3, 20, 0, 10, 0],

Expand Down
1 change: 1 addition & 0 deletions src/advance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ int advance(Planets &planet,
time.restart_file(input.get_restartout_dir(), DoWrite);
}


iErr = output(neutrals, ions, gGrid, time, planet);

logfile.write_logfile(indices, neutrals, ions, gGrid, time);
Expand Down
32 changes: 32 additions & 0 deletions src/inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,38 @@ json Inputs::get_perturb_values() {
return values;
}


// -----------------------------------------------------------------------
// Flag to check neutral and ions for nans and infinites
// -----------------------------------------------------------------------


bool Inputs::get_check_for_nans() {
return settings.at("Debug").at("check_for_nans");;
}




// -----------------------------------------------------------------------
// Checks to see if nan_test is needed
// -----------------------------------------------------------------------


bool Inputs::get_nan_test() {
return settings.at("Debug").at("nan_test").at("insert");
}


// -----------------------------------------------------------------------
// Returns which variable is being tested for nans
// -----------------------------------------------------------------------


std::string Inputs::get_nan_test_variable() {
return settings.at("Debug").at("nan_test").at("variable");
}

// -----------------------------------------------------------------------
// Flag to have a latitude dependent radius, and by extension gravity
// -----------------------------------------------------------------------
Expand Down
43 changes: 43 additions & 0 deletions src/ions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,49 @@ int Ions::read_planet_file(Planets planet) {
}


//----------------------------------------------------------------------
// Reports location of nans inserted into specified variable
//----------------------------------------------------------------------
void Ions::nan_test(std::string variable){
std::vector<int> locations;
std::string message = ("For Ions " + variable + " ");
if (variable == "temperature_scgc") {
locations = insert_indefinites(temperature_scgc);
message += print_nan_vector(locations, temperature_scgc);
}
if (variable == "density_scgc") {
locations = insert_indefinites(density_scgc);
message += print_nan_vector(locations, density_scgc);
}
if (variable == "velocity_vcgc") {
locations = insert_indefinites(velocity_vcgc[0]);
message +=
"at the x loc " + print_nan_vector(locations, velocity_vcgc[0]);
locations = insert_indefinites(velocity_vcgc[1]);
message +=
"at the y loc " + print_nan_vector(locations, velocity_vcgc[1]);
locations = insert_indefinites(velocity_vcgc[2]);
message +=
"at the z loc " + print_nan_vector(locations, velocity_vcgc[2]);
}
std::cout << message;
}

//----------------------------------------------------------------------
// Checks for nans and +/- infinities in density, temp, and velocity
//----------------------------------------------------------------------

bool Ions::check_for_nonfinites() {
bool non_finites_exist = false;
if (!all_finite(density_scgc, "density_scgc") ||
!all_finite(temperature_scgc, "temperature_scgc") ||
!all_finite(velocity_vcgc, "velocity_vcgc"))
non_finites_exist = true;
if (non_finites_exist)
throw std::string("Check for nonfinites failed!!!\n");
return non_finites_exist;
}

// -----------------------------------------------------------------------------
// Calculate the electron density from the sum of all ion species
// -----------------------------------------------------------------------------
Expand Down
Loading
Loading