-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrw.hpp
More file actions
110 lines (98 loc) · 2.91 KB
/
rw.hpp
File metadata and controls
110 lines (98 loc) · 2.91 KB
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
103
104
105
106
107
108
109
110
#pragma once
#pragma warning ( disable : 4996 )
#include <cstdio>
#include <omp.h>
#include <ctime>
#include <string>
#include "body.hpp"
#include <Windows.h>
using namespace std;
void read_test(const int argc, char *argv[], int &bodies_number, coord_t &space_radius, body_t *bodies, RGBTRIPLE *spheres, speed_t *speed, duration_t &interval ){
string file_name = argc >= 2 ? string( argv[1] ) : "input.dat";
FILE *fh = fopen ( file_name.c_str(), "r" );
if ( NULL == fh ){
printf ( "File %s not found\n", file_name.c_str() );
exit(1);
}
if ( fscanf( fh, "%d ", &bodies_number ) != 1 ){
printf ( "Bodies number not found\n" );
exit(2);
}
if ( fscanf ( fh, "%lf ", &space_radius ) != 1 ){
printf ( "Space radius not found\n" );
exit(3);
}
for ( int i = 0; i < bodies_number; ++i ){
static int red, green, blue;
if ( fscanf (
fh,
"%lf %lf %lf %lf %lf %d %d %d ",
&bodies[i].x, &bodies[i].y,
&speed[i].x, &speed[i].y,
&bodies[i].mass,
&red, &green, &blue
) != 8 ){
printf ( "Full information about body #%d not found\n", i+1 );
exit(4);
}
spheres[i].rgbtBlue = (BYTE)blue;
spheres[i].rgbtGreen = (BYTE)green;
spheres[i].rgbtRed = (BYTE)red;
}
interval = argc >= 3 ? atof ( argv[2] ) : 1;
}
void write_forces( const int argc, char *argv[], const int bodies_number, const force_t *forces ){
string file_name = argc >= 3 ? argv[2] : "output.txt";
FILE *fh = fopen(file_name.c_str(), "w");
for ( int i = 0; i < bodies_number; ++i ){
fprintf ( fh, "Body #%d: force: (%f; %f)\n", i+1, forces[i].x, forces[i].y );
}
fclose(fh);
}
void write_bodies ( const int argc, char *argv[], const int bodies_number, const body_t *bodies, const RGBTRIPLE *spheres, const speed_t *speed, const coord_t space_radius ){
string file_name = argc >= 4 ? argv[3] : "bodies.txt";
FILE *fh = fopen ( file_name.c_str(), "w" );
fprintf ( fh, "%d\n%lf\n", bodies_number, space_radius );
for ( int i = 0; i < bodies_number; ++i ){
fprintf (
fh,
"%lf %lf %lf %lf %lf %d %d %d\n",
bodies[i].x, bodies[i].y,
speed[i].x, speed[i].y,
bodies[i].mass,
spheres[i].rgbtRed, spheres[i].rgbtGreen, spheres[i].rgbtBlue
);
}
fclose ( fh );
}
static
#ifdef STUDIO
double
#else
time_t
#endif
start, finish;
inline void timer_start(){
start =
#ifdef STUDIO
omp_get_wtime();
#else
time(NULL);
#endif
}
inline void timer_finish(){
finish =
#ifdef STUDIO
omp_get_wtime();
#else
time(NULL);
#endif
}
inline double timer_duration(){
return
#ifdef STUDIO
finish-start;
#else
difftime(finish, start);
#endif
}