Skip to content

Commit 3204969

Browse files
committed
Add user sources from external wave files
1 parent 808a86b commit 3204969

31 files changed

+1575
-28
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,16 @@ endif()
8888
FIND_PACKAGE(VTK COMPONENTS IOXML REQUIRED)
8989
SET(VTK_DIR "/usr/lib/x86_64-linux-gnu/cmake/vtk-9.1" CACHE PATH "VTK directory override" FORCE)
9090

91+
SET(WAVE_INCLUDE src/wave)
92+
INCLUDE_DIRECTORIES(${WAVE_INCLUDE})
9193

94+
SET(WAVE_LD ${PROJECT_SOURCE_DIR}/build/bin)
95+
LINK_DIRECTORIES(${WAVE_LD})
9296

9397
# Libraries
9498
INCLUDE_DIRECTORIES(include)
9599

96100
# Sources
101+
ADD_SUBDIRECTORY(src/wave)
97102
ADD_SUBDIRECTORY(src)
103+

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ c0 = 100
113113

114114
# Source:
115115
# name = fct,x,y,z,size,intensity,frequency,phase,duration
116-
# - fct supported = [monopole, dipole, quadrupole, formula, file (coming soon)]
116+
# - fct supported = [monopole, dipole, quadrupole, formula, file (csv, wav)]
117117
# - if fct = formula => name = fct,"formula expr",x,y,z,size,duration (ex: formulat = 0.1 * sin(2 * pi * 50 * t))
118118
# - if fct = file => name = fct,"filename",x,y,z,size
119+
# suported file formats are : csv, wav
119120
# - (x,y,z) = source position
120121
# - intensity = source intensity
121122
# - frequency = source frequency
@@ -124,6 +125,7 @@ c0 = 100
124125
source1 = formula, "0.1 * sin(2 * pi * 50 * t)", 0.0,0.0,0.0, 0.1, 0.1
125126
source2 = monopole, 0.0,0.0,0.0, 0.1, 0.1,50,0,0.1
126127
source3 = file,"data/data.csv", 0.0,0.0,0.0, 0.1
128+
source4 = file,"data/data.wav", 0.0,0.0,0.0, 0.1
127129
# source4 = udf, "-0.1 * sin(2 * pi * 50 * t)", -0.5,0.0,0.0, 0.1, 0.1
128130

129131
# Initial condition:

data/data1.wav

32.5 KB
Binary file not shown.

data/data2.wav

210 KB
Binary file not shown.

data/data3.wav

152 KB
Binary file not shown.

data/data4.wav

32.6 KB
Binary file not shown.

doc/config/Square.conf

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
# Initial, final time and time step(t>0)
99
timeStart=0
10-
timeEnd=0.075
11-
timeStep=0.00001
10+
timeEnd=1.0
11+
timeStep=0.00005
1212

1313
# Saving rate:
1414
timeRate=0.001
@@ -39,25 +39,24 @@ c0 = 100
3939

4040
# Source:
4141
# name = fct,x,y,z,size,intensity,frequency,phase,duration
42-
# - fct supported = [monopole, dipole, quadrupole, formula, file (coming soon)]
43-
# - if fct = formula => name = fct,"formula expr",x,y,z,size,duration (ex: formula = 0.1 * sin(2 * pi * 50 * t))
42+
# - fct supported = [monopole, dipole, quadrupole, formula, file (csv, wav)]
43+
# - if fct = formula => name = fct,"formula expr",x,y,z,size,duration (ex: formulat = 0.1 * sin(2 * pi * 50 * t))
4444
# - if fct = file => name = fct,"filename",x,y,z,size
45+
# suported file formats are : csv, wav
4546
# - (x,y,z) = source position
4647
# - intensity = source intensity
4748
# - frequency = source frequency
4849
# NB: Extended source or Multiple sources are supported.
4950
# (source1 = ..., source2 = ...) indice must change.
50-
# source1 = monopole, 0.0,0.0,0.0, 0.1, 0.1,50,0,0.1
51-
# source1 = formula, "0.1 * sin(2 * pi * 50 * t)", 0.0,0.0,0.0, 0.1, 0.2
5251
source1 = file,"data/data.csv", 0.0,0.0,0.0, 0.1
53-
# source2 = udf, "-0.1 * sin(2 * pi * 50 * t)", -0.5,0.0,0.0, 0.1, 0.1
52+
# source2 = file,"data/data2.wav", 0.0,0.0,0.0, 0.1
5453

5554
# Observers position:
5655
# name = x,y,z, size
5756
# - (x,y,z) = position
5857
# NB: Multiple observers are supported and recursively added.
5958
# (observer1 = ..., observer2 = ...)
60-
# observer1 = 2.11792,0.00340081,0.0,0.1
59+
observer1 = 2.11792,0.00340081,0.0,0.1
6160
# observer2 = -2.11792,0.00340081,0.0,0.1
6261

6362
# Initial condition:

include/solver.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#include <configParser.h>
21
#include <Mesh.h>
2+
#include <configParser.h>
33

44
#ifndef DGALERKIN_SOLVER_H
55
#define DGALERKIN_SOLVER_H
66

7-
namespace solver {
7+
namespace solver
8+
{
89
/**
910
* Solve using forward explicit scheme. O(h)
1011
*
@@ -22,6 +23,9 @@ namespace solver {
2223
* @param config
2324
*/
2425
void rungeKutta(std::vector<std::vector<double>> &u, Mesh &mesh, Config config);
26+
27+
// std::vector<std::vector<float>> data4wave;
28+
2529
}
2630

2731
#endif

include/utils.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#include <iomanip>
66
#include <iostream>
77
#include <vector>
8+
9+
#include "file.h"
10+
#include "err.h"
11+
12+
813
// #include<execution>
914
// #include <assert.h>
1015

@@ -37,6 +42,8 @@
3742
//////////////////////////////////////////////////////////////
3843
// using namespace std;
3944

45+
std::string fileExtension(std::string file);
46+
4047
namespace screen_display
4148
{
4249
void write_string(std::string text, std::string color = YELLOW);
@@ -88,7 +95,9 @@ namespace io
8895
* @return data as vector of vector of doubles.
8996
*/
9097
std::vector<std::vector<double>> parseCSVFile(std::string inputFileName, char separator);
91-
98+
void writeWave(std::vector<float> V, std::string filename, uint32_t sample_rate, uint16_t bits_per_sample=16, uint16_t channel_number=1, size_t nb_sequence=1);
99+
void readWave(std::string filename, std::vector<float> &V, uint32_t &sample_rate);
100+
std::vector<std::vector<double>> parseWAVEFile(std::string inputFileName);
92101
}
93102
/////////////////////////
94103

src/CMakeLists.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# programs
2-
32
SET(SRCS dgalerkin.cpp configParser.cpp ../include/configParser.h Mesh.cpp ../include/Mesh.h utils.cpp ../include/utils.h solver.cpp ../include/solver.h ../include/eqEdit.h eqEdit.cpp)
43

54
ADD_EXECUTABLE(dgalerkin ${SRCS})
6-
7-
TARGET_LINK_LIBRARIES(dgalerkin ${GMSH_LIBRARIES} ${LAPACKBLAS_LIBRARIES} ${VTK_LIBRARIES} -fopenmp -ltbb -lpthread)
5+
TARGET_LINK_LIBRARIES(dgalerkin ${GMSH_LIBRARIES} ${LAPACKBLAS_LIBRARIES} ${VTK_LIBRARIES} -fopenmp -ltbb -lpthread -lwave)
86

97
vtk_module_autoinit(
108
TARGETS dgalerkin

src/configParser.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace config
7373
if (key.find("source") == 0)
7474
{
7575
std::vector<std::string> sep = split(iter->second, ',');
76-
76+
7777
if (sep[0] != "formula" && sep[0] != "file")
7878
{
7979
double x = std::stod(sep[1]);
@@ -116,7 +116,7 @@ namespace config
116116
}
117117
}
118118
else
119-
{
119+
{
120120

121121
if (sep[0] == "formula")
122122
{
@@ -148,9 +148,23 @@ namespace config
148148
double z = std::stod(sep[4]);
149149
double size = std::stod(sep[5]);
150150
// double duration = std::stod(sep[6]);
151-
// double pole = -1;
152-
Sources S("", {-1, x, y, z, size},io::parseCSVFile(filename,';'));
153-
config.sources.push_back(S);
151+
// double pole = -1;
152+
if (fileExtension(filename) == "csv")
153+
{
154+
Sources S("", {-1, x, y, z, size}, io::parseCSVFile(filename, ';'));
155+
config.sources.push_back(S);
156+
}
157+
else if (fileExtension(filename) == "wav")
158+
{
159+
Sources S("", {-1, x, y, z, size}, io::parseWAVEFile(filename));
160+
config.sources.push_back(S);
161+
}
162+
else
163+
{
164+
Fatal_Error("Not supported data")
165+
}
166+
167+
// getchar();
154168
}
155169
}
156170
}

src/solver.cpp

+30-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace solver
2323
std::vector<double> elStiffvector;
2424
std::vector<std::vector<std::vector<double>>> Flux;
2525

26+
std::vector<std::vector<float>> data4wave;
27+
2628
/**
2729
* Perform a numerical step: u[t+1] = dt*M^-1*(S[u[t]]-F[u[t]]) + beta*u[t]
2830
* for all elements in mesh object.
@@ -139,6 +141,8 @@ namespace solver
139141
outfile << "time;res_p;res_rho;res_vx;res_vy;res_vz;elapsed_time" << std::endl;
140142

141143
std::vector<std::ofstream> obs_outfile(config.observers.size());
144+
data4wave.clear();
145+
data4wave.resize(config.observers.size());
142146
for (int obs = 0; obs < config.observers.size(); ++obs)
143147
{
144148
std::string filename = "results/observers" + std::to_string(obs + 1) + ".txt";
@@ -192,7 +196,7 @@ namespace solver
192196

193197
for (int src = 0; src < config.sources.size(); ++src)
194198
{
195-
if (config.sources[src].formula == "")
199+
if (config.sources[src].formula == "" && config.sources[src].data.empty())
196200
{
197201
double amp = config.sources[src].source[5];
198202
double freq = config.sources[src].source[6];
@@ -204,10 +208,18 @@ namespace solver
204208
}
205209
else
206210
{
207-
double duration = config.sources[src].source[5];
208-
if (t < duration)
211+
if (config.sources[src].data.empty())
212+
{
213+
double duration = config.sources[src].source[5];
214+
if (t < duration)
215+
for (int n = 0; n < srcIndices[src].size(); ++n)
216+
u[0][srcIndices[src][n]] = config.sources[src].value(t);
217+
}
218+
else
219+
{
209220
for (int n = 0; n < srcIndices[src].size(); ++n)
210-
u[0][srcIndices[src][n]] = config.sources[src].value(t);
221+
u[0][srcIndices[src][n]] = config.sources[src].interpolate_value(t);
222+
}
211223
}
212224
}
213225

@@ -274,9 +286,13 @@ namespace solver
274286
v[0] /= w_sum;
275287
v[1] /= w_sum;
276288
v[2] /= w_sum;
289+
data4wave[obs].push_back(p);
277290
obs_outfile[obs] << t << ";" << rho << ";" << p << ";" << v[0] << ";" << v[1] << ";" << v[2] << std::endl;
278291
}
279292
}
293+
for (int obs = 0; obs < config.observers.size(); ++obs)
294+
io::writeWave(data4wave[obs], "results/observer_" + std::to_string(obs + 1) + ".wav", 1.0 / config.timeStep, 16, 1, 1);
295+
280296
outfile.close();
281297
for (int obs = 0; obs < config.observers.size(); ++obs)
282298
obs_outfile[obs].close();
@@ -381,7 +397,8 @@ namespace solver
381397
outfile << "time;log(res_p);log(res_rho);log(res_vx);log(res_vy);log(res_vz);elapsed_time(s)" << std::endl;
382398

383399
std::vector<std::ofstream> obs_outfile(config.observers.size());
384-
400+
data4wave.clear();
401+
data4wave.resize(config.observers.size());
385402
for (int obs = 0; obs < config.observers.size(); ++obs)
386403
{
387404
std::string filename = "results/observers" + std::to_string(obs + 1) + ".txt";
@@ -453,10 +470,11 @@ namespace solver
453470
if (t < duration)
454471
for (int n = 0; n < srcIndices[src].size(); ++n)
455472
u[0][srcIndices[src][n]] = config.sources[src].value(t);
456-
}else
473+
}
474+
else
457475
{
458-
for (int n = 0; n < srcIndices[src].size(); ++n)
459-
u[0][srcIndices[src][n]] = config.sources[src].interpolate_value(t);
476+
for (int n = 0; n < srcIndices[src].size(); ++n)
477+
u[0][srcIndices[src][n]] = config.sources[src].interpolate_value(t);
460478
}
461479
}
462480
}
@@ -547,9 +565,13 @@ namespace solver
547565
v[0] /= w_sum;
548566
v[1] /= w_sum;
549567
v[2] /= w_sum;
568+
data4wave[obs].push_back(p);
550569
obs_outfile[obs] << t << ";" << rho << ";" << p << ";" << v[0] << ";" << v[1] << ";" << v[2] << std::endl;
551570
}
552571
}
572+
for (int obs = 0; obs < config.observers.size(); ++obs)
573+
io::writeWave(data4wave[obs], "results/observer_" + std::to_string(obs + 1) + ".wav", 1.0 / config.timeStep, 16, 1, 1);
574+
553575
outfile.close();
554576
for (int obs = 0; obs < config.observers.size(); ++obs)
555577
obs_outfile[obs].close();

src/utils.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ extern "C"
2828

2929
//! added by Sofiane KHELLADI in 11/03/2022 /////////////////////
3030

31+
std::string fileExtension(std::string file)
32+
{
33+
34+
std::size_t found = file.find_last_of(".");
35+
return file.substr(found + 1);
36+
}
37+
38+
3139
namespace screen_display
3240
{
3341
void write_string(std::string text, std::string color)
@@ -168,6 +176,83 @@ namespace io
168176

169177
return data;
170178
}
179+
180+
void writeWave(std::vector<float> V, std::string filename, uint32_t sample_rate, uint16_t bits_per_sample, uint16_t channel_number, size_t nb_sequence)
181+
{
182+
wave::File write_file;
183+
// uint32_t sample_rate = 10000;//V.size();
184+
// uint16_t bits_per_sample = 16;
185+
// uint16_t channel_number = 1;
186+
// filename += ".wav";
187+
188+
std::vector<float> content;
189+
190+
for (size_t i = 0; i < nb_sequence; i++)
191+
for (auto v : V)
192+
{
193+
content.push_back(v);
194+
}
195+
196+
wave::Error err = write_file.Open(filename, wave::kOut);
197+
if (err)
198+
{
199+
Fatal_Error("Something went wrong in out open")
200+
}
201+
write_file.set_sample_rate(sample_rate);
202+
write_file.set_bits_per_sample(bits_per_sample);
203+
write_file.set_channel_number(channel_number);
204+
205+
err = write_file.Write(content);
206+
if (err)
207+
{
208+
Fatal_Error("Something went wrong in write")
209+
}
210+
}
211+
212+
void readWave(std::string filename, std::vector<float> &V, uint32_t &sample_rate)
213+
{
214+
wave::File read_file;
215+
wave::Error err = read_file.Open(filename, wave::kIn);
216+
if (err)
217+
{
218+
Fatal_Error("Something went wrong in out open")
219+
}
220+
221+
// ASSERT_EQ(read_file.sample_rate(), 44100);
222+
// ASSERT_EQ(read_file.bits_per_sample(), 16);
223+
// ASSERT_EQ(read_file.channel_number(), 2);
224+
sample_rate = read_file.sample_rate();
225+
err = read_file.Read(&V);
226+
if (err)
227+
{
228+
Fatal_Error("Something went wrong in read")
229+
}
230+
231+
std::cout << "source file="<< filename << std::endl;
232+
std::cout << "sample_rate=" << read_file.sample_rate() << std::endl;
233+
std::cout << "bits_per_sample=" << read_file.bits_per_sample() << std::endl;
234+
std::cout << "channel_number=" << read_file.channel_number() << std::endl;
235+
}
236+
237+
std::vector<std::vector<double>> parseWAVEFile(std::string inputFileName)
238+
{
239+
std::vector<std::vector<double>> data;
240+
uint32_t sample_rate;
241+
std::vector<float> V;
242+
243+
readWave(inputFileName,V,sample_rate);
244+
double timeStep = 1.0/sample_rate;
245+
double time(0);
246+
for(auto v:V)
247+
{
248+
data.push_back({time,v});
249+
time += timeStep;
250+
}
251+
252+
// writeWave(V,"data/data_write.wav",sample_rate,16,1,1);
253+
254+
return data;
255+
}
171256
}
172257

173258
// Lapack with direct calling to fortran interface.

0 commit comments

Comments
 (0)