forked from open-atmos/PyPartMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin_grid.hpp
132 lines (114 loc) · 3.26 KB
/
bin_grid.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*##################################################################################################
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
# Copyright (C) 2022 University of Illinois Urbana-Champaign #
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
##################################################################################################*/
#pragma once
#include "pmc_resource.hpp"
#include "pybind11/stl.h"
extern "C" void f_bin_grid_ctor(void *ptr) noexcept;
extern "C" void f_bin_grid_dtor(void *ptr) noexcept;
extern "C" void f_bin_grid_init(
const void *ptr,
const int *n_bin,
const int *type,
const double *min,
const double *max
) noexcept;
extern "C" void f_bin_grid_size(
const void *ptr,
int *val
) noexcept;
extern "C" void f_bin_grid_edges(
const void *ptr,
void *arr_data,
const int *arr_size
) noexcept;
extern "C" void f_bin_grid_centers(
const void *ptr,
void *arr_data,
const int *arr_size
) noexcept;
extern "C" void f_bin_grid_histogram_1d(
const void *x_bin_grid_ptr_c,
const void *x_data,
const void *weight_data,
const int *arr_size,
void *output_data,
const int *grid_size
) noexcept;
extern "C" void f_bin_grid_histogram_2d(
const void *x_bin_grid_ptr_c,
const void *x_data,
const void *y_bin_grid_ptr_c,
const void *y_data,
const void *weight_data,
const int *arr_size,
void *output_data,
const int *x_grid_size,
const int *y_grid_size
) noexcept;
struct BinGrid {
PMCResource ptr;
BinGrid(const int &n_bin, const std::string &grid_type, const double &min, const double &max) :
ptr(f_bin_grid_ctor, f_bin_grid_dtor)
{
int type = 0;
if (grid_type == "log") type = 1;
if (grid_type == "linear") type = 2;
if (type == 0)
throw std::invalid_argument( "Invalid grid spacing." );
f_bin_grid_init(ptr.f_arg(), &n_bin, &type, &min, &max);
}
static std::size_t __len__(const BinGrid &self) {
int len;
f_bin_grid_size(
self.ptr.f_arg(),
&len
);
return len;
}
static auto edges(const BinGrid &self)
{
int len;
f_bin_grid_size(
self.ptr.f_arg(),
&len
);
len++;
std::valarray<double> data(len);
f_bin_grid_edges(
self.ptr.f_arg(),
begin(data),
&len
);
return data;
}
static auto centers(const BinGrid &self)
{
int len;
f_bin_grid_size(
self.ptr.f_arg(),
&len
);
std::valarray<double> data(len);
f_bin_grid_centers(
self.ptr.f_arg(),
begin(data),
&len
);
return data;
}
};
std::valarray<double> histogram_1d(
const BinGrid &bin_grid,
std::valarray<double> values,
std::valarray<double> weights
);
std::vector<std::vector<double>> histogram_2d(
const BinGrid &x_bin_grid,
std::valarray<double> x_values,
const BinGrid &y_bin_grid,
std::valarray<double> y_values,
std::valarray<double> weights
);