Comments (2)
UPDATE: Integrating the following C++ code would give a major speedup. Since the C++ already compiles and runs, and the python code already creates arrays with dimensions the C++ code expects, all we'd need to figure out are the C/Python bindings, which should be straightforward though probably time consuming.
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
// number of components
static int NC = 2;
// number of stations
static int NS = 20;
// number of time samples
static int NT = 950;
// number of Green's functions per component
static int NF = 6;
// number of time shift groups
static int NG = 2;
// number of depths
static int ND = 1;
// number of moment tensors
static int NM = 50000;
static int min_shift = -100;
static int max_shift = 100;
// length of padding
int NP = max_shift - min_shift + 1;
// length of padded records
int NTP = NT + max_shift - min_shift + 1;
void get_synthetics(double greens[], double synthetics[], double mt[]) {
int i,j;
// computes synthetics through linear combination of Green's functions
for (j=0 ; j<NC*NS*NTP; j++) {
synthetics[j] = 0.;
}
for (i=0 ; i<NF; i++) {
for (j=0 ; j<NC*NS*NTP; j++) {
synthetics[j] += greens[i*NC*NS*NTP + j] * mt[i];
}
}
}
void get_shifts(double corr[], double corr_sum[], double corr_max[], int corr_argmax[], double mt[], int shifts[], int is_component_in_group[], int component_to_group[]) {
// gets time shifts between the given data and synthetics corresponding to
// the given source
// this implementation is very flexible with regard to whether
// time shifts are fixed or vary
int i,j,k,l;
int i0;
for (j=0 ; j<NG*NS*NP; j++) {
corr_sum[j] = 0.;
}
for (j=0 ; j<NG*NS; j++) {
corr_max[j] = 0.;
}
// compute cross correlations
for (i=0; i<NF; i++) {
for (j=0; j<NG; j++) {
for (k=0; k<NC; k++) {
if (is_component_in_group[j*NC + k]==1) {
continue;
}
for (l=0; l<NS*NP; l++) {
corr_sum[i*NC*NS*NP + j*NC*NS + l] +=
corr[i*NC*NS*NP + k*NC*NS + l] * mt[i];
}
}
}
}
// which offset has the largest correlation?
for (i=0; i<NG*NS; i++) {
for (j=0; j<NP; j++) {
if (corr_max[i] < corr_sum[i*NP + j]) {
corr_max[i] = corr_sum[i*NP + j];
corr_argmax[i] = i*NP + j;
}
}
}
// fill in components
for (i=0; i<NC; i++) {
i0 = component_to_group[i];
for (j=0; j<NS; j++) {
shifts[i*NS + j] = corr_argmax[i0*NS + j];
}
}
}
double L1(double data[], double synthetics[], int mask[], int shifts[]) {
// evaluates L1 misfit between data and shifted synthetics
int i,j;
int j0;
double sum_misfit = 0.;
for (i=0 ; i<NC*NS; i++) {
// skip missing components
if (mask[i]==0) {
continue;
}
j0 = shifts[i];
for (j=0 ; j<NT; j++) {
sum_misfit += mask[i*NT + j] * abs(
data[i*NT + j] - synthetics[i*NT + j+j0]);
}
}
}
int main() {
double *data;
double *synthetics;
int *mask;
double *greens;
double *corr;
double *corr_sum;
double *corr_max;
int *corr_argmax;
double *mt;
double *results;
int *component_to_group;
int *is_component_in_group;
int *shifts;
data = new double [NC*NS*NT];
synthetics = new double [NC*NS*NTP];
mask = new int [NC*NS];
greens = new double [NF*NC*NS*NTP];
corr = new double [NF*NC*NS*NP];
corr_sum = new double [NG*NS*NP];
corr_max = new double [NG*NS];
corr_argmax = new int [NG*NS];
is_component_in_group = new int [NG*NC]();
component_to_group = new int [NC]();
shifts = new int [NC*NS]();
mt = new double [NF*NM];
results = new double [NM];
// populate arrays
for (int i=0; i<NC*NS; i++) {
mask[i] = 1;
}
// start timer
std::clock_t start;
double duration;
start = std::clock();
for (int i=0; i<NM; i++) {
//cout << "i: " << i << endl;
// compute shifted synthetics
get_synthetics(greens, synthetics, &mt[i]);
get_shifts(corr, corr_sum, corr_max, corr_argmax, &mt[i],
shifts, is_component_in_group, component_to_group);
// evaluate misfit
results[i] = L1(data, synthetics, mask, shifts);
}
// stop timer
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout<<"elapsed time: "<< duration <<'\n';
return 0;
}
from mtuq.
Implemented in newest version
from mtuq.
Related Issues (20)
- user_supplied arrivals at selected stations HOT 2
- Need for small QOL improvement for mtuq.event.Force HOT 3
- C/C++ Pointer error when running container with apptainer HOT 2
- meca [error] running serialgridsearch.dc.py example HOT 4
- mtuq install fails due to instaseis & gfortran HOT 5
- Plotting error function plot_waveforms1 HOT 2
- Specfem3D-derived Green Functions expected units HOT 2
- Update instructions to use mamba (or libmamba) solver HOT 6
- ImportError on jupyter notebook HOT 3
- polarity text label for waveform fits plot HOT 2
- incorporate static time shifts into the values displayed in the waveform fits plots HOT 3
- Error when using download_greens_tensors(stations, origin, model) HOT 3
- Using both HN? and BH? components for a single station HOT 2
- Suggested improvement for cross correlation time shifts
- amplitude scaling for waveform plots HOT 1
- Resample following bandpass filtering?
- ImportError during pip installation of mtuq HOT 1
- Run examples in terminal encountered plot errors HOT 3
- Possible discussion topic: Combined PySEP + MTUQ workflow HOT 2
- Possible discussion topics: Default colorbar limits and misfit normalization HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mtuq.