Skip to content

Commit c8c9e05

Browse files
author
Elizabeth Fischer
committed
Everything builds after re-doing to use ibmisc/linear
1 parent ff23463 commit c8c9e05

9 files changed

+71
-219
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ find_package(Ibmisc REQUIRED)
102102
list(APPEND EXTERNAL_LIBS ${IBMISC_LIBRARY})
103103
include_directories(${IBMISC_INCLUDE_DIR})
104104

105+
find_package(ZLIB REQUIRED)
106+
list(APPEND EXTERNAL_LIBS ${ZLIB_LIBRARIES})
107+
include_directories(${ZLIB_INCLUDE_DIRS})
108+
105109

106110
# --- Proj.4
107111
find_package(PROJ4 REQUIRED)

modele/combine_global_ec.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include <ibmisc/netcdf.hpp>
44
#include <ibmisc/runlength.hpp>
55
#include <icebin/error.hpp>
6-
#include <icebin/RLWeightedSparse.hpp>
6+
#include <ibmisc/linear/linear.hpp>
7+
#include <ibmisc/linear/compressed.hpp>
78

89
using namespace std;
910
using namespace ibmisc;
@@ -62,11 +63,11 @@ void combine_chunks(
6263

6364

6465
// Allocate
65-
RLWeightedSparse ret(sparse_extents);
66+
linear::Weighted_Compressed ret;
6667

67-
{auto wM(ret.wM.accum());
68+
{auto wM(ret.weights[0].accum());
6869
auto M(ret.M.accum());
69-
auto Mw(ret.Mw.accum());
70+
auto Mw(ret.weights[1].accum());
7071

7172
// Aggregate them together
7273
int iM=0;
@@ -78,16 +79,16 @@ void combine_chunks(
7879
auto dimA(nc_read_blitz<int,1>(ncio.nc, "dim"+sgrids[1]));
7980

8081
auto wM_d(nc_read_blitz<double,1>(ncio.nc, BvA+".wM"));
81-
for (int i=0; i<wM_d.extent(0); ++i) wM->add({dimB(i)}, wM_d(i));
82+
for (int i=0; i<wM_d.extent(0); ++i) wM.add({dimB(i)}, wM_d(i));
8283

8384
auto indices_d(nc_read_blitz<int,2>(ncio.nc, BvA+".M.indices"));
8485
auto values_d(nc_read_blitz<double,1>(ncio.nc, BvA+".M.values"));
8586
for (int i=0; i<values_d.extent(0); ++i) {
86-
M->add({dimB(indices_d(i,0)), dimA(indices_d(i,1))}, values_d(i));
87+
M.add({dimB(indices_d(i,0)), dimA(indices_d(i,1))}, values_d(i));
8788
}
8889

8990
auto Mw_d(nc_read_blitz<double,1>(ncio.nc, BvA+".Mw"));
90-
for (int i=0; i<Mw_d.extent(0); ++i) Mw->add({dimA(i)}, Mw_d(i));
91+
for (int i=0; i<Mw_d.extent(0); ++i) Mw.add({dimA(i)}, Mw_d(i));
9192
}
9293

9394
// Check

pylib/_icebin.pyx

+12-28
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ cdef class RegridMatrices:
5353
def __dealloc__(self):
5454
del self.cself
5555

56-
def matrix(self, str spec_name, bool scale=True, bool correctA=True,
57-
sigma=(0,0,0), conserve=True):
56+
def matrix(self, str spec_name):
5857
"""Compute a regrid matrix.
5958
spec_name:
6059
Type of regrid matrix to obtain. Choice are:
@@ -68,12 +67,11 @@ cdef class RegridMatrices:
6867
for the output grid).
6968
returns: WeightedSparse
7069
"""
71-
cdef cicebin.CythonWeightedSparse *crm
72-
crm = cicebin.RegridMatrices_matrix(
73-
self.cself, spec_name.encode(), scale, correctA,
74-
sigma[0], sigma[1], sigma[2], conserve)
75-
ret = WeightedSparse()
76-
ret.cself = crm
70+
cdef cibmisc.linear_Weighted *lw
71+
lw = cicebin.RegridMatrices_matrix(self.cself, spec_name.encode())
72+
cdef ibmisc.linear_Weighted ret
73+
ret = ibmisc.linear_Weighted()
74+
ret.cself = lw
7775
return ret
7876

7977
cdef class GCMRegridder:
@@ -167,33 +165,19 @@ cdef class GCMRegridder:
167165
exgrid_fname.encode(), exgrid_vname.encode(),
168166
interp_style.encode())
169167

170-
def regrid_matrices(self, str sheet_name, elevmaskI):
168+
def regrid_matrices(self, str sheet_name, elevmaskI,
169+
bool scale=True, bool correctA=True,
170+
sigma=(0,0,0), conserve=True):
171+
171172
elevmaskI = elevmaskI.reshape(-1)
172173
cdef cicebin.RegridMatrices *crm = \
173174
cicebin.new_regrid_matrices(self.cself.get(), sheet_name.encode(),
174-
<PyObject *>elevmaskI) # PyObject=Borrowed reference, object = owned reference
175+
<PyObject *>elevmaskI, # PyObject=Borrowed reference, object = owned reference
176+
scale, correctA, sigma[0], sigma[1], sigma[2], conserve)
175177
rm = RegridMatrices()
176178
rm.cself = crm
177179
return rm
178180

179-
def coo_multiply(M, xx, double fill=np.nan, ignore_nan=False):
180-
"""M:
181-
SciPy sparse matrix"""
182-
183-
warnings.warn(
184-
"coo_multiply is deprecated; use WeightedSparse.apply() instead.",
185-
DeprecationWarning)
186-
187-
xx = xx.reshape(-1)
188-
yy = np.zeros(M._shape[0])
189-
yy[:] = fill
190-
191-
cicebin.coo_matvec(<PyObject *>yy, <PyObject *>xx, ignore_nan,
192-
M._shape[0], M._shape[1],
193-
<PyObject *>M.row, <PyObject *>M.col, <PyObject *>M.data)
194-
195-
return yy
196-
197181
# ============================================================
198182

199183
cdef class HntrSpec:

pylib/cicebin.pxd

+5-16
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ cdef extern from "icebin/GCMRegridder.hpp" namespace "icebin":
7474
GCMRegridder_Standard() except +
7575

7676
cdef extern from "icebin_cython.hpp" namespace "icebin::cython":
77-
cdef cppclass CythonWeightedSparse:
78-
object shape() except +
79-
8077
cdef void read_fgrid(
8178
cibmisc.unique_ptr[Grid] &fgridA,
8279
string &gridA_fname,
@@ -108,22 +105,14 @@ cdef extern from "icebin_cython.hpp" namespace "icebin::cython":
108105
string &exgrid_fname, string &exgrid_vname,
109106
string &sinterp_style) except +
110107

111-
cdef CythonWeightedSparse *RegridMatrices_matrix(
112-
RegridMatrices *self, string spec_name, bool scale, bool correctA,
113-
double sigma_x, double sigma_y, double sigma_z, bool conserve) except +
114-
115-
cdef object CythonWeightedSparse_dense_extent(CythonWeightedSparse *self) except +
116-
cdef object CythonWeightedSparse_sparse_extent(CythonWeightedSparse *self) except +
117-
cdef object CythonWeightedSparse_to_tuple(CythonWeightedSparse *self) except +
118-
119-
cdef object CythonWeightedSparse_apply(CythonWeightedSparse *BvA, PyObject *A, double fill, bool force_conservationlmake) except +
120-
121-
cdef void coo_matvec(PyObject *yy_py, PyObject *xx_py, bool ignore_nan,
122-
int M_nrow, int M_ncol, PyObject *M_row_py, PyObject *M_col_py, PyObject *M_data_py) except +
108+
cdef cibmisc.linear_Weighted *RegridMatrices_matrix(
109+
RegridMatrices *self, string spec_name) except +
123110

124111
cdef Hntr_regrid(Hntr *hntr, object WTA_py, object A_py, bool mean_polar) except +
125112

126-
cdef RegridMatrices *new_regrid_matrices(GCMRegridder *gcm, string &sheet_name, PyObject *elevmaskI_py) except +
113+
cdef RegridMatrices *new_regrid_matrices(GCMRegridder *gcm, string &sheet_name, PyObject *elevmaskI_py,
114+
bool scale, bool correctA,
115+
double sigma_x, double sigma_y, double sigma_z, bool conserve) except +
127116

128117
cdef void update_topo(GCMRegridder *_gcmA, string &topoO_fname,
129118
PyObject *elev_sigmas_py, bool initial_timestep, string &segments_py, string &primary_segment_py,

pylib/icebin_cython.cpp

+18-122
Original file line numberDiff line numberDiff line change
@@ -185,129 +185,11 @@ void coo_matvec(PyObject *yy_py, PyObject *xx_py, bool ignore_nan,
185185
}
186186

187187

188-
extern CythonWeightedSparse *RegridMatrices_matrix(RegridMatrices *cself, std::string const &spec_name, bool scale, bool correctA, double sigma_x, double sigma_y, double sigma_z, bool conserve)
188+
extern linear::Weighted *RegridMatrices_matrix(RegridMatrices *cself, std::string const &spec_name)
189189
{
190-
std::unique_ptr<CythonWeightedSparse> CRM(new CythonWeightedSparse());
191-
192-
CRM->RM = cself->matrix_d(spec_name,
193-
{&CRM->dims[0], &CRM->dims[1]},
194-
RegridParams(scale, correctA, {sigma_x, sigma_y, sigma_z}));
195-
196-
CythonWeightedSparse *ret = CRM.release();
197-
return ret;
190+
return cself->matrix(spec_name).release();
198191
}
199192

200-
extern PyObject *CythonWeightedSparse_apply(
201-
CythonWeightedSparse *BvA,
202-
PyObject *A_s_py, // A_b{nj_s} One row per variable
203-
double fill, bool force_conservation)
204-
{
205-
// |j_s| = size of sparse input vector space (A_s)
206-
// |j_d] = size of dense input vector space (A_d)
207-
// |n| = number of variables being processed together
208-
209-
// Allocate dense A matrix
210-
auto &bdim(BvA->dims[0]);
211-
auto &adim(BvA->dims[1]);
212-
auto A_s(np_to_blitz<double,2>(A_s_py, "A", {-1,-1})); // Sparse indexed dense vector
213-
int n_n = A_s.extent(0);
214-
215-
// Densify the A matrix
216-
blitz::Array<double,2> A_d(n_n, adim.dense_extent());
217-
for (int j_d=0; j_d < adim.dense_extent(); ++j_d) {
218-
int j_s = adim.to_sparse(j_d);
219-
for (int n=0; n < n_n; ++n) {
220-
A_d(n,j_d) = A_s(n,j_s);
221-
}
222-
}
223-
224-
// Apply...
225-
auto B_d_eigen(BvA->RM->apply_e(A_d, fill, force_conservation)); // Column major indexing
226-
227-
// Allocate output vector and get a Blitz view
228-
// We will copy from the Eigen data structure to the Python
229-
PyObject *B_s_py = ibmisc::cython::new_pyarray<double,2>(
230-
std::array<int,2>{n_n, bdim.sparse_extent()});
231-
auto B_s(np_to_blitz<double,2>(B_s_py, "B_s_py", {-1,-1}));
232-
233-
// Sparsify the output B
234-
for (int n=0; n < n_n; ++n)
235-
for (int j_s=0; j_s < bdim.sparse_extent(); ++j_s) {
236-
B_s(n,j_s) = fill;
237-
}
238-
239-
for (int j_d=0; j_d < bdim.dense_extent(); ++j_d) {
240-
int j_s = bdim.to_sparse(j_d);
241-
for (int n=0; n < n_n; ++n) {
242-
B_s(n,j_s) = B_d_eigen(j_d,n);
243-
}
244-
}
245-
246-
return B_s_py;
247-
}
248-
249-
PyObject *CythonWeightedSparse_dense_extent(CythonWeightedSparse const *cself)
250-
{
251-
return Py_BuildValue("ll",
252-
(long)cself->dims[0].dense_extent(),
253-
(long)cself->dims[1].dense_extent());
254-
}
255-
256-
PyObject *CythonWeightedSparse_sparse_extent(CythonWeightedSparse const *cself)
257-
{
258-
return Py_BuildValue("ll",
259-
(long)cself->dims[0].sparse_extent(),
260-
(long)cself->dims[1].sparse_extent());
261-
}
262-
263-
264-
PyObject *CythonWeightedSparse_to_tuple(CythonWeightedSparse *cself)
265-
{
266-
// ----- Convert a dense vector w/ dense indices to a dense vector with sparse indices
267-
if (cself->dims[0].sparse_extent() <= 0) (*icebin_error)(-1,
268-
"dims[0].sparse_extent() must be > 0");
269-
270-
// ---------- wM
271-
// Allocate the output Python vector
272-
PyObject *wM_py = ibmisc::cython::new_pyarray<double,1>(
273-
std::array<int,1>{cself->dims[0].sparse_extent()});
274-
// Get a Blitz view on it
275-
auto wM_pyb(np_to_blitz<double,1>(wM_py, "wM_py", {-1}));
276-
// Copy, while translating the dimension
277-
spcopy(
278-
accum::to_sparse(make_array(&cself->dims[0]),
279-
accum::blitz_existing(wM_pyb)),
280-
cself->RM->wM);
281-
282-
// -------------- M
283-
// Convert a sparse matrix w/ dense indices to a sparse matrix with sparse indices
284-
TupleListT<2> RM_sp;
285-
printf("DD1 about to copy: sparse_extent=(%ld, %ld), dense_extent=(%d, %d)\n",
286-
cself->dims[0].sparse_extent(),
287-
cself->dims[1].sparse_extent(),
288-
cself->dims[0].dense_extent(),
289-
cself->dims[1].dense_extent());
290-
291-
spcopy(
292-
accum::to_sparse(make_array(&cself->dims[0], &cself->dims[1]),
293-
accum::ref(RM_sp)),
294-
*cself->RM->M);
295-
PyObject *M_py = ibmisc::cython::spsparse_to_tuple(RM_sp);
296-
297-
// ---------- Mw
298-
// Allocate the output Python vector
299-
PyObject *Mw_py = ibmisc::cython::new_pyarray<double,1>(
300-
std::array<int,1>{cself->dims[1].sparse_extent()});
301-
// Get a Blitz view on it
302-
auto Mw_pyb(np_to_blitz<double,1>(Mw_py, "Mw_py", {-1}));
303-
// Copy, while translating the dimension
304-
spcopy(
305-
accum::to_sparse(make_array(&cself->dims[1]),
306-
accum::blitz_existing(Mw_pyb)),
307-
cself->RM->Mw);
308-
309-
return Py_BuildValue("OOO", wM_py, M_py, Mw_py);
310-
}
311193

312194
// ------------------------------------------------------------
313195
PyObject *Hntr_regrid(Hntr const *hntr, PyObject *WTA_py, PyObject *A_py, bool mean_polar)
@@ -323,13 +205,27 @@ PyObject *Hntr_regrid(Hntr const *hntr, PyObject *WTA_py, PyObject *A_py, bool m
323205
return B_py;
324206
}
325207

326-
RegridMatrices *new_regrid_matrices(GCMRegridder const *gcm, std::string const &sheet_name, PyObject *elevmaskI_py)
208+
RegridMatrices *new_regrid_matrices(
209+
GCMRegridder const *gcm,
210+
std::string const &sheet_name,
211+
PyObject *elevmaskI_py,
212+
// --------- Params
213+
bool scale,
214+
bool correctA,
215+
double sigma_x,
216+
double sigma_y,
217+
double sigma_z,
218+
bool conserve)
327219
{
220+
221+
328222
auto sheet_index = gcm->ice_regridders().index.at(sheet_name);
329223
IceRegridder *ice_regridder = &*gcm->ice_regridders()[sheet_index];
330224
auto elevmaskI(np_to_blitz<double,1>(elevmaskI_py, "elevmaskI", {ice_regridder->nI()}));
331225

332-
return new RegridMatrices(gcm->regrid_matrices(sheet_index, elevmaskI));
226+
return gcm->regrid_matrices(
227+
sheet_index, elevmaskI,
228+
RegridParams(scale, correctA, {sigma_x, sigma_y, sigma_z})).release();
333229
}
334230

335231
std::string to_string(PyObject *str, std::string const &vname)

pylib/icebin_cython.hpp

+13-28
Original file line numberDiff line numberDiff line change
@@ -65,39 +65,24 @@ extern void GCMRegridder_add_sheet(GCMRegridder *cself,
6565
std::string const &sinterp_style);
6666

6767

68-
/** Wraps WeightedSparse to keep around the dense/sparse dimension
69-
translators */
70-
struct CythonWeightedSparse {
71-
std::array<SparseSetT,2> dims;
72-
std::unique_ptr<WeightedSparse> RM;
68+
extern ibmisc::linear::Weighted *RegridMatrices_matrix(RegridMatrices *cself,
69+
std::string const &spec_name);
7370

74-
/** @return 2-D shape of the stored matrix */
75-
PyObject *shape();
76-
};
77-
78-
79-
extern CythonWeightedSparse *RegridMatrices_matrix(RegridMatrices *cself,
80-
std::string const &spec_name, bool scale, bool correctA,
81-
double sigma_x, double sigma_y, double sigma_z, bool conserve);
82-
83-
extern PyObject *CythonWeightedSparse_apply(
84-
CythonWeightedSparse *BvA,
85-
PyObject *A_s_py,
86-
double fill, bool force_conservation); // A_b{nj_s} One row per variable
87-
88-
89-
PyObject *CythonWeightedSparse_dense_extent(CythonWeightedSparse const *cself);
90-
PyObject *CythonWeightedSparse_sparse_extent(CythonWeightedSparse const *cself);
91-
92-
PyObject *CythonWeightedSparse_to_tuple(CythonWeightedSparse *cself);
93-
94-
void coo_matvec(PyObject *yy_py, PyObject *xx_py, bool ignore_nan,
95-
size_t M_nrow, size_t M_ncol, PyObject *M_row_py, PyObject *M_col_py, PyObject *M_data_py);
9671

9772
PyObject *Hntr_regrid(modele::Hntr const *hntr, PyObject *WTA_py, PyObject *A_py, bool mean_polar);
9873

9974

100-
RegridMatrices *new_regrid_matrices(GCMRegridder const *gcm, std::string const &sheet_name, PyObject *elevmaskI_py);
75+
RegridMatrices *new_regrid_matrices(
76+
GCMRegridder const *gcm,
77+
std::string const &sheet_name,
78+
PyObject *elevmaskI_py,
79+
// --------- Params
80+
bool scale,
81+
bool correctA,
82+
double sigma_x,
83+
double sigma_y,
84+
double sigma_z,
85+
bool conserve);
10186

10287
/** Allows Python users access to GCMCoupler_Modele::update_topo().
10388
Starting from output of Gary's program (on the Ocean grid), this subroutine

slib/icebin/IceCoupler.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,10 @@ bool run_ice)
475475
dimA1.ncio(ncio, "dimA");
476476
dimE1.ncio(ncio, "dimE");
477477

478-
AvE1->ncio_nodim(ncio, "AvE");
479-
E1vI_nc->ncio_nodim(ncio, "EvI_nc");
480-
A1vI->ncio_nodim(ncio, "AvI");
481-
IvE1->ncio_nodim(ncio, "IvE");
478+
AvE1->ncio(ncio, "AvE", {"dimA", "dimE"});
479+
E1vI_nc->ncio(ncio, "EvI_nc", {"dimE", "dimI"});
480+
A1vI->ncio(ncio, "AvI", {"dimA", "dimI"});
481+
IvE1->ncio(ncio, "IvE", {"dimI", "dimE"});
482482
}
483483

484484
// Save stuff for next time around

0 commit comments

Comments
 (0)