From 970aa6ea9a63d264596463645c17566cc5d26047 Mon Sep 17 00:00:00 2001 From: jingang Date: Wed, 5 Jun 2019 11:26:39 -0400 Subject: [PATCH] fixed compiling errors with visual studio, mostly VLAs --- include/openmc/error.h | 2 +- include/openmc/hdf5_interface.h | 23 ++++++++++++--- src/cell.cpp | 2 +- src/finalize.cpp | 4 +-- src/hdf5_interface.cpp | 9 ++++-- src/initialize.cpp | 2 +- src/lattice.cpp | 12 ++++---- src/math_functions.cpp | 13 +++++---- src/mesh.cpp | 43 ++++++++++++++-------------- src/mgxs.cpp | 3 +- src/surface.cpp | 1 + src/tallies/filter_legendre.cpp | 4 +-- src/tallies/filter_mesh.cpp | 4 +-- src/tallies/filter_sph_harm.cpp | 8 +++--- src/tallies/filter_sptl_legendre.cpp | 4 +-- src/tallies/filter_zernike.cpp | 8 +++--- 16 files changed, 81 insertions(+), 61 deletions(-) diff --git a/include/openmc/error.h b/include/openmc/error.h index 5f30f0252..32ed36c8b 100644 --- a/include/openmc/error.h +++ b/include/openmc/error.h @@ -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); diff --git a/include/openmc/hdf5_interface.h b/include/openmc/hdf5_interface.h index 0be98718c..e52d6c805 100644 --- a/include/openmc/hdf5_interface.h +++ b/include/openmc/hdf5_interface.h @@ -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 @@ -203,7 +204,10 @@ read_attribute(hid_t obj_id, const char* name, std::vector& 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& 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& 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& 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 inline void diff --git a/src/cell.cpp b/src/cell.cpp index 650a18a8a..157d0d1a2 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -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 stack(rpn_.size()); int i_stack = -1; for (int32_t token : rpn_) { diff --git a/src/finalize.cpp b/src/finalize.cpp index 34c4b9781..81d7559b8 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -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; } diff --git a/src/hdf5_interface.cpp b/src/hdf5_interface.cpp index 1fb10b706..729f00891 100644 --- a/src/hdf5_interface.cpp +++ b/src/hdf5_interface.cpp @@ -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; } diff --git a/src/initialize.cpp b/src/initialize.cpp index f8a4f4a39..fd524a2de 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -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(); diff --git a/src/lattice.cpp b/src/lattice.cpp index b16fecc6e..5e679042a 100644 --- a/src/lattice.cpp +++ b/src/lattice.cpp @@ -388,7 +388,7 @@ RectLattice::to_hdf5_inner(hid_t lat_group) const hsize_t nx {static_cast(n_cells_[0])}; hsize_t ny {static_cast(n_cells_[1])}; hsize_t nz {static_cast(n_cells_[2])}; - int out[nx*ny*nz]; + std::vector 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(n_cells_[0])}; hsize_t ny {static_cast(n_cells_[1])}; - int out[nx*ny]; + std::vector 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(2*n_rings_ - 1)}; hsize_t ny {static_cast(2*n_rings_ - 1)}; hsize_t nz {static_cast(n_axial_)}; - int out[nx*ny*nz]; + std::vector 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); } //============================================================================== diff --git a/src/math_functions.cpp b/src/math_functions.cpp index 54d0e9344..ec5d64eac 100644 --- a/src/math_functions.cpp +++ b/src/math_functions.cpp @@ -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 sin_phi_vec(n + 1); // Sin[n * phi] + std::vector 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> zn_mat(n + 1, std::vector(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 c_new(n-1); // Set natural boundary conditions c_new[0] = 0.0; diff --git a/src/mesh.cpp b/src/mesh.cpp index ddfa272a6..075e58de2 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -181,13 +181,13 @@ int RegularMesh::get_bin(Position r) const } // Determine indices - int ijk[n_dimension_]; + std::vector 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& bins, // Determine the mesh indices for the starting and ending coords. int n = n_dimension_; - int ijk0[n], ijk1[n]; + std::vector 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& 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& 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 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& 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 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 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); diff --git a/src/mgxs.cpp b/src/mgxs.cpp index efe562fa2..3aac41f35 100644 --- a/src/mgxs.cpp +++ b/src/mgxs.cpp @@ -96,7 +96,7 @@ Mgxs::metadata_from_hdf5(hid_t xs_id, const std::vector& 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& 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 diff --git a/src/surface.cpp b/src/surface.cpp index f30b44431..9361fa424 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "openmc/error.h" #include "openmc/hdf5_interface.h" diff --git a/src/tallies/filter_legendre.cpp b/src/tallies/filter_legendre.cpp index 1d1d440b2..0446cb295 100644 --- a/src/tallies/filter_legendre.cpp +++ b/src/tallies/filter_legendre.cpp @@ -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 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]); diff --git a/src/tallies/filter_mesh.cpp b/src/tallies/filter_mesh.cpp index f69a96652..c62578b47 100644 --- a/src/tallies/filter_mesh.cpp +++ b/src/tallies/filter_mesh.cpp @@ -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 ijk(n_dim); + mesh.get_indices_from_bin(bin, &ijk[0]); std::stringstream out; out << "Mesh Index (" << ijk[0]; diff --git a/src/tallies/filter_sph_harm.cpp b/src/tallies/filter_sph_harm.cpp index 1e97e64c1..bbb34b16c 100644 --- a/src/tallies/filter_sph_harm.cpp +++ b/src/tallies/filter_sph_harm.cpp @@ -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 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 rn(n_bins_); + calc_rn(order_, p->u_last_, &rn[0]); int j = 0; for (int n = 0; n < order_ + 1; n++) { diff --git a/src/tallies/filter_sptl_legendre.cpp b/src/tallies/filter_sptl_legendre.cpp index 616dbbbd4..6727bfed1 100644 --- a/src/tallies/filter_sptl_legendre.cpp +++ b/src/tallies/filter_sptl_legendre.cpp @@ -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 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]); diff --git a/src/tallies/filter_zernike.cpp b/src/tallies/filter_zernike.cpp index 9f66a68f9..9ab7b4bcd 100644 --- a/src/tallies/filter_zernike.cpp +++ b/src/tallies/filter_zernike.cpp @@ -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 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 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]);