Loading...
Searching...
No Matches
MeshValueCollection.h
// Copyright (C) 2011-2013 Anders Logg and Garth N. Wells
//
// 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 Chris Richardson, 2013.
//
// First added: 2006-08-30
// Last changed: 2013-05-22
#ifndef __MESH_VALUE_COLLECTION_H
#define __MESH_VALUE_COLLECTION_H
#include <map>
#include <utility>
#include <memory>
#include <dolfin/common/NoDeleter.h>
#include <dolfin/common/Variable.h>
#include <dolfin/log/log.h>
#include "Cell.h"
#include "Mesh.h"
#include "MeshEntity.h"
#include "MeshFunction.h"
namespace dolfin
{
template <typename T>
class MeshValueCollection : public Variable
{
public:
MeshValueCollection();
explicit MeshValueCollection(std::shared_ptr<const Mesh> mesh);
explicit MeshValueCollection(const MeshFunction<T>& mesh_function);
MeshValueCollection(std::shared_ptr<const Mesh> mesh, std::size_t dim);
MeshValueCollection(std::shared_ptr<const Mesh> mesh, const std::string filename);
~MeshValueCollection() {}
MeshValueCollection<T>& operator=(const MeshFunction<T>& mesh_function);
MeshValueCollection<T>&
operator=(const MeshValueCollection<T>& mesh_value_collection);
void init(std::shared_ptr<const Mesh> mesh, std::size_t dim);
void init(std::size_t dim);
std::size_t dim() const;
std::shared_ptr<const Mesh> mesh() const;
bool empty() const;
std::size_t size() const;
bool set_value(std::size_t cell_index, std::size_t local_entity,
const T& value);
bool set_value(std::size_t entity_index, const T& value);
T get_value(std::size_t cell_index, std::size_t local_entity);
std::map<std::pair<std::size_t, std::size_t>, T>& values();
const std::map<std::pair<std::size_t, std::size_t>, T>& values() const;
void clear();
std::string str(bool verbose) const;
private:
// Associated mesh
std::shared_ptr<const Mesh> _mesh;
// Topological dimension
int _dim;
// The values
std::map<std::pair<std::size_t, std::size_t>, T> _values;
};
//---------------------------------------------------------------------------
// Implementation of MeshValueCollection
//---------------------------------------------------------------------------
template <typename T>
MeshValueCollection<T>::MeshValueCollection()
: Variable("m", "unnamed MeshValueCollection"), _dim(-1)
{
// Do nothing
}
//---------------------------------------------------------------------------
template <typename T>
MeshValueCollection<T>::MeshValueCollection(std::shared_ptr<const Mesh>
mesh) : _mesh(mesh), _dim(-1)
{
// Do nothing
}
//---------------------------------------------------------------------------
template <typename T>
MeshValueCollection<T>::MeshValueCollection(std::shared_ptr<const Mesh>
mesh, std::size_t dim)
: Variable("m", "unnamed MeshValueCollection"), _mesh(mesh), _dim(dim)
{
// Do nothing
}
//---------------------------------------------------------------------------
template <typename T>
MeshValueCollection<T>::MeshValueCollection(const MeshFunction<T>&
mesh_function)
: Variable("m", "unnamed MeshValueCollection"), _mesh(mesh_function.mesh()),
_dim(mesh_function.dim())
{
dolfin_assert(_mesh);
const std::size_t D = _mesh->topology().dim();
// Handle cells as a special case
if ((int) D == _dim)
{
for (std::size_t cell_index = 0; cell_index < mesh_function.size();
++cell_index)
{
const std::pair<std::size_t, std::size_t> key(cell_index, 0);
_values.insert({key, mesh_function[cell_index]});
}
}
else
{
_mesh->init(_dim, D);
const MeshConnectivity& connectivity = _mesh->topology()(_dim, D);
dolfin_assert(!connectivity.empty());
for (std::size_t entity_index = 0; entity_index < mesh_function.size();
++entity_index)
{
// Find the cell
dolfin_assert(connectivity.size(entity_index) > 0);
const MeshEntity entity(*_mesh, _dim, entity_index);
for (std::size_t i = 0; i < entity.num_entities(D) ; ++i)
{
// Create cell
const Cell cell(*_mesh, connectivity(entity_index)[i]);
// Find the local entity index
const std::size_t local_entity = cell.index(entity);
// Insert into map
const std::pair<std::size_t, std::size_t> key(cell.index(),
local_entity);
_values.insert({key, mesh_function[entity_index]});
}
}
}
}
//---------------------------------------------------------------------------
template <typename T>
MeshValueCollection<T>::MeshValueCollection(std::shared_ptr<const Mesh> mesh,
const std::string filename)
: Variable("m", "unnamed MeshValueCollection"),
_mesh(mesh), _dim(-1)
{
File file(filename);
file >> *this;
dolfin_assert(_dim > -1);
}
//---------------------------------------------------------------------------
template <typename T>
MeshValueCollection<T>&
MeshValueCollection<T>::operator=(const MeshFunction<T>& mesh_function)
{
_mesh = mesh_function.mesh();
_dim = mesh_function.dim();
dolfin_assert(_mesh);
const std::size_t D = _mesh->topology().dim();
// FIXME: Use iterators
// Handle cells as a special case
if ((int) D == _dim)
{
for (std::size_t cell_index = 0; cell_index < mesh_function.size();
++cell_index)
{
const std::pair<std::size_t, std::size_t> key(cell_index, 0);
_values.insert({key, mesh_function[cell_index]});
}
}
else
{
_mesh->init(_dim, D);
const MeshConnectivity& connectivity = _mesh->topology()(_dim, D);
dolfin_assert(!connectivity.empty());
for (std::size_t entity_index = 0; entity_index < mesh_function.size();
++entity_index)
{
// Find the cell
dolfin_assert(connectivity.size(entity_index) > 0);
const MeshEntity entity(*_mesh, _dim, entity_index);
for (std::size_t i = 0; i < entity.num_entities(D) ; ++i)
{
// Create cell
const Cell cell(*_mesh, connectivity(entity_index)[i]);
// Find the local entity index
const std::size_t local_entity = cell.index(entity);
// Insert into map
const std::pair<std::size_t, std::size_t> key(cell.index(),
local_entity);
_values.insert({key, mesh_function[entity_index]});
}
}
}
return *this;
}
//---------------------------------------------------------------------------
template <typename T>
MeshValueCollection<T>&
MeshValueCollection<T>::operator=(const MeshValueCollection<T>&
mesh_value_collection)
{
_mesh = mesh_value_collection._mesh;
_dim = mesh_value_collection.dim();
_values = mesh_value_collection.values();
return *this;
}
//---------------------------------------------------------------------------
template <typename T>
void MeshValueCollection<T>::init(std::shared_ptr<const Mesh> mesh,
std::size_t dim)
{
mesh->init(dim);
_mesh = mesh;
_dim = dim;
_values.clear();
}
//---------------------------------------------------------------------------
template <typename T>
void MeshValueCollection<T>::init(std::size_t dim)
{
dolfin_assert(_mesh);
dolfin_assert(_dim < 0);
_dim = dim;
}
//---------------------------------------------------------------------------
template <typename T>
std::size_t MeshValueCollection<T>::dim() const
{
dolfin_assert(_dim >= 0);
return _dim;
}
//---------------------------------------------------------------------------
template <typename T>
bool MeshValueCollection<T>::empty() const
{
return _values.empty();
}
//---------------------------------------------------------------------------
template <typename T>
std::size_t MeshValueCollection<T>::size() const
{
return _values.size();
}
//---------------------------------------------------------------------------
template <typename T>
std::shared_ptr<const Mesh> MeshValueCollection<T>::mesh() const
{
dolfin_assert(_mesh);
return _mesh;
}
//---------------------------------------------------------------------------
template <typename T>
bool MeshValueCollection<T>::set_value(std::size_t cell_index,
std::size_t local_entity,
const T& value)
{
dolfin_assert(_dim >= 0);
if (!_mesh)
{
dolfin_error("MeshValueCollection.h",
"set value",
"A mesh has not been associated with this MeshValueCollection");
}
const std::pair<std::size_t, std::size_t> pos(cell_index, local_entity);
std::pair<typename std::map<std::pair<std::size_t, std::size_t>, T>::iterator, bool>
it = _values.insert({pos, value});
// If an item with same key already exists the value has not been
// set and we need to update it
if (!it.second)
it.first->second = value;
return it.second;
}
//---------------------------------------------------------------------------
template <typename T>
bool MeshValueCollection<T>::set_value(std::size_t entity_index,
const T& value)
{
if (!_mesh)
{
dolfin_error("MeshValueCollection.h",
"set value",
"A mesh has not been associated with this MeshValueCollection");
}
dolfin_assert(_dim >= 0);
// Special case when d = D
const std::size_t D = _mesh->topology().dim();
if (_dim == (int) D)
{
// Set local entity index to zero when we mark a cell
const std::pair<std::size_t, std::size_t> pos(entity_index, 0);
std::pair<typename std::map<std::pair<std::size_t,
std::size_t>, T>::iterator, bool> it;
it = _values.insert({pos, value});
// If an item with same key already exists the value has not been
// set and we need to update it
if (!it.second)
it.first->second = value;
return it.second;
}
// Get mesh connectivity d --> D
_mesh->init(_dim, D);
const MeshConnectivity& connectivity = _mesh->topology()(_dim, D);
// Find the cell
dolfin_assert(!connectivity.empty());
dolfin_assert(connectivity.size(entity_index) > 0);
const MeshEntity entity(*_mesh, _dim, entity_index);
const Cell cell(*_mesh, connectivity(entity_index)[0]); // choose first
// Find the local entity index
const std::size_t local_entity = cell.index(entity);
// Add value
const std::pair<std::size_t, std::size_t> pos(cell.index(), local_entity);
std::pair<typename std::map<std::pair<std::size_t,
std::size_t>, T>::iterator, bool> it;
it = _values.insert({pos, value});
// If an item with same key already exists the value has not been
// set and we need to update it
if (!it.second)
it.first->second = value;
return it.second;
}
//---------------------------------------------------------------------------
template <typename T>
T MeshValueCollection<T>::get_value(std::size_t cell_index,
std::size_t local_entity)
{
dolfin_assert(_dim >= 0);
const std::pair<std::size_t, std::size_t> pos(cell_index, local_entity);
const typename std::map<std::pair<std::size_t,
std::size_t>, T>::const_iterator
it = _values.find(pos);
if (it == _values.end())
{
dolfin_error("MeshValueCollection.h",
"extract value",
"No value stored for cell index: %d and local index: %d",
cell_index, local_entity);
}
return it->second;
}
//---------------------------------------------------------------------------
template <typename T>
std::map<std::pair<std::size_t, std::size_t>, T>&
MeshValueCollection<T>::values()
{
return _values;
}
//---------------------------------------------------------------------------
template <typename T>
const std::map<std::pair<std::size_t, std::size_t>, T>&
MeshValueCollection<T>::values() const
{
return _values;
}
//---------------------------------------------------------------------------
template <typename T>
void MeshValueCollection<T>::clear()
{
_values.clear();
}
//---------------------------------------------------------------------------
template <typename T>
std::string MeshValueCollection<T>::str(bool verbose) const
{
std::stringstream s;
if (verbose)
{
s << str(false) << std::endl << std::endl;
warning("Verbose output of MeshValueCollection must be implemented manually.");
}
else
{
s << "<MeshValueCollection of topological dimension " << dim()
<< " containing " << size() << " values>";
}
return s.str();
}
//---------------------------------------------------------------------------
}
#endif
dolfin::Cell
A Cell is a MeshEntity of topological codimension 0.
Definition Cell.h:43
dolfin::File
Definition File.h:46
dolfin::MeshConnectivity
Definition MeshConnectivity.h:42
dolfin::MeshConnectivity::size
std::size_t size() const
Return total number of connections.
Definition MeshConnectivity.h:62
dolfin::MeshConnectivity::empty
bool empty() const
Return true if the total number of connections is equal to zero.
Definition MeshConnectivity.h:58
dolfin::MeshEntity
Definition MeshEntity.h:43
dolfin::MeshEntity::index
std::size_t index() const
Definition MeshEntity.h:113
dolfin::MeshEntity::num_entities
std::size_t num_entities(std::size_t dim) const
Definition MeshEntity.h:140
dolfin::MeshFunction
Definition MeshFunction.h:58
dolfin::MeshFunction::mesh
std::shared_ptr< const Mesh > mesh() const
Definition MeshFunction.h:491
dolfin::MeshFunction::dim
std::size_t dim() const
Definition MeshFunction.h:498
dolfin::MeshFunction::size
std::size_t size() const
Definition MeshFunction.h:510
dolfin::MeshValueCollection
Definition MeshValueCollection.h:51
dolfin::MeshValueCollection::init
void init(std::shared_ptr< const Mesh > mesh, std::size_t dim)
Definition MeshValueCollection.h:371
dolfin::MeshValueCollection::mesh
std::shared_ptr< const Mesh > mesh() const
Definition MeshValueCollection.h:408
dolfin::MeshValueCollection::dim
std::size_t dim() const
Definition MeshValueCollection.h:389
dolfin::MeshValueCollection::empty
bool empty() const
Definition MeshValueCollection.h:396
dolfin::MeshValueCollection::get_value
T get_value(std::size_t cell_index, std::size_t local_entity)
Definition MeshValueCollection.h:498
dolfin::MeshValueCollection::~MeshValueCollection
~MeshValueCollection()
Destructor.
Definition MeshValueCollection.h:89
dolfin::MeshValueCollection::set_value
bool set_value(std::size_t cell_index, std::size_t local_entity, const T &value)
Definition MeshValueCollection.h:415
dolfin::MeshValueCollection::clear
void clear()
Clear all values.
Definition MeshValueCollection.h:534
dolfin::MeshValueCollection::MeshValueCollection
MeshValueCollection()
Definition MeshValueCollection.h:227
dolfin::MeshValueCollection::size
std::size_t size() const
Definition MeshValueCollection.h:402
dolfin::MeshValueCollection::operator=
MeshValueCollection< T > & operator=(const MeshFunction< T > &mesh_function)
Definition MeshValueCollection.h:308
dolfin::MeshValueCollection::values
std::map< std::pair< std::size_t, std::size_t >, T > & values()
Definition MeshValueCollection.h:521
dolfin::MeshValueCollection::str
std::string str(bool verbose) const
Definition MeshValueCollection.h:540
dolfin::Variable::Variable
Variable()
Create unnamed variable.
Definition Variable.cpp:31
dolfin
Definition adapt.h:30
dolfin::warning
void warning(std::string msg,...)
Print warning.
Definition log.cpp:115
dolfin::dolfin_error
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129