initial upload

This commit is contained in:
2024-09-10 20:25:18 +08:00
parent b8de03ee4f
commit f1cc876972
377 changed files with 2721267 additions and 34 deletions

View File

@@ -0,0 +1,384 @@
#ifndef _DISPHELP_H
#define _DISPHELP_H
#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iomanip>
#include <sys/ioctl.h>
#include "vector"
using namespace std;
typedef vector<string> strArray;
struct option
{
string flag_s,flag_l;
string message;
strArray sec_message;
option()
{
flag_s = flag_l = message = "";
}
};
typedef vector<option> opArray;
class dispHelp
{
public:
dispHelp(){
front_space = 0;
back_space = 10;
ex_name = "Execuable";
version = "0.0.1";
descript = "Brief information about this command.";
author = "Author's information.";
}
~dispHelp(){}
void addHeadInfo(string,string,string,string);
void addUsage(string);
void addOption(string,string,string);
void addOptionSec(string,int);
void addExample(string);
void changeLayerOut(int,int);
void show();
private:
string ex_name,version,descript,author;
int front_space,back_space;
opArray options;
strArray examples;
strArray usages;
};
void dispHelp::addHeadInfo(string s1,string s2,string s3,string s4)
{
ex_name = s1; version = s2; descript = s3; author = s4;
return;
}
void dispHelp::addUsage(string usg)
{
usages.push_back(usg);
return;
}
void dispHelp::addOption(string msg,string sflag,string lflag = "")
{
option tmp_option;
tmp_option.message = msg; tmp_option.flag_s = sflag; tmp_option.flag_l = lflag;
options.push_back(tmp_option);
return;
}
void dispHelp::addOptionSec(string msg,int index = -1)
{
if (index < 0)
{
options.back().sec_message.push_back(msg);
}
else options[index].sec_message.push_back(msg);
return;
}
void dispHelp::addExample(string ex)
{
examples.push_back(ex);
return;
}
void dispHelp::changeLayerOut(int left,int right)
{
front_space = left; back_space = right;
return;
}
void dispHelp::show()
{
int line_length;
string segment,full_message;
stringstream ss_message;
//获取终端窗口的行列数
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
//显示头信息
full_message = ex_name + " " + version + " - " + descript;
ss_message.clear(); ss_message.str(full_message);
line_length = front_space + back_space;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space + back_space))
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+9+front_space+back_space);
}
}
clog << endl;
ss_message.clear(); ss_message.str(author);
line_length = front_space + back_space;;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space + back_space))
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << "Author: " << segment << " ";
line_length += (segment.length()+9);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+9+front_space+back_space);
}
}
clog << endl;
if (!usages.empty())
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << "Usage:" << endl;
for (int i = 0; i < usages.size(); i++)
{
ss_message.clear(); ss_message.str(usages[i]);
line_length = front_space + back_space + 4;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+4))
{
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+10+front_space+back_space);
}
}
clog << endl;
}
}
if (!options.empty())
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << "Options:" << endl;
for (int i = 0; i < options.size(); i++)
{
if (options[i].flag_l == "")
{
full_message = options[i].flag_s+" : "+options[i].message;
ss_message.clear(); ss_message.str(full_message);
line_length = front_space + back_space + 4;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+4))
{
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+10+front_space+back_space);
}
}
clog << endl;
if (!options[i].sec_message.empty())
{
for (int j = 0; j < options[i].sec_message.size(); j++)
{
ss_message.clear(); ss_message.str(options[i].sec_message[j]);
line_length = front_space + back_space + 9;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+9))
{
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+13; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+14+front_space+back_space);
}
}
clog << endl;
}
}
}
else
{
full_message = options[i].flag_s+" | "+options[i].flag_l+" : "+options[i].message;
ss_message.clear(); ss_message.str(full_message);
line_length = front_space + back_space + 4;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+4))
{
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+10+front_space+back_space);
}
}
clog << endl;
if (!options[i].sec_message.empty())
{
for (int j = 0; j < options[i].sec_message.size(); j++)
{
ss_message.clear(); ss_message.str(options[i].sec_message[j]);
line_length = front_space + back_space + 9;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+9))
{
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+13; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+14+front_space+back_space);
}
}
clog << endl;
}
}
}
}
}
if (!examples.empty())
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << "Examples:" << endl;
for (int i = 0; i < examples.size(); i++)
{
ss_message.clear(); ss_message.str(examples[i]);
line_length = front_space + back_space + 4;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+4))
{
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+10+front_space+back_space);
}
}
clog << endl;
}
}
return;
}
#endif

View File

@@ -0,0 +1,318 @@
#include "includes.h"
class grav2d_cube
{
public:
grav2d_cube(){}
~grav2d_cube(){}
int routine(char*,char*,char*);
int initCubes(char*);
int initObs(char*);
void outObs();
void calG();
void calGx();
void calGy();
void calGz();
private:
int obsNum, cubeNum;
cubeArray modCube;
obspointArray obsPoint;
};
int grav2d_cube::routine(char* calType,char* obsPara,char* modPara)
{
if (initCubes(modPara)) return -1;
if (initObs(obsPara)) return -1;
if (!strcmp(calType,"gravity")) calG();
else if (!strcmp(calType,"gx")) calGx();
else if (!strcmp(calType,"gy")) calGy();
else if (!strcmp(calType,"gz")) calGz();
else
{
cerr << BOLDRED << "error ==> " << RESET << "unknown calculation type: " << calType << endl;
return -1;
}
outObs();
return 0;
}
int grav2d_cube::initCubes(char* para)
{
cube temp_cube;
string temp_str;
stringstream temp_ss;
if (7 == sscanf(para,"%lf/%lf/%lf/%lf/%lf/%lf/%lf",
&temp_cube.cen.x,&temp_cube.cen.y,&temp_cube.cen.z,&temp_cube.dx,&temp_cube.dy,&temp_cube.dz,&temp_cube.rho))
{
modCube.push_back(temp_cube);
}
else
{
ifstream infile;
if (open_infile(infile,para)) return -1;
while(getline(infile,temp_str))
{
if (*(temp_str.begin()) == '#') continue;
else
{
//按每行7个数据解析 初始化为用于正演的观测点
if (7 == sscanf(temp_str.c_str(),"%lf/%lf/%lf/%lf/%lf/%lf/%lf",
&temp_cube.cen.x,&temp_cube.cen.y,&temp_cube.cen.z,&temp_cube.dx,&temp_cube.dy,&temp_cube.dz,&temp_cube.rho))
modCube.push_back(temp_cube);
else
{
cerr << BOLDYELLOW << "ignored ==> " << RESET << "wrong input: " << temp_str << endl;
continue;
}
}
}
infile.close();
}
if (modCube.empty())
{
cerr << BOLDRED << "error ==> " << RESET << "fail to initial cubes with the parameter: " << para << endl;
return -1;
}
else cubeNum = modCube.size();
return 0;
}
int grav2d_cube::initObs(char* para)
{
obspoint temp_obs;
string temp_str;
stringstream temp_ss;
double x,y;
double xmin,xmax,ymin,ymax;
double xs,xe,ys,ye,eleva,dx,dy;
//按格式解析参数 初始化观测位置 用于正演计算
if (7 == sscanf(para,"%lf/%lf/%lf/%lf/%lf/%lf/%lf",&xs,&dx,&xe,&ys,&dy,&ye,&eleva))
{
xmin = MIN(xs,xe); xmax = MAX(xs,xe);
ymin = MIN(ys,ye); ymax = MAX(ys,ye);
y = ys;
while(y >= ymin && y <= ymax)
{
x = xs;
while(x >= xmin && x <= xmax)
{
temp_obs.id = obsPoint.size();
temp_obs.x = x; temp_obs.y = y; temp_obs.z = -1.0*eleva;
temp_obs.val = 0.0;
obsPoint.push_back(temp_obs);
x += dx;
}
y += dy;
}
}
//解析失败 按文件读入 用于反演使用或者正演计算
else
{
ifstream infile;
if (open_infile(infile,para)) return -1;
while(getline(infile,temp_str))
{
if (*(temp_str.begin()) == '#') continue;
else
{
//按每行3个数据解析 初始化为用于正演的观测点
if (3 == sscanf(temp_str.c_str(),"%lf %lf %lf",&temp_obs.x,&temp_obs.y,&temp_obs.z))
{
temp_obs.z *= -1.0;
temp_obs.val = 0.0;
temp_obs.id = obsPoint.size();
obsPoint.push_back(temp_obs);
}
else
{
cerr << BOLDYELLOW << "ignored ==> " << RESET << "wrong input: " << temp_str << endl;
continue;
}
}
}
infile.close();
}
if (obsPoint.empty())
{
cerr << BOLDRED << "error ==> " << RESET << "fail to initial observations with the parameter: " << para << endl;
return -1;
}
else obsNum = obsPoint.size();
return 0;
}
void grav2d_cube::outObs()
{
cout << "# This file is generated by grav2d-cube. Use -h to see options" << endl;
cout << "# x(m) y(m) ele(m) obs-val(mGal)" << endl;
for (int i = 0; i < obsNum; i++)
cout << obsPoint[i].x << " " << obsPoint[i].y << " " << -1.0*obsPoint[i].z << " " << setprecision(16) << obsPoint[i].val << endl;
return;
}
void grav2d_cube::calG()
{
int i,j;
double x1,x2,y1,y2,z1,z2;
double R222,R122,R212,R112,R221,R121,R211,R111;
double G222,G122,G212,G112,G221,G121,G211,G111;
for (j = 0; j < cubeNum; j++)
{
x1 = modCube[j].cen.x - 0.5*modCube[j].dx; x2 = modCube[j].cen.x + 0.5*modCube[j].dx;
y1 = modCube[j].cen.y - 0.5*modCube[j].dy; y2 = modCube[j].cen.y + 0.5*modCube[j].dy;
z1 = modCube[j].cen.z - 0.5*modCube[j].dz; z2 = modCube[j].cen.z + 0.5*modCube[j].dz;
#pragma omp parallel for private(i,R222,R122,R212,R112,R221,R121,R211,R111,G222,G122,G212,G112,G221,G121,G211,G111) shared(x1,x2,y1,y2,z1,z2) schedule(guided)
for (i = 0; i < obsNum; i++)
{
R222=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R122=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R212=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R112=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R221=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R121=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R211=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R111=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
G222=(x2-obsPoint[i].x)*log((y2-obsPoint[i].y)+R222)+(y2-obsPoint[i].y)*log((x2-obsPoint[i].x)+R222)+(z2-obsPoint[i].z)*arctg((z2-obsPoint[i].z)*R222/(x2-obsPoint[i].x)/(y2-obsPoint[i].y));
G122=(x1-obsPoint[i].x)*log((y2-obsPoint[i].y)+R122)+(y2-obsPoint[i].y)*log((x1-obsPoint[i].x)+R122)+(z2-obsPoint[i].z)*arctg((z2-obsPoint[i].z)*R122/(x1-obsPoint[i].x)/(y2-obsPoint[i].y));
G212=(x2-obsPoint[i].x)*log((y1-obsPoint[i].y)+R212)+(y1-obsPoint[i].y)*log((x2-obsPoint[i].x)+R212)+(z2-obsPoint[i].z)*arctg((z2-obsPoint[i].z)*R212/(x2-obsPoint[i].x)/(y1-obsPoint[i].y));
G112=(x1-obsPoint[i].x)*log((y1-obsPoint[i].y)+R112)+(y1-obsPoint[i].y)*log((x1-obsPoint[i].x)+R112)+(z2-obsPoint[i].z)*arctg((z2-obsPoint[i].z)*R112/(x1-obsPoint[i].x)/(y1-obsPoint[i].y));
G221=(x2-obsPoint[i].x)*log((y2-obsPoint[i].y)+R221)+(y2-obsPoint[i].y)*log((x2-obsPoint[i].x)+R221)+(z1-obsPoint[i].z)*arctg((z1-obsPoint[i].z)*R221/(x2-obsPoint[i].x)/(y2-obsPoint[i].y));
G121=(x1-obsPoint[i].x)*log((y2-obsPoint[i].y)+R121)+(y2-obsPoint[i].y)*log((x1-obsPoint[i].x)+R121)+(z1-obsPoint[i].z)*arctg((z1-obsPoint[i].z)*R121/(x1-obsPoint[i].x)/(y2-obsPoint[i].y));
G211=(x2-obsPoint[i].x)*log((y1-obsPoint[i].y)+R211)+(y1-obsPoint[i].y)*log((x2-obsPoint[i].x)+R211)+(z1-obsPoint[i].z)*arctg((z1-obsPoint[i].z)*R211/(x2-obsPoint[i].x)/(y1-obsPoint[i].y));
G111=(x1-obsPoint[i].x)*log((y1-obsPoint[i].y)+R111)+(y1-obsPoint[i].y)*log((x1-obsPoint[i].x)+R111)+(z1-obsPoint[i].z)*arctg((z1-obsPoint[i].z)*R111/(x1-obsPoint[i].x)/(y1-obsPoint[i].y));
obsPoint[i].val += -1.0*G0*(G222-G122-G212+G112-G221+G121+G211-G111)*modCube[j].rho;
}
}
return;
}
void grav2d_cube::calGx()
{
int i,j;
double x1,x2,y1,y2,z1,z2;
double R222,R122,R212,R112,R221,R121,R211,R111;
double G222,G122,G212,G112,G221,G121,G211,G111;
for (j = 0; j < cubeNum; j++)
{
x1 = modCube[j].cen.x - 0.5*modCube[j].dx; x2 = modCube[j].cen.x + 0.5*modCube[j].dx;
y1 = modCube[j].cen.y - 0.5*modCube[j].dy; y2 = modCube[j].cen.y + 0.5*modCube[j].dy;
z1 = modCube[j].cen.z - 0.5*modCube[j].dz; z2 = modCube[j].cen.z + 0.5*modCube[j].dz;
#pragma omp parallel for private(i,R222,R122,R212,R112,R221,R121,R211,R111,G222,G122,G212,G112,G221,G121,G211,G111) shared(x1,x2,y1,y2,z1,z2) schedule(guided)
for (i = 0; i < obsNum; i++)
{
R222=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R122=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R212=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R112=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R221=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R121=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R211=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R111=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
G222=log((x2-obsPoint[i].x)+R222);
G122=log((x1-obsPoint[i].x)+R122);
G212=log((x2-obsPoint[i].x)+R212);
G112=log((x1-obsPoint[i].x)+R112);
G221=log((x2-obsPoint[i].x)+R221);
G121=log((x1-obsPoint[i].x)+R121);
G211=log((x2-obsPoint[i].x)+R211);
G111=log((x1-obsPoint[i].x)+R111);
obsPoint[i].val += 1.0e+4*G0*(G222-G122-G212+G112-G221+G121+G211-G111)*modCube[j].rho;
}
}
return;
}
void grav2d_cube::calGy()
{
int i,j;
double x1,x2,y1,y2,z1,z2;
double R222,R122,R212,R112,R221,R121,R211,R111;
double G222,G122,G212,G112,G221,G121,G211,G111;
for (j = 0; j < cubeNum; j++)
{
x1 = modCube[j].cen.x - 0.5*modCube[j].dx; x2 = modCube[j].cen.x + 0.5*modCube[j].dx;
y1 = modCube[j].cen.y - 0.5*modCube[j].dy; y2 = modCube[j].cen.y + 0.5*modCube[j].dy;
z1 = modCube[j].cen.z - 0.5*modCube[j].dz; z2 = modCube[j].cen.z + 0.5*modCube[j].dz;
#pragma omp parallel for private(i,R222,R122,R212,R112,R221,R121,R211,R111,G222,G122,G212,G112,G221,G121,G211,G111) shared(x1,x2,y1,y2,z1,z2) schedule(guided)
for (i = 0; i < obsNum; i++)
{
R222=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R122=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R212=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R112=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R221=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R121=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R211=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R111=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
G222=log((y2-obsPoint[i].y)+R222);
G122=log((y2-obsPoint[i].y)+R122);
G212=log((y1-obsPoint[i].y)+R212);
G112=log((y1-obsPoint[i].y)+R112);
G221=log((y2-obsPoint[i].y)+R221);
G121=log((y2-obsPoint[i].y)+R121);
G211=log((y1-obsPoint[i].y)+R211);
G111=log((y1-obsPoint[i].y)+R111);
obsPoint[i].val += 1.0e+4*G0*(G222-G122-G212+G112-G221+G121+G211-G111)*modCube[j].rho;
}
}
return;
}
void grav2d_cube::calGz()
{
int i,j;
double x1,x2,y1,y2,z1,z2;
double R222,R122,R212,R112,R221,R121,R211,R111;
double G222,G122,G212,G112,G221,G121,G211,G111;
for (j = 0; j < cubeNum; j++)
{
x1 = modCube[j].cen.x - 0.5*modCube[j].dx; x2 = modCube[j].cen.x + 0.5*modCube[j].dx;
y1 = modCube[j].cen.y - 0.5*modCube[j].dy; y2 = modCube[j].cen.y + 0.5*modCube[j].dy;
z1 = modCube[j].cen.z - 0.5*modCube[j].dz; z2 = modCube[j].cen.z + 0.5*modCube[j].dz;
#pragma omp parallel for private(i,R222,R122,R212,R112,R221,R121,R211,R111,G222,G122,G212,G112,G221,G121,G211,G111) shared(x1,x2,y1,y2,z1,z2) schedule(guided)
for (i = 0; i < obsNum; i++)
{
R222=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R122=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R212=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R112=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z2-obsPoint[i].z)*(z2-obsPoint[i].z));
R221=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R121=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y2-obsPoint[i].y)*(y2-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R211=sqrt((x2-obsPoint[i].x)*(x2-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
R111=sqrt((x1-obsPoint[i].x)*(x1-obsPoint[i].x)+(y1-obsPoint[i].y)*(y1-obsPoint[i].y)+(z1-obsPoint[i].z)*(z1-obsPoint[i].z));
G222=atan((x2-obsPoint[i].x)*(y2-obsPoint[i].y)/(R222*(z2-obsPoint[i].z)));
G122=atan((x1-obsPoint[i].x)*(y2-obsPoint[i].y)/(R122*(z2-obsPoint[i].z)));
G212=atan((x2-obsPoint[i].x)*(y1-obsPoint[i].y)/(R212*(z2-obsPoint[i].z)));
G112=atan((x1-obsPoint[i].x)*(y1-obsPoint[i].y)/(R112*(z2-obsPoint[i].z)));
G221=atan((x2-obsPoint[i].x)*(y2-obsPoint[i].y)/(R221*(z1-obsPoint[i].z)));
G121=atan((x1-obsPoint[i].x)*(y2-obsPoint[i].y)/(R121*(z1-obsPoint[i].z)));
G211=atan((x2-obsPoint[i].x)*(y1-obsPoint[i].y)/(R211*(z1-obsPoint[i].z)));
G111=atan((x1-obsPoint[i].x)*(y1-obsPoint[i].y)/(R111*(z1-obsPoint[i].z)));
obsPoint[i].val += -1.0e+4*G0*(G222-G122-G212+G112-G221+G121+G211-G111)*modCube[j].rho;
}
}
return;
}

View File

@@ -0,0 +1,108 @@
#ifndef _INCLUDES_H
#define _INCLUDES_H
#include "iostream"
#include "fstream"
#include "sstream"
#include "string.h"
#include "cmath"
#include "iomanip"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "vector"
#include "map"
#include "algorithm"
#include "ctime"
#include "omp.h"
#include "random"
using namespace std;
//数学常量
#define BDL_MAX 1e+30
#define BDL_MIN -1e+30
#define ZERO 1e-20
//物理常量
#define Pi (4.0*atan(1.0))
#define G0 6.67408e-3 //注意这里本来应该是e-11考虑到单位转换取维度单位为m密度单位为g/cm^3乘以G0则重力单位即为mGal
//宏函数
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define SetToBox(a,b,in) (MAX(a,MIN(b,in))) //如果in在a和b之间返回in 否则返回边界值
//终端显示控制符
#define BOLDRED "\033[1m\033[31m"
#define BOLDGREEN "\033[1m\033[32m"
#define BOLDYELLOW "\033[1m\033[33m"
#define BOLDBLUE "\033[1m\033[34m"
#define UNDERLINE "\033[1m\033[4m"
#define RESET "\033[0m"
#define MOVEUP(x) printf("\033[%dA", (x))
#define MOVEDOWN(x) printf("\033[%dB", (x))
#define MOVELEFT(x) printf("\033[%dD", (x))
#define MOVERIGHT(x) printf("\033[%dC", (x))
#define MOVETO(y,x) printf("\033[%d;%dH", (y), (x))
#define CLEARLINE "\033[K"
#define CLEARALL "\033[2J"
//数据结构
typedef vector<int> _1iArray;
typedef vector<double> _1dArray;
typedef vector<string> _1sArray;
typedef vector<vector<int> > _2iArray;
typedef vector<vector<double> > _2dArray;
typedef map<int,int> _i2iMap;
struct cpoint
{
int id = -1;
double x = BDL_MAX; double y = BDL_MAX; double z = BDL_MAX;
};
typedef vector<cpoint> cpointArray;
struct obspoint : public cpoint
{
double val = BDL_MAX; double dev = BDL_MAX;
};
typedef vector<obspoint> obspointArray;
struct cube
{
cpoint cen;
double rho = 0.0;
double dx = BDL_MAX; double dy = BDL_MAX; double dz = BDL_MAX;
};
typedef vector<cube> cubeArray;
/*************************全局函数********************************/
//正负分离的atan函数 正数返回atan 负数返回atan+pi
double arctg(double v)
{
double ang;
if(v>=0) ang=atan(v);
else if(v<0) ang=atan(v)+Pi;
return ang;
}
//将string转换为stringstream
stringstream str2ss(string s){
stringstream sstr;
sstr.str(""); sstr.clear(); sstr.str(s);
return sstr;
}
//测试打开输入文件 如果成功则返回0并输出信息 否则返回1
int open_infile(ifstream &infile,char* filename){
infile.open(filename);
if (!infile){
cerr << BOLDRED << "error ==> " << RESET << "file not found: " << filename << endl;
return -1;
}
return 0;
}
//测试打开输出文件 如果成功则返回0并输出信息 否则返回1
int open_outfile(ofstream &outfile,char* filename){
outfile.open(filename);
if (!outfile){
cerr << BOLDRED << "error ==> " << RESET << "fail to create the file: " << filename << endl;
return -1;
}
return 0;
}
#endif

View File

@@ -0,0 +1,82 @@
#include "grav2d_cube.h"
#include "dispHelp.h"
void disp_help(char* proname)
{
string exName = proname;
string exUsage = proname;
exUsage += " -r<x-start>/<x-step>/<x-end>/<y-start>/<y-step>/<y-end>/<elevation>|<filename> \
-c<x-cen>/<y-cen>/<z-cen>/<dx>/<dy>/<dz>/<density>|<filename> -tgravity|gx|gy|gz > out-file";
dispHelp dh;
dh.changeLayerOut(0,10);
dh.addHeadInfo(exName,"0.1","Forward calculation of gravitational data of cubes.","Yi Zhang (zhangyi.cugwuhan@gmail.com)");
dh.addUsage(exUsage);
dh.addOption("Range of calculation, which could get from parameters or a file contains x y z locations. Defaults are 0/10/1000/0/10/1000/0","-r");
dh.addOption("Cube parameters, which could get from parameters (single) or a file (multiple). Defaults are 500/500/200/100/100/100/1.0","-c");
dh.addOption("Calculation type equals gravity (default), gx, gy or gz.","-t");
dh.show();
return;
}
int main(int argc, char* argv[])
{
grav2d_cube gc;
char rangeChar[1024] = "0/10/1000/0/10/1000/0";
char cubeChar[1024] = "500/500/200/100/100/100/1.0";
char typeChar[1024] = "gravity";
opterr = 0; //内置参数 若不为0则会在发生遭遇错误时输出一条信息到屏幕
int curr;
/*循环拾取参数 最后一个参数为-1 需要变量的参数后跟一个冒号 可有可无参数跟两个冒号*/
while((curr = getopt(argc,argv,"hr:c:t:")) != -1)
{
/*匹配命令*/
switch (curr)
{
case 'h': //显示帮助信息
disp_help(argv[0]);
return 0;
case 'r':
if (1!=sscanf(optarg,"%s",rangeChar))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 'c':
if (1!=sscanf(optarg,"%s",cubeChar))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 't':
if (1!=sscanf(optarg,"%s",typeChar))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case '?': //处理未定义或错误参数
if (optopt == 'r' || optopt == 'c' || optopt == 't')
{
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
return -1;
}
else if (isprint(optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
return -1;
}
else
{
fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
return -1;
}
break;
default:
abort();
}
}
gc.routine(typeChar,rangeChar,cubeChar);
return 0;
}

View File

@@ -0,0 +1,15 @@
CC = g++-9
PROM = /usr/local/sbin/grav2d-cube
CFLAGS = -I.
DEPS = $(shell find . -name "*.h")
SRC = $(shell find . -name "*.cpp")
OBJ = $(SRC:%.cpp=%.o)
$(PROM): $(OBJ)
$(CC) -o $(PROM) $(OBJ) $(CFLAGS) -O2
%.o:%.cpp $(DEPS)
$(CC) -c $< -o $@ $(CFLAGS) -O2
clean:
rm -rf $(OBJ)