From 970aa6ea9a63d264596463645c17566cc5d26047 Mon Sep 17 00:00:00 2001 From: jingang Date: Wed, 5 Jun 2019 11:26:39 -0400 Subject: [PATCH 01/10] 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 5f30f0252d..32ed36c8b8 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 0be98718ce..e52d6c8054 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 650a18a8a0..157d0d1a2c 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 34c4b9781b..81d7559b8e 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 1fb10b7066..729f008914 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 f8a4f4a39a..fd524a2de9 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 b16fecc6e3..5e679042ad 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 54d0e93441..ec5d64eac7 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 ddfa272a6d..075e58de24 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 efe562fa28..3aac41f35b 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 f30b444310..9361fa4241 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 1d1d440b28..0446cb2955 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 f69a966523..c62578b47e 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 1e97e64c1b..bbb34b16cc 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 616dbbbd4d..6727bfed10 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 9f66a68f98..9ab7b4bcda 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]); From 12b5f337d45e533c9eb6a0e6cfeab01d0a529508 Mon Sep 17 00:00:00 2001 From: jingang Date: Wed, 5 Jun 2019 15:19:24 -0400 Subject: [PATCH 02/10] HDF5 linking errors --- CMakeLists.txt | 6 ++++++ src/hdf5_interface.cpp | 2 ++ 2 files changed, 8 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index fea014f12d..7448713663 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -272,6 +272,12 @@ if(GIT_SHA1_SUCCESS EQUAL 0) target_compile_definitions(libopenmc PRIVATE -DGIT_SHA1="${GIT_SHA1}") endif() +# To use the shared HDF5 libraries on Windows with Visual Studio, +# the H5_BUILT_AS_DYNAMIC_LIB compile definition must be specified. +if(MSVC) + target_compile_definitions(libopenmc PRIVATE -DH5_BUILT_AS_DYNAMIC_LIB) +endif() + # target_link_libraries treats any arguments starting with - but not -l as # linker flags. Thus, we can pass both linker flags and libraries together. target_link_libraries(libopenmc ${ldflags} ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES} diff --git a/src/hdf5_interface.cpp b/src/hdf5_interface.cpp index 729f008914..b1a90cb8f7 100644 --- a/src/hdf5_interface.cpp +++ b/src/hdf5_interface.cpp @@ -792,6 +792,8 @@ const hid_t H5TypeMap::type_id = H5T_NATIVE_INT; template<> const hid_t H5TypeMap::type_id = H5T_NATIVE_ULONG; template<> +const hid_t H5TypeMap::type_id = H5T_NATIVE_ULLONG; +template<> const hid_t H5TypeMap::type_id = H5T_NATIVE_UINT; template<> const hid_t H5TypeMap::type_id = H5T_NATIVE_INT64; From 97b8b6bef71b812c7e0376ef91a776ca10d09b96 Mon Sep 17 00:00:00 2001 From: jingang Date: Wed, 5 Jun 2019 16:22:23 -0400 Subject: [PATCH 03/10] read windows cross section libaries --- src/cross_sections.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cross_sections.cpp b/src/cross_sections.cpp index 5d8d18c219..8ea59c1f08 100644 --- a/src/cross_sections.cpp +++ b/src/cross_sections.cpp @@ -402,6 +402,10 @@ void read_ce_cross_sections_xml() // If no directory is listed in cross_sections.xml, by default select the // directory in which the cross_sections.xml file resides auto pos = filename.rfind("/"); + if (pos == std::string::npos) { + // no '/' found, probably a Windows directory + pos = filename.rfind("\\"); + } directory = filename.substr(0, pos); } From e5b108be7a14a1bc6a42034dc5e0bd801a4219fe Mon Sep 17 00:00:00 2001 From: jingang Date: Thu, 6 Jun 2019 10:36:15 -0400 Subject: [PATCH 04/10] fix write 2D char arrays in HDF5 --- include/openmc/hdf5_interface.h | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/include/openmc/hdf5_interface.h b/include/openmc/hdf5_interface.h index e52d6c8054..c87abd84a1 100644 --- a/include/openmc/hdf5_interface.h +++ b/include/openmc/hdf5_interface.h @@ -464,22 +464,16 @@ write_dataset(hid_t obj_id, const char* name, const std::vector& bu } // Copy data into contiguous buffer - 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'); + char* temp = new char[n*m]; + std::fill(temp, temp + n*m, '\0'); for (int i = 0; i < n; ++i) { - std::copy(buffer[i].begin(), buffer[i].end(), temp[i]); + std::copy(buffer[i].begin(), buffer[i].end(), temp + i*m); } // Write 2D data - write_string(obj_id, 1, dims, m, name, temp[0], false); + write_string(obj_id, 1, dims, m, name, temp, false); // Free temp array - for (int i = 0; i < n; i++) { - delete[] temp[i]; - } delete[] temp; } From 950c684e6ce92b9c7b39cf0bdab548813ba21b4e Mon Sep 17 00:00:00 2001 From: jingang Date: Thu, 6 Jun 2019 10:46:01 -0400 Subject: [PATCH 05/10] Set libopenmc as static library in CMake for MSVC: Built openmc.exe successfully with visual studio --- CMakeLists.txt | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7448713663..70ff63e978 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -159,7 +159,7 @@ target_compile_options(faddeeva PRIVATE ${cxxflags}) # libopenmc #=============================================================================== -add_library(libopenmc SHARED +list(APPEND libopenmc_SOURCES src/bank.cpp src/bremsstrahlung.cpp src/dagmc.cpp @@ -246,6 +246,18 @@ add_library(libopenmc SHARED src/xml_interface.cpp src/xsdata.cpp) +# For Visual Studio compilers +if(MSVC) + # Use static library (otherwise explicit symbol portings are needed) + add_library(libopenmc STATIC ${libopenmc_SOURCES}) + + # To use the shared HDF5 libraries on Windows, the H5_BUILT_AS_DYNAMIC_LIB + # compile definition must be specified. + target_compile_definitions(libopenmc PRIVATE -DH5_BUILT_AS_DYNAMIC_LIB) +else() + add_library(libopenmc SHARED ${libopenmc_SOURCES}) +endif() + set_target_properties(libopenmc PROPERTIES OUTPUT_NAME openmc) @@ -272,12 +284,6 @@ if(GIT_SHA1_SUCCESS EQUAL 0) target_compile_definitions(libopenmc PRIVATE -DGIT_SHA1="${GIT_SHA1}") endif() -# To use the shared HDF5 libraries on Windows with Visual Studio, -# the H5_BUILT_AS_DYNAMIC_LIB compile definition must be specified. -if(MSVC) - target_compile_definitions(libopenmc PRIVATE -DH5_BUILT_AS_DYNAMIC_LIB) -endif() - # target_link_libraries treats any arguments starting with - but not -l as # linker flags. Thus, we can pass both linker flags and libraries together. target_link_libraries(libopenmc ${ldflags} ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES} From 59046be677cd4526bb568330e385da07d718b485 Mon Sep 17 00:00:00 2001 From: Jingang Liang Date: Thu, 6 Jun 2019 17:20:14 +0000 Subject: [PATCH 06/10] fix reading 2D string vector from attribute in hdf5 --- include/openmc/hdf5_interface.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/include/openmc/hdf5_interface.h b/include/openmc/hdf5_interface.h index c87abd84a1..d0474c8344 100644 --- a/include/openmc/hdf5_interface.h +++ b/include/openmc/hdf5_interface.h @@ -204,23 +204,19 @@ 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 = new char*[m]; - for (int i = 0; i < m; i++) { - buffer[i] = new char[n]; - } + char* buffer = new char[m*n]; // Read char data in attribute - read_attr_string(obj_id, name, n, buffer[0]); + read_attr_string(obj_id, name, n, buffer); for (int i = 0; i < m; ++i) { // Determine proper length of string -- strlen doesn't work because // buffer[i] might not have any null characters std::size_t k = 0; - for (; k < n; ++k) if (buffer[i][k] == '\0') break; + for (; k < n; ++k) if (buffer[i*n + k] == '\0') break; // Create string based on (char*, size_t) constructor - vec.emplace_back(&buffer[i][0], k); - delete[] buffer[i]; + vec.emplace_back(&buffer[i*n], k); } delete[] buffer; } From f57c4b919c6f067668eb1691942089fcb24488bc Mon Sep 17 00:00:00 2001 From: jingang Date: Thu, 6 Jun 2019 11:36:39 -0400 Subject: [PATCH 07/10] do not set compile/link flags for visual studio generator --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70ff63e978..5c0734d23f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,9 @@ endif() # Set compile/link flags based on which compiler is being used #=============================================================================== +# Skip for Visual Stduio which has its own configurations through GUI +if(NOT MSVC) + if(openmp) # Requires CMake 3.1+ find_package(OpenMP) @@ -104,6 +107,8 @@ endif() message(STATUS "OpenMC C++ flags: ${cxxflags}") message(STATUS "OpenMC Linker flags: ${ldflags}") +endif() + #=============================================================================== # pugixml library #=============================================================================== From e5b2c6b477ae517419c6f7be813a43c095838ee5 Mon Sep 17 00:00:00 2001 From: jingang Date: Fri, 14 Jun 2019 19:59:27 -0400 Subject: [PATCH 08/10] &name[0] -> name.data() --- src/initialize.cpp | 2 +- src/lattice.cpp | 6 +++--- src/mesh.cpp | 28 ++++++++++++++-------------- src/tallies/filter_legendre.cpp | 2 +- src/tallies/filter_mesh.cpp | 2 +- src/tallies/filter_sph_harm.cpp | 4 ++-- src/tallies/filter_sptl_legendre.cpp | 2 +- src/tallies/filter_zernike.cpp | 4 ++-- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/initialize.cpp b/src/initialize.cpp index fd524a2de9..8331c30b5d 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -102,7 +102,7 @@ void initialize_mpi(MPI_Comm intracomm) // Create bank datatype Particle::Bank b; MPI_Aint disp[6]; - MPI_Get_address(&b.r, &disp[0]); + MPI_Get_address(c&b.r, &disp[0]); MPI_Get_address(&b.u, &disp[1]); MPI_Get_address(&b.E, &disp[2]); MPI_Get_address(&b.wgt, &disp[3]); diff --git a/src/lattice.cpp b/src/lattice.cpp index 5e679042ad..42e15013d4 100644 --- a/src/lattice.cpp +++ b/src/lattice.cpp @@ -401,7 +401,7 @@ RectLattice::to_hdf5_inner(hid_t lat_group) const } hsize_t dims[3] {nz, ny, nx}; - write_int(lat_group, 3, dims, "universes", &out[0], false); + write_int(lat_group, 3, dims, "universes", out.data(), false); } else { hsize_t nx {static_cast(n_cells_[0])}; @@ -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[0], false); + write_int(lat_group, 3, dims, "universes", out.data(), false); } } @@ -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[0], false); + write_int(lat_group, 3, dims, "universes", out.data(), false); } //============================================================================== diff --git a/src/mesh.cpp b/src/mesh.cpp index 075e58de24..01ad329b7a 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -183,11 +183,11 @@ int RegularMesh::get_bin(Position r) const // Determine indices std::vector ijk(n_dimension_); bool in_mesh; - get_indices(r, &ijk[0], &in_mesh); + get_indices(r, ijk.data(), &in_mesh); if (!in_mesh) return -1; // Convert indices to bin - return get_bin_from_indices(&ijk[0]); + return get_bin_from_indices(ijk.data()); } int RegularMesh::get_bin_from_indices(const int* ijk) const @@ -497,9 +497,9 @@ void RegularMesh::bins_crossed(const Particle* p, std::vector& bins, int n = n_dimension_; std::vector ijk0(n), ijk1(n); bool start_in_mesh; - get_indices(r0, &ijk0[0], &start_in_mesh); + get_indices(r0, ijk0.data(), &start_in_mesh); bool end_in_mesh; - get_indices(r1, &ijk1[0], &end_in_mesh); + get_indices(r1, ijk1.data(), &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[0])) return; + if (!intersects(r0, r1, ijk0.data())) return; } r1 = r; @@ -521,7 +521,7 @@ void RegularMesh::bins_crossed(const Particle* p, std::vector& bins, // 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[0])); + bins.push_back(get_bin_from_indices(ijk0.data())); lengths.push_back(distance / total_distance); break; } @@ -543,7 +543,7 @@ 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.begin(), d.end()) - d.begin(); double distance = d[j]; - bins.push_back(get_bin_from_indices(&ijk0[0])); + bins.push_back(get_bin_from_indices(ijk0.data())); lengths.push_back(distance / total_distance); // Translate to the oncoming mesh surface. @@ -584,15 +584,15 @@ void RegularMesh::surface_bins_crossed(const Particle* p, int n = n_dimension_; std::vector ijk0(n), ijk1(n); bool start_in_mesh; - get_indices(r0, &ijk0[0], &start_in_mesh); + get_indices(r0, ijk0.data(), &start_in_mesh); bool end_in_mesh; - get_indices(r1, &ijk1[0], &end_in_mesh); + get_indices(r1, ijk1.data(), &end_in_mesh); // Check if the track intersects any part of the mesh. if (!start_in_mesh) { Position r0_copy = r0; std::vector ijk0_copy(ijk0); - if (!intersects(r0_copy, r1, &ijk0_copy[0])) return; + if (!intersects(r0_copy, r1, ijk0_copy.data())) return; } // ======================================================================== @@ -650,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[0]); + int i_mesh = get_bin_from_indices(ijk0.data()); int i_bin = 4*n*i_mesh + i_surf - 1; bins.push_back(i_bin); @@ -671,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[0]); + int i_mesh = get_bin_from_indices(ijk0.data()); int i_bin = 4*n*i_mesh + i_surf - 1; bins.push_back(i_bin); @@ -683,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[0]); + int i_mesh = get_bin_from_indices(ijk0.data()); int i_bin = 4*n*i_mesh + i_surf - 1; bins.push_back(i_bin); @@ -704,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[0]); + int i_mesh = get_bin_from_indices(ijk0.data()); int i_bin = 4*n*i_mesh + i_surf - 1; bins.push_back(i_bin); diff --git a/src/tallies/filter_legendre.cpp b/src/tallies/filter_legendre.cpp index 0446cb2955..6fa041c76b 100644 --- a/src/tallies/filter_legendre.cpp +++ b/src/tallies/filter_legendre.cpp @@ -19,7 +19,7 @@ LegendreFilter::get_all_bins(const Particle* p, int estimator, FilterMatch& match) const { std::vector wgt(n_bins_); - calc_pn_c(order_, p->mu_, &wgt[0]); + calc_pn_c(order_, p->mu_, wgt.data()); 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 c62578b47e..babc4b6e61 100644 --- a/src/tallies/filter_mesh.cpp +++ b/src/tallies/filter_mesh.cpp @@ -59,7 +59,7 @@ MeshFilter::text_label(int bin) const int n_dim = mesh.n_dimension_; std::vector ijk(n_dim); - mesh.get_indices_from_bin(bin, &ijk[0]); + mesh.get_indices_from_bin(bin, ijk.data()); 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 bbb34b16cc..0b3a371187 100644 --- a/src/tallies/filter_sph_harm.cpp +++ b/src/tallies/filter_sph_harm.cpp @@ -37,14 +37,14 @@ SphericalHarmonicsFilter::get_all_bins(const Particle* p, int estimator, // Determine cosine term for scatter expansion if necessary std::vector wgt(order_ + 1); if (cosine_ == SphericalHarmonicsCosine::scatter) { - calc_pn_c(order_, p->mu_, &wgt[0]); + calc_pn_c(order_, p->mu_, wgt.data()); } else { for (int i = 0; i < order_ + 1; i++) wgt[i] = 1; } // Find the Rn,m values std::vector rn(n_bins_); - calc_rn(order_, p->u_last_, &rn[0]); + calc_rn(order_, p->u_last_, rn.data()); 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 6727bfed10..6562ff01c7 100644 --- a/src/tallies/filter_sptl_legendre.cpp +++ b/src/tallies/filter_sptl_legendre.cpp @@ -51,7 +51,7 @@ SpatialLegendreFilter::get_all_bins(const Particle* p, int estimator, // Compute and return the Legendre weights. std::vector wgt(order_ + 1); - calc_pn_c(order_, x_norm, &wgt[0]); + calc_pn_c(order_, x_norm, wgt.data()); 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 9ab7b4bcda..125bfada89 100644 --- a/src/tallies/filter_zernike.cpp +++ b/src/tallies/filter_zernike.cpp @@ -37,7 +37,7 @@ ZernikeFilter::get_all_bins(const Particle* p, int estimator, if (r <= 1.0) { // Compute and return the Zernike weights. std::vector zn(n_bins_); - calc_zn(order_, r, theta, &zn[0]); + calc_zn(order_, r, theta, zn.data()); for (int i = 0; i < n_bins_; i++) { match.bins_.push_back(i); match.weights_.push_back(zn[i]); @@ -94,7 +94,7 @@ ZernikeRadialFilter::get_all_bins(const Particle* p, int estimator, if (r <= 1.0) { // Compute and return the Zernike weights. std::vector zn(n_bins_); - calc_zn_rad(order_, r, &zn[0]); + calc_zn_rad(order_, r, zn.data()); for (int i = 0; i < n_bins_; i++) { match.bins_.push_back(i); match.weights_.push_back(zn[i]); From 23d7e41592609c6d782d044ea436ff17e4d26bd6 Mon Sep 17 00:00:00 2001 From: jingang Date: Fri, 14 Jun 2019 20:07:42 -0400 Subject: [PATCH 09/10] and -> &&, or -> || --- src/surface.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/surface.cpp b/src/surface.cpp index 9361fa4241..f329fda6d2 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "openmc/error.h" #include "openmc/hdf5_interface.h" @@ -298,7 +297,7 @@ template double axis_aligned_plane_distance(Position r, Direction u, bool coincident, double offset) { const double f = offset - r[i]; - if (coincident or std::abs(f) < FP_COINCIDENT or u[i] == 0.0) return INFTY; + if (coincident || std::abs(f) < FP_COINCIDENT || u[i] == 0.0) return INFTY; const double d = f / u[i]; if (d < 0.0) return INFTY; return d; @@ -494,7 +493,7 @@ SurfacePlane::distance(Position r, Direction u, bool coincident) const { const double f = A_*r.x + B_*r.y + C_*r.z - D_; const double projection = A_*u.x + B_*u.y + C_*u.z; - if (coincident or std::abs(f) < FP_COINCIDENT or projection == 0.0) { + if (coincident || std::abs(f) < FP_COINCIDENT || projection == 0.0) { return INFTY; } else { const double d = -f / projection; @@ -574,7 +573,7 @@ axis_aligned_cylinder_distance(Position r, Direction u, // No intersection with cylinder. return INFTY; - } else if (coincident or std::abs(c) < FP_COINCIDENT) { + } else if (coincident || std::abs(c) < FP_COINCIDENT) { // Particle is on the cylinder, thus one distance is positive/negative // and the other is zero. The sign of k determines if we are facing in or // out. @@ -744,7 +743,7 @@ double SurfaceSphere::distance(Position r, Direction u, bool coincident) const // No intersection with sphere. return INFTY; - } else if (coincident or std::abs(c) < FP_COINCIDENT) { + } else if (coincident || std::abs(c) < FP_COINCIDENT) { // Particle is on the sphere, thus one distance is positive/negative and // the other is zero. The sign of k determines if we are facing in or out. if (k >= 0.0) { @@ -821,7 +820,7 @@ axis_aligned_cone_distance(Position r, Direction u, // No intersection with cone. return INFTY; - } else if (coincident or std::abs(c) < FP_COINCIDENT) { + } else if (coincident || std::abs(c) < FP_COINCIDENT) { // Particle is on the cone, thus one distance is positive/negative // and the other is zero. The sign of k determines if we are facing in or // out. @@ -1009,7 +1008,7 @@ SurfaceQuadric::distance(Position r, Direction ang, bool coincident) const // No intersection with surface. return INFTY; - } else if (coincident or std::abs(c) < FP_COINCIDENT) { + } else if (coincident || std::abs(c) < FP_COINCIDENT) { // Particle is on the surface, thus one distance is positive/negative and // the other is zero. The sign of k determines which distance is zero and // which is not. @@ -1156,27 +1155,27 @@ void read_surfaces(pugi::xml_node node) // See if this surface makes part of the global bounding box. BoundingBox bb = surf->bounding_box(); - if (bb.xmin > -INFTY and bb.xmin < xmin) { + if (bb.xmin > -INFTY && bb.xmin < xmin) { xmin = bb.xmin; i_xmin = i_surf; } - if (bb.xmax < INFTY and bb.xmax > xmax) { + if (bb.xmax < INFTY && bb.xmax > xmax) { xmax = bb.xmax; i_xmax = i_surf; } - if (bb.ymin > -INFTY and bb.ymin < ymin) { + if (bb.ymin > -INFTY && bb.ymin < ymin) { ymin = bb.ymin; i_ymin = i_surf; } - if (bb.ymax < INFTY and bb.ymax > ymax) { + if (bb.ymax < INFTY && bb.ymax > ymax) { ymax = bb.ymax; i_ymax = i_surf; } - if (bb.zmin > -INFTY and bb.zmin < zmin) { + if (bb.zmin > -INFTY && bb.zmin < zmin) { zmin = bb.zmin; i_zmin = i_surf; } - if (bb.zmax < INFTY and bb.zmax > zmax) { + if (bb.zmax < INFTY && bb.zmax > zmax) { zmax = bb.zmax; i_zmax = i_surf; } From 7cf9bca90845ca8f94108553fca79915f5175b72 Mon Sep 17 00:00:00 2001 From: jingang Date: Fri, 14 Jun 2019 20:39:49 -0400 Subject: [PATCH 10/10] added include in cell.cpp --- src/cell.cpp | 1 + src/initialize.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cell.cpp b/src/cell.cpp index 157d0d1a2c..a60f20fbea 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "openmc/capi.h" #include "openmc/constants.h" diff --git a/src/initialize.cpp b/src/initialize.cpp index 8331c30b5d..fd524a2de9 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -102,7 +102,7 @@ void initialize_mpi(MPI_Comm intracomm) // Create bank datatype Particle::Bank b; MPI_Aint disp[6]; - MPI_Get_address(c&b.r, &disp[0]); + MPI_Get_address(&b.r, &disp[0]); MPI_Get_address(&b.u, &disp[1]); MPI_Get_address(&b.E, &disp[2]); MPI_Get_address(&b.wgt, &disp[3]);