summaryrefslogtreecommitdiff
path: root/src/physics.h
blob: 80f3fdf83074ddb09980b0b70921a47996ae6d23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef _PHYSICS_H
#define _PHYSICS_H

#include "common.h"
#include "itc.h"    

#define LIGHT_SPEED (299792458.0f)
#define MU_ZERO (4.0f * M_PI * 1e-7f)
#define EPS_ZERO (1.0f / (MU_ZERO * LIGHT_SPEED * LIGHT_SPEED))
#define IMPEDANCE_ZERO (sqrt(MU_ZERO/EPS_ZERO))

typedef struct {
	size_t dims[3];
	size_t zstr, ystr, xstr, size; // strides in no. of points (not bytes)
	size_t zstr1, ystr1, xstr1, size1; // same as above but for aux
	float spacing; // in meters
} phy_field_info;

typedef struct {
	float *E;
	float *H;
} phy_field_em;

typedef struct {
	float eps, mu; // for visualisation, not needed for stepping

	float uec_H[3][4];
	float uec_E[3][4];

	vec3_t curl_E, curl_H;

	vec3_t int_E, int_H;
	vec3_t int_curl_E, int_curl_H;
} phy_field_aux_point;

typedef struct {
	bool enabled;

	size_t z;
	float Px, Py;
} phy_source_xyplane;

enum {
	PHY_CMD_QUIT,
	PHY_CMD_PAUSE,
	PHY_CMD_RESUME,
	PHY_CMD_STEP,
	PHY_CMD_ZERO,
	PHY_CMD_DEBUG,
	PHY_CMD_RESET,
	PHY_CMD_CSG_PUSH,
	PHY_CMD_CSG_CLEAR
};

typedef enum {
	CSG_SPHERE
} phy_csg_shape;

typedef struct phy_csg_step_s phy_csg_step;
struct phy_csg_step_s {
	phy_csg_shape shape;
	vec3_t x;
	vec3_t d;
	float value;
	phy_csg_step *next;
};

typedef struct {
	bool valid;

	phy_field_info field_info;
	phy_field_em fields[3];
	phy_field_aux_point *aux;
	size_t pml_widths[3];
	float time, time_delta;

	phy_source_xyplane source_xyplane;

	phy_csg_step *csg_stack;
	float eps_min, eps_max;

	// UI stuff
	bool running;
	itc_chan ctl;
	SDL_mutex *rotate_lock;
	int64_t frame_index; // to avoid re-drawing the same fields
	int64_t step_real_time;
} phy_sim;

#include "console.h"

int phy_sim_create(phy_sim *sim);
void phy_sim_destroy(phy_sim *sim);
int phy_sim_create_fields(phy_sim *sim);
void phy_sim_destroy_fields(phy_sim *sim);
void phy_sim_compute_const_fields(phy_sim *sim);
void phy_sim_step(phy_sim *sim);
void phy_sim_csg_clear(phy_sim *sim);

int phy_thread(phy_sim *sim);

#endif // _PHYSICS_H