|
| 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 |
0 commit comments