/usr/include/dolfin/mesh/MeshFunction.h is in libdolfin-dev 2017.2.0.post0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | // Copyright (C) 2006-2009 Anders Logg
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// Modified by Johan Hoffman, 2007.
// Modified by Garth N. Wells, 2010-2013
//
// First added: 2006-05-22
// Last changed: 2013-05-10
#ifndef __MESH_FUNCTION_H
#define __MESH_FUNCTION_H
#include <map>
#include <vector>
#include <memory>
#include <unordered_set>
#include <dolfin/common/Hierarchical.h>
#include <dolfin/common/MPI.h>
#include <dolfin/common/NoDeleter.h>
#include <dolfin/common/Variable.h>
#include <dolfin/log/log.h>
#include <dolfin/io/File.h>
#include "LocalMeshValueCollection.h"
#include "MeshDomains.h"
#include "MeshEntity.h"
#include "Mesh.h"
#include "MeshConnectivity.h"
namespace dolfin
{
class MeshEntity;
/// A MeshFunction is a function that can be evaluated at a set of
/// mesh entities. A MeshFunction is discrete and is only defined at
/// the set of mesh entities of a fixed topological dimension. A
/// MeshFunction may for example be used to store a global numbering
/// scheme for the entities of a (parallel) mesh, marking sub
/// domains or boolean markers for mesh refinement.
template <typename T> class MeshFunction : public Variable,
public Hierarchical<MeshFunction<T>>
{
public:
/// Create empty mesh function
MeshFunction();
/// Create empty mesh function on given mesh
///
/// @param mesh (Mesh)
/// The mesh to create mesh function on.
explicit MeshFunction(std::shared_ptr<const Mesh> mesh);
/// Create mesh function of given dimension on given mesh
///
/// @param mesh (Mesh)
/// The mesh to create mesh function on.
/// @param dim (std::size_t)
/// The mesh entity dimension for the mesh function.
MeshFunction(std::shared_ptr<const Mesh> mesh, std::size_t dim);
/// Create mesh of given dimension on given mesh and initialize
/// to a value
///
/// @param mesh (_Mesh_)
/// The mesh to create mesh function on.
/// @param dim (std::size_t)
/// The mesh entity dimension.
/// @param value (T)
/// The value.
MeshFunction(std::shared_ptr<const Mesh> mesh, std::size_t dim,
const T& value);
/// Create function from data file (shared_ptr version)
///
/// @param mesh (_Mesh_)
/// The mesh to create mesh function on.
/// @param filename (std::string)
/// The filename to create mesh function from.
MeshFunction(std::shared_ptr<const Mesh> mesh,
const std::string filename);
/// Create function from a MeshValueCollecion (shared_ptr version)
///
/// @param mesh (_Mesh_)
/// The mesh to create mesh function on.
/// @param value_collection (_MeshValueCollection_)
/// The mesh value collection for the mesh function data.
MeshFunction(std::shared_ptr<const Mesh> mesh,
const MeshValueCollection<T>& value_collection);
/// Create function from MeshDomains
///
/// @param mesh (_Mesh_)
/// The mesh to create mesh function on.
/// @param dim (std::size_t)
/// The dimension of the MeshFunction
/// @param domains (_MeshDomains)
/// The domains from which to extract the domain markers
MeshFunction(std::shared_ptr<const Mesh> mesh,
std::size_t dim, const MeshDomains& domains);
/// Copy constructor
///
/// @param f (_MeshFunction_)
/// The object to be copied.
MeshFunction(const MeshFunction<T>& f);
/// Destructor
~MeshFunction() {}
/// Assign mesh function to other mesh function
/// Assignment operator
///
/// @param f (_MeshFunction_)
/// A _MeshFunction_ object to assign to another MeshFunction.
MeshFunction<T>& operator= (const MeshFunction<T>& f);
/// Assignment operator
///
/// @param mesh (_MeshValueCollection_)
/// A _MeshValueCollection_ object used to construct a MeshFunction.
MeshFunction<T>& operator=(const MeshValueCollection<T>& mesh);
/// Return mesh associated with mesh function
///
/// @return _Mesh_
/// The mesh.
std::shared_ptr<const Mesh> mesh() const;
/// Return topological dimension
///
/// @return std::size_t
/// The dimension.
std::size_t dim() const;
/// Return true if empty
///
/// @return bool
/// True if empty.
bool empty() const;
/// Return size (number of entities)
///
/// @return std::size_t
/// The size.
std::size_t size() const;
/// Return array of values (const. version)
///
/// return T
/// The values.
const T* values() const;
/// Return array of values
///
/// return T
/// The values.
T* values();
/// Return value at given mesh entity
///
/// @param entity (_MeshEntity_)
/// The mesh entity.
///
/// return T
/// The value at the given entity.
T& operator[] (const MeshEntity& entity);
/// Return value at given mesh entity (const version)
///
/// @param entity (_MeshEntity_)
/// The mesh entity.
///
/// @return T
/// The value at the given entity.
const T& operator[] (const MeshEntity& entity) const;
/// Return value at given index
///
/// @param index (std::size_t)
/// The index.
///
/// @return T
/// The value at the given index.
T& operator[] (std::size_t index);
/// Return value at given index (const version)
///
/// @param index (std::size_t)
/// The index.
///
/// @return T
/// The value at the given index.
const T& operator[] (std::size_t index) const;
/// Set all values to given value
/// @param value (T)
const MeshFunction<T>& operator= (const T& value);
/// Initialize mesh function for given topological dimension
///
/// @param dim (std::size_t)
/// The dimension.
void init(std::size_t dim);
/// Initialize mesh function for given topological dimension of
/// given size
///
/// @param dim (std::size_t)
/// The dimension.
/// @param size (std::size_t)
/// The size.
void init(std::size_t dim, std::size_t size);
/// Initialize mesh function for given topological dimension
///
/// @param mesh (_Mesh_)
/// The mesh.
/// @param dim (std::size_t)
/// The dimension.
void init(std::shared_ptr<const Mesh> mesh, std::size_t dim);
/// Initialize mesh function for given topological dimension of
/// given size (shared_ptr version)
///
/// @param mesh (_Mesh_)
/// The mesh.
/// @param dim (std::size_t)
/// The dimension.
/// @param size (std::size_t)
/// The size.
void init(std::shared_ptr<const Mesh> mesh, std::size_t dim,
std::size_t size);
/// Set value at given index
///
/// @param index (std::size_t)
/// The index.
/// @param value (T)
/// The value.
void set_value(std::size_t index, const T& value);
/// Compatibility function for use in SubDomains
void set_value(std::size_t index, const T& value, const Mesh& mesh)
{ set_value(index, value); }
/// Set values
///
/// @param values (std::vector<T>)
/// The values.
void set_values(const std::vector<T>& values);
/// Set all values to given value
///
/// @param value (T)
/// The value to set all values to.
void set_all(const T& value);
/// Get indices where meshfunction is equal to given value
///
/// *Arguments*
/// value (T)
/// The value.
/// *Returns*
/// std::vector<T>
/// The indices.
std::vector<std::size_t> where_equal(T value);
/// Return informal string representation (pretty-print)
///
/// @param verbose (bool)
/// Flag to turn on additional output.
///
/// @return std::string
/// An informal representation.
std::string str(bool verbose) const;
private:
// Values at the set of mesh entities. We don't use a
// std::vector<T> here because it has trouble with bool, which C++
// specialises.
std::unique_ptr<T[]> _values;
// The mesh
std::shared_ptr<const Mesh> _mesh;
// Topological dimension
std::size_t _dim;
// Number of mesh entities
std::size_t _size;
};
template<> std::string MeshFunction<double>::str(bool verbose) const;
template<> std::string MeshFunction<std::size_t>::str(bool verbose) const;
//---------------------------------------------------------------------------
// Implementation of MeshFunction
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>::MeshFunction() : MeshFunction(nullptr)
{
// Do nothing
}
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>::MeshFunction(std::shared_ptr<const Mesh> mesh)
: Variable("f", "unnamed MeshFunction"),
Hierarchical<MeshFunction<T>>(*this), _mesh(mesh), _dim(0), _size(0)
{
// Do nothing
}
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>::MeshFunction(std::shared_ptr<const Mesh> mesh,
std::size_t dim)
: Variable("f", "unnamed MeshFunction"),
Hierarchical<MeshFunction<T>>(*this), _mesh(mesh), _dim(0), _size(0)
{
init(dim);
}
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>::MeshFunction(std::shared_ptr<const Mesh> mesh,
std::size_t dim, const T& value)
: MeshFunction(mesh, dim)
{
set_all(value);
}
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>::MeshFunction(std::shared_ptr<const Mesh> mesh,
const std::string filename)
: Variable("f", "unnamed MeshFunction"),
Hierarchical<MeshFunction<T>>(*this), _mesh(mesh), _dim(0), _size(0)
{
File file(mesh->mpi_comm(), filename);
file >> *this;
}
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>::MeshFunction(std::shared_ptr<const Mesh> mesh,
const MeshValueCollection<T>& value_collection)
: Variable("f", "unnamed MeshFunction"),
Hierarchical<MeshFunction<T>>(*this), _mesh(mesh),
_dim(value_collection.dim()), _size(0)
{
*this = value_collection;
}
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>::MeshFunction(std::shared_ptr<const Mesh> mesh,
std::size_t dim, const MeshDomains& domains)
: Variable("f", "unnamed MeshFunction"),
Hierarchical<MeshFunction<T>>(*this), _mesh(mesh), _dim(0), _size(0)
{
dolfin_assert(_mesh);
// Initialise MeshFunction
init(dim);
// Initialise mesh
mesh->init(dim);
// Set MeshFunction with default value
set_all(std::numeric_limits<T>::max());
// Get mesh dimension
const std::size_t D = _mesh->topology().dim();
dolfin_assert(dim <= D);
// Get domain data
const std::map<std::size_t, std::size_t>& data = domains.markers(dim);
// Iterate over all values and copy into MeshFunctions
std::map<std::size_t, std::size_t>::const_iterator it;
for (it = data.begin(); it != data.end(); ++it)
{
// Get value collection entry data
const std::size_t entity_index = it->first;
const T value = it->second;
dolfin_assert(entity_index < _size);
_values[entity_index] = value;
}
}
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>::MeshFunction(const MeshFunction<T>& f) :
Variable("f", "unnamed MeshFunction"),
Hierarchical<MeshFunction<T>>(*this), _dim(0), _size(0)
{
*this = f;
}
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>& MeshFunction<T>::operator= (const MeshFunction<T>& f)
{
if (_size != f._size)
_values.reset(new T[f._size]);
_mesh = f._mesh;
_dim = f._dim;
_size = f._size;
std::copy(f._values.get(), f._values.get() + _size, _values.get());
Hierarchical<MeshFunction<T>>::operator=(f);
return *this;
}
//---------------------------------------------------------------------------
template <typename T>
MeshFunction<T>& MeshFunction<T>::operator=(const MeshValueCollection<T>& mesh_value_collection)
{
_dim = mesh_value_collection.dim();
init(_dim);
dolfin_assert(_mesh);
// Get mesh connectivity D --> d
const std::size_t d = _dim;
const std::size_t D = _mesh->topology().dim();
dolfin_assert(d <= D);
// Generate connectivity if it does not exist
_mesh->init(D, d);
const MeshConnectivity& connectivity = _mesh->topology()(D, d);
dolfin_assert(!connectivity.empty());
// Set MeshFunction with default value
set_all(std::numeric_limits<T>::max());
// Iterate over all values
std::unordered_set<std::size_t> entities_values_set;
typename std::map<std::pair<std::size_t, std::size_t>, T>::const_iterator it;
const std::map<std::pair<std::size_t, std::size_t>, T>& values
= mesh_value_collection.values();
for (it = values.begin(); it != values.end(); ++it)
{
// Get value collection entry data
const std::size_t cell_index = it->first.first;
const std::size_t local_entity = it->first.second;
const T value = it->second;
std::size_t entity_index = 0;
if (d != D)
{
// Get global (local to to process) entity index
dolfin_assert(cell_index < _mesh->num_cells());
entity_index = connectivity(cell_index)[local_entity];
}
else
{
entity_index = cell_index;
dolfin_assert(local_entity == 0);
}
// Set value for entity
dolfin_assert(entity_index < _size);
_values[entity_index] = value;
// Add entity index to set (used to check that all values are set)
entities_values_set.insert(entity_index);
}
// Check that all values have been set, if not issue a debug message
if (entities_values_set.size() != _size)
dolfin_debug("Mesh value collection does not contain all values for all entities");
return *this;
}
//---------------------------------------------------------------------------
template <typename T>
std::shared_ptr<const Mesh> MeshFunction<T>::mesh() const
{
dolfin_assert(_mesh);
return _mesh;
}
//---------------------------------------------------------------------------
template <typename T>
std::size_t MeshFunction<T>::dim() const
{
return _dim;
}
//---------------------------------------------------------------------------
template <typename T>
bool MeshFunction<T>::empty() const
{
return _size == 0;
}
//---------------------------------------------------------------------------
template <typename T>
std::size_t MeshFunction<T>::size() const
{
return _size;
}
//---------------------------------------------------------------------------
template <typename T>
const T* MeshFunction<T>::values() const
{
return _values.get();
}
//---------------------------------------------------------------------------
template <typename T>
T* MeshFunction<T>::values()
{
return _values.get();
}
//---------------------------------------------------------------------------
template <typename T>
T& MeshFunction<T>::operator[] (const MeshEntity& entity)
{
dolfin_assert(_values);
dolfin_assert(&entity.mesh() == _mesh.get());
dolfin_assert(entity.dim() == _dim);
dolfin_assert(entity.index() < _size);
return _values[entity.index()];
}
//---------------------------------------------------------------------------
template <typename T>
const T& MeshFunction<T>::operator[] (const MeshEntity& entity) const
{
dolfin_assert(_values);
dolfin_assert(&entity.mesh() == _mesh.get());
dolfin_assert(entity.dim() == _dim);
dolfin_assert(entity.index() < _size);
return _values[entity.index()];
}
//---------------------------------------------------------------------------
template <typename T>
T& MeshFunction<T>::operator[] (std::size_t index)
{
dolfin_assert(_values);
dolfin_assert(index < _size);
return _values[index];
}
//---------------------------------------------------------------------------
template <typename T>
const T& MeshFunction<T>::operator[] (std::size_t index) const
{
dolfin_assert(_values);
dolfin_assert(index < _size);
return _values[index];
}
//---------------------------------------------------------------------------
template <typename T>
const MeshFunction<T>& MeshFunction<T>::operator= (const T& value)
{
set_all(value);
//Hierarchical<MeshFunction<T>>::operator=(value);
return *this;
}
//---------------------------------------------------------------------------
template <typename T>
void MeshFunction<T>::init(std::size_t dim)
{
if (!_mesh)
{
dolfin_error("MeshFunction.h",
"initialize mesh function",
"Mesh has not been specified for mesh function");
}
_mesh->init(dim);
init(_mesh, dim, _mesh->num_entities(dim));
}
//---------------------------------------------------------------------------
template <typename T>
void MeshFunction<T>::init(std::size_t dim, std::size_t size)
{
if (!_mesh)
{
dolfin_error("MeshFunction.h",
"initialize mesh function",
"Mesh has not been specified for mesh function");
}
_mesh->init(dim);
init(_mesh, dim, size);
}
//---------------------------------------------------------------------------
template <typename T>
void MeshFunction<T>::init(std::shared_ptr<const Mesh> mesh,
std::size_t dim)
{
dolfin_assert(mesh);
mesh->init(dim);
init(mesh, dim, mesh->num_entities(dim));
}
//---------------------------------------------------------------------------
template <typename T>
void MeshFunction<T>::init(std::shared_ptr<const Mesh> mesh,
std::size_t dim, std::size_t size)
{
dolfin_assert(mesh);
// Initialize mesh for entities of given dimension
mesh->init(dim);
dolfin_assert(mesh->num_entities(dim) == size);
// Initialize data
if (_size != size)
_values.reset(new T[size]);
_mesh = mesh;
_dim = dim;
_size = size;
}
//---------------------------------------------------------------------------
template <typename T>
void MeshFunction<T>::set_value(std::size_t index, const T& value)
{
dolfin_assert(_values);
dolfin_assert(index < _size);
_values[index] = value;
}
//---------------------------------------------------------------------------
template <typename T>
void MeshFunction<T>::set_values(const std::vector<T>& values)
{
dolfin_assert(_values);
dolfin_assert(_size == values.size());
std::copy(values.begin(), values.end(), _values.get());
}
//---------------------------------------------------------------------------
template <typename T>
void MeshFunction<T>::set_all(const T& value)
{
dolfin_assert(_values);
std::fill(_values.get(), _values.get() + _size, value);
}
//---------------------------------------------------------------------------
template <typename T>
std::vector<std::size_t> MeshFunction<T>::where_equal(T value)
{
dolfin_assert(_values);
std::size_t n = std::count(_values.get(), _values.get() + _size, value);
std::vector<std::size_t> indices;
indices.reserve(n);
for (std::size_t i = 0; i < size(); ++i)
{
if (_values[i] == value)
indices.push_back(i);
}
return indices;
}
//---------------------------------------------------------------------------
template <typename T>
std::string MeshFunction<T>::str(bool verbose) const
{
std::stringstream s;
if (verbose)
{
s << str(false) << std::endl << std::endl;
warning("Verbose output of MeshFunctions must be implemented manually.");
// This has been disabled as it severely restricts the ease with which
// templated MeshFunctions can be used, e.g. it is not possible to
// template over std::vector.
//for (std::size_t i = 0; i < _size; i++)
// s << " (" << _dim << ", " << i << "): " << _values[i] << std::endl;
}
else
{
s << "<MeshFunction of topological dimension " << dim()
<< " containing " << size() << " values>";
}
return s.str();
}
//---------------------------------------------------------------------------
}
#endif
|