Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pygrt/C_extension/include/grt/common/checkerror.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@

// GRT自定义报错信息
#define GRTRaiseError(ErrorMessage, ...) ({\
fprintf(stderr, BOLD_RED ErrorMessage DEFAULT_RESTORE, ##__VA_ARGS__);\
fprintf(stderr, BOLD_RED ErrorMessage "\n" DEFAULT_RESTORE, ##__VA_ARGS__);\
exit(EXIT_FAILURE);\
})

// GRT自定义警告信息,不结束程序
#define GRTRaiseWarning(WarnMessage, ...) ({\
fprintf(stderr, BOLD_YELLOW WarnMessage "\n" DEFAULT_RESTORE, ##__VA_ARGS__);\
})

// GRT报错:选项设置不符要求
#define GRTBadOptionError(name, X, MoreErrorMessage, ...) ({\
GRTRaiseError("[%s] Error in \"-"#X"\". "MoreErrorMessage" Use \"-h\" for help.\n", name, ##__VA_ARGS__);\
Expand Down
37 changes: 13 additions & 24 deletions pygrt/C_extension/src/common/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "grt/common/model.h"
#include "grt/common/prtdbg.h"
#include "grt/common/attenuation.h"
#include "grt/common/colorstr.h"
#include "grt/common/util.h"

#include "grt/common/checkerror.h"
Expand Down Expand Up @@ -230,8 +229,7 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat
h = va = vb = rho = qa = qb = 0.0;
MYINT nscan = sscanf(line, "%lf %lf %lf %lf %lf %lf\n", &h, &va, &vb, &rho, &qa, &qb);
if(ncols != nscan && ncols_noQ != nscan){
fprintf(stderr, "[%s] " BOLD_RED "Model file read error in line %d.\n" DEFAULT_RESTORE, command, iline);
return NULL;
GRTRaiseError("[%s] Model file read error in line %d.\n", command, iline);
};

// 读取首行,如果首行首列为 0 ,则首列指示每层顶界面深度而非厚度
Expand All @@ -240,18 +238,15 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat
}

if(va <= 0.0 || rho <= 0.0 || (ncols == nscan && (qa <= 0.0 || qb <= 0.0))){
fprintf(stderr, "[%s] " BOLD_RED "In model file, line %d, nonpositive value is not supported.\n" DEFAULT_RESTORE, command, iline);
return NULL;
GRTRaiseError("[%s] In model file, line %d, nonpositive value is not supported.\n", command, iline);
}

if(vb < 0.0){
fprintf(stderr, "[%s] " BOLD_RED "In model file, line %d, negative Vs is not supported.\n" DEFAULT_RESTORE, command, iline);
return NULL;
GRTRaiseError("[%s] In model file, line %d, negative Vs is not supported.\n", command, iline);
}

if(!allowLiquid && vb == 0.0){
fprintf(stderr, "[%s] " BOLD_RED "In model file, line %d, Vs==0.0 is not supported.\n" DEFAULT_RESTORE, command, iline);
return NULL;
GRTRaiseError("[%s] In model file, line %d, Vs==0.0 is not supported.\n", command, iline);
}

modarr = (double(*)[ncols])realloc(modarr, sizeof(double)*ncols*(nlay+1));
Expand All @@ -267,8 +262,7 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat
}

if(iline==0 || modarr==NULL){
fprintf(stderr, "[%s] " BOLD_RED "Model file read error.\n" DEFAULT_RESTORE, command);
return NULL;
GRTRaiseError("[%s] Model file %s read error.\n", command, modelpath);
}

// 如果读取了深度,转为厚度
Expand All @@ -293,9 +287,8 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat

// 允许最后一层厚度为任意值
if(h <= 0.0 && i < nlay0-1 ) {
fprintf(stderr, "[%s] " BOLD_RED "In line %d, nonpositive thickness (except last layer)"
" is not supported.\n" DEFAULT_RESTORE, command, i+1);
return NULL;
GRTRaiseError("[%s] In line %d, nonpositive thickness (except last layer)"
" is not supported.\n", command, i+1);
}

// 划分震源层和接收层
Expand Down Expand Up @@ -345,24 +338,20 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat

// 检查,接收点不能位于液-液、固-液界面
if(ircv < nlay-1 && mod1d->Thk[ircv] == 0.0 && mod1d->Vb[ircv]*mod1d->Vb[ircv+1] == 0.0){
fprintf(stderr,
"[%s] " BOLD_RED "The receiver is located on the interface where there is liquid on one side. "
GRTRaiseError(
"[%s] The receiver is located on the interface where there is liquid on one side. "
"Due to the discontinuity of the tangential displacement on this interface, "
"to reduce ambiguity, you should add a small offset to the receiver depth, "
"thereby explicitly placing it within a specific layer. \n"
DEFAULT_RESTORE, command);
return NULL;
"thereby explicitly placing it within a specific layer. \n", command);
}

// 检查 --> 源点不能位于液-液、固-液界面
if(isrc < nlay-1 && mod1d->Thk[isrc] == 0.0 && mod1d->Vb[isrc]*mod1d->Vb[isrc+1] == 0.0){
fprintf(stderr,
"[%s] " BOLD_RED "The source is located on the interface where there is liquid on one side. "
GRTRaiseError(
"[%s] The source is located on the interface where there is liquid on one side. "
"Due to the discontinuity of the tangential displacement on this interface, "
"to reduce ambiguity, you should add a small offset to the source depth, "
"thereby explicitly placing it within a specific layer. \n"
DEFAULT_RESTORE, command);
return NULL;
"thereby explicitly placing it within a specific layer. \n", command);
}

// 将每层顶界面深度写入数组
Expand Down
9 changes: 4 additions & 5 deletions pygrt/C_extension/src/common/sacio2.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@

#include "grt/common/sacio2.h"
#include "grt/common/sacio.h"
#include "grt/common/colorstr.h"

#include "grt/common/checkerror.h"


void grt_read_SAC_HEAD(const char *command, const char *name, SACHEAD *hd){
int lswap = read_sac_head(name, hd);
if(lswap == -1){
fprintf(stderr, "[%s] " BOLD_RED "read %s head failed.\n" DEFAULT_RESTORE, command, name);
exit(EXIT_FAILURE);
GRTRaiseError("[%s] read %s head failed.\n", command, name);
}
}


float * grt_read_SAC(const char *command, const char *name, SACHEAD *hd, float *arrout){
float *arrin=NULL;
if((arrin = read_sac(name, hd)) == NULL){
fprintf(stderr, "[%s] " BOLD_RED "read %s failed.\n" DEFAULT_RESTORE, command, name);
exit(EXIT_FAILURE);
GRTRaiseError("[%s] read %s failed.\n", command, name);
}

if(arrout!=NULL){
Expand Down
8 changes: 5 additions & 3 deletions pygrt/C_extension/src/common/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "grt/common/myfftw.h"
#include "grt/travt/travt.h"

#include "grt/common/checkerror.h"

char ** grt_string_split(const char *string, const char *delim, int *size)
{
char *str_copy = strdup(string); // 创建字符串副本,以免修改原始字符串
Expand Down Expand Up @@ -388,10 +390,10 @@ void grt_GF_freq2time_write_to_file(

// 输出警告:当震源位于液体层中时,仅允许计算爆炸源对应的格林函数
if(mod1d->Vb[mod1d->isrc]==0.0){
fprintf(stderr, "[%s] " BOLD_YELLOW
"The source is located in the liquid layer, "
GRTRaiseWarning(
"[%s] The source is located in the liquid layer, "
"therefore only the Green's Funtions for the Explosion source will be computed.\n"
DEFAULT_RESTORE, command);
, command);
}

}
3 changes: 1 addition & 2 deletions pygrt/C_extension/src/dynamic/grt_greenfn.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,7 @@ static void getopt_from_command(GRT_MODULE_CTRL *Ctrl, int argc, char **argv){
int i1, i2, i3, i4;
i1 = i2 = i3 = i4 = 0;
if(0 == sscanf(optarg, "%d/%d/%d/%d", &i1, &i2, &i3, &i4)){
fprintf(stderr, "[%s] " BOLD_RED "Error in -G.\n" DEFAULT_RESTORE, command);
exit(EXIT_FAILURE);
GRTBadOptionError(command, G, "");
};
Ctrl->G.doEX = (i1!=0);
Ctrl->G.doVF = (i2!=0);
Expand Down
2 changes: 1 addition & 1 deletion pygrt/C_extension/src/dynamic/grt_rotation.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int rotation_main(int argc, char **argv){
// 保存文件前缀
Ctrl->s_prefix = (char*)malloc(sizeof(char)*(strlen(Ctrl->s_dirpath)+1));
if(2 != sscanf(Ctrl->s_dirpath, "%[^/]/%s", Ctrl->s_synpath, Ctrl->s_prefix)){
GRTRaiseError("[%s] " BOLD_RED "Error format in \"%s\".\n" DEFAULT_RESTORE, command, Ctrl->s_dirpath);
GRTRaiseError("[%s] Error format in \"%s\".\n", command, Ctrl->s_dirpath);
}

// 检查是否存在该目录
Expand Down
2 changes: 1 addition & 1 deletion pygrt/C_extension/src/dynamic/grt_strain.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int strain_main(int argc, char **argv){
// 保存文件前缀
Ctrl->s_prefix = (char*)malloc(sizeof(char)*(strlen(Ctrl->s_dirpath)+1));
if(2 != sscanf(Ctrl->s_dirpath, "%[^/]/%s", Ctrl->s_synpath, Ctrl->s_prefix)){
GRTRaiseError("[%s] " BOLD_RED "Error format in \"%s\".\n" DEFAULT_RESTORE, command, Ctrl->s_dirpath);
GRTRaiseError("[%s] Error format in \"%s\".\n", command, Ctrl->s_dirpath);
}

// 检查是否存在该目录
Expand Down
4 changes: 2 additions & 2 deletions pygrt/C_extension/src/dynamic/grt_stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ int stress_main(int argc, char **argv){
// 保存文件前缀
Ctrl->s_prefix = (char*)malloc(sizeof(char)*(strlen(Ctrl->s_dirpath)+1));
if(2 != sscanf(Ctrl->s_dirpath, "%[^/]/%s", Ctrl->s_synpath, Ctrl->s_prefix)){
GRTRaiseError("[%s] " BOLD_RED "Error format in \"%s\".\n" DEFAULT_RESTORE, command, Ctrl->s_dirpath);
GRTRaiseError("[%s] Error format in \"%s\".\n", command, Ctrl->s_dirpath);
}

// 检查是否存在该目录
Expand Down Expand Up @@ -119,7 +119,7 @@ int stress_main(int argc, char **argv){
float Qainv=hd.user4;
float Qbinv=hd.user5;
if(va <= 0.0 || vb < 0.0 || rho <= 0.0){
fprintf(stderr, "[%s] " BOLD_RED "Error! Bad rcv_va, rcv_vb or rcv_rho in \"%s\" header.\n" DEFAULT_RESTORE, command, s_filepath);
fprintf(stderr, "[%s] Error! Bad rcv_va, rcv_vb or rcv_rho in \"%s\" header.\n", command, s_filepath);
exit(EXIT_FAILURE);
}
// 申请内存
Expand Down
8 changes: 3 additions & 5 deletions pygrt/C_extension/src/dynamic/layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include "grt/common/model.h"
#include "grt/common/prtdbg.h"
#include "grt/common/matrix.h"
#include "grt/common/colorstr.h"

#include "grt/common/checkerror.h"

void grt_calc_R_tilt_PSV(MYCOMPLEX xa0, MYCOMPLEX xb0, MYCOMPLEX kbkb0, MYREAL k, MYCOMPLEX R_tilt[2][2], MYINT *stats)
{
Expand Down Expand Up @@ -214,8 +214,7 @@ void grt_calc_RT_ls_PSV(
bool isfluidUp = (mu1 == 0.0); // 上层是否为液体
MYINT sgn = 1;
if(isfluidUp && mu2 == 0.0){
fprintf(stderr, BOLD_RED "Error: fluid-fluid interface is not allowed in calc_RT_ls_2x2\n" DEFAULT_RESTORE);
exit(EXIT_FAILURE);
GRTRaiseError("Error: fluid-fluid interface is not allowed in function %s\n", __func__);
}

// 使用指针
Expand Down Expand Up @@ -299,8 +298,7 @@ void grt_calc_RT_ls_SH(
// 讨论液-固 or 固-液
bool isfluidUp = (mu1 == 0.0); // 上层是否为液体
if(isfluidUp && mu2 == 0.0){
fprintf(stderr, BOLD_RED "Error: fluid-fluid interface is not allowed in calc_RT_ls_2x2\n" DEFAULT_RESTORE);
exit(EXIT_FAILURE);
GRTRaiseError("Error: fluid-fluid interface is not allowed in function %s\n", __func__);
}

// 使用指针
Expand Down
37 changes: 13 additions & 24 deletions pygrt/C_extension/src/dynamic/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

#include "grt/dynamic/signals.h"
#include "grt/common/const.h"
#include "grt/common/colorstr.h"
#include "grt/common/util.h"

#include "grt/common/checkerror.h"


bool grt_check_tftype_tfparams(const char tftype, const char *tfparams){

Expand All @@ -32,45 +33,39 @@ bool grt_check_tftype_tfparams(const char tftype, const char *tfparams){
float t0=0.0;
if(1 != sscanf(tfparams, "%f", &t0)) return false;
if(t0 <= 0){
fprintf(stderr, BOLD_RED "Error! t0(%s) should be larger than 0.\n" DEFAULT_RESTORE, tfparams);
return false;
GRTRaiseError("Error! t0(%s) should be larger than 0.\n", tfparams);
}
}
// 梯形波
else if(GRT_SIG_TRAPEZOID == tftype){
float t1=0.0, t2=0.0, t3=0.0;
if(3 != sscanf(tfparams, "%f/%f/%f", &t1, &t2, &t3)) return false;
if(t1 < 0.0 || t2 < 0.0 || t3 <= 0.0){
fprintf(stderr, BOLD_RED "Error! It should be t1>=0.0, t2>=0.0 and t3>0.0 (%s).\n" DEFAULT_RESTORE, tfparams);
return false;
GRTRaiseError("Error! It should be t1>=0.0, t2>=0.0 and t3>0.0 (%s).\n", tfparams);
}
if(! (t1 <= t2 && t2 < t3)){
fprintf(stderr, BOLD_RED "Error! It should be t1<=t2<t3 (%s).\n" DEFAULT_RESTORE, tfparams);
return false;
GRTRaiseError("Error! It should be t1<=t2<t3 (%s).\n", tfparams);
}
}
// 雷克子波
else if(GRT_SIG_RICKER == tftype){
float f0;
if(1 != sscanf(tfparams, "%f", &f0)) return false;
if(f0 <= 0){
fprintf(stderr, BOLD_RED "Error! f0(%s) should be larger than 0.\n" DEFAULT_RESTORE, tfparams);
return false;
GRTRaiseError("Error! f0(%s) should be larger than 0.\n", tfparams);
}
}
// 自定义时间函数
else if(GRT_SIG_CUSTOM == tftype){
// tfparams为存储自定义时间函数的文件名
// 检查文件是否存在
if(access(tfparams, F_OK) != 0){
fprintf(stderr, BOLD_RED "Error! (%s) not exists.\n" DEFAULT_RESTORE, tfparams);
return false;
GRTRaiseError("Error! (%s) not exists.\n", tfparams);
}
}
// 不符合要求
else{
fprintf(stderr, BOLD_RED "Error! Unsupported type '%c'.\n " DEFAULT_RESTORE, tftype);
return false;
GRTRaiseError("Error! Unsupported time function type '%c'.\n", tftype);
}

return true;
Expand Down Expand Up @@ -204,8 +199,7 @@ float * grt_get_parabola_wave(float dt, float *Tlen, int *Nt){
int nt = floorf(tlen/dt);
if(fabsf(tlen - nt*dt) <= 1e-6) nt--;
if(nt==0) {
fprintf(stderr, BOLD_RED "Error! window length of time function is too short.\n" DEFAULT_RESTORE);
return NULL;
GRTRaiseError("Error! window length of time function is too short.\n");
}
nt += 2;
tlen = (nt-1)*dt;
Expand Down Expand Up @@ -292,9 +286,7 @@ float * grt_get_trap_wave(float dt, float *T1, float *T2, float *T3, int *Nt){

float * grt_get_ricker_wave(float dt, float f0, int *Nt){
if(1.0/dt <= 2.0*f0) { // 在当前采样率下,主频f0过高
fprintf(stderr, BOLD_RED "Error! Compare to sampling freq (%.3f), dominant freq (%.3f) is too high.\n" DEFAULT_RESTORE,
1.0/dt, f0);
return NULL;
GRTRaiseError("Error! Compare to sampling freq (%.3f), dominant freq (%.3f) is too high.\n", 1.0/dt, f0);
}

float t0 = 1.0/f0;
Expand All @@ -319,8 +311,7 @@ float * grt_get_custom_wave(int *Nt, const char *tfparams){
float *tfarr = (float*)malloc(sizeof(float)*1);
FILE *fp;
if((fp = fopen(tfparams, "r")) == NULL){
fprintf(stderr, BOLD_RED "custom time function file open error.\n" DEFAULT_RESTORE);
return NULL;
GRTRaiseError("custom time function file open error.\n");
}

// 逐行读入
Expand All @@ -334,15 +325,13 @@ float * grt_get_custom_wave(int *Nt, const char *tfparams){

tfarr = (float*)realloc(tfarr, sizeof(float)*(nt+1));
if(sscanf(line, " %f", &tfarr[nt]) < 1){
fprintf(stderr, BOLD_RED "custom time function file read error.\n" DEFAULT_RESTORE);
return NULL;
GRTRaiseError("custom time function file read error.\n");
}
nt++;
}

if(nt == 0){
fprintf(stderr, BOLD_RED "custom time function file read error. Empty?\n" DEFAULT_RESTORE);
return NULL;
GRTRaiseError("custom time function file read error. Empty?\n");
}

fclose(fp);
Expand Down