Commit 1ac8416d authored by Thomas Witkowski's avatar Thomas Witkowski
Browse files

Removed SolutionDataStorage, was never used.

parent 797e54bc
......@@ -100,7 +100,6 @@
#include "RefinementManager2d.h"
#include "RefinementManager3d.h"
#include "RobinBC.h"
#include "SolutionDataStorage.h"
#include "SurfaceOperator.h"
#include "SurfaceQuadrature.h"
#include "SystemVector.h"
......
// ============================================================================
// == ==
// == AMDiS - Adaptive multidimensional simulations ==
// == ==
// == http://www.amdis-fem.org ==
// == ==
// ============================================================================
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.
/** \file SolutionDataStorage.h */
#ifndef AMDIS_SOLUTION_DATA_STORAGE_H
#define AMDIS_SOLUTION_DATA_STORAGE_H
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include "DOFVector.h"
#include "SystemVector.h"
#include "Initfile.h"
namespace AMDiS {
template<typename T>
struct SolutionHelper
{
typedef FiniteElemSpace* type;
};
template <>
struct SolutionHelper<SystemVector>
{
typedef std::vector<FiniteElemSpace*> type;
};
/** \brief
* Class to store solution data for one timestep.
*
* Within this class, the solution data for one timestep is stored. The
* type of the solution data is the typename of the class, i.e., either
* DOFVector or SystemVector. The class is resonsible for serialization
* and deserialization of the solution.
*
* The class is used only from class \ref SolutionDataStorage, and must
* not be used by the user in other context.
*/
template<typename T>
class SolutionData
{
public:
/** \brief
* The constructor requires already the data. So no empty data class
* can be created.
*/
SolutionData(T* sol,
typename SolutionHelper<T>::type fes,
double ts,
int i)
: solution(sol),
feSpace(fes),
timestamp(ts),
id(i),
serialized(false),
filename("")
{}
/** \brief
* The destructor either deletes the data files or deletes the pointers
* to the data.
*/
~SolutionData()
{
if (!serialized) {
//delete solution;
//deleteFeSpace(feSpace);
}
}
/// Serialize the data to a file.
void serialize(std::string fn)
{
std::ofstream out(fn.c_str());
serializeFeSpace(out, feSpace);
solution->serialize(out);
out.close();
serialized = true;
filename = fn;
delete solution;
deleteFeSpace(feSpace);
}
/// Deserialize the data from file.
void deserialize()
{
std::string feName = "";
int dim = 0;
int degree = 0;
std::ifstream in(filename.c_str());
deserializeFeSpace(in, feSpace);
solution = new T("", feSpace, 0);
solution->deserialize(in);
in.close();
serialized = false;
filename = "";
}
/** \brief
* Returns the data. In the case the data was serialized to the disk,
* it will be first deserialized from the corresponding file.
*/
void getData(T** ptrSolution, double *ptrTimestamp)
{
if (serialized)
deserialize();
*ptrSolution = solution;
*ptrTimestamp = timestamp;
}
/// Returns \ref serialized.
bool isSerialized()
{
return serialized;
}
protected:
/// Serialize a fe space to a file.
void serializeFeSpace(std::ofstream &out, FiniteElemSpace* fe)
{
// Write the mesh to file.
out << fe->getMesh()->getName() << "\n";
int dim = fe->getMesh()->getDim();
SerUtil::serialize(out, dim);
fe->getMesh()->serialize(out);
// Then, write all other fe space data to file.
dim = fe->getBasisFcts()->getDim();
int degree = fe->getBasisFcts()->getDegree();
out << ((fe->getName() != "") ? fe->getName() : "noname") << "\n";
SerUtil::serialize(out, dim);
SerUtil::serialize(out, degree);
}
/** \brief
* Serialize a vector of fe spaces to a file.
*
* Takes special care about multiple fe space pointers, pointing to the
* same fe space. In this case, only the first fe space is serialized
* completely.
*/
void serializeFeSpace(std::ofstream &out, std::vector<FiniteElemSpace*>& fe)
{
int size = fe.size();
SerUtil::serialize(out, size);
for (int i = 0; i < size; i++) {
// Check, if the pointer points to an fe space serialized before.
int found = -1;
for (int j = 0; j < i; j++) {
if (fe[j] == fe[i]) {
found = j;
break;
}
}
SerUtil::serialize(out, found);
// Only in case this fe space was not serialized before, write it to the file.
if (found == -1)
serializeFeSpace(out, fe[i]);
}
}
void deserializeFeSpace(std::ifstream &in, FiniteElemSpace** fe)
{
std::string name = "";
int dim = 0;
int degree = 0;
in >> name;
in.get();
SerUtil::deserialize(in, dim);
Mesh *mesh = new Mesh(name, dim);
mesh->deserialize(in);
in >> name;
in.get();
SerUtil::deserialize(in, dim);
SerUtil::deserialize(in, degree);
*fe = FiniteElemSpace::provideFeSpace(NULL,
Lagrange::getLagrange(dim, degree),
mesh,
name);
(*fe)->setAdmin(const_cast<DOFAdmin*>(&(mesh->getDofAdmin(0))));
}
void deserializeFeSpace(std::ifstream &in, std::vector<FiniteElemSpace*>& fe)
{
int size;
SerUtil::deserialize(in, size);
fe.resize(size);
for (int i = 0; i < size; i++) {
int found;
SerUtil::deserialize(in, found);
if (found == -1)
deserializeFeSpace(in, &(fe[i]));
else
fe[i] = fe[found];
}
}
/// Remove one fe space from memory.
void deleteFeSpace(FiniteElemSpace* fe)
{
if (fe) {
delete fe->getMesh();
delete fe;
fe = NULL;
}
}
/// Remove a vector of fe spaces from memory.
void deleteFeSpace(std::vector<FiniteElemSpace*>& fe)
{
// Stores all pointers to fe spaces, that were deleted. This should
// avoid double deletion of pointer.
std::map<FiniteElemSpace*, bool> deletedFeSpaces;
for (int i = 0; i < static_cast<int>(fe.size()); i++) {
if (deletedFeSpaces.find(fe[i]) == deletedFeSpaces.end()) {
deleteFeSpace(fe[i]);
deletedFeSpaces[fe[i]] = true;
}
}
fe.clear();
}
public:
/// Here, all the solutions (i.e. either DOFVectors or SystemVectors) are stored.
T* solution;
/** \brief
* Stores to every solution its FE Space. If \ref fixedFeSpace is set
* to true, only one entry exists. This is than the FE Space for all
* solutions.
*/
typename SolutionHelper<T>::type feSpace;
/** \brief
* Stores to every solutions the timestamp at which the solution was
* created in the adaption loop.
*/
double timestamp;
/** \brief
* If true, the solution data is serialied to disk and the pointers
* are not valid.
*/
bool serialized;
/// If solution data is serialied, here the corresponding filename is stored.
std::string filename;
///
int id;
};
/** \brief
* Storage for solution datas with optimization process.
*
* This class may be used to store solutions within an optimization process.
* Within the forward step, solutions are stored within this data storage, and
* are then used within the backward step. The storage supports serialization
* and deserialization of solutions (and the corresponding fe spaces) to save
* memory.
*
* The template typename must be the type of the solution data to be stored, i.e,
* either DOFVector<double> or SystemVector.
*/
template<typename T>
class SolutionDataStorage
{
public:
/** \brief
* Constructor. The parameter is a string that will be used to search for
* parameters of the storage within the init file.
*/
SolutionDataStorage(std::string name);
/// Destructor. Deletes all stored solutions and deletes all serialized files.
~SolutionDataStorage();
/// Add a solution and its timestamp to the storage.
void push(T *solution, double timestamp);
/// Get solution data from storage. Returns true, if solution data is valid.
bool pop(T **solution, double *timestep);
/// Delete all pointers and empties all internal vectors.
void clear();
/** \brief
* Return for a given solution number the corresponding fe Space. If the
* the fe Space is fixed, the fe Space for all solutions is stored at
* position 0.
*/
typename SolutionHelper<T>::type getFeSpace(int i = 0)
{
return solutions[i]->feSpace;
}
/// Returns \ref poped.
bool isPoped()
{
return poped;
}
/// Add a new container to the storage.
void addContainer(std::string name);
/// Add value to a container.
void push(std::string name, WorldVector<double> value);
/// Get value from a container.
void pop(std::string name, WorldVector<double> &value);
/// Reset, , a container/
void reset(std::string name);
/// Clear, i.e. delete all values, a container.
void clear(std::string name);
protected:
///
int addMemoryUsage(FiniteElemSpace* feSpace)
{
memoryUsage += feSpace->getMesh()->calcMemoryUsage();
return memoryUsage;
}
///
int addMemoryUsage(std::vector<FiniteElemSpace*> feSpaces) {
// Is used to determine equal meshes for different components.
std::vector<Mesh*> meshes;
for (int i = 0; i < static_cast<int>(feSpaces.size()); i++) {
if (find(meshes.begin(), meshes.end(), feSpaces[i]->getMesh()) != meshes.end()) {
memoryUsage += feSpaces[i]->getMesh()->calcMemoryUsage();
meshes.push_back(feSpaces[i]->getMesh());
}
}
return memoryUsage;
}
/// Number of MBytes of memory that can be used for solution storage.
int maxMemoryUsage;
/// If true, it is allowed to write solutions also to disk.
bool useDisk;
/// Directory, where solutions can be written temporarly.
std::string writeDirectory;
/// Position of the latest valid solution.
int lastPos;
/// If true, the last operation on the data storage was pop.
bool poped;
/// Counts the memory usage of all solutions stored in memory.
int memoryUsage;
/** \brief
* Storage for the solutions, i.e., the DOFVector, the FeSpace and the
* corresponding timestep.
*/
std::vector<SolutionData<T>* > solutions;
///
std::map<std::string, std::vector<WorldVector<double > > > containers;
///
std::map<std::string, int> containersPos;
///
int idcounter;
};
}
#include "SolutionDataStorage.hh"
#endif // AMDIS_SOLUTION_DATA_STORAGE_H
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.
#include <iostream>
#include <sstream>
namespace AMDiS {
template<typename T>
SolutionDataStorage<T>::SolutionDataStorage(std::string name)
: maxMemoryUsage(100),
useDisk(true),
writeDirectory(""),
lastPos(-1),
poped(false),
memoryUsage(0),
idcounter(0)
{
solutions.clear();
int tmp = 0;
Parameters::get(name + "->memory usage", maxMemoryUsage);
Parameters::get(name + "->use disk", tmp);
useDisk = (tmp == 0) ? false : true;
Parameters::get(name + "->directory", writeDirectory);
}
template<typename T>
SolutionDataStorage<T>::~SolutionDataStorage()
{
clear();
}
template<typename T>
void SolutionDataStorage<T>::push(T *solution,
double timestamp)
{
FUNCNAME("SolutionDataStorage<T>::push()");
// If pop was the last operation, cleanup and reset the data storage.
if (poped) {
clear();
poped = false;
}
std::vector<FiniteElemSpace*> feSpace(solution->getFeSpaces().size());
for (int i = 0; i < feSpace.size(); i++) {
int found = -1;
for (int j = 0; j < i; j++) {
if (solution->getFeSpace(j) == solution->getFeSpace(i)) {
found = j;
break;
}
}
if (found == -1) {
feSpace[i] = new FiniteElemSpace();
*(feSpace[i]) = *(solution->getFeSpace(i));
memoryUsage += feSpace[i]->calcMemoryUsage();
} else {
feSpace[i] = feSpace[found];
}
}
SystemVector *vec = new SystemVector("tmp", feSpace, solution->getFeSpaces().size());
vec->createNewDOFVectors("tmp");
vec->setCoarsenOperation(COARSE_INTERPOL);
vec->interpol(solution, 1.0);
memoryUsage += vec->calcMemoryUsage();
solutions.push_back(new SolutionData<T>(vec, feSpace, timestamp, idcounter++));
lastPos++;
/*
if (memoryUsage / (1024 * 1024) > maxMemoryUsage) {
for (int i = 0; i < solutions.size() - 1; i++) {
if (solutions[i]->isSerialized() == false) {
std::ostringstream oss;
oss << writeDirectory << "/solutiondata_" << i << ".ser";
solutions[i]->serialize(oss.str());
}
}
}
*/
}
template<typename T>
bool SolutionDataStorage<T>::pop(T **solution,
double *timestamp)
{
if (lastPos < 0) {
return false;
}
solutions[lastPos]->getData(solution, timestamp);
lastPos--;
poped = true;
return true;
}
template<typename T>
void SolutionDataStorage<T>::clear()
{
for (int i = 0; i < static_cast<int>(solutions.size()); i++) {
delete solutions[i];
}
solutions.clear();
lastPos = -1;
memoryUsage = 0;
}
template<typename T>
void SolutionDataStorage<T>::addContainer(std::string name)
{
std::vector<WorldVector<double> > emptyVec;
containers.insert(std::pair<std::string, std::vector<WorldVector<double> > >(name, emptyVec) );
containersPos.insert(std::pair<std::string, int>(name, -1));
}
template<typename T>
void SolutionDataStorage<T>::push(std::string name, WorldVector<double> value)
{
containers[name].push_back(value);
containersPos[name] = containers[name].size() - 1;
}
template<typename T>
void SolutionDataStorage<T>::pop(std::string name, WorldVector<double> &value)
{
value = containers[name][containersPos[name]];
containersPos[name]--;
}
template<typename T>
void SolutionDataStorage<T>::reset(std::string name)
{
containersPos[name] = containers[name].size() - 1;
}
template<typename T>
void SolutionDataStorage<T>::clear(std::string name)
{
containers[name].clear();
containersPos[name] = -1;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment