OpenLB 1.7
Loading...
Searching...
No Matches
reactionPostProcessor2D.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2020 Davide Dapelo
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program 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 General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
28#ifndef REACTION_POST_PROCESSOR_2D_HH
29#define REACTION_POST_PROCESSOR_2D_HH
30
31namespace olb {
32
33
35
36template <typename T, typename DESCRIPTOR, typename REACTIONS>
38 std::vector<std::shared_ptr<Rate<T>>> rate, std::shared_ptr<REACTIONS> reactions,
39 std::vector<BlockStructureD<2>*> partners )
40 : _x0(x0), _x1(x1), _y0(y0), _y1(y1),
41 _rate(rate), _reactions(reactions), _partners(partners)
42{
43 std::size_t iReaction = 0;
44 _sizes = std::vector<std::size_t>(std::tuple_size_v<REACTIONS>);
45 meta::tuple_for_each(*_reactions.get(), [&](auto& line){
46 using line_type = typename std::remove_reference_t<decltype(line)>;
47 _sizes[iReaction++] = std::tuple_size_v<line_type>;
48 });
49 if (std::accumulate(_sizes.begin(), _sizes.end(), std::size_t(0)) != partners.size()) {
50 throw std::invalid_argument("The number of species must equate the number of input lattices.");
51 }
52}
53
54template <typename T, typename DESCRIPTOR, typename REACTIONS>
56 std::vector<std::shared_ptr<Rate<T>>> rate, std::shared_ptr<REACTIONS> reactions,
57 std::vector<BlockStructureD<2>*> partners )
58 : ReactionPostProcessor2D<T,DESCRIPTOR,REACTIONS>(0,0,0,0,rate,reactions,partners)
59{}
60
61template <typename T, typename DESCRIPTOR, typename REACTIONS>
62template <typename VECT_TYPE, typename F>
63void ReactionPostProcessor2D<T,DESCRIPTOR,REACTIONS>::functOverReactions(std::vector<VECT_TYPE>& vect, F&& f)
64{
65 std::size_t iReaction = 0;
66 std::vector<size_t> sizes(std::tuple_size_v<REACTIONS>);
67 meta::tuple_for_each(*_reactions.get(), [&](auto& line){
68 std::size_t iReagent = 0;
69 meta::tuple_for_each(line, [&](auto& elem){
70 f(elem, vect[std::accumulate(_sizes.begin(), _sizes.begin()+iReaction, 0) + (iReagent++)]);
71 });
72 iReaction++;
73 });
74}
75
76template <typename T, typename DESCRIPTOR, typename REACTIONS>
78{
79 processSubDomain(blockLattice, _x0, _x1, _y0, _y1);
80}
81
82template <typename T, typename DESCRIPTOR, typename REACTIONS>
84 int x0, int x1, int y0, int y1 )
85{
86 int newX0, newX1, newY0, newY1;
87 if ( util::intersect ( _x0, _x1, _y0, _y1,
88 x0, x1, y0, y1,
89 newX0, newX1, newY0, newY1 ) ) {
90
91 for (int iX=newX0-1; iX<=newX1+1; ++iX) {
92 for (int iY=newY0-1; iY<=newY1+1; ++iY) {
93
94 // storing the old values of the local field from the partner lattices & resetting the sources
95 std::vector<T> fields_asSingleVector;
96 functOverReactions(_partners, [&](auto& elem, auto& component){
97 fields_asSingleVector.push_back( elem.getField(component, iX, iY) );
98 elem.resetSource(component, iX, iY);
99 });
100
101 // computing the rates from the old values of the local fields as per rate's own law
102 std::size_t iPartner = 0;
103 std::vector<std::tuple<T,BlockStructureD<2>*>> ratePartner;
104 for (std::size_t iReaction=0; iReaction<_sizes.size(); ++iReaction) {
105 auto reagents_begin = fields_asSingleVector.begin() + std::accumulate(_sizes.begin(), _sizes.begin()+iReaction, 0);
106 std::vector<T> reagents = { reagents_begin, reagents_begin + _sizes[iReaction] };
107 T rate_thisReaction = _rate[iReaction]->compute(reagents);
108 for (std::size_t iReagent=0; iReagent<_sizes[iReaction]; ++iReagent) {
109 ratePartner.push_back(std::make_tuple(rate_thisReaction, _partners[iPartner++]));
110 }
111 }
112
113 // updating local field in the partner lattice: source = sum_i rate_i*coeff
114 functOverReactions(ratePartner, [&](auto& elem, auto& component){
115 elem.incrementSource(std::get<1>(component), std::get<0>(component) * elem.getStoichioCoeff(), iX, iY);
116 });
117 }
118 }
119 }
120}
121
122
124
125template <typename T, typename DESCRIPTOR, typename REACTIONS>
127 std::vector<std::shared_ptr<Rate<T>>> rate, REACTIONS&& reactions)
128 : LatticeCouplingGenerator2D<T,DESCRIPTOR>(x0_, x1_, y0_, y1_),
129 _rate(rate), _reactions(std::make_shared<REACTIONS>(reactions))
130{}
131
132template <typename T, typename DESCRIPTOR, typename REACTIONS>
134 std::vector<std::shared_ptr<Rate<T>>> rate, REACTIONS&& reactions)
135 : ReactionGenerator2D<T,DESCRIPTOR,REACTIONS>(0,0,0,0,rate,std::move(reactions))
136{}
137
138template<typename T, typename DESCRIPTOR, typename REACTIONS>
140{
142 this->x0,this->x1,this->y0,this->y1, _rate, _reactions, partners);
143}
144
145template<typename T, typename DESCRIPTOR, typename REACTIONS>
150
151} // namespace olb
152
153#endif
Platform-abstracted block lattice for external access and inter-block interaction.
Base of a regular block.
Interface of 2D post-processing steps.
ReactionGenerator2D(int x0_, int x1_, int y0_, int y1_, std::vector< std::shared_ptr< Rate< T > > > rate, REACTIONS &&reactions)
LatticeCouplingGenerator2D< T, DESCRIPTOR > * clone() const override
PostProcessor2D< T, DESCRIPTOR > * generate(std::vector< BlockStructureD< 2 > * > partners) const override
ReactionPostProcessor2D(int x0, int x1, int y0, int y1, std::vector< std::shared_ptr< Rate< T > > > rate, std::shared_ptr< REACTIONS > reactions, std::vector< BlockStructureD< 2 > * > partners)
void tuple_for_each(TUPLE &tuple, F &&f)
Apply F to each element of TUPLE.
Definition meta.h:369
Top level namespace for all of OpenLB.