fixed compiling errors with visual studio, mostly VLAs

This commit is contained in:
jingang 2019-06-05 11:26:39 -04:00
parent 1dd3ffbfcc
commit 970aa6ea9a
16 changed files with 81 additions and 61 deletions

View file

@ -44,7 +44,7 @@ void fatal_error(const std::stringstream& message)
[[noreturn]] inline
void fatal_error(const char* message)
{
fatal_error({message, std::strlen(message)});
fatal_error(std::string{message, std::strlen(message)});
}
void warning(const std::string& message);

View file

@ -187,11 +187,12 @@ read_attribute(hid_t obj_id, const char* name, std::string& str)
{
// Create buffer to read data into
auto n = attribute_typesize(obj_id, name);
char buffer[n];
char* buffer = new char[n];
// Read attribute and set string
read_attr_string(obj_id, name, n, buffer);
str = std::string{buffer, n};
delete[] buffer;
}
// overload for std::vector<std::string>
@ -203,7 +204,10 @@ read_attribute(hid_t obj_id, const char* name, std::vector<std::string>& vec)
// Allocate a C char array to get strings
auto n = attribute_typesize(obj_id, name);
char buffer[m][n];
char** buffer = new char*[m];
for (int i = 0; i < m; i++) {
buffer[i] = new char[n];
}
// Read char data in attribute
read_attr_string(obj_id, name, n, buffer[0]);
@ -216,7 +220,9 @@ read_attribute(hid_t obj_id, const char* name, std::vector<std::string>& vec)
// Create string based on (char*, size_t) constructor
vec.emplace_back(&buffer[i][0], k);
delete[] buffer[i];
}
delete[] buffer;
}
//==============================================================================
@ -240,7 +246,7 @@ read_dataset(hid_t obj_id, const char* name, std::string& str, bool indep=false)
{
// Create buffer to read data into
auto n = dataset_typesize(obj_id, name);
char buffer[n];
char* buffer = new char[n];
// Read attribute and set string
read_string(obj_id, name, n, buffer, indep);
@ -458,7 +464,10 @@ write_dataset(hid_t obj_id, const char* name, const std::vector<std::string>& bu
}
// Copy data into contiguous buffer
char temp[n][m];
char** temp = new char*[n];
for (int i = 0; i < n; i++) {
temp[i] = new char[m];
}
std::fill(temp[0], temp[0] + n*m, '\0');
for (int i = 0; i < n; ++i) {
std::copy(buffer[i].begin(), buffer[i].end(), temp[i]);
@ -466,6 +475,12 @@ write_dataset(hid_t obj_id, const char* name, const std::vector<std::string>& bu
// Write 2D data
write_string(obj_id, 1, dims, m, name, temp[0], false);
// Free temp array
for (int i = 0; i < n; i++) {
delete[] temp[i];
}
delete[] temp;
}
template<typename T> inline void

View file

@ -560,7 +560,7 @@ CSGCell::contains_complex(Position r, Direction u, int32_t on_surface) const
{
// Make a stack of booleans. We don't know how big it needs to be, but we do
// know that rpn.size() is an upper-bound.
bool stack[rpn_.size()];
std::vector<bool> stack(rpn_.size());
int i_stack = -1;
for (int32_t token : rpn_) {

View file

@ -117,7 +117,7 @@ int openmc_finalize()
data::energy_max = {INFTY, INFTY};
data::energy_min = {0.0, 0.0};
model::root_universe = -1;
openmc_set_seed(DEFAULT_SEED);
openmc::openmc_set_seed(DEFAULT_SEED);
// Deallocate arrays
free_memory();
@ -159,6 +159,6 @@ int openmc_hard_reset()
simulation::total_gen = 0;
// Reset the random number generator state
openmc_set_seed(DEFAULT_SEED);
openmc::openmc_set_seed(DEFAULT_SEED);
return 0;
}

View file

@ -366,10 +366,11 @@ member_names(hid_t group_id, H5O_type_t type)
i, nullptr, 0, H5P_DEFAULT);
// Read name
char buffer[size];
char* buffer = new char[size];
H5Lget_name_by_idx(group_id, ".", H5_INDEX_NAME, H5_ITER_INC, i,
buffer, size, H5P_DEFAULT);
names.emplace_back(&buffer[0]);
delete[] buffer;
}
return names;
}
@ -404,11 +405,13 @@ object_name(hid_t obj_id)
{
// Determine size and create buffer
size_t size = 1 + H5Iget_name(obj_id, nullptr, 0);
char buffer[size];
char* buffer = new char[size];
// Read and return name
H5Iget_name(obj_id, buffer, size);
return buffer;
std::string str = buffer;
delete[] buffer;
return str;
}

View file

@ -68,7 +68,7 @@ int openmc_init(int argc, char* argv[], const void* intracomm)
// Initialize random number generator -- if the user specifies a seed, it
// will be re-initialized later
openmc_set_seed(DEFAULT_SEED);
openmc::openmc_set_seed(DEFAULT_SEED);
// Read XML input files
read_input_xml();

View file

@ -388,7 +388,7 @@ RectLattice::to_hdf5_inner(hid_t lat_group) const
hsize_t nx {static_cast<hsize_t>(n_cells_[0])};
hsize_t ny {static_cast<hsize_t>(n_cells_[1])};
hsize_t nz {static_cast<hsize_t>(n_cells_[2])};
int out[nx*ny*nz];
std::vector<int> out(nx*ny*nz);
for (int m = 0; m < nz; m++) {
for (int k = 0; k < ny; k++) {
@ -401,12 +401,12 @@ RectLattice::to_hdf5_inner(hid_t lat_group) const
}
hsize_t dims[3] {nz, ny, nx};
write_int(lat_group, 3, dims, "universes", out, false);
write_int(lat_group, 3, dims, "universes", &out[0], false);
} else {
hsize_t nx {static_cast<hsize_t>(n_cells_[0])};
hsize_t ny {static_cast<hsize_t>(n_cells_[1])};
int out[nx*ny];
std::vector<int> out(nx*ny);
for (int k = 0; k < ny; k++) {
for (int j = 0; j < nx; j++) {
@ -417,7 +417,7 @@ RectLattice::to_hdf5_inner(hid_t lat_group) const
}
hsize_t dims[3] {1, ny, nx};
write_int(lat_group, 3, dims, "universes", out, false);
write_int(lat_group, 3, dims, "universes", &out[0], false);
}
}
@ -877,7 +877,7 @@ HexLattice::to_hdf5_inner(hid_t lat_group) const
hsize_t nx {static_cast<hsize_t>(2*n_rings_ - 1)};
hsize_t ny {static_cast<hsize_t>(2*n_rings_ - 1)};
hsize_t nz {static_cast<hsize_t>(n_axial_)};
int out[nx*ny*nz];
std::vector<int> out(nx*ny*nz);
for (int m = 0; m < nz; m++) {
for (int k = 0; k < ny; k++) {
@ -897,7 +897,7 @@ HexLattice::to_hdf5_inner(hid_t lat_group) const
}
hsize_t dims[3] {nz, ny, nx};
write_int(lat_group, 3, dims, "universes", out, false);
write_int(lat_group, 3, dims, "universes", &out[0], false);
}
//==============================================================================

View file

@ -110,12 +110,13 @@ void calc_pn_c(int n, double x, double pnx[])
double evaluate_legendre(int n, const double data[], double x)
{
double pnx[n + 1];
double* pnx = new double[n + 1];
double val = 0.0;
calc_pn_c(n, x, pnx);
for (int l = 0; l <= n; l++) {
val += (l + 0.5) * data[l] * pnx[l];
}
delete[] pnx;
return val;
}
@ -536,8 +537,8 @@ void calc_zn(int n, double rho, double phi, double zn[]) {
double sin_phi = std::sin(phi);
double cos_phi = std::cos(phi);
double sin_phi_vec[n + 1]; // Sin[n * phi]
double cos_phi_vec[n + 1]; // Cos[n * phi]
std::vector<double> sin_phi_vec(n + 1); // Sin[n * phi]
std::vector<double> cos_phi_vec(n + 1); // Cos[n * phi]
sin_phi_vec[0] = 1.0;
cos_phi_vec[0] = 1.0;
sin_phi_vec[1] = 2.0 * cos_phi;
@ -554,8 +555,8 @@ void calc_zn(int n, double rho, double phi, double zn[]) {
// ===========================================================================
// Calculate R_pq(rho)
double zn_mat[n + 1][n + 1]; // Matrix forms of the coefficients which are
// easier to work with
// Matrix forms of the coefficients which are easier to work with
std::vector<std::vector<double>> zn_mat(n + 1, std::vector<double>(n + 1));
// Fill the main diagonal first (Eq 3.9 in Chong)
for (int p = 0; p <= n; p++) {
@ -763,7 +764,7 @@ void broaden_wmp_polynomials(double E, double dopp, int n, double factors[])
void spline(int n, const double x[], const double y[], double z[])
{
double c_new[n-1];
std::vector<double> c_new(n-1);
// Set natural boundary conditions
c_new[0] = 0.0;

View file

@ -181,13 +181,13 @@ int RegularMesh::get_bin(Position r) const
}
// Determine indices
int ijk[n_dimension_];
std::vector<int> ijk(n_dimension_);
bool in_mesh;
get_indices(r, ijk, &in_mesh);
get_indices(r, &ijk[0], &in_mesh);
if (!in_mesh) return -1;
// Convert indices to bin
return get_bin_from_indices(ijk);
return get_bin_from_indices(&ijk[0]);
}
int RegularMesh::get_bin_from_indices(const int* ijk) const
@ -495,11 +495,11 @@ void RegularMesh::bins_crossed(const Particle* p, std::vector<int>& bins,
// Determine the mesh indices for the starting and ending coords.
int n = n_dimension_;
int ijk0[n], ijk1[n];
std::vector<int> ijk0(n), ijk1(n);
bool start_in_mesh;
get_indices(r0, ijk0, &start_in_mesh);
get_indices(r0, &ijk0[0], &start_in_mesh);
bool end_in_mesh;
get_indices(r1, ijk1, &end_in_mesh);
get_indices(r1, &ijk1[0], &end_in_mesh);
// Reset coordinates and check for a mesh intersection if necessary.
if (start_in_mesh) {
@ -509,7 +509,7 @@ void RegularMesh::bins_crossed(const Particle* p, std::vector<int>& bins,
// The initial coords do not lie in the mesh. Check to see if the particle
// eventually intersects the mesh and compute the relevant coords and
// indices.
if (!intersects(r0, r1, ijk0)) return;
if (!intersects(r0, r1, &ijk0[0])) return;
}
r1 = r;
@ -517,17 +517,17 @@ void RegularMesh::bins_crossed(const Particle* p, std::vector<int>& bins,
// Find which mesh cells are traversed and the length of each traversal.
while (true) {
if (std::equal(ijk0, ijk0+n, ijk1)) {
if (ijk0 == ijk1) {
// The track ends in this cell. Use the particle end location rather
// than the mesh surface and stop iterating.
double distance = (r1 - r0).norm();
bins.push_back(get_bin_from_indices(ijk0));
bins.push_back(get_bin_from_indices(&ijk0[0]));
lengths.push_back(distance / total_distance);
break;
}
// The track exits this cell. Determine the distance to each mesh surface.
double d[n];
std::vector<double> d(n);
for (int k = 0; k < n; ++k) {
if (std::fabs(u[k]) < FP_PRECISION) {
d[k] = INFTY;
@ -541,9 +541,9 @@ void RegularMesh::bins_crossed(const Particle* p, std::vector<int>& bins,
}
// Pick the closest mesh surface and append this traversal to the output.
auto j = std::min_element(d, d+n) - d;
auto j = std::min_element(d.begin(), d.end()) - d.begin();
double distance = d[j];
bins.push_back(get_bin_from_indices(ijk0));
bins.push_back(get_bin_from_indices(&ijk0[0]));
lengths.push_back(distance / total_distance);
// Translate to the oncoming mesh surface.
@ -582,18 +582,17 @@ void RegularMesh::surface_bins_crossed(const Particle* p,
// Determine indices for starting and ending location.
int n = n_dimension_;
int ijk0[n], ijk1[n];
std::vector<int> ijk0(n), ijk1(n);
bool start_in_mesh;
get_indices(r0, ijk0, &start_in_mesh);
get_indices(r0, &ijk0[0], &start_in_mesh);
bool end_in_mesh;
get_indices(r1, ijk1, &end_in_mesh);
get_indices(r1, &ijk1[0], &end_in_mesh);
// Check if the track intersects any part of the mesh.
if (!start_in_mesh) {
Position r0_copy = r0;
int ijk0_copy[n];
for (int i = 0; i < n; ++i) ijk0_copy[i] = ijk0[i];
if (!intersects(r0_copy, r1, ijk0_copy)) return;
std::vector<int> ijk0_copy(ijk0);
if (!intersects(r0_copy, r1, &ijk0_copy[0])) return;
}
// ========================================================================
@ -651,7 +650,7 @@ void RegularMesh::surface_bins_crossed(const Particle* p,
// Outward current on i max surface
if (in_mesh) {
int i_surf = 4*i + 3;
int i_mesh = get_bin_from_indices(ijk0);
int i_mesh = get_bin_from_indices(&ijk0[0]);
int i_bin = 4*n*i_mesh + i_surf - 1;
bins.push_back(i_bin);
@ -672,7 +671,7 @@ void RegularMesh::surface_bins_crossed(const Particle* p,
// i min surface
if (in_mesh) {
int i_surf = 4*i + 2;
int i_mesh = get_bin_from_indices(ijk0);
int i_mesh = get_bin_from_indices(&ijk0[0]);
int i_bin = 4*n*i_mesh + i_surf - 1;
bins.push_back(i_bin);
@ -684,7 +683,7 @@ void RegularMesh::surface_bins_crossed(const Particle* p,
// Outward current on i min surface
if (in_mesh) {
int i_surf = 4*i + 1;
int i_mesh = get_bin_from_indices(ijk0);
int i_mesh = get_bin_from_indices(&ijk0[0]);
int i_bin = 4*n*i_mesh + i_surf - 1;
bins.push_back(i_bin);
@ -705,7 +704,7 @@ void RegularMesh::surface_bins_crossed(const Particle* p,
// i max surface
if (in_mesh) {
int i_surf = 4*i + 4;
int i_mesh = get_bin_from_indices(ijk0);
int i_mesh = get_bin_from_indices(&ijk0[0]);
int i_bin = 4*n*i_mesh + i_surf - 1;
bins.push_back(i_bin);

View file

@ -96,7 +96,7 @@ Mgxs::metadata_from_hdf5(hid_t xs_id, const std::vector<double>& temperature,
// Determine the available temperatures
hid_t kT_group = open_group(xs_id, "kTs");
int num_temps = get_num_datasets(kT_group);
char* dset_names[num_temps];
char** dset_names = new char*[num_temps];
for (int i = 0; i < num_temps; i++) {
dset_names[i] = new char[151];
}
@ -111,6 +111,7 @@ Mgxs::metadata_from_hdf5(hid_t xs_id, const std::vector<double>& temperature,
// Done with dset_names, so delete it
delete[] dset_names[i];
}
delete[] dset_names;
std::sort(available_temps.begin(), available_temps.end());
// If only one temperature is available, lets just use nearest temperature

View file

@ -4,6 +4,7 @@
#include <cmath>
#include <sstream>
#include <utility>
#include <ciso646>
#include "openmc/error.h"
#include "openmc/hdf5_interface.h"

View file

@ -18,8 +18,8 @@ void
LegendreFilter::get_all_bins(const Particle* p, int estimator,
FilterMatch& match) const
{
double wgt[n_bins_];
calc_pn_c(order_, p->mu_, wgt);
std::vector<double> wgt(n_bins_);
calc_pn_c(order_, p->mu_, &wgt[0]);
for (int i = 0; i < n_bins_; i++) {
match.bins_.push_back(i);
match.weights_.push_back(wgt[i]);

View file

@ -58,8 +58,8 @@ MeshFilter::text_label(int bin) const
auto& mesh = *model::meshes[mesh_];
int n_dim = mesh.n_dimension_;
int ijk[n_dim];
mesh.get_indices_from_bin(bin, ijk);
std::vector<int> ijk(n_dim);
mesh.get_indices_from_bin(bin, &ijk[0]);
std::stringstream out;
out << "Mesh Index (" << ijk[0];

View file

@ -35,16 +35,16 @@ SphericalHarmonicsFilter::get_all_bins(const Particle* p, int estimator,
FilterMatch& match) const
{
// Determine cosine term for scatter expansion if necessary
double wgt[order_ + 1];
std::vector<double> wgt(order_ + 1);
if (cosine_ == SphericalHarmonicsCosine::scatter) {
calc_pn_c(order_, p->mu_, wgt);
calc_pn_c(order_, p->mu_, &wgt[0]);
} else {
for (int i = 0; i < order_ + 1; i++) wgt[i] = 1;
}
// Find the Rn,m values
double rn[n_bins_];
calc_rn(order_, p->u_last_, rn);
std::vector<double> rn(n_bins_);
calc_rn(order_, p->u_last_, &rn[0]);
int j = 0;
for (int n = 0; n < order_ + 1; n++) {

View file

@ -50,8 +50,8 @@ SpatialLegendreFilter::get_all_bins(const Particle* p, int estimator,
double x_norm = 2.0*(x - min_) / (max_ - min_) - 1.0;
// Compute and return the Legendre weights.
double wgt[order_ + 1];
calc_pn_c(order_, x_norm, wgt);
std::vector<double> wgt(order_ + 1);
calc_pn_c(order_, x_norm, &wgt[0]);
for (int i = 0; i < order_ + 1; i++) {
match.bins_.push_back(i);
match.weights_.push_back(wgt[i]);

View file

@ -36,8 +36,8 @@ ZernikeFilter::get_all_bins(const Particle* p, int estimator,
if (r <= 1.0) {
// Compute and return the Zernike weights.
double zn[n_bins_];
calc_zn(order_, r, theta, zn);
std::vector<double> zn(n_bins_);
calc_zn(order_, r, theta, &zn[0]);
for (int i = 0; i < n_bins_; i++) {
match.bins_.push_back(i);
match.weights_.push_back(zn[i]);
@ -93,8 +93,8 @@ ZernikeRadialFilter::get_all_bins(const Particle* p, int estimator,
if (r <= 1.0) {
// Compute and return the Zernike weights.
double zn[n_bins_];
calc_zn_rad(order_, r, zn);
std::vector<double> zn(n_bins_);
calc_zn_rad(order_, r, &zn[0]);
for (int i = 0; i < n_bins_; i++) {
match.bins_.push_back(i);
match.weights_.push_back(zn[i]);