Skip to content

Commit

Permalink
Initial implementation of a Cubed Sphere 2 grid builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-jonasganderton committed Dec 2, 2024
1 parent 3e78945 commit d905290
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/atlas/grid/CubedSphereGrid2.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#pragma once

#include "atlas/grid/Grid.h"
#include "atlas/grid/detail/grid/CubedSphere2.h"

namespace atlas {

class CubedSphereGrid2 : public atlas::Grid {
public:
using grid_t = grid::detail::grid::CubedSphere2;

CubedSphereGrid2(idx_t resolution);

private:
const grid_t* grid_;
};

} // namespace atlas
89 changes: 89 additions & 0 deletions src/atlas/grid/detail/grid/CubedSphere2.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#include "atlas/grid/detail/grid/CubedSphere2.h"

#include <cmath>
#include <iomanip>

#include "atlas/grid/CubedSphereGrid2.h"
#include "atlas/grid/detail/grid/GridBuilder.h"
#include "atlas/grid/detail/grid/GridFactory.h"
#include "atlas/runtime/Exception.h"
#include "eckit/geometry/Sphere.h"
#include "eckit/utils/Hash.h"
#include "eckit/utils/Translator.h"

namespace atlas {
namespace grid {
namespace detail {
namespace grid {

static eckit::Translator<std::string, int> to_int;

// Public methods

CubedSphere2::CubedSphere2(idx_t resolution) : N_(resolution) {}
Expand Down Expand Up @@ -135,6 +143,87 @@ PointXYZ CubedSphere2::tangent_to_xyz_coord(PointXY& tan_coord, idx_t tile) cons
return {xyz(0), xyz(1), xyz(2)};
}

namespace {
GridFactoryBuilder<CubedSphere2> __register_CubedSphere2(CubedSphere2::static_type());
}

static class cubedsphere2_lfric : public GridBuilder {
public:
cubedsphere2_lfric():
GridBuilder("cubedsphere2_lfric", {"^[Cc][Ss][_-][Ll][Ff][Rr][-_]([1-9][0-9]*)[_-][2]$"},
{"CS-LFR-<N>-2"}) {}

void print(std::ostream& os) const override {
os << std::left << std::setw(20) << "CS-LFR-<n>-2"
<< "Cubed sphere for LFRic";
}

// Factory constructor
const atlas::Grid::Implementation* create(const std::string& name, const Grid::Config& config) const override {
int id;
std::vector<std::string> matches;
if (match(name, matches, id)) {
util::Config gridconf(config);
int N = to_int(matches[0]);
gridconf.set("type", type());
gridconf.set("N", N);
return create(gridconf);
}
return nullptr;
}

// Factory constructor
const atlas::Grid::Implementation* create(const Grid::Config& config) const override {
int N = 0;
if (not config.get("N", N)) {
throw_AssertionFailed("Could not find \"N\" in configuration of cubed sphere grid 2", Here());
}

return new CubedSphereGrid2::grid_t(N);

// TODO: requires CubedSphereGrid2 constructor with projection

// std::string name = "CS-LFR-" + std::to_string(N) + "-2";
// util::Config projconf;
// projconf.set("type", "cubedsphere2");
// projconf.set("tile.type", "cubedsphere2_lfric");

// // Shift projection by a longitude
// if (config.has("ShiftLon")) {
// double shiftLon = 0.0;
// config.get("ShiftLon", shiftLon);
// projconf.set("ShiftLon", shiftLon);
// }

// // Apply a Schmidt transform
// if (config.has("DoSchmidt")) {
// bool doSchmidt = false;
// config.get("DoSchmidt", doSchmidt);
// if (doSchmidt) {
// double stretchFac;
// double targetLon;
// double targetLat;
// config.get("StretchFac", stretchFac);
// config.get("TargetLon", targetLon);
// config.get("TargetLat", targetLat);
// projconf.set("DoSchmidt", doSchmidt);
// projconf.set("StretchFac", stretchFac);
// projconf.set("TargetLon", targetLon);
// projconf.set("TargetLat", targetLat);
// }
// }

// return new CubedSphereGrid2::grid_t(name, N, Projection(projconf));
}

void force_link() {}

} cubedsphere2_lfric_;

void force_link_CubedSphere2() {
cubedsphere2_lfric_.force_link();
}

} // namespace grid
} // namespace detail
} // namespace grid
Expand Down
2 changes: 2 additions & 0 deletions src/atlas/grid/detail/grid/GridBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ static void init() {
namespace detail {
namespace grid {
void force_link_CubedSphere();
void force_link_CubedSphere2();
void force_link_Gaussian();
void force_link_LonLat();
void force_link_Regional();
Expand All @@ -130,6 +131,7 @@ void force_link_Regional_var_resolution();

const GridBuilder::Registry& GridBuilder::nameRegistry() {
detail::grid::force_link_CubedSphere();
detail::grid::force_link_CubedSphere2();
detail::grid::force_link_Gaussian();
detail::grid::force_link_LonLat();
detail::grid::force_link_Regional();
Expand Down
9 changes: 9 additions & 0 deletions src/tests/grid/test_cubedsphere_2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ CASE("cubed_sphere_grid_kgo") {
EXPECT(compare_2D_points<PointXY>(points_xy, kgo_lonlat));
}

CASE("cubed_sphere_grid_builder") {
const int n = 2;
const std::string name = "CS-LFR-" + std::to_string(n) + "-2";
const Grid grid = Grid(name);
EXPECT(grid.name() == name);
EXPECT(grid.type() == "cubedsphere2");
EXPECT(grid.size() == n * n * 6);
}

} // namespace
} // namespace test
} // namespace atlas
Expand Down

0 comments on commit d905290

Please sign in to comment.