DOFVectorBase.inc.hpp 4.32 KB
Newer Older
1
2
#pragma once

3
4
5
6
#include <cstdint>
#include <fstream>
#include <functional>

7
8
#include <amdis/Assembler.hpp>
#include <amdis/LocalOperator.hpp>
9
#include <amdis/typetree/Traversal.hpp>
10
#include <amdis/utility/AssembleOperators.hpp>
11
12
13

namespace AMDiS {

14
15
template <class GB, class B>
void DOFVectorBase<GB,B>::
16
17
18
19
20
21
22
23
24
25
26
init(bool asmVector)
{
  backend_.resize(size());
  if (asmVector)
    backend_.set(0);

  auto const localSize = basis_->localView().maxSize();
  elementVector_.resize(localSize);
}


27
28
template <class GB, class B>
void DOFVectorBase<GB,B>::
29
30
finish(bool /*asmVector*/)
{}
31
32


33
34
template <class GB, class B>
void DOFVectorBase<GB,B>::
35
36
37
38
39
40
41
42
43
44
45
insert(LocalView const& localView, ElementVector const& elementVector)
{
  for (size_type i = 0; i < localView.size(); ++i) {
    if (std::abs(elementVector[i]) > threshold<double>) {
      size_type const idx = flatMultiIndex(localView.index(i));
      backend_[idx] += elementVector[i];
    }
  }
}


46
47
48
49
template <class GB, class B>
  template <class ContextTag, class Expr, class TreePath>
void DOFVectorBase<GB,B>::
addOperator(ContextTag contextTag, Expr const& expr, TreePath path)
50
51
52
53
54
55
{
  static_assert( Concepts::PreTreePath<TreePath>,
      "path must be a valid treepath, or an integer/index-constant");

  auto i = child(basis_->localView().tree(), makeTreePath(path));

56
57
58
  using LocalContext = typename ContextTag::type;
  auto op = makeLocalOperator<LocalContext>(expr, basis_->gridView());
  auto localAssembler = makeUniquePtr(makeAssembler<LocalContext>(std::move(op), i));
59

60
  operators_[i].push(contextTag, std::move(localAssembler));
61
62
63
}


64
65
template <class GB, class B>
void DOFVectorBase<GB,B>::
66
67
68
69
70
71
72
assemble(LocalView const& localView)
{
  elementVector_ = 0;
  auto const& gv = basis_->gridView();
  auto const& element = localView.element();
  auto geometry = element.geometry();

73
  for_each_node(localView.tree(), [&](auto const& node, auto) {
74
    auto& rhsOp = operators_[node];
Praetorius, Simon's avatar
Praetorius, Simon committed
75
    if (rhsOp) {
76
      rhsOp.bind(element, geometry);
77
      assembleOperators(gv, element, rhsOp, makeVectorAssembler(node, elementVector_));
78
79
80
81
82
83
84
85
      rhsOp.unbind();
    }
  });

  insert(localView, elementVector_);
}


86
87
template <class GB, class B>
void DOFVectorBase<GB,B>::
88
89
90
91
92
93
94
95
assemble()
{
  auto localView = basis_->localView();

  init(true);
  for (auto const& element : elements(basis_->gridView())) {
    localView.bind(element);
    assemble(localView);
96
    localView.unbind();
97
98
99
100
  }
  finish(true);
}

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

template <class GB, class B>
void DOFVectorBase<GB,B>::
backup(std::string const& filename)
{
  std::ofstream out(filename, std::ios::binary);

  std::int64_t numElements = basis_->gridView().size(0);
  out.write((char*)&numElements, sizeof(std::int64_t));

  auto localView = basis_->localView();
  std::vector<value_type> data;
  for (auto const& element : elements(basis_->gridView()))
  {
    localView.bind(element);
    data.clear();
    for_each_leaf_node(localView.tree(), [&](auto const& node, auto) -> void {
      auto const& fe = node.finiteElement();
      std::size_t size = fe.size();

      for (std::size_t i = 0; i < size; ++i)
        data.push_back((*this)[localView.index(node.localIndex(i))]);
    });

    std::uint64_t len = data.size();
    out.write((char*)&len, sizeof(std::uint64_t));
    out.write((char*)data.data(), len*sizeof(value_type));

    localView.unbind();
  }
}

template <class GB, class B>
void DOFVectorBase<GB,B>::
restore(std::string const& filename)
{
  std::ifstream in(filename, std::ios::binary);

  std::int64_t numElements = 0;
  in.read((char*)&numElements, sizeof(std::int64_t));
  assert(numElements == basis_->gridView().size(0));

  // assume the order of element traversal is not changed
  auto localView = basis_->localView();
  std::vector<value_type> data;
  for (auto const& element : elements(basis_->gridView()))
  {
    std::uint64_t len = 0;
    in.read((char*)&len, sizeof(std::uint64_t));
    data.resize(len);

    in.read((char*)data.data(), len*sizeof(value_type));

    localView.bind(element);
    std::size_t shift = 0;
    for_each_leaf_node(localView.tree(), [&](auto const& node, auto) -> void {
      auto const& fe = node.finiteElement();
      std::size_t size = fe.size();

      assert(data.size() >= shift+size);
      for (std::size_t i = 0; i < size; ++i)
        (*this)[localView.index(node.localIndex(i))] = data[shift + i];

      shift += size;
    });

    localView.unbind();
  }
}

171
} // end namespace AMDiS