Flow123d  DF_patch_fe_data_tables-9d4017b
python_field_proxy.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file python_field_proxy.hh
15  * @brief
16  */
17 
18 #ifndef PYTHON_FIELD_BASE_HH_
19 #define PYTHON_FIELD_BASE_HH_
20 
21 #include <pybind11/pybind11.h>
22 #include <pybind11/stl.h>
23 #include <pybind11/numpy.h>
24 //#include <pybind11/common.h>
26 
27 namespace py = pybind11;
28 #pragma GCC visibility push(hidden)
29 
30 /// Helper class, holds data of one field
32 {
33 public:
34  /**
35  * Method encapsulates FieldValueCache data array for usage in Python.
36  * Allows to create C++ and Python objects above shared block of memory.
37  */
38  static py::buffer_info field_proxy_get_buffer(FieldCacheProxy &proxy)
39  {
40  ssize_t n_comp = ( (proxy.shape_.size()==1) ? proxy.shape_[0] : proxy.shape_[0]*proxy.shape_[1]);
41  ssize_t size = proxy.data_size_ / n_comp;
43  std::vector<ssize_t> strides;
44 
45  if (proxy.shape_[0] > 1) { // add dimensions only for vector and tensor
46  shape.push_back(proxy.shape_[0]);
47  if (proxy.shape_.size() == 2) shape.push_back(proxy.shape_[1]);
48  }
49  shape.push_back(size);
50 
51  ssize_t n_dim = shape.size();
52  strides.resize(n_dim);
53  strides[n_dim-1] = sizeof(double);
54  for(uint i=n_dim-1; i>0; i--) {
55  strides[i-1] = strides[i] * shape[i];
56  }
57 
58  // create n_dim NumPy array
59  return py::buffer_info(
60  proxy.field_cache_data_, /* data as contiguous array */
61  sizeof(double), /* size of one scalar */
62  py::format_descriptor<double>::format(), /* data type */
63  n_dim, /* number of dimensions */
64  shape, /* shape of the matrix */
65  strides /* strides for each axis */
66  );
67  }
68 
69  /// Constructor
70  FieldCacheProxy(std::string field_name, std::vector<uint> shape, double * field_cache_data, uint data_size)
71  : field_name_(field_name), shape_(shape), field_cache_data_(field_cache_data), data_size_(data_size)
72  {}
73 
74  /// Copy constructor
77  {}
78 
79  /// Getter returns field name
80  const std::string &field_name() const { return field_name_; }
81 
82 
83 private:
84  std::string field_name_;
88 };
89 
90 #pragma GCC visibility pop
91 
92 #endif /* PYTHON_FIELD_BASE_HH_ */
Helper class, holds data of one field.
std::vector< uint > shape_
FieldCacheProxy(std::string field_name, std::vector< uint > shape, double *field_cache_data, uint data_size)
Constructor.
std::string field_name_
FieldCacheProxy(const FieldCacheProxy &other)
Copy constructor.
const std::string & field_name() const
Getter returns field name.
static py::buffer_info field_proxy_get_buffer(FieldCacheProxy &proxy)
manipulators::Array< T, Delim > format(T const &deduce, Delim delim=", ")
Definition: logger.hh:325
unsigned int uint