GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
Math.hh
Go to the documentation of this file.
1//
2// Copyright (C) 2009 Tero Pulkkinen
3// Released under LGPL License.
4//
5// This file is part of Polygon.
6//
7// Polygon is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Polygon is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU Library General Public License
18// along with Polygon. If not, see <http://www.gnu.org/licenses/>.
19//
20
21#include "VectorTools.hh"
22
24{
25public:
26 virtual Vector Field(Point p, float time) const=0;
27};
28
29
31{
32public:
33 virtual float Field(Point p, float time) const=0;
34};
35
37{
38public:
39 GradientField(ScalarField &field) : field(field) {  }
40 Vector Field(Point p, float time) const
41 {
42 Vector dx = Vector(0.001,0.0,0.0);
43 Vector dy = Vector(0.0,0.001,0.0);
44 Vector dz = Vector(0.0,0.0,0.001);
45
46 Vector v;
47 v.x = field.Field(p+dx, time)-field.Field(p, time);
48 v.y = field.Field(p+dy, time)-field.Field(p, time);
49 v.z = field.Field(p+dz, time)-field.Field(p, time);
50 v.x /= 0.001;
51 v.y /= 0.001;
52 v.z /= 0.001;
53 return v;
54 }
55private:
56 ScalarField &field;
57};
58
60{
61 return new GradientField(*field);
62}
63
65{
66public:
67 DivergenceField(VectorField &field) : field(field) {  }
68 float Field(Point p, float time) const
69 {
70 Point px = p + Vector(0.001,0.0,0.0);
71 Point py = p + Vector(0.0, 0.001, 0.0);
72 Point pz = p + Vector(0.0,0.0,0.001);
73 Vector vx = field.Field(px, time)-field.Field(p, time);
74 Vector vy = field.Field(py, time)-field.Field(p, time);
75 Vector vz = field.Field(pz, time)-field.Field(p, time);
76 return vx.x/0.001+vy.y/0.001+vz.z/0.01;
77 }
78private:
79 VectorField &field;
80};
81
83{
84 return new DivergenceField(*field);
85}
87{
88public:
89 TensorDivergence(Tensor &t) : t(t) { }
90 // tr(del v)
91private:
92 Tensor &t;
93};
94
95class Curl : public XXXX
96{
97public:
98 Curl(VectorField &v) : v(v) {  }
99};
100
102{
103public:
105 : field(field),
106 d(field),g(d), c1(field), c2(c1)
107 { }
108 void Update(Point p, float time)
109 {
110 return g.Field(p, time) - c2.Field(p, time);
111 }
112private:
113 VectorField &field;
114 DivergenceField d; // (D(_D_.A))
115 GradientField g; //(_D_(D.A))
116 Curl c1; // D x (_D_ x A)
117 Curl c2; // _D_ x (D x A)
118};
119
120
121class TensorDerivate : public Vector
122{
123public:
124 TensorDerivate(VectorField &field, Vector u, Point P) : field(field), u(u), P(P) { }
125 void Update(float time)
126 {
127 Vector v1 = field.Field(P, time);
128 Vector v2 = field.Field(P+0.001*u, time);
129 Vector v = v2-v1;
130 v /= 0.001;
131 dx = v.dx;
132 dy = v.dy;
133 dz = v.dz;
134 }
135private:
136 VectorField &field;
137 Vector u;
138 Point P;
139};
140
142{
143public:
144 Derivative(VectorField &v) : v(v) { }
145 Vector Field(Point p, float time) const
146 {
147 Vector vv = v.Field(p, time+0.01)-v.Field(p, time);
148 return vv;
149 }
150private:
151 VectorField &v;
152};
153
154class Advection : public Vector
155{
156public:
157 Advection(Vector v, VectorField &field) : v(v), field(field) { }
158 void Update(float time)
159 {
160 Vector ux = field.Field(p+Vector(0.001,0.0,0.0), time)-field.Field(p,time);
161 Vector uy = field.Field(p+Vector(0.0,0.001,0.0), time)-field.Field(p,time);
162 Vector uz = field.Field(p+Vector(0.0,0.0,0.001), time)-field.Field(p,time);
163 ux *= v.dx;
164 uy *= v.dy;
165 uz *= v.dz;
166 Vector vv = ux+uy+uz;
167 dx = vv.dx;
168 dy = vv.dy;
169 dz = vv.dz;
170 }
171 private:
172 Vector v;
173 VectorField &field;
174};
175
176template<class T>
178{
179public:
180 T left_side() const=0;
181 T right_side() const=0;
182 bool compare(T,T) const=0;
183};
184
185class NavierStokesEquation : public Equation<Vector>
186{
187public:
188 NavierStokesEquation(float density,
189 VectorField &flow_velocity,
190 ScalarField &pressure,
191 VectorField &stress_tensor,
192 float time,
193 Point point,
194 float viscosity,
195 Vector body_forces)
196 : density(density),
197 flow_velocity(flow_velocity),
198 pressure(pressure),
199 time(time),
200 point(point),
201 viscosity(viscosity),
202 body_forces(body_forces),
203 stress_tensor(stress_tensor), der(flow_velocity), adv(flow_velocity.Field(point, time), flow_velocity) { }
204
206 {
207 return density*(der.Field(point, time)+adv);
208 }
210 {
211 return -grad_p.Field(point, time)+viscosity*lap + body_forces;
212 }
213 bool compare(Vector a, Vector b) const
214 {
215 return a.dx==b.dx && a.dy==b.dy && a.dz == b.dz;
216 }
217private:
218 Derivative_T der;
219 Advection adv;
220 GradientField grad_p;
221 DivergenceField div_T;
222 float viscosity;
223 Vector body_forces;
224};
size dy
Definition GameApi_gui.cc:1727
VectorField * Gradient(ScalarField *field)
Definition Math.hh:59
ScalarField * Divergence(VectorField *field)
Definition Math.hh:82
Definition Math.hh:155
void Update(float time)
Definition Math.hh:158
Advection(Vector v, VectorField &field)
Definition Math.hh:157
Definition Math.hh:96
Curl(VectorField &v)
Definition Math.hh:98
Definition Math.hh:142
Derivative(VectorField &v)
Definition Math.hh:144
Vector Field(Point p, float time) const
Definition Math.hh:145
Definition Math.hh:65
float Field(Point p, float time) const
Definition Math.hh:68
DivergenceField(VectorField &field)
Definition Math.hh:67
Definition Math.hh:178
bool compare(T, T) const =0
T left_side() const =0
T right_side() const =0
Definition Graph.hh:6087
Definition Math.hh:37
Vector Field(Point p, float time) const
Definition Math.hh:40
GradientField(ScalarField &field)
Definition Math.hh:39
Vector right_side() const
Definition Math.hh:209
NavierStokesEquation(float density, VectorField &flow_velocity, ScalarField &pressure, VectorField &stress_tensor, float time, Point point, float viscosity, Vector body_forces)
Definition Math.hh:188
bool compare(Vector a, Vector b) const
Definition Math.hh:213
Vector left_side() const
Definition Math.hh:205
Definition Math.hh:31
virtual float Field(Point p, float time) const =0
TensorDerivate(VectorField &field, Vector u, Point P)
Definition Math.hh:124
void Update(float time)
Definition Math.hh:125
TensorDivergence(Tensor &t)
Definition Math.hh:89
Definition Math.hh:24
virtual Vector Field(Point p, float time) const =0
VectorLaplacian(VectorField &field)
Definition Math.hh:104
void Update(Point p, float time)
Definition Math.hh:108
Definition VectorTools.hh:125
float dy
Definition VectorTools.hh:268
Vector()
Definition VectorTools.hh:128
float dz
Definition VectorTools.hh:268
float dx
Definition VectorTools.hh:268
d
Definition meshpage.php:3895
Definition GameApi.h:23
Definition Parser.cc:60
Definition GameApi.h:42