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
680a680d
Commit
680a680d
authored
3 years ago
by
tanguy.cavagna
Browse files
Options
Downloads
Patches
Plain Diff
Began movement simulation
parent
24583103
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
+42
-1
42 additions, 1 deletion
charge/charge.c
charge/charge.h
+39
-0
39 additions, 0 deletions
charge/charge.h
draw/draw.c
+1
-1
1 addition, 1 deletion
draw/draw.c
main.c
+36
-6
36 additions, 6 deletions
main.c
with
118 additions
and
8 deletions
charge/charge.c
+
42
−
1
View file @
680a680d
...
@@ -72,5 +72,46 @@ charge_t charge_create(double q, vec2 pos) {
...
@@ -72,5 +72,46 @@ charge_t charge_create(double q, vec2 pos) {
}
}
double
random_charge_q
(
bool
positive
)
{
double
random_charge_q
(
bool
positive
)
{
return
rand_one
()
*
(
positive
?
1
:
-
1
);
return
(
rand
()
%
100
)
*
(
positive
?
1
:
-
1
);
}
vec2
electristatic_force
(
charge_t
a
,
charge_t
b
)
{
vec2
rab
=
vec2_sub
(
b
.
pos
,
a
.
pos
);
vec2
res
=
vec2_mul
(
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
;
}
vec2
resulting_electrostatic_force
(
charge_t
*
charges
,
int
num_charges
,
charge_t
a
)
{
vec2
resulting
=
vec2_create_zero
();
for
(
int
i
=
0
;
i
<
num_charges
;
i
++
)
{
// Very unlickly to have two charges with the exact same charge
if
(
charges
[
i
].
q
==
a
.
q
)
continue
;
vec2
ef
=
electristatic_force
(
a
,
charges
[
i
]);
resulting
=
vec2_add
(
resulting
,
ef
);
}
return
resulting
;
}
void
update_charge_pos
(
charge_t
*
charges
,
int
num_charges
,
charge_t
*
a
)
{
vec2
resulting
=
resulting_electrostatic_force
(
charges
,
num_charges
,
*
a
);
a
->
pos
=
vec2_add
(
a
->
pos
,
resulting
);
}
void
update_simulation
(
charge_t
*
charges
,
int
num_charges
)
{
for
(
int
i
=
0
;
i
<
num_charges
;
i
++
)
{
vec2
resulting
=
resulting_electrostatic_force
(
charges
,
num_charges
,
charges
[
i
]);
printf
(
"RES: %.19f
\n
"
,
vec2_norm
(
resulting
));
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
+
39
−
0
View file @
680a680d
...
@@ -20,6 +20,7 @@
...
@@ -20,6 +20,7 @@
#include
<stdlib.h>
#include
<stdlib.h>
#define K 8.988e9
#define K 8.988e9
#define ELEMENTARY_CHARGE 1.602176634e-19
#define CHARGE_RADIUS 10
#define CHARGE_RADIUS 10
#define EPS 0.015
#define EPS 0.015
#define UNIV_MIN_X 0
#define UNIV_MIN_X 0
...
@@ -118,4 +119,42 @@ charge_t charge_create(double q, vec2 pos);
...
@@ -118,4 +119,42 @@ charge_t charge_create(double q, vec2 pos);
*/
*/
double
random_charge_q
(
bool
positive
);
double
random_charge_q
(
bool
positive
);
/**
* @brief Compute the electrostatic force between two charges
*
* @param a
* @param b
* @return vec2
*/
vec2
electristatic_force
(
charge_t
a
,
charge_t
b
);
/**
* @brief Compute the resulting electrostatic force
*
* @param charges
* @param num_charges
* @param a
* @return vec2
*/
vec2
resulting_electrostatic_force
(
charge_t
*
charges
,
int
num_charges
,
charge_t
a
);
/**
* @brief Update the pos of the given charge based on the resulting
* electrostatic force
*
* @param charges
* @param num_charges
* @param a
*/
void
update_charge_pos
(
charge_t
*
charges
,
int
num_charges
,
charge_t
*
a
);
/**
* @brief Update the whole simulation
*
* @param charges
* @param num_charges
*/
void
update_simulation
(
charge_t
*
charges
,
int
num_charges
);
#endif
#endif
\ No newline at end of file
This diff is collapsed.
Click to expand it.
draw/draw.c
+
1
−
1
View file @
680a680d
...
@@ -202,7 +202,7 @@ void draw_charges(charge_t *charges, int num_charges, int w, int h) {
...
@@ -202,7 +202,7 @@ void draw_charges(charge_t *charges, int num_charges, int w, int h) {
draw_circle
(
chCoord
,
CHARGE_RADIUS
);
draw_circle
(
chCoord
,
CHARGE_RADIUS
);
char
charge_str
[
8
];
char
charge_str
[
8
];
snprintf
(
charge_str
,
8
,
"%.
4
f"
,
charges
[
i
].
q
);
snprintf
(
charge_str
,
8
,
"%.
0
f"
,
charges
[
i
].
q
);
vec2
text_pos
=
vec2_create
(
chCoord
.
x
,
chCoord
.
y
+
CHARGE_RADIUS
+
4
);
vec2
text_pos
=
vec2_create
(
chCoord
.
x
,
chCoord
.
y
+
CHARGE_RADIUS
+
4
);
draw_textbox
(
text_pos
,
charge_str
);
draw_textbox
(
text_pos
,
charge_str
);
}
}
...
...
This diff is collapsed.
Click to expand it.
main.c
+
36
−
6
View file @
680a680d
...
@@ -29,6 +29,9 @@
...
@@ -29,6 +29,9 @@
#define UNUSED(x) x
#define UNUSED(x) x
#endif
#endif
#define ONE_SECOND_IN_MILLISECONDS 1000
#define REFRESH_RATE 200
#define WINDOW_WIDTH 800
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 800
#define WINDOW_HEIGHT 800
#define NB_CHARGES 5
#define NB_CHARGES 5
...
@@ -42,11 +45,24 @@ int charge_count = NB_CHARGES;
...
@@ -42,11 +45,24 @@ int charge_count = NB_CHARGES;
vec2
starting_points
[
NB_POINTS
];
vec2
starting_points
[
NB_POINTS
];
vec2
default_charges_pos
[
MAXIMUM_CHARGES
];
vec2
default_charges_pos
[
MAXIMUM_CHARGES
];
double
default_charges
[
MAXIMUM_CHARGES
];
double
default_charges
[
MAXIMUM_CHARGES
];
charge_t
*
charges
;
bool
toggle_lines_view
=
tru
e
;
bool
toggle_lines_view
=
fals
e
;
bool
toggle_vector_view
=
false
;
bool
toggle_vector_view
=
false
;
bool
toggle_heatmap_view
=
false
;
bool
toggle_heatmap_view
=
false
;
void
update
()
{
update_simulation
(
charges
,
charge_count
);
};
void
update_timer
()
{
update
();
glutTimerFunc
(
ONE_SECOND_IN_MILLISECONDS
/
REFRESH_RATE
,
update_timer
,
0
);
}
void
draw_timer
()
{
glutPostRedisplay
();
glutTimerFunc
(
ONE_SECOND_IN_MILLISECONDS
/
REFRESH_RATE
,
draw_timer
,
0
);
}
/**
/**
* @brief Generate a grid of points
* @brief Generate a grid of points
*
*
...
@@ -72,11 +88,6 @@ void display() {
...
@@ -72,11 +88,6 @@ void display() {
glClearColor
(
0
,
0
,
0
,
1
);
glClearColor
(
0
,
0
,
0
,
1
);
glClear
(
GL_COLOR_BUFFER_BIT
);
glClear
(
GL_COLOR_BUFFER_BIT
);
// Generate all the charges
charge_t
charges
[
charge_count
];
for
(
int
i
=
0
;
i
<
charge_count
;
i
++
)
charges
[
i
]
=
charge_create
(
default_charges
[
i
],
default_charges_pos
[
i
]);
if
(
toggle_heatmap_view
)
if
(
toggle_heatmap_view
)
draw_heatmap
(
charges
,
charge_count
,
WINDOW_WIDTH
,
WINDOW_HEIGHT
);
draw_heatmap
(
charges
,
charge_count
,
WINDOW_WIDTH
,
WINDOW_HEIGHT
);
...
@@ -110,6 +121,7 @@ void handle_mouse_input(int button, int state, int x, int y) {
...
@@ -110,6 +121,7 @@ void handle_mouse_input(int button, int state, int x, int y) {
if
(
state
==
GLUT_UP
)
if
(
state
==
GLUT_UP
)
return
;
return
;
// Define new charges attributes
if
(
button
==
GLUT_LEFT_BUTTON
||
button
==
GLUT_RIGHT_BUTTON
)
{
if
(
button
==
GLUT_LEFT_BUTTON
||
button
==
GLUT_RIGHT_BUTTON
)
{
charge_count
+=
1
;
charge_count
+=
1
;
default_charges_pos
[
charge_count
-
1
]
=
default_charges_pos
[
charge_count
-
1
]
=
...
@@ -122,6 +134,13 @@ void handle_mouse_input(int button, int state, int x, int y) {
...
@@ -122,6 +134,13 @@ void handle_mouse_input(int button, int state, int x, int y) {
if
(
button
==
GLUT_RIGHT_BUTTON
)
if
(
button
==
GLUT_RIGHT_BUTTON
)
default_charges
[
charge_count
-
1
]
=
random_charge_q
(
false
);
default_charges
[
charge_count
-
1
]
=
random_charge_q
(
false
);
// Realloc charge array when a new one is added
charges
=
realloc
(
charges
,
sizeof
(
charge_t
)
*
charge_count
);
// Redefine all charges infos
for
(
int
i
=
0
;
i
<
charge_count
;
i
++
)
charges
[
i
]
=
charge_create
(
default_charges
[
i
],
default_charges_pos
[
i
]);
glutPostRedisplay
();
glutPostRedisplay
();
}
}
...
@@ -158,12 +177,18 @@ void keyboard(unsigned char key, int UNUSED(x), int UNUSED(y)) {
...
@@ -158,12 +177,18 @@ void keyboard(unsigned char key, int UNUSED(x), int UNUSED(y)) {
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
srand
(
time
(
NULL
));
srand
(
time
(
NULL
));
charges
=
malloc
(
sizeof
(
charge_t
)
*
charge_count
);
// Generate all default charges
// Generate all default charges
for
(
int
i
=
0
;
i
<
charge_count
;
i
++
)
{
for
(
int
i
=
0
;
i
<
charge_count
;
i
++
)
{
default_charges
[
i
]
=
random_charge_q
((
bool
)(
rand
()
%
2
==
0
));
default_charges
[
i
]
=
random_charge_q
((
bool
)(
rand
()
%
2
==
0
));
default_charges_pos
[
i
]
=
vec2_create
(
rand_one
(),
rand_one
());
default_charges_pos
[
i
]
=
vec2_create
(
rand_one
(),
rand_one
());
}
}
// Init charges
for
(
int
i
=
0
;
i
<
charge_count
;
i
++
)
charges
[
i
]
=
charge_create
(
default_charges
[
i
],
default_charges_pos
[
i
]);
// Generate all field lines starting points
// Generate all field lines starting points
generate_points
(
starting_points
);
generate_points
(
starting_points
);
...
@@ -184,9 +209,14 @@ int main(int argc, char **argv) {
...
@@ -184,9 +209,14 @@ 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,
// 0); glutTimerFunc(ONE_SECOND_IN_MILLISECONDS / REFRESH_RATE, draw_timer,
// 0);
glutMainLoop
();
glutMainLoop
();
// Object destroy
// Object destroy
glutDestroyWindow
(
window
);
glutDestroyWindow
(
window
);
free
(
charges
);
return
EXIT_SUCCESS
;
return
EXIT_SUCCESS
;
}
}
\ No newline at end of file
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