Skip to content

Commit c75f82b

Browse files
authored
Density on grid (#49)
* g2g seems to work * backup * density2grid works (I think) * fix warnings
1 parent 788bc76 commit c75f82b

6 files changed

Lines changed: 148 additions & 2 deletions

File tree

src/scf/xc/density2grid.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2025 NWChemEx-Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "xc.hpp"
18+
#include <simde/integration_grids/collocation_matrix.hpp>
19+
20+
namespace scf::xc {
21+
namespace {
22+
const auto desc = R"(
23+
24+
DensityCollocationMatrix
25+
-----------------
26+
)";
27+
28+
struct Kernel {
29+
using buffer_base = tensorwrapper::buffer::BufferBase;
30+
31+
template<typename FloatType>
32+
auto run(const buffer_base& aos_on_grid, const buffer_base& X,
33+
parallelzone::runtime::RuntimeView& rv) {
34+
tensorwrapper::allocator::Eigen<FloatType> allocator(rv);
35+
36+
const auto& eigen_aos_on_grid = allocator.rebind(aos_on_grid);
37+
const auto* paos_on_grid = eigen_aos_on_grid.get_immutable_data();
38+
const auto& eigen_X = allocator.rebind(X);
39+
const auto* pX = eigen_X.get_immutable_data();
40+
const auto& shape_X = eigen_X.layout().shape().as_smooth();
41+
auto n_aos = shape_X.extent(0);
42+
auto n_grid = shape_X.extent(1);
43+
44+
tensorwrapper::shape::Smooth rv_shape{n_grid};
45+
tensorwrapper::layout::Physical rv_layout(rv_shape);
46+
auto rv_buffer = allocator.allocate(rv_layout);
47+
48+
// AOs on rows, grid points on columns
49+
for(std::size_t grid_i = 0; grid_i < n_grid; ++grid_i) {
50+
FloatType sum = 0;
51+
for(std::size_t ao_i = 0; ao_i < n_aos; ++ao_i) {
52+
const auto idx = ao_i * n_grid + grid_i;
53+
sum += paos_on_grid[idx] * pX[idx];
54+
}
55+
rv_buffer->set_elem(std::vector{grid_i}, sum);
56+
}
57+
return simde::type::tensor(rv_shape, std::move(rv_buffer));
58+
}
59+
};
60+
} // namespace
61+
62+
using pt = simde::EDensityCollocationMatrix;
63+
using ao2grid_pt = simde::AOCollocationMatrix;
64+
65+
MODULE_CTOR(Density2Grid) {
66+
satisfies_property_type<pt>();
67+
description(desc);
68+
69+
add_submodule<ao2grid_pt>("AOs on a grid");
70+
}
71+
72+
MODULE_RUN(Density2Grid) {
73+
const auto& [grid, density] = pt::unwrap_inputs(inputs);
74+
75+
const auto& rho = density.value();
76+
const auto& aos = density.basis_set().ao_basis_set();
77+
78+
auto& ao2grid_mod = submods.at("AOs on a grid");
79+
auto aos_on_grid = ao2grid_mod.run_as<ao2grid_pt>(grid, aos);
80+
81+
simde::type::tensor X;
82+
X("m,i") = rho("m,n") * aos_on_grid("n,i");
83+
84+
using tensorwrapper::utilities::floating_point_dispatch;
85+
Kernel k;
86+
auto& runtime = get_runtime();
87+
const auto& aos_buffer = aos_on_grid.buffer();
88+
const auto& X_buffer = X.buffer();
89+
auto rho_on_grid =
90+
floating_point_dispatch(k, aos_buffer, X_buffer, runtime);
91+
92+
auto rv = results();
93+
return pt::wrap_results(rv, std::move(rho_on_grid));
94+
}
95+
96+
} // namespace scf::xc

src/scf/xc/gau2grid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ std::vector<T> flatten_grid(const chemist::Grid& grid) {
4040

4141
} // namespace
4242

43-
using pt = simde::CollocationMatrix;
43+
using pt = simde::AOCollocationMatrix;
4444

4545
MODULE_CTOR(Gau2Grid) {
4646
satisfies_property_type<pt>();

src/scf/xc/xc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void load_modules(pluginplay::ModuleManager& mm) {
2222
gauxc::load_modules(mm);
2323
mm.add_module<Gau2Grid>("Gau2Grid");
2424
mm.add_module<GridFromFile>("Grid From File");
25+
mm.add_module<Density2Grid>("Density2Grid");
2526
}
2627

2728
void set_defaults(pluginplay::ModuleManager& mm) { gauxc::set_defaults(mm); }

src/scf/xc/xc.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace scf::xc {
2121

22+
DECLARE_MODULE(Density2Grid);
2223
DECLARE_MODULE(Gau2Grid);
2324
DECLARE_MODULE(GridFromFile);
2425

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2025 NWChemEx-Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "../../test_scf.hpp"
18+
#include <iostream>
19+
#include <pluginplay/pluginplay.hpp>
20+
21+
using namespace scf;
22+
23+
using pt = simde::EDensityCollocationMatrix;
24+
using ao_pt = simde::AOCollocationMatrix;
25+
using tensorwrapper::operations::approximately_equal;
26+
27+
TEST_CASE("Density2Grid") {
28+
pluginplay::ModuleManager mm;
29+
scf::load_modules(mm);
30+
auto& mod = mm.at("Density2Grid");
31+
32+
chemist::Grid grid; // Value doesn't matter for this test b/c of submodule
33+
34+
SECTION("H2") {
35+
auto rho = test_scf::h2_density<double>();
36+
simde::type::tensor ao2grid{{1.0, 2.0, 3.0, 4.0}, {5.0, 6.0, 7.0, 8.0}};
37+
auto ao_mod =
38+
pluginplay::make_lambda<ao_pt>([&](auto&& grid_in, auto&& aos) {
39+
REQUIRE(grid_in == grid);
40+
REQUIRE(aos == rho.basis_set().ao_basis_set());
41+
return ao2grid;
42+
});
43+
mod.change_submod("AOs on a grid", ao_mod);
44+
auto rv = mod.run_as<pt>(grid, rho);
45+
simde::type::tensor corr{11.513088, 20.467712, 31.9808, 46.052352};
46+
REQUIRE(approximately_equal(rv, corr, 1e-4));
47+
}
48+
}

tests/cxx/unit_tests/xc/gau2grid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
using namespace scf;
2222

23-
using pt = simde::CollocationMatrix;
23+
using pt = simde::AOCollocationMatrix;
2424

2525
using tensorwrapper::operations::approximately_equal;
2626

0 commit comments

Comments
 (0)