OpenLB 1.7
Loading...
Searching...
No Matches
reactionPostProcessor3D.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_3D_HH
29#define REACTION_POST_PROCESSOR_3D_HH
30namespace olb {
31
32
34
35template <typename T, typename DESCRIPTOR, typename REACTIONS>
36ReactionPostProcessor3D<T,DESCRIPTOR,REACTIONS>::ReactionPostProcessor3D ( int x0, int x1, int y0, int y1, int z0, int z1,
37 std::vector<std::shared_ptr<Rate<T>>> rate, std::shared_ptr<REACTIONS> reactions,
38 std::vector<BlockStructureD<3>*> partners )
39 : _x0(x0), _x1(x1), _y0(y0), _y1(y1), _z0(z0), _z1(z1),
40 _rate(rate), _reactions(reactions), _partners(partners)
41{
42 std::size_t iReaction = 0;
43 _sizes = std::vector<std::size_t>(std::tuple_size_v<REACTIONS>);
44 meta::tuple_for_each(*_reactions.get(), [&](auto& line){
45 using line_type = typename std::remove_reference_t<decltype(line)>;
46 _sizes[iReaction++] = std::tuple_size_v<line_type>;
47 });
48 if (std::accumulate(_sizes.begin(), _sizes.end(), std::size_t(0)) != partners.size()) {
49 throw std::invalid_argument("The number of species must equate the number of input lattices.");
50 }
51}
52
53template <typename T, typename DESCRIPTOR, typename REACTIONS>
55 std::vector<std::shared_ptr<Rate<T>>> rate, std::shared_ptr<REACTIONS> reactions,
56 std::vector<BlockStructureD<3>*> partners )
57 : ReactionPostProcessor3D<T,DESCRIPTOR,REACTIONS>(0,0,0,0,0,0,rate,reactions,partners)
58{}
59
60template <typename T, typename DESCRIPTOR, typename REACTIONS>
61template <typename VECT_TYPE, typename F>
62void ReactionPostProcessor3D<T,DESCRIPTOR,REACTIONS>::functOverReactions(std::vector<VECT_TYPE>& vect, F&& f)
63{
64 std::size_t iReaction = 0;
65 std::vector<size_t> sizes(std::tuple_size_v<REACTIONS>);
66 meta::tuple_for_each(*_reactions.get(), [&](auto& line){
67 std::size_t iReagent = 0;
68 meta::tuple_for_each(line, [&](auto& elem){
69 f(elem, vect[std::accumulate(_sizes.begin(), _sizes.begin()+iReaction, 0) + (iReagent++)]);
70 });
71 iReaction++;
72 });
73}
74
75template <typename T, typename DESCRIPTOR, typename REACTIONS>
77{
78 processSubDomain(blockLattice, _x0, _x1, _y0, _y1, _z0, _z1);
79}
80
81template <typename T, typename DESCRIPTOR, typename REACTIONS>
83 int x0, int x1, int y0, int y1, int z0, int z1 )
84{
85 int newX0, newX1, newY0, newY1, newZ0, newZ1;
86 if ( util::intersect ( _x0, _x1, _y0, _y1, _z0, _z1,
87 x0, x1, y0, y1, z0, z1,
88 newX0, newX1, newY0, newY1, newZ0, newZ1 ) ) {
89
90 for (int iX=newX0-1; iX<=newX1+1; ++iX) {
91 for (int iY=newY0-1; iY<=newY1+1; ++iY) {
92 for (int iZ=newZ0-1; iZ<=newZ1+1; ++iZ) {
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, iZ) );
98 elem.resetSource(component, iX, iY, iZ);
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<3>*>> 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, iZ);
116 });
117 }
118 }
119 }
120 }
121}
122
123
125
126template <typename T, typename DESCRIPTOR, typename REACTIONS>
127ReactionGenerator3D<T,DESCRIPTOR,REACTIONS>::ReactionGenerator3D ( int x0_, int x1_, int y0_, int y1_, int z0_, int z1_,
128 std::vector<std::shared_ptr<Rate<T>>> rate, REACTIONS&& reactions)
129 : LatticeCouplingGenerator3D<T,DESCRIPTOR>(x0_, x1_, y0_, y1_, z0_, z1_),
130 _rate(rate), _reactions(std::make_shared<REACTIONS>(reactions))
131{}
132
133template <typename T, typename DESCRIPTOR, typename REACTIONS>
135 std::vector<std::shared_ptr<Rate<T>>> rate, REACTIONS&& reactions)
136 : ReactionGenerator3D<T,DESCRIPTOR,REACTIONS>(0,0,0,0,0,0,rate,std::move(reactions))
137{}
138
139template<typename T, typename DESCRIPTOR, typename REACTIONS>
141{
143 this->x0,this->x1,this->y0,this->y1,this->z0,this->z1, _rate, _reactions, partners);
144}
145
146template<typename T, typename DESCRIPTOR, typename REACTIONS>
151
152} // namespace olb
153
154#endif
Platform-abstracted block lattice for external access and inter-block interaction.
Base of a regular block.
LatticeCouplingGenerator3D< T, DESCRIPTOR > * clone() const override
ReactionGenerator3D(int x0_, int x1_, int y0_, int y1_, int z0_, int z1_, std::vector< std::shared_ptr< Rate< T > > > rate, REACTIONS &&reactions)
PostProcessor3D< T, DESCRIPTOR > * generate(std::vector< BlockStructureD< 3 > * > partners) const override
ReactionPostProcessor3D(int x0, int x1, int y0, int y1, int z0, int z1, std::vector< std::shared_ptr< Rate< T > > > rate, std::shared_ptr< REACTIONS > reactions, std::vector< BlockStructureD< 3 > * > 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.