@@ -54,12 +54,8 @@ void CubedSphereBilinear::do_setup(const FunctionSpace& source, const FunctionSp
5454 const auto N = CubedSphereGrid (ncSource.mesh ().grid ()).N ();
5555 const auto tolerance = 2 . * std::numeric_limits<double >::epsilon () * N;
5656
57- // Loop over target at calculate interpolation weights.
58- using TriWeights = std::array<Triplet, 3 >;
59- using QuadWeights = std::array<Triplet, 4 >;
60- using WeightsElement = std::variant<std::monostate, TriWeights, QuadWeights>;
61-
62- auto weights = std::vector<WeightsElement>(target->size ());
57+ // Weights vector is oversized. Empty triplets are ignored by Matrix constructor.
58+ auto weights = Triplets (4 * target_.size ());
6359 const auto ghostView = array::make_view<int , 1 >(target_.ghost ());
6460 const auto lonlatView = array::make_view<double , 2 >(target_.lonlat ());
6561
@@ -81,15 +77,17 @@ void CubedSphereBilinear::do_setup(const FunctionSpace& source, const FunctionSp
8177 switch (cell.nodes .size ()) {
8278 case (3 ): {
8379 // Cell is a triangle.
84- weights[i] = TriWeights{Triplet (i, j[0 ], 1 . - isect.u - isect.v ), Triplet (i, j[1 ], isect.u ),
85- Triplet (i, j[2 ], isect.v )};
80+ weights[4 * i] = Triplet (i, j[0 ], 1 . - isect.u - isect.v );
81+ weights[4 * i + 1 ] = Triplet (i, j[1 ], isect.u );
82+ weights[4 * i + 2 ] = Triplet (i, j[2 ], isect.v );
8683 break ;
8784 }
8885 case (4 ): {
8986 // Cell is quad.
90- weights[i] = QuadWeights{
91- Triplet (i, j[0 ], (1 . - isect.u ) * (1 . - isect.v )), Triplet (i, j[1 ], isect.u * (1 . - isect.v )),
92- Triplet (i, j[2 ], isect.u * isect.v ), Triplet (i, j[3 ], (1 . - isect.u ) * isect.v )};
87+ weights[4 * i] = Triplet (i, j[0 ], (1 . - isect.u ) * (1 . - isect.v ));
88+ weights[4 * i + 1 ] = Triplet (i, j[1 ], isect.u * (1 . - isect.v ));
89+ weights[4 * i + 2 ] = Triplet (i, j[2 ], isect.u * isect.v );
90+ weights[4 * i + 3 ] = Triplet (i, j[3 ], (1 . - isect.u ) * isect.v );
9391 break ;
9492 }
9593 default : {
@@ -99,22 +97,8 @@ void CubedSphereBilinear::do_setup(const FunctionSpace& source, const FunctionSp
9997 }
10098 }
10199
102- auto flattenedWeights = std::vector<Triplet>{};
103- flattenedWeights.reserve (4 * target_.size ()); // Reserve enough space for the maximum number of triplets.
104- auto weightVisitor =
105- eckit::Overloaded{[&](const auto & polyWeights) {
106- flattenedWeights.insert (flattenedWeights.end (), polyWeights.begin (), polyWeights.end ());
107- },
108- [](const std::monostate&) {
109- // Do nothing for empty weights.
110- }};
111-
112- for (const auto & weight : weights) {
113- std::visit (weightVisitor, weight);
114- }
115-
116100 // fill sparse matrix and return.
117- setMatrix (target_.size (), source_.size (), flattenedWeights );
101+ setMatrix (target_.size (), source_.size (), weights );
118102}
119103
120104void CubedSphereBilinear::print (std::ostream&) const {
0 commit comments