| 1 |
/* |
| 2 |
* Advanced Simulation Library <http://asl.org.il> |
| 3 |
* |
| 4 |
* Copyright 2015 Avtech Scientific <http://avtechscientific.com> |
| 5 |
* |
| 6 |
* |
| 7 |
* This file is part of Advanced Simulation Library (ASL). |
| 8 |
* |
| 9 |
* ASL is free software: you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU Affero General Public License as |
| 11 |
* published by the Free Software Foundation, version 3 of the License. |
| 12 |
* |
| 13 |
* ASL is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Affero General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Affero General Public License |
| 19 |
* along with ASL. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
#include "aslWriter.h" |
| 25 |
#include <acl/aclMath/aclVectorOfElementsOperations.h> |
| 26 |
#include <acl/Kernels/aclKernel.h> |
| 27 |
#include <acl/acl.h> |
| 28 |
#include <acl/DataTypes/aclArray.h> |
| 29 |
#include <aslGenerators.h> |
| 30 |
#include <acl/aclGenerators.h> |
| 31 |
#include <data/aslDataWrapper.h> |
| 32 |
#include <data/aslDataWithGhostNodes.h> |
| 33 |
|
| 34 |
using namespace acl; |
| 35 |
|
| 36 |
namespace asl |
| 37 |
{ |
| 38 |
|
| 39 |
Writer * Writer::current(NULL); |
| 40 |
|
| 41 |
Writer::Writer(const string & file_): |
| 42 |
file(file_), |
| 43 |
numOfWrites(0) |
| 44 |
{ |
| 45 |
enable(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
Writer::~Writer() |
| 50 |
{ |
| 51 |
// Deactivates this instance of Writer |
| 52 |
if (current == this) |
| 53 |
current = NULL; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
void Writer::enable() |
| 58 |
{ |
| 59 |
// Activates this instance of Writer |
| 60 |
current = this; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
void Writer::addScalars(string name, AbstractData & data) |
| 65 |
{ |
| 66 |
if ((scalarFields.size() == 0) && (vectorFields.size() == 0)) |
| 67 |
block= make_shared<Block>(data.getBlock()); |
| 68 |
|
| 69 |
// check whether the new block is compatible with the old one |
| 70 |
if (block->getSize() != data.getBlock().getSize()) |
| 71 |
errorMessage("Writer::addScalars() - attempt to add AbstractData with incompatible block size"); |
| 72 |
|
| 73 |
for (unsigned int i = 0; i < data.getDContainer().size() ; ++i) |
| 74 |
{ |
| 75 |
scalarFields.push_back(make_pair(name + "-" + numToStr(i), |
| 76 |
subVE(data.getDContainer(), i, i))); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
void Writer::addVector(string name, AbstractData & data) |
| 82 |
{ |
| 83 |
if ((scalarFields.size() == 0) && (vectorFields.size() == 0)) |
| 84 |
block= make_shared<Block>(data.getBlock()); |
| 85 |
|
| 86 |
// check whether the new block is compatible with the old one |
| 87 |
if (block->getSize() != data.getBlock().getSize()) |
| 88 |
errorMessage("Writer::addVector() - attempt to add AbstractData with incompatible block size"); |
| 89 |
|
| 90 |
vectorFields.push_back(make_pair(name, data.getDContainer())); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
void Writer::addScalars(string name, VectorOfElementsData & data) |
| 95 |
{ |
| 96 |
if ((scalarFields.size() == 0) && (vectorFields.size() == 0)) |
| 97 |
errorMessage("Writer::addScalars() - attempt to add VectorOfElementsData before any Block was defined"); |
| 98 |
|
| 99 |
// check whether the new data is compatible with the old block |
| 100 |
if (!compatibleSizes((unsigned int)productOfElements(block->getSize()), data)) |
| 101 |
errorMessage("Writer::addScalars() - attempt to add VectorOfElementsData with incompatible block size"); |
| 102 |
|
| 103 |
for (unsigned int i = 0; i < data.size() ; ++i) |
| 104 |
{ |
| 105 |
scalarFields.push_back(make_pair(name + "-" + numToStr(i), |
| 106 |
subVE(data, i, i))); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
void Writer::addVector(string name, VectorOfElementsData & data) |
| 112 |
{ |
| 113 |
if ((scalarFields.size() == 0) && (vectorFields.size() == 0)) |
| 114 |
errorMessage("Writer::addVector() - attempt to add VectorOfElementsData before any Block was defined"); |
| 115 |
|
| 116 |
// check whether the new data is compatible with the old block |
| 117 |
if (!compatibleSizes((unsigned int)productOfElements(block->getSize()), data)) |
| 118 |
errorMessage("Writer::addVector() - attempt to add VectorOfElementsData with incompatible block size"); |
| 119 |
|
| 120 |
vectorFields.push_back(make_pair(name, data)); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
void Writer::addScalars(string name, |
| 125 |
const VectorOfElements & data, |
| 126 |
Kernel & kernel, |
| 127 |
unsigned int nGhost) |
| 128 |
{ |
| 129 |
if ((scalarFields.size() == 0) && (vectorFields.size() == 0)) |
| 130 |
errorMessage("Writer::addScalars() - attempt to add VectorOfElements before any Block was defined"); |
| 131 |
|
| 132 |
if (kernel.getQueue().get() == 0) |
| 133 |
errorMessage("Writer::addScalars() - attempt to add VectorOfElements before any Queue was defined in Kernel"); |
| 134 |
|
| 135 |
if (data.size() == 0) |
| 136 |
errorMessage("Writer::addScalars() - attempt to add VectorOfElements with size 0"); |
| 137 |
|
| 138 |
auto queue(kernel.getQueue()); |
| 139 |
auto v(generateDataContainerACL_SP(offset(*block,-nGhost), |
| 140 |
acl::getElementType(data), |
| 141 |
data.size(), |
| 142 |
nGhost, |
| 143 |
queue)); |
| 144 |
acl::initData(v->getEContainer(), generateVEConstantN(data.size(), 0.)); |
| 145 |
|
| 146 |
for (unsigned int i = 0; i < data.size() ; ++i) |
| 147 |
{ |
| 148 |
scalarFields.push_back(make_pair(name + "-" + numToStr(i), |
| 149 |
subVE(v->getDContainer(), i, i))); |
| 150 |
} |
| 151 |
|
| 152 |
kernel << assignmentSafe(v->getSubContainer(), data); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
void Writer::addVector(string name, |
| 157 |
const VectorOfElements & data, |
| 158 |
Kernel & kernel, |
| 159 |
unsigned int nGhost) |
| 160 |
{ |
| 161 |
if ((scalarFields.size() == 0) && (vectorFields.size() == 0)) |
| 162 |
errorMessage("Writer::addVector() - attempt to add VectorOfElements before any Block was defined"); |
| 163 |
|
| 164 |
if (kernel.getQueue().get() == 0) |
| 165 |
errorMessage("Writer::addVector() - attempt to add VectorOfElements before any Queue was defined in Kernel"); |
| 166 |
|
| 167 |
if (data.size() == 0) |
| 168 |
errorMessage("Writer::addVector() - attempt to add VectorOfElements with size 0"); |
| 169 |
|
| 170 |
auto queue(kernel.getQueue()); |
| 171 |
auto v(generateDataContainerACL_SP(offset(*block,-nGhost), |
| 172 |
acl::getElementType(data), |
| 173 |
data.size(), |
| 174 |
nGhost, |
| 175 |
queue)); |
| 176 |
acl::initData(v->getEContainer(), generateVEConstantN(data.size(), 0.)); |
| 177 |
|
| 178 |
vectorFields.push_back(make_pair(name, v->getDContainer())); |
| 179 |
|
| 180 |
kernel << assignmentSafe(v->getSubContainer(), data); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
} // namespace asl |