Skip to content

Commit 4fa8b63

Browse files
ttadanoclaude
andcommitted
anphon: convert kpoint/system/fcs_phonon/write_phonons arrays to NDArray (P8)
Final phase of the allocate()/deallocate() retirement. The three k-point owner classes (KpointGeneral, KpointMeshUniform, KpointBandStructure) hold xk/kvec_na/kaxis via NDArray, which makes their tables deep-const through the const KpointMeshUniform* borrows everywhere; the read-only view parameters that were sloppily declared mutable become const along the way (exec_interpolation's xk_dense/kvec_dense, duplicate_xk_boundary, compute_dV2_dumn and compute_d2V2_dumn2, and the non-template rotvec overloads' vec_in), and pointer-copy aliases of the k tables become reference bindings. Fcs_phonon::force_constant_with_cell converts too, surfacing three methods that mutated it through a shallow const (replicate_force_constants, load_fcs_from_file, MPI_Bcast_fcs_array) - they lose the qualifier, and examine_translational_invariance's switch cases gain scopes for the now non-trivially-constructed accumulators. System, symmetry_core, and write_phonons locals convert wholesale. The only allocate() calls left in anphon are the spglib C-ABI boundaries (double(*)[3] / int(*)[3][3] in symmetry_core and relaxation), each carrying a comment saying why; no raw new[] remains. Full 8-suite regression battery + test_units pass, alm still builds against the shared mathfunctions.h, and the ndarray unit test passes. A/B timing vs the pre-migration binary: Si RTA -0.14%, BaTiO3 SCPH +0.32%. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent cea3c78 commit 4fa8b63

17 files changed

Lines changed: 263 additions & 310 deletions

anphon/anharmonic_core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ void AnharmonicCore::calc_damping_tetrahedron(const unsigned int ntemp, const do
912912

913913
const auto knum = kmesh_in->kpoint_irred_all[ik_in][0].knum;
914914
const auto knum_minus = kmesh_in->kindex_minus_xk[knum];
915-
const auto xk = kmesh_in->xk;
915+
const auto &xk = kmesh_in->xk;
916916

917917
kmap_identity.resize(nk);
918918

anphon/dynamical.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ void Dynamical::replicate_dymat_for_all_kpoints(const KpointMeshUniform *kmesh_c
19821982

19831983

19841984
void Dynamical::exec_interpolation(const unsigned int kmesh_orig[3], std::complex<double> ***dymat_r,
1985-
const unsigned int nk_dense, double **xk_dense, double **kvec_dense,
1985+
const unsigned int nk_dense, const double *const *xk_dense, const double *const *kvec_dense,
19861986
double **eval_out, std::complex<double> ***evec_out,
19871987
const std::vector<Eigen::MatrixXcd> &dymat_short,
19881988
const std::vector<Eigen::MatrixXcd> &dymat_long, MinimumDistList ***mindist_list_in,
@@ -2298,7 +2298,7 @@ void Dynamical::calc_new_dymat_with_evec(std::complex<double> ***dymat_out, doub
22982298
}
22992299
}
23002300

2301-
void Dynamical::duplicate_xk_boundary(double *xk_in, std::vector<std::vector<double>> &vec_xk)
2301+
void Dynamical::duplicate_xk_boundary(const double *xk_in, std::vector<std::vector<double>> &vec_xk)
23022302
{
23032303
int i;
23042304
int n[3];

anphon/dynamical.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class Dynamical: protected Pointers
175175
const double *const *get_xrs_image() const;
176176

177177
void exec_interpolation(const unsigned int kmesh_orig[3], std::complex<double> ***dymat_r,
178-
const unsigned int nk_dense, double **xk_dense, double **kvec_dense, double **eval_out,
178+
const unsigned int nk_dense, const double *const *xk_dense, const double *const *kvec_dense, double **eval_out,
179179
std::complex<double> ***evec_out, const std::vector<Eigen::MatrixXcd> &dymat_short,
180180
const std::vector<Eigen::MatrixXcd> &dymat_long, MinimumDistList ***mindist_list_in,
181181
const bool use_precomputed_dymat = false, const bool return_sqrt = true) const;
@@ -220,7 +220,7 @@ class Dynamical: protected Pointers
220220
Eigen::MatrixXcd &evec_sub) const;
221221

222222

223-
void duplicate_xk_boundary(double *, std::vector<std::vector<double>> &);
223+
void duplicate_xk_boundary(const double *, std::vector<std::vector<double>> &);
224224

225225

226226
NDArray<double, 2> xshift_s;

anphon/fcs_phonon.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,10 @@ void Fcs_phonon::set_default_variables()
5252
file_fc4 = "";
5353

5454
update_fc2 = false;
55-
force_constant_with_cell = nullptr;
5655
}
5756

5857
void Fcs_phonon::deallocate_variables()
5958
{
60-
if (force_constant_with_cell) {
61-
deallocate(force_constant_with_cell);
62-
}
6359
}
6460

6561
void Fcs_phonon::setup(const std::string &mode)
@@ -111,7 +107,7 @@ void Fcs_phonon::setup(const std::string &mode)
111107
// parser (parse_analysis_vars).
112108
}
113109

114-
allocate(force_constant_with_cell, maxorder);
110+
force_constant_with_cell.resize(maxorder);
115111

116112
if (mympi->my_rank == 0) {
117113

@@ -138,7 +134,7 @@ void Fcs_phonon::setup(const std::string &mode)
138134
replicate_force_constants(maxorder);
139135
}
140136

141-
void Fcs_phonon::replicate_force_constants(const int maxorder_in) const
137+
void Fcs_phonon::replicate_force_constants(const int maxorder_in)
142138
{
143139
for (auto order = 0; order < maxorder_in; ++order) {
144140
replicate_force_constant(system.get(), force_constant_with_cell[order]);
@@ -245,7 +241,7 @@ void Fcs_phonon::replicate_force_constant(const System *system_in, std::vector<F
245241
}
246242

247243

248-
void Fcs_phonon::load_fcs_from_file(const int maxorder_in) const
244+
void Fcs_phonon::load_fcs_from_file(const int maxorder_in)
249245
{
250246
std::vector filename_list{file_fc2, file_fc3, file_fc4};
251247

@@ -725,9 +721,9 @@ double Fcs_phonon::examine_translational_invariance(const int order, const unsig
725721
const auto natmin3 = 3 * natmin;
726722

727723
switch (order) {
728-
case 0:
729-
double **sum2;
730-
allocate(sum2, natmin3, 3);
724+
case 0: {
725+
NDArray<double, 2> sum2;
726+
sum2.resize(natmin3, 3);
731727

732728
for (j = 0; j < natmin3; ++j) {
733729
for (k = 0; k < 3; ++k) {
@@ -747,11 +743,12 @@ double Fcs_phonon::examine_translational_invariance(const int order, const unsig
747743
ret = std::max(ret, dev);
748744
}
749745
}
750-
deallocate(sum2);
746+
sum2.clear();
751747
break;
752-
case 1:
753-
double ***sum3;
754-
allocate(sum3, 3 * natmin, 3 * nat, 3);
748+
}
749+
case 1: {
750+
NDArray<double, 3> sum3;
751+
sum3.resize(3 * natmin, 3 * nat, 3);
755752

756753
for (j = 0; j < natmin3; ++j) {
757754
for (k = 0; k < nat3; ++k) {
@@ -775,11 +772,12 @@ double Fcs_phonon::examine_translational_invariance(const int order, const unsig
775772
}
776773
}
777774
}
778-
deallocate(sum3);
775+
sum3.clear();
779776
break;
780-
case 2:
781-
double ****sum4;
782-
allocate(sum4, natmin3, nat3, nat3, 3);
777+
}
778+
case 2: {
779+
NDArray<double, 4> sum4;
780+
sum4.resize(natmin3, nat3, nat3, 3);
783781

784782
for (j = 0; j < natmin3; ++j) {
785783
for (k = 0; k < nat3; ++k) {
@@ -809,21 +807,22 @@ double Fcs_phonon::examine_translational_invariance(const int order, const unsig
809807
}
810808
}
811809
}
812-
deallocate(sum4);
810+
sum4.clear();
813811
break;
812+
}
814813
default:
815814
break;
816815
}
817816

818817
return ret;
819818
}
820819

821-
void Fcs_phonon::MPI_Bcast_fcs_array(const unsigned int N) const
820+
void Fcs_phonon::MPI_Bcast_fcs_array(const unsigned int N)
822821
{
823822
int j, k;
824-
double *fcs_tmp;
825-
unsigned int ***ind;
826-
double ***relative_vector_tmp;
823+
NDArray<double, 1> fcs_tmp;
824+
NDArray<unsigned int, 3> ind;
825+
NDArray<double, 3> relative_vector_tmp;
827826

828827
std::vector<AtomCellSuper> ivec_array;
829828
std::vector<unsigned int> atoms_s_tmp;
@@ -840,9 +839,9 @@ void Fcs_phonon::MPI_Bcast_fcs_array(const unsigned int N) const
840839

841840
if (len == 0) continue;
842841

843-
allocate(fcs_tmp, len);
844-
allocate(ind, len, nelem, 4);
845-
allocate(relative_vector_tmp, len, nelem - 1, 3);
842+
fcs_tmp.resize(len);
843+
ind.resize(len, nelem, 4);
844+
relative_vector_tmp.resize(len, nelem - 1, 3);
846845

847846
if (mympi->my_rank == 0) {
848847
for (j = 0; j < len; ++j) {
@@ -892,8 +891,8 @@ void Fcs_phonon::MPI_Bcast_fcs_array(const unsigned int N) const
892891
}
893892
}
894893

895-
deallocate(fcs_tmp);
896-
deallocate(ind);
897-
deallocate(relative_vector_tmp);
894+
fcs_tmp.clear();
895+
ind.clear();
896+
relative_vector_tmp.clear();
898897
}
899898
}

anphon/fcs_phonon.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <set>
1414
#include <string>
1515
#include <vector>
16+
#include "ndarray.h"
1617
#include "mathfunctions.h"
1718
#include "pointers.h"
1819

@@ -193,7 +194,7 @@ class Fcs_phonon: protected Pointers
193194
unsigned int maxorder;
194195
std::string file_fcs, file_fc2, file_fc3, file_fc4;
195196

196-
std::vector<FcsArrayWithCell> *force_constant_with_cell;
197+
NDArray<std::vector<FcsArrayWithCell>, 1> force_constant_with_cell;
197198

198199
bool update_fc2;
199200

@@ -229,17 +230,17 @@ class Fcs_phonon: protected Pointers
229230

230231
void append_delta_fc2_from_scph(const std::string &fname_dfc2, std::vector<FcsArrayWithCell> &fcs_out) const;
231232

232-
void load_fcs_from_file(const int maxorder_in) const;
233+
void load_fcs_from_file(const int maxorder_in);
233234

234235

235236
static double examine_translational_invariance(int order, unsigned int nat, unsigned int natmin,
236237
const std::vector<FcsArrayWithCell> &fc_in);
237238

238-
void replicate_force_constants(const int maxorder_in) const;
239+
void replicate_force_constants(const int maxorder_in);
239240

240241
void MPI_Bcast_fc_class(unsigned int) const;
241242

242-
void MPI_Bcast_fcs_array(unsigned int) const;
243+
void MPI_Bcast_fcs_array(unsigned int);
243244

244245
void MPI_Bcast_fc2_ext();
245246
};

anphon/gruneisen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void Gruneisen::calc_gruneisen()
132132

133133
if (kpoint->kpoint_bs.get()) {
134134
const auto nk = kpoint->kpoint_bs->nk;
135-
const auto xk = kpoint->kpoint_bs->xk;
135+
const auto &xk = kpoint->kpoint_bs->xk;
136136
const auto eval = dynamical->dymat_band->get_eigenvalues();
137137
const auto evec = dynamical->dymat_band->get_eigenvectors();
138138

@@ -165,7 +165,7 @@ void Gruneisen::calc_gruneisen()
165165

166166
if (dos->kmesh_dos.get()) {
167167
const auto nk = dos->kmesh_dos->nk;
168-
const auto xk = dos->kmesh_dos->xk;
168+
const auto &xk = dos->kmesh_dos->xk;
169169
const auto eval = dos->dymat_dos->get_eigenvalues();
170170
const auto evec = dos->dymat_dos->get_eigenvectors();
171171

anphon/ifc_derivative.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void DerivativeIFC::compute_d3V1_dumn3(MatrixXcdRowMajor &del3_v1_del_umn3,
214214

215215
void DerivativeIFC::compute_dV2_dumn(std::vector<MatrixXcdRowMajor> &del_v2_del_umn,
216216
const std::complex<double> *const *const *const evec_harmonic,
217-
const unsigned int nk, double **xk_in) const
217+
const unsigned int nk, const double *const *xk_in) const
218218
{
219219
using namespace Eigen;
220220

@@ -270,7 +270,7 @@ void DerivativeIFC::compute_dV2_dumn(std::vector<MatrixXcdRowMajor> &del_v2_del_
270270

271271
void DerivativeIFC::compute_d2V2_dumn2(std::vector<MatrixXcdRowMajor> &del2_v2_del_umn2,
272272
const std::complex<double> *const *const *const evec_harmonic,
273-
const unsigned int nk, double **xk_in) const
273+
const unsigned int nk, const double *const *xk_in) const
274274
{
275275
using namespace Eigen;
276276

anphon/ifc_derivative.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class DerivativeIFC
5454

5555
void compute_dV2_dumn(std::vector<MatrixXcdRowMajor> &dV2_dumn,
5656
const std::complex<double> *const *const *const evec_harmonic, unsigned int nk,
57-
double **xk_in) const;
57+
const double *const *xk_in) const;
5858

5959
void compute_d2V2_dumn2(std::vector<MatrixXcdRowMajor> &d2V2_dumn2,
6060
const std::complex<double> *const *const *const evec_harmonic, unsigned int nk,
61-
double **xk_in) const;
61+
const double *const *xk_in) const;
6262

6363
void compute_dV3_dumn(std::vector<std::vector<MatrixXcdRowMajor>> &dV3_dumn, double **omega2_harmonic,
6464
const std::complex<double> *const *const *const evec_harmonic,

0 commit comments

Comments
 (0)