|
| 1 | +#include "Probe.h" |
| 2 | + |
| 3 | +using namespace dolfin; |
| 4 | + |
| 5 | +Probe::~Probe() |
| 6 | +{ |
| 7 | + clear(); |
| 8 | +} |
| 9 | + |
| 10 | +Probe::Probe(const Array<double>& x, const FunctionSpace& V) : |
| 11 | + _element(V.element()), _num_evals(0) |
| 12 | +{ |
| 13 | + auto mesh = V.mesh(); |
| 14 | + std::size_t gdim = mesh->geometry().dim(); |
| 15 | + _x.resize(3); |
| 16 | + |
| 17 | + // Find the cell that contains probe |
| 18 | + const Point point(gdim, x.data()); |
| 19 | + unsigned int cell_id = mesh->bounding_box_tree()->compute_first_entity_collision(point); |
| 20 | + |
| 21 | + // If the cell is on this process, then create an instance |
| 22 | + // of the Probe class. Otherwise raise a dolfin_error. |
| 23 | + if (cell_id != std::numeric_limits<unsigned int>::max()) |
| 24 | + { |
| 25 | + // Store position of probe |
| 26 | + for (std::size_t i = 0; i < 3; i++) |
| 27 | + _x[i] = (i < gdim ? x[i] : 0.0); |
| 28 | + |
| 29 | + // Compute in tensor (one for scalar function, . . .) |
| 30 | + _value_size_loc = 1; |
| 31 | + for (std::size_t i = 0; i < _element->value_rank(); i++) |
| 32 | + _value_size_loc *= _element->value_dimension(i); |
| 33 | + |
| 34 | + _probes.resize(_value_size_loc); |
| 35 | + |
| 36 | + // Create cell that contains point |
| 37 | + dolfin_cell.reset(new Cell(*mesh, cell_id)); |
| 38 | + dolfin_cell->get_cell_data(ufc_cell); |
| 39 | + |
| 40 | + coefficients.resize(_element->space_dimension()); |
| 41 | + |
| 42 | + // Cell vertices |
| 43 | + dolfin_cell->get_vertex_coordinates(vertex_coordinates); |
| 44 | + |
| 45 | + // Create work vector for basis |
| 46 | + basis_matrix.resize(_value_size_loc); |
| 47 | + for (std::size_t i = 0; i < _value_size_loc; ++i) |
| 48 | + basis_matrix[i].resize(_element->space_dimension()); |
| 49 | + |
| 50 | + std::vector<double> basis(_value_size_loc); |
| 51 | + const int cell_orientation = 0; |
| 52 | + for (std::size_t i = 0; i < _element->space_dimension(); ++i) |
| 53 | + { |
| 54 | + _element->evaluate_basis(i, &basis[0], &x[0], |
| 55 | + vertex_coordinates.data(), |
| 56 | + cell_orientation); |
| 57 | + for (std::size_t j = 0; j < _value_size_loc; ++j) |
| 58 | + basis_matrix[j][i] = basis[j]; |
| 59 | + } |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + dolfin_error("Probe.cpp","set probe","Probe is not found on processor"); |
| 64 | + } |
| 65 | +} |
| 66 | +// |
| 67 | +Probe::Probe(const Probe& p) |
| 68 | +{ |
| 69 | + basis_matrix = p.basis_matrix; |
| 70 | + coefficients = p.coefficients; |
| 71 | + _x = p._x; |
| 72 | + vertex_coordinates = p.vertex_coordinates; |
| 73 | + _element = p._element; |
| 74 | + dolfin_cell.reset(new Cell(p.dolfin_cell->mesh(), p.dolfin_cell->index())); |
| 75 | + ufc_cell = p.ufc_cell; |
| 76 | + _value_size_loc = p._value_size_loc; |
| 77 | + _num_evals = p._num_evals; |
| 78 | + _probes = p._probes; |
| 79 | +} |
| 80 | +// |
| 81 | +void Probe::eval(const Function& u) |
| 82 | +{ |
| 83 | + // Restrict function to cell |
| 84 | + u.restrict(&coefficients[0], *_element, *dolfin_cell, |
| 85 | + vertex_coordinates.data(), ufc_cell); |
| 86 | + |
| 87 | + // Make room for one more evaluation |
| 88 | + for (std::size_t j = 0; j < _value_size_loc; j++) |
| 89 | + _probes[j].push_back(0.); |
| 90 | + |
| 91 | + // Compute linear combination |
| 92 | + for (std::size_t i = 0; i < _element->space_dimension(); i++) |
| 93 | + { |
| 94 | + for (std::size_t j = 0; j < _value_size_loc; j++) |
| 95 | + _probes[j][_num_evals] += coefficients[i]*basis_matrix[j][i]; |
| 96 | + } |
| 97 | + |
| 98 | + _num_evals++; |
| 99 | +} |
| 100 | +// Remove one instance of the probe |
| 101 | +void Probe::erase_snapshot(std::size_t i) |
| 102 | +{ |
| 103 | + for (std::size_t j = 0; j < _value_size_loc; j++) |
| 104 | + _probes[j].erase(_probes[j].begin()+i); |
| 105 | + |
| 106 | + _num_evals--; |
| 107 | +} |
| 108 | +// Reset probe by removing all values |
| 109 | +void Probe::clear() |
| 110 | +{ |
| 111 | + for (std::size_t j = 0; j < _value_size_loc; j++) |
| 112 | + _probes[j].clear(); |
| 113 | + |
| 114 | + _num_evals = 0; |
| 115 | +} |
| 116 | +// Return probe values for chosen component of tensor |
| 117 | +std::vector<double> Probe::get_probe_sub(std::size_t i) |
| 118 | +{ |
| 119 | + return _probes[i]; |
| 120 | +} |
| 121 | +// Return probe values for entire tensor at snapshot |
| 122 | +std::vector<double> Probe::get_probe_at_snapshot(std::size_t i) |
| 123 | +{ |
| 124 | + std::vector<double> x(_value_size_loc); |
| 125 | + for (std::size_t j = 0; j < _value_size_loc; j++) |
| 126 | + x[j] = _probes[j][i]; |
| 127 | + return x; |
| 128 | +} |
| 129 | +// Return robe value for given component at given snapshot |
| 130 | +double Probe::get_probe_component_and_snapshot(std::size_t comp, std::size_t i) |
| 131 | +{ |
| 132 | + return _probes[comp][i]; |
| 133 | +} |
| 134 | +// Return coordinates of probe |
| 135 | +std::vector<double> Probe::coordinates() |
| 136 | +{ |
| 137 | + std::vector<double> x(_x); |
| 138 | + return x; |
| 139 | +} |
| 140 | +// |
| 141 | +void Probe::dump(std::size_t i, std::string filename, std::size_t id) |
| 142 | +{ |
| 143 | + //std::ofstream fp(filename.c_str(), std::ios_base::app); |
| 144 | + std::ofstream fp(filename.c_str(), std::ios_base::out); |
| 145 | + std::ostringstream ss; |
| 146 | + ss << std::scientific; |
| 147 | + ss << "Probe id = " << id << std::endl; |
| 148 | + ss << "Number of evaluations = " << number_of_evaluations() << std::endl; |
| 149 | + ss << "Coordinates:" << std::endl; |
| 150 | + ss << _x[0] << " " << _x[1] << " " << _x[2] << std::endl; |
| 151 | + ss << "Values for component " << i << std::endl; |
| 152 | + for (std::size_t j = 0; j < _probes[i].size(); j++) |
| 153 | + ss << _probes[i][j] << std::endl; |
| 154 | + ss << std::endl; |
| 155 | + cout << ss.str() << endl; |
| 156 | + fp << ss.str(); |
| 157 | +} |
| 158 | +void Probe::dump(std::string filename, std::size_t id) |
| 159 | +{ |
| 160 | + //std::ofstream fp(filename.c_str(), std::ios_base::app); |
| 161 | + std::ofstream fp(filename.c_str(), std::ios_base::out); |
| 162 | + std::ostringstream ss; |
| 163 | + ss << std::scientific; |
| 164 | + ss << "Probe id = " << id << std::endl; |
| 165 | + ss << "Number of evaluations = " << number_of_evaluations() << std::endl; |
| 166 | + ss << "Coordinates:" << std::endl; |
| 167 | + ss << _x[0] << " " << _x[1] << " " << _x[2] << std::endl; |
| 168 | + ss << "Values for all components:" << std::endl; |
| 169 | + std::size_t N = _probes[0].size(); |
| 170 | + for (std::size_t j = 0; j < N; j++) |
| 171 | + { |
| 172 | + for (std::size_t i = 0; i < _value_size_loc; i++) |
| 173 | + ss << _probes[i][j] << " " ; |
| 174 | + ss << std::endl; |
| 175 | + } |
| 176 | + cout << ss.str() << endl; |
| 177 | + fp << ss.str(); |
| 178 | +} |
| 179 | +// |
| 180 | +void Probe::restart_probe(const Array<double>& u) |
| 181 | +{ |
| 182 | + for (std::size_t j = 0; j < _value_size_loc; j++) |
| 183 | + _probes[j].push_back(u[j]); |
| 184 | +} |
| 185 | + |
| 186 | + |
0 commit comments