Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ message(STATUS "Include ${CMAKE_CURRENT_SOURCE_DIR}/cmake/parameters.cmake")
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/parameters.cmake)

message(STATUS "${Blue}${PROJECT_NAME}-3. Setting up system libraries ...${ColorReset}")
set(BUILD_SHARED_LIBS OFF)
find_package(Torch REQUIRED)
find_package(Disort REQUIRED)
find_package(Harp REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ target_link_libraries(${namel}_${buildl}
${TORCH_LIBRARY}
${TORCH_CPU_LIBRARY}
${C10_LIBRARY}
archive
archive_static
fmt::fmt
yaml-cpp::yaml-cpp
)
Expand Down
30 changes: 30 additions & 0 deletions src/implicit/implicit_hydro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@

namespace snap {

ImplicitOptions ImplicitOptionsImpl::from_yaml(const std::string& filename,
bool /*verbose*/) {
auto config = YAML::LoadFile(filename);
if (!config["integration"]) return nullptr;
if (!config["integration"]["implicit-scheme"]) return nullptr;
return from_yaml(config["integration"]["implicit-scheme"]);
}

ImplicitOptions ImplicitOptionsImpl::from_yaml(const YAML::Node& node) {
auto op = ImplicitOptionsImpl::create();
op->scheme(node.as<int>());
return op;
}

std::string ImplicitOptionsImpl::type() const {
switch (scheme()) {
case 0:
return "none";
break;
case 1:
return "vic-partial";
break;
case 9:
return "vic-full";
break;
Comment on lines +34 to +40
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The break statements after return statements are unreachable code. Since each case returns a value, the break statements on lines 34, 37, and 40 will never be executed and should be removed.

Suggested change
break;
case 1:
return "vic-partial";
break;
case 9:
return "vic-full";
break;
case 1:
return "vic-partial";
case 9:
return "vic-full";

Copilot uses AI. Check for mistakes.
default:
TORCH_CHECK(false, "Unsupported implicit scheme");
}
}

ImplicitHydroImpl::ImplicitHydroImpl(ImplicitOptions const& options_,
torch::nn::Module* p)
: options(options_) {
Expand Down
2 changes: 1 addition & 1 deletion src/implicit/implicit_hydro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct ImplicitOptionsImpl {
}
}

ADD_ARG(std::string, type) = "none";
std::string type() const;
ADD_ARG(int, scheme) = 0;
};
using ImplicitOptions = std::shared_ptr<ImplicitOptionsImpl>;
Expand Down
38 changes: 0 additions & 38 deletions src/implicit/implicit_options.cpp

This file was deleted.

54 changes: 54 additions & 0 deletions src/utils/refine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "refine.hpp"

namespace snap {

torch::Tensor conservative_refine(torch::Tensor x) {
auto opts_y = torch::nn::functional::InterpolateFuncOptions()
.scale_factor(std::vector<double>({2.0, 2.0, 1.0}))
.mode(torch::kTrilinear)
.align_corners(false);

auto opts_x = torch::nn::functional::InterpolateFuncOptions()
.scale_factor(std::vector<double>({0.5, 0.5, 1.0}))
.mode(torch::kArea);

auto opts_dy = torch::nn::functional::InterpolateFuncOptions()
.scale_factor(std::vector<double>({2.0, 2.0, 1.0}))
.mode(torch::kArea);

// bilinear refine
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says "bilinear refine" but the interpolation mode is set to torch::kTrilinear on line 8. The comment should be updated to "trilinear refine" to accurately reflect the interpolation method being used.

Suggested change
// bilinear refine
// trilinear refine

Copilot uses AI. Check for mistakes.
int dim = 0;
while (x.dim() < 5) {
++dim;
x = x.unsqueeze(0);
}
auto y1 = torch::nn::functional::interpolate(x, opts_y);

// conservative coarsen
auto x1 = torch::nn::functional::interpolate(y1, opts_x);

// conservative correction
auto dy = torch::nn::functional::interpolate(x - x1, opts_dy);
auto y = y1 + dy;

for (int i = 0; i < dim; ++i) y = y.squeeze(0);
return y;
}

torch::Tensor conservative_coarsen(torch::Tensor x) {
auto opts = torch::nn::functional::InterpolateFuncOptions()
.scale_factor(std::vector<double>({0.5, 0.5, 1.0}))
.mode(torch::kArea);
int dim = 0;
while (x.dim() < 5) {
++dim;
x = x.unsqueeze(0);
}

auto y = torch::nn::functional::interpolate(x, opts);

for (int i = 0; i < dim; ++i) y = y.squeeze(0);
return y;
}

} // namespace snap
11 changes: 11 additions & 0 deletions src/utils/refine.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

// torch
#include <torch/torch.h>

namespace snap {

torch::Tensor conservative_refine(torch::Tensor x);
Comment on lines +7 to +8
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The public API functions conservative_refine and conservative_coarsen are missing documentation comments. Similar utility functions in this codebase (e.g., pull_neighbors2/3/4 in src/utils/pull_neighbors.hpp) include Doxygen-style documentation that explains the purpose, parameters, and return values. Consider adding documentation to describe what these functions do, their input tensor requirements (e.g., expected dimensions), and what the output tensor represents.

Suggested change
torch::Tensor conservative_refine(torch::Tensor x);
/**
* @brief Perform conservative refinement of a tensor field.
*
* This utility takes values defined on a coarse resolution grid and produces
* a refined representation while conserving the total quantity represented
* by @p x along the refined dimensions.
*
* The input is typically a tensor of shape (batch, channels, spatial...),
* where the trailing dimensions correspond to spatial axes that will be
* refined. The exact refinement scheme and resulting shape depend on the
* implementation, but the operation is designed to be conservative
* (i.e., the sum over corresponding regions is preserved).
*
* @param x Input tensor containing cell-averaged or cell-integrated values
* on a coarse grid.
* @return A tensor defined on a refined grid, with values adjusted so that
* the total quantity is conserved with respect to @p x.
*/
torch::Tensor conservative_refine(torch::Tensor x);
/**
* @brief Perform conservative coarsening of a tensor field.
*
* This utility takes values defined on a fine resolution grid and produces
* a coarsened representation while conserving the total quantity represented
* by @p x along the coarsened dimensions.
*
* The input is typically a tensor of shape (batch, channels, spatial...),
* where the trailing dimensions correspond to spatial axes that will be
* coarsened (e.g., by aggregating neighboring cells). The exact coarsening
* scheme and resulting shape depend on the implementation, but the operation
* is designed to be conservative (i.e., the sum over aggregated regions is
* preserved).
*
* @param x Input tensor containing cell-averaged or cell-integrated values
* on a fine grid.
* @return A tensor defined on a coarser grid, with values adjusted so that
* the total quantity is conserved with respect to @p x.
*/

Copilot uses AI. Check for mistakes.
torch::Tensor conservative_coarsen(torch::Tensor x);

} // namespace snap
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ setup_test(test_slab)
setup_test(test_cubed)
setup_test(test_cubed_sphere)
setup_test(test_coordinate)
setup_test(test_refine)
#setup_test(test_read_topo)
#setup_cuda_test(test_thomas_solver)
#setup_test(test_aneos)
Expand Down
44 changes: 44 additions & 0 deletions tests/test_refine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// external
#include <gtest/gtest.h>
#include <yaml-cpp/yaml.h>

// torch
#include <torch/torch.h>

// snapy
#include <snap/snap.h>

#include <snap/utils/refine.hpp>

// tests
#include "device_testing.hpp"

using namespace snap;

TEST_P(DeviceTest, refine_funcs) {
int nc1 = 2;
int nc2 = 3;
int nc3 = 3;
int nvar = 1;

auto x = torch::empty({nvar, nc3, nc2, nc1}, torch::dtype(dtype));

for (int n = 0; n < nvar; ++n)
for (int k = 0; k < nc3; ++k)
for (int j = 0; j < nc2; ++j)
for (int i = 0; i < nc1; ++i) {
x[n][k][j][i] = static_cast<float>(n + k + j + i + 1);
}

x = x.to(device);
auto y = conservative_refine(x);
auto z = conservative_coarsen(y);

EXPECT_TRUE(torch::allclose(x, z, 1.E-6, 1.E-6));
}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}
Loading