Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tf_flux_eval.h
1/*******************************************************************************
2 * This file is part of mdcore.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
20#ifndef _MDCORE_SOURCE_TF_FLUX_EVAL_H_
21#define _MDCORE_SOURCE_TF_FLUX_EVAL_H_
22
23#include <tfFlux.h>
24#include <state/tfStateVector.h>
25#include <tfEngine.h>
26#include <iostream>
27
28
29namespace TissueForge {
30
31
32 TF_ALWAYS_INLINE FPTYPE flux_fick(Flux *flux, int i, FPTYPE si, FPTYPE sj) {
33 return flux->coef[i] * (si - sj);
34 }
35
36 TF_ALWAYS_INLINE FPTYPE flux_secrete(Flux *flux, int i, FPTYPE si, FPTYPE sj) {
37 FPTYPE q = flux->coef[i] * (si - flux->target[i]);
38 FPTYPE scale = q > 0.f; // forward only, 1 if > 0, 0 if < 0.
39 return scale * q;
40 }
41
42 TF_ALWAYS_INLINE FPTYPE flux_uptake(Flux *flux, int i, FPTYPE si, FPTYPE sj) {
43 FPTYPE q = flux->coef[i] * (flux->target[i] - sj) * si;
44 FPTYPE scale = q > 0.f;
45 return scale * q;
46 }
47
48 TF_ALWAYS_INLINE void flux_eval_ex(
49 struct Fluxes *f, FPTYPE r2, Particle *part_i, Particle *part_j)
50 {
51
52 Flux *flux = &f->fluxes[0];
53 FPTYPE r = FPTYPE_SQRT(r2);
54
55 for(int i = 0; i < flux->size; ++i) {
56 FPTYPE cutoff = flux->cutoff[i];
57 if(r > cutoff)
58 continue;
59
60 FPTYPE term = 1 - r / cutoff;
61 term = term * term;
62
63 // NOTE: order important here, type ids could be the same, i.e.
64 // Fick flux, the true branch of each assignemnt gets evaluated.
65 Particle *pi = part_i->typeId == flux->type_ids[i].a ? part_i : part_j;
66 Particle *pj = part_j->typeId == flux->type_ids[i].b ? part_j : part_i;
67
68 assert(pi->typeId == flux->type_ids[i].a);
69 assert(pj->typeId == flux->type_ids[i].b);
70 assert(pi != pj);
71
72 FPTYPE *si = pi->state_vector->fvec;
73 FPTYPE *sj = pj->state_vector->fvec;
74
75 FPTYPE *qi = pi->state_vector->q;
76 FPTYPE *qj = pj->state_vector->q;
77
78 int32_t *ii = flux->indices_a;
79 int32_t *ij = flux->indices_b;
80
81 FPTYPE ssi = si[ii[i]];
82 FPTYPE ssj = sj[ij[i]];
83 FPTYPE q = term;
84
85 switch(flux->kinds[i]) {
86 case FLUX_FICK:
87 q *= flux_fick(flux, i, ssi, ssj);
88 break;
89 case FLUX_SECRETE:
90 q *= flux_secrete(flux, i, ssi, ssj);
91 break;
92 case FLUX_UPTAKE:
93 q *= flux_uptake(flux, i, ssi, ssj);
94 break;
95 default:
96 assert(0);
97 }
98
99 FPTYPE half_decay = flux->decay_coef[i] / 2.f;
100 qi[ii[i]] = qi[ii[i]] - q - half_decay * ssi;
101 qj[ij[i]] = qj[ij[i]] + q - half_decay * ssj;
102 }
103 }
104
105 inline Fluxes *get_fluxes(const Particle *a, const Particle *b) {
106 int index = _Engine.max_type * a->typeId + b->typeId;
107 return _Engine.fluxes[index];
108 }
109
110};
111
112#endif // _MDCORE_SOURCE_TF_FLUX_EVAL_H_
Include Python header, disable linking to pythonX_d.lib on Windows in debug mode.
Definition tfAngleConfig.h:26
engine _Engine
Definition tfFlux.h:53
A flux is defined between a pair of types, and acts on the state vector between a pair of instances.
Definition tfFlux.h:88
Definition tfParticle.h:101
int16_t typeId
Definition tfParticle.h:185