Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
electric-field
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
repos.tanguy.cavagna
physics
electric-field
Commits
7aaf3676
Commit
7aaf3676
authored
3 years ago
by
tanguy.cavagna
Browse files
Options
Downloads
Patches
Plain Diff
Added dynamics and color scheme changed
parent
64f5eb72
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
charge/charge.c
+31
-12
31 additions, 12 deletions
charge/charge.c
charge/charge.h
+7
-4
7 additions, 4 deletions
charge/charge.h
draw/draw.c
+17
-8
17 additions, 8 deletions
draw/draw.c
main.c
+10
-8
10 additions, 8 deletions
main.c
with
65 additions
and
32 deletions
charge/charge.c
+
31
−
12
View file @
7aaf3676
...
@@ -75,12 +75,13 @@ double random_charge_q(bool positive) {
...
@@ -75,12 +75,13 @@ double random_charge_q(bool positive) {
return
(
rand
()
%
100
)
*
(
positive
?
1
:
-
1
);
return
(
rand
()
%
100
)
*
(
positive
?
1
:
-
1
);
}
}
int
sign
(
double
a
)
{
return
(
a
<
0
)
?
-
1
:
1
;
}
vec2
electristatic_force
(
charge_t
a
,
charge_t
b
)
{
vec2
electristatic_force
(
charge_t
a
,
charge_t
b
)
{
bool
attractive
=
sign
(
a
.
q
)
!=
sign
(
b
.
q
);
vec2
rab
=
vec2_sub
(
b
.
pos
,
a
.
pos
);
vec2
rab
=
vec2_sub
(
b
.
pos
,
a
.
pos
);
vec2
res
=
vec2_mul
(
vec2
res
=
vec2_mul
(
rab
,
K
*
(((
a
.
q
*
10e-6
)
*
(
b
.
q
*
10e-6
))
/
pow
(
vec2_norm
(
rab
),
2
)));
rab
,
-
K
*
(((
a
.
q
*
10e-6
)
*
(
b
.
q
*
10e-6
))
/
pow
(
vec2_norm
(
rab
),
2
)));
printf
(
"COEF: %.25f
\n
"
,
(
a
.
q
*
10e-6
)
*
(
b
.
q
*
10e-6
));
return
res
;
return
res
;
}
}
...
@@ -100,18 +101,36 @@ vec2 resulting_electrostatic_force(charge_t *charges, int num_charges,
...
@@ -100,18 +101,36 @@ vec2 resulting_electrostatic_force(charge_t *charges, int num_charges,
return
resulting
;
return
resulting
;
}
}
void
update_charge_pos
(
charge_t
*
charges
,
int
num_charges
,
charge_t
*
a
)
{
vec2
compute_initial_vecocity
(
charge_t
c
,
charge_t
*
charges
,
int
num_charges
)
{
vec2
resulting
=
resulting_electrostatic_force
(
charges
,
num_charges
,
*
a
);
double
norm
=
0
;
a
->
pos
=
vec2_add
(
a
->
pos
,
resulting
);
double
angle
=
rand
()
%
360
;
return
vec2_create
(
norm
*
cos
(
angle
),
norm
*
sin
(
angle
));
}
vec2
compute_acceleration
(
charge_t
c
,
charge_t
*
charges
,
int
num_charges
)
{
return
vec2_mul
(
resulting_electrostatic_force
(
charges
,
num_charges
,
c
),
1
.
0
/
CHARGE_MASS
);
}
vec2
compute_first_pos
(
charge_t
c
,
charge_t
*
charges
,
int
num_charges
)
{
vec2
velocity
=
compute_initial_vecocity
(
c
,
charges
,
num_charges
);
vec2
a
=
compute_acceleration
(
c
,
charges
,
num_charges
);
vec2
pos
=
vec2_add
(
vec2_add
(
c
.
pos
,
vec2_mul
(
velocity
,
DELTA_T
)),
vec2_mul
(
a
,
pow
(
DELTA_T
,
2
)
/
2
));
return
pos
;
}
vec2
compute_next_pos
(
charge_t
c
,
charge_t
*
charges
,
int
num_charges
)
{
vec2
a
=
compute_acceleration
(
c
,
charges
,
num_charges
);
vec2
pos
=
vec2_add
(
vec2_sub
(
vec2_mul
(
c
.
pos
,
2
),
c
.
prev_pos
),
vec2_mul
(
a
,
pow
(
DELTA_T
,
2
)));
return
pos
;
}
}
void
update_simulation
(
charge_t
*
charges
,
int
num_charges
)
{
void
update_simulation
(
charge_t
*
charges
,
int
num_charges
)
{
for
(
int
i
=
0
;
i
<
num_charges
;
i
++
)
{
for
(
int
i
=
0
;
i
<
num_charges
;
i
++
)
{
vec2
resulting
=
vec2
tmp
=
charges
[
i
].
pos
;
resulting_electrostatic_force
(
charges
,
num_charges
,
charges
[
i
]);
charges
[
i
].
pos
=
compute_next_pos
(
charges
[
i
],
charges
,
num_charges
);
printf
(
"RES: %.19f
\n
"
,
vec2_norm
(
resulting
));
charges
[
i
].
prev_pos
=
tmp
;
charges
[
i
].
pos
=
vec2_add
(
charges
[
i
].
pos
,
resulting
);
}
}
printf
(
"%.19f, %.19f
\n
"
,
charges
[
0
].
pos
.
x
,
charges
[
0
].
pos
.
x
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
charge/charge.h
+
7
−
4
View file @
7aaf3676
...
@@ -23,6 +23,8 @@
...
@@ -23,6 +23,8 @@
#define ELEMENTARY_CHARGE 1.602176634e-19
#define ELEMENTARY_CHARGE 1.602176634e-19
#define CHARGE_RADIUS 10
#define CHARGE_RADIUS 10
#define EPS 0.015
#define EPS 0.015
#define DELTA_T 10e-4
#define CHARGE_MASS 1500
#define UNIV_MIN_X 0
#define UNIV_MIN_X 0
#define UNIV_MAX_X 1
#define UNIV_MAX_X 1
#define UNIV_MIN_Y 0
#define UNIV_MIN_Y 0
...
@@ -31,6 +33,7 @@
...
@@ -31,6 +33,7 @@
typedef
struct
_charge_t
{
typedef
struct
_charge_t
{
double
q
;
double
q
;
vec2
pos
;
vec2
pos
;
vec2
prev_pos
;
}
charge_t
;
}
charge_t
;
/**
/**
...
@@ -140,14 +143,14 @@ vec2 resulting_electrostatic_force(charge_t *charges, int num_charges,
...
@@ -140,14 +143,14 @@ vec2 resulting_electrostatic_force(charge_t *charges, int num_charges,
charge_t
a
);
charge_t
a
);
/**
/**
* @brief Update the pos of the given charge based on the resulting
* @brief Compute the first pos for the dynamic simulation
* electrostatic force
*
*
* @param c
* @param charges
* @param charges
* @param num_charges
* @param num_charges
* @
param a
* @
return vec2
*/
*/
v
oid
update_
charge_
pos
(
charge_t
*
charges
,
int
num_charges
,
charge_t
*
a
);
v
ec2
compute_first_pos
(
charge_
t
c
,
charge_t
*
charges
,
int
num_charges
);
/**
/**
* @brief Update the whole simulation
* @brief Update the whole simulation
...
...
This diff is collapsed.
Click to expand it.
draw/draw.c
+
17
−
8
View file @
7aaf3676
...
@@ -49,12 +49,19 @@ void draw_line(vec2 start, vec2 end) {
...
@@ -49,12 +49,19 @@ void draw_line(vec2 start, vec2 end) {
}
}
void
get_heatmap_color
(
double
value
,
double
*
red
,
double
*
green
,
double
*
blue
)
{
void
get_heatmap_color
(
double
value
,
double
*
red
,
double
*
green
,
double
*
blue
)
{
double
aR
=
0
.
0
;
double
aR
=
1
.
0
;
double
aG
=
0
.
0
;
double
aG
=
1
.
0
;
double
aB
=
0
.
0
;
// RGB for our 1st color (blue in this case).
double
aB
=
1
.
0
;
// RGB for our 1st color
double
bR
=
0
.
31
;
double
bR
=
0
.
67
;
double
bG
=
0
.
23
;
double
bG
=
0
.
12
;
double
bB
=
0
.
31
;
// RGB for our 2nd color (red in this case).
double
bB
=
0
.
47
;
// RGB for our 2nd color
// if (value < 0.3) {
// *red = 1.0;
// *green = 1.0;
// *blue = 1.0;
// return;
// }
*
red
=
(
bR
-
aR
)
*
value
+
aR
;
// Evaluated as -255*value + 255.
*
red
=
(
bR
-
aR
)
*
value
+
aR
;
// Evaluated as -255*value + 255.
*
green
=
(
bG
-
aG
)
*
value
+
aG
;
// Evaluates as 0.
*
green
=
(
bG
-
aG
)
*
value
+
aG
;
// Evaluates as 0.
...
@@ -170,12 +177,14 @@ void draw_vector_field(charge_t *charges, int num_charges, vec2 *points,
...
@@ -170,12 +177,14 @@ void draw_vector_field(charge_t *charges, int num_charges, vec2 *points,
for
(
int
i
=
0
;
i
<
nb_points
;
i
++
)
{
for
(
int
i
=
0
;
i
<
nb_points
;
i
++
)
{
vec2
a
=
position_to_coordinates
(
w
,
h
,
points
[
i
]);
vec2
a
=
position_to_coordinates
(
w
,
h
,
points
[
i
]);
glLineWidth
(
2
);
glBegin
(
GL_LINES
);
glBegin
(
GL_LINES
);
glColor3f
(
0
.
6
1
,
0
.
87
,
0
.
6
1
);
glColor3f
(
0
.
1
8
,
0
.
46
,
0
.
1
8
);
glVertex2d
(
a
.
x
,
a
.
y
);
glVertex2d
(
a
.
x
,
a
.
y
);
a
=
vec2_add
(
a
,
vec2_mul
(
vec2_div
(
vs
[
i
],
max
),
1
0
));
a
=
vec2_add
(
a
,
vec2_mul
(
vec2_div
(
vs
[
i
],
max
),
4
0
));
glVertex2d
(
a
.
x
,
a
.
y
);
glVertex2d
(
a
.
x
,
a
.
y
);
glEnd
();
glEnd
();
glLineWidth
(
1
);
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
main.c
+
10
−
8
View file @
7aaf3676
...
@@ -30,12 +30,12 @@
...
@@ -30,12 +30,12 @@
#endif
#endif
#define ONE_SECOND_IN_MILLISECONDS 1000
#define ONE_SECOND_IN_MILLISECONDS 1000
#define REFRESH_RATE
20
0
#define REFRESH_RATE
6
0
#define WINDOW_WIDTH 800
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 800
#define WINDOW_HEIGHT 800
#define NB_CHARGES 5
#define NB_CHARGES 5
#define GRID_SPACING
10
#define GRID_SPACING
25
#define NB_POINTS \
#define NB_POINTS \
((WINDOW_WIDTH / GRID_SPACING) * (WINDOW_WIDTH / GRID_SPACING))
((WINDOW_WIDTH / GRID_SPACING) * (WINDOW_WIDTH / GRID_SPACING))
#define MAXIMUM_CHARGES 30 // Can be replace by a dynamic array
#define MAXIMUM_CHARGES 30 // Can be replace by a dynamic array
...
@@ -47,7 +47,7 @@ vec2 default_charges_pos[MAXIMUM_CHARGES];
...
@@ -47,7 +47,7 @@ vec2 default_charges_pos[MAXIMUM_CHARGES];
double
default_charges
[
MAXIMUM_CHARGES
];
double
default_charges
[
MAXIMUM_CHARGES
];
charge_t
*
charges
;
charge_t
*
charges
;
bool
toggle_lines_view
=
fals
e
;
bool
toggle_lines_view
=
tru
e
;
bool
toggle_vector_view
=
false
;
bool
toggle_vector_view
=
false
;
bool
toggle_heatmap_view
=
false
;
bool
toggle_heatmap_view
=
false
;
...
@@ -85,7 +85,7 @@ void generate_points(vec2 points[]) {
...
@@ -85,7 +85,7 @@ void generate_points(vec2 points[]) {
*
*
*/
*/
void
display
()
{
void
display
()
{
glClearColor
(
0
,
0
,
0
,
1
);
glClearColor
(
1
,
1
,
1
,
1
);
glClear
(
GL_COLOR_BUFFER_BIT
);
glClear
(
GL_COLOR_BUFFER_BIT
);
if
(
toggle_lines_view
)
{
if
(
toggle_lines_view
)
{
...
@@ -186,8 +186,11 @@ int main(int argc, char **argv) {
...
@@ -186,8 +186,11 @@ int main(int argc, char **argv) {
}
}
// Init charges
// Init charges
for
(
int
i
=
0
;
i
<
charge_count
;
i
++
)
for
(
int
i
=
0
;
i
<
charge_count
;
i
++
)
{
charges
[
i
]
=
charge_create
(
default_charges
[
i
],
default_charges_pos
[
i
]);
charges
[
i
]
=
charge_create
(
default_charges
[
i
],
default_charges_pos
[
i
]);
charges
[
i
].
prev_pos
=
charges
[
i
].
pos
;
charges
[
i
].
pos
=
compute_first_pos
(
charges
[
i
],
charges
,
charge_count
);
}
// Generate all field lines starting points
// Generate all field lines starting points
generate_points
(
starting_points
);
generate_points
(
starting_points
);
...
@@ -209,9 +212,8 @@ int main(int argc, char **argv) {
...
@@ -209,9 +212,8 @@ int main(int argc, char **argv) {
glutMouseFunc
(
handle_mouse_input
);
glutMouseFunc
(
handle_mouse_input
);
glutKeyboardFunc
(
keyboard
);
glutKeyboardFunc
(
keyboard
);
// glutTimerFunc(ONE_SECOND_IN_MILLISECONDS / REFRESH_RATE, update_timer,
glutTimerFunc
(
ONE_SECOND_IN_MILLISECONDS
/
REFRESH_RATE
,
update_timer
,
0
);
// 0); glutTimerFunc(ONE_SECOND_IN_MILLISECONDS / REFRESH_RATE, draw_timer,
glutTimerFunc
(
ONE_SECOND_IN_MILLISECONDS
/
REFRESH_RATE
,
draw_timer
,
0
);
// 0);
glutMainLoop
();
glutMainLoop
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment