initial upload
This commit is contained in:
77
archive/xyz2shc/cal_c_kernel.cpp
Normal file
77
archive/xyz2shc/cal_c_kernel.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "xyz2shc.h"
|
||||
|
||||
void XYZ2SHC::CalCKernel(){
|
||||
int i,k,n;
|
||||
int start_index,former_index,flow_index;
|
||||
//初始化c_kernel_;
|
||||
c_kernel_.resize(obs_num_);
|
||||
for (int i = 0; i < obs_num_; i++){
|
||||
c_kernel_[i].resize(shc_num_,0.0);
|
||||
}
|
||||
|
||||
//先计算对角线上的伴随勒让德系数 对角线上第一个值
|
||||
#pragma omp parallel for private(i) schedule(guided)
|
||||
for (int i = 0; i < obs_num_; i++){
|
||||
c_kernel_[i][0] = 1.0;
|
||||
c_kernel_[i][half_shc_num_] = 1.0;
|
||||
}
|
||||
|
||||
//对角线上第二个值
|
||||
start_index = shc_order_ + 1;
|
||||
#pragma omp parallel for private(i) schedule(guided)
|
||||
for (int i = 0; i < obs_num_; i++){
|
||||
c_kernel_[i][start_index] = sqrt(3.0)*sin(obs_point_[i].theta*Pi/180.0);
|
||||
c_kernel_[i][start_index + half_shc_num_] = sqrt(3.0)*sin(obs_point_[i].theta*Pi/180.0);
|
||||
}
|
||||
|
||||
//对角线上的其他值
|
||||
for (k = 2; k < shc_order_+1; k++){
|
||||
former_index = int(0.5*(k-1)*(2*shc_order_+4-k));
|
||||
start_index = int(0.5*k*(2*shc_order_+3-k));
|
||||
#pragma omp parallel for private(i) shared(k,start_index,former_index) schedule(guided)
|
||||
for (int i = 0; i < obs_num_; i++){
|
||||
c_kernel_[i][start_index] = sin(obs_point_[i].theta*Pi/180.0)*sqrt(0.5*(2.0*k+1)/k)*c_kernel_[i][former_index];
|
||||
c_kernel_[i][start_index + half_shc_num_] = sin(obs_point_[i].theta*Pi/180.0)*sqrt(0.5*(2.0*k+1)/k)*c_kernel_[i][former_index + half_shc_num_];
|
||||
}
|
||||
}
|
||||
|
||||
//计算副对角线上的值
|
||||
for (k = 0; k < shc_order_; k++){
|
||||
start_index = int(0.5*k*(2*shc_order_+3-k));
|
||||
#pragma omp parallel for private(i) shared(k,start_index) schedule(guided)
|
||||
for (int i = 0; i < obs_num_; i++){
|
||||
c_kernel_[i][start_index + 1] = cos(obs_point_[i].theta*Pi/180.0)*sqrt(2.0*k+3)*c_kernel_[i][start_index];
|
||||
c_kernel_[i][start_index + half_shc_num_ + 1] = cos(obs_point_[i].theta*Pi/180.0)*sqrt(2.0*k+3)*c_kernel_[i][start_index + half_shc_num_];
|
||||
}
|
||||
}
|
||||
|
||||
//计算其他位置上的值
|
||||
for (n = 0; n < shc_order_; n++){
|
||||
start_index = int(0.5*n*(2*shc_order_+3-n));
|
||||
for (k = n+2; k < shc_order_+1; k++){
|
||||
former_index = k - n;
|
||||
#pragma omp parallel for private(i) shared(start_index,former_index,n,k) schedule(guided)
|
||||
for (int i = 0; i < obs_num_; i++){
|
||||
c_kernel_[i][start_index + former_index] = Anm_[k][n]*cos(obs_point_[i].theta*Pi/180.0)*c_kernel_[i][start_index+flow_index-1] - Bnm_[k][n]*c_kernel_[i][start_index+flow_index-2];
|
||||
c_kernel_[i][start_index + former_index + half_shc_num_] = Anm_[k][n]*cos(obs_point_[i].theta*Pi/180.0)*c_kernel_[i][start_index+flow_index-1+half_shc_num_] - Bnm_[k][n]*c_kernel_[i][start_index+flow_index-2+half_shc_num_];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//添加经度位置信息
|
||||
ProgressBar *bar = new ProgressBar(shc_order_+1,"Calculating Ckernel");
|
||||
for (n = 0; n < shc_order_+1; n++){
|
||||
bar->Progressed(i);
|
||||
|
||||
start_index = int(0.5*n*(2*shc_order_+3-n));
|
||||
for (k = n; k < shc_order_+1; k++){
|
||||
former_index = k - n;
|
||||
#pragma omp parallel for private(i) shared(start_index,former_index,n) schedule(guided)
|
||||
for (int i = 0; i < obs_num_; i++){
|
||||
c_kernel_[i][start_index + former_index] *= cos(n*obs_point_[i].phi*Pi/180.0);;
|
||||
c_kernel_[i][start_index + former_index + half_shc_num_] *= sin(n*obs_point_[i].phi*Pi/180.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
13
archive/xyz2shc/cal_part_b.cpp
Normal file
13
archive/xyz2shc/cal_part_b.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "xyz2shc.h"
|
||||
|
||||
void XYZ2SHC::CalPartB(){
|
||||
int i,j;
|
||||
PartB_.resize(shc_num_,0.0);
|
||||
#pragma omp parallel for private(i,j) schedule(guided)
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
for (i = 0; i < obs_num_; i++){
|
||||
PartB_[j] += c_kernel_[i][j]*wdTwd_[i]*obs_point_[i].val;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
12
archive/xyz2shc/cal_wd.cpp
Normal file
12
archive/xyz2shc/cal_wd.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "xyz2shc.h"
|
||||
|
||||
void XYZ2SHC::CalWd(){
|
||||
wdTwd_.resize(obs_num_,0.0);
|
||||
|
||||
ProgressBar *bar = new ProgressBar(obs_num_,"Calculating Wd");
|
||||
for (int i = 0; i < obs_num_; i++){
|
||||
bar->Progressed(i);
|
||||
wdTwd_[i] = 1.0/(obs_point_[i].dev*obs_point_[i].dev*obs_num_);
|
||||
}
|
||||
return;
|
||||
}
|
71
archive/xyz2shc/head_func.cpp
Normal file
71
archive/xyz2shc/head_func.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "head_func.h"
|
||||
|
||||
/*************************全局函数********************************/
|
||||
double sign(double input){
|
||||
if (input >= 0.0) return 1.0;
|
||||
else return -1.0;
|
||||
}
|
||||
//将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;
|
||||
}
|
||||
//计算一个参考椭球或者参考球在指定纬度位置的半径
|
||||
double ellipRadius(double lati,double refr,double refR){
|
||||
return refr*refR/sqrt(pow(refr,2)*pow(cos(lati*Pi/180.0),2)+pow(refR,2)*pow(sin(lati*Pi/180.0),2));
|
||||
}
|
||||
|
||||
/*************************球谐系数操作********************************/
|
||||
//计算伴随勒让德函数递推系数
|
||||
_2dArray get_Anm_array(int MaxOrder){
|
||||
int i,j;
|
||||
_2dArray cs;
|
||||
cs.resize(MaxOrder);
|
||||
for (i = 0; i < MaxOrder; i++)
|
||||
cs[i].resize(i+1);
|
||||
//向下列推计算
|
||||
#pragma omp parallel for private(i,j) schedule(guided)
|
||||
for (j = 0; j < MaxOrder; j++){
|
||||
cs[j][j] = 0; //对角线上的值直接给0 反正用不到
|
||||
for (i = j+1; i < MaxOrder; i++){
|
||||
cs[i][j] = sqrt(((2.0*i-1)*(2.0*i+1))/((i-j)*(i+j)));
|
||||
}
|
||||
}
|
||||
return cs;
|
||||
}
|
||||
|
||||
_2dArray get_Bnm_array(int MaxOrder){
|
||||
int i,j;
|
||||
_2dArray cs;
|
||||
cs.resize(MaxOrder);
|
||||
for (i = 0; i < MaxOrder; i++)
|
||||
cs[i].resize(i+1);
|
||||
//向下列推计算
|
||||
#pragma omp parallel for private(i,j) schedule(guided)
|
||||
for (j = 0; j < MaxOrder; j++){
|
||||
cs[j][j] = 0; //对角线上的值直接给0 反正用不到
|
||||
for (i = j+1; i < MaxOrder; i++){
|
||||
cs[i][j] = sqrt(((2.0*i+1)*(i+j-1)*(i-j-1))/((i-j)*(i+j)*(2.0*i-3)));
|
||||
}
|
||||
}
|
||||
return cs;
|
||||
}
|
89
archive/xyz2shc/head_func.h
Normal file
89
archive/xyz2shc/head_func.h
Normal file
@@ -0,0 +1,89 @@
|
||||
#ifndef _HEAD_FUNC_H
|
||||
#define _HEAD_FUNC_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 WGS84_PoleRadius 6356752.314//WGS84椭球极半径
|
||||
#define WGS84_EquatorRadius 6378137.0//WGS84椭球长半径
|
||||
#define EarthRadius 6371000.0
|
||||
#define MoonRadius 1738000.0
|
||||
#define Pi (4.0*atan(1.0))
|
||||
//宏函数
|
||||
#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 spoint{
|
||||
int id = -1;
|
||||
double phi = BDL_MAX, theta = BDL_MAX, rad = BDL_MAX;
|
||||
};
|
||||
typedef vector<spoint> spointArray;
|
||||
|
||||
struct obspoint : spoint{
|
||||
double val = BDL_MAX, dev = BDL_MAX;
|
||||
};
|
||||
typedef vector<obspoint> obspointArray;
|
||||
|
||||
/*************************全局函数********************************/
|
||||
double sign(double);
|
||||
stringstream str2ss(string); //将string转换为stringstream
|
||||
int open_infile(ifstream&,char*); //测试打开输入文件 如果成功则返回0并输出信息 否则返回1
|
||||
int open_outfile(ofstream&,char*); //测试打开输出文件 如果成功则返回0并输出信息 否则返回1
|
||||
double ellipRadius(double,double,double); //计算一个参考椭球或者参考球在指定纬度位置的半径
|
||||
/*************************球谐系数操作********************************/
|
||||
//计算伴随勒让德函数递推系数
|
||||
_2dArray get_Anm_array(int);
|
||||
_2dArray get_Bnm_array(int);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
19
archive/xyz2shc/init_shc.cpp
Normal file
19
archive/xyz2shc/init_shc.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "xyz2shc.h"
|
||||
|
||||
int XYZ2SHC::InitShc(int given_order){
|
||||
if (given_order <= 0){
|
||||
cerr << BOLDRED << "error ==> " << RESET << "spherical order must be bigger than zero: " << given_order << endl;
|
||||
return -1;
|
||||
}
|
||||
else{
|
||||
shc_order_ = given_order;
|
||||
shc_num_ = (shc_order_+1)*(shc_order_+2);
|
||||
half_shc_num_ = (shc_order_+1)*(shc_order_+2)/2;
|
||||
//初始化shc_value_
|
||||
shc_value_.resize(shc_num_,0.0);
|
||||
//初始化Anm_和Bnm_
|
||||
Anm_ = get_Anm_array(shc_order_+1);
|
||||
Bnm_ = get_Bnm_array(shc_order_+1);
|
||||
return 0;
|
||||
}
|
||||
}
|
97
archive/xyz2shc/main.cpp
Normal file
97
archive/xyz2shc/main.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "xyz2shc.h"
|
||||
|
||||
void disp_help()
|
||||
{
|
||||
cout << "xyz2shc - v0.1 calculate table data to spherical harmonic coefficients" << endl
|
||||
<< "Author: zhangyi.cugwuhan@gmail.com" << endl << endl
|
||||
<< "usage: xyz2shc -i<table>+d<lon-col>,<lat-col>,<val-col>[,<dev-col>] -o<output-file> -f<shc-order> [-d<dev-value>] [-t<iter-times>] [-h]" << endl
|
||||
<< "-i\tinput table name, use +d to specify the input columns." << endl
|
||||
<< "-o\toutput shc filename." << endl
|
||||
<< "-f\tspherical harmonic coefficients order." << endl
|
||||
<< "-d\testimated data accuracy. default value is 1e-3." << endl
|
||||
<< "-t\titeration times. default value is 300." << endl
|
||||
<< "-h\tshow this info" << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]){
|
||||
char infilename[1024] = "NULL";
|
||||
char outfilename[1024] = "NULL";
|
||||
int shcorder = 0, itertimes = 300;
|
||||
double datadev = 1e-3;
|
||||
|
||||
opterr = 0; //内置参数 若不为0则会在发生遭遇错误时输出一条信息到屏幕
|
||||
|
||||
int curr;
|
||||
/*循环拾取参数 最后一个参数为-1 需要变量的参数后跟一个冒号 可有可无参数跟两个冒号*/
|
||||
while((curr = getopt(argc,argv,"hi:o:f:d:t:")) != -1)
|
||||
{
|
||||
/*匹配命令*/
|
||||
switch (curr)
|
||||
{
|
||||
case 'h': //显示帮助信息
|
||||
disp_help();
|
||||
break;
|
||||
case 'i':
|
||||
if (1!=sscanf(optarg,"%s",infilename))
|
||||
{
|
||||
cout << "error ==> wrong format of " << optarg << endl;
|
||||
}
|
||||
break;
|
||||
case 'o':
|
||||
if (1!=sscanf(optarg,"%s",outfilename))
|
||||
{
|
||||
cout << "error ==> wrong format of " << optarg << endl;
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
if (1!=sscanf(optarg,"%d",&shcorder))
|
||||
{
|
||||
cout << "error ==> wrong format of " << optarg << endl;
|
||||
}
|
||||
break;
|
||||
case 'd':
|
||||
if (1!=sscanf(optarg,"%lf",&datadev))
|
||||
{
|
||||
cout << "error ==> wrong format of " << optarg << endl;
|
||||
}
|
||||
break;
|
||||
case 't':
|
||||
if (1!=sscanf(optarg,"%d",&itertimes))
|
||||
{
|
||||
cout << "error ==> wrong format of " << optarg << endl;
|
||||
}
|
||||
break;
|
||||
case '?': //处理未定义或错误参数
|
||||
if (optopt == 'i' || optopt == 'o' || optopt == 'f' || optopt == 'd' || 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();
|
||||
}
|
||||
}
|
||||
|
||||
XYZ2SHC instance;
|
||||
if (instance.ReadXyz(infilename,datadev)) return 0;
|
||||
if (instance.InitShc(shcorder)) return 0;
|
||||
|
||||
instance.CalCKernel();
|
||||
instance.CalWd();
|
||||
instance.CalPartB();
|
||||
instance.Optimize_CG();
|
||||
|
||||
if (instance.OutShc(outfilename)) return 0;
|
||||
return 0;
|
||||
}
|
15
archive/xyz2shc/makefile
Normal file
15
archive/xyz2shc/makefile
Normal file
@@ -0,0 +1,15 @@
|
||||
CC = g++-8
|
||||
PROM = /usr/local/sbin/xyz2shc
|
||||
CFLAGS = -I.
|
||||
DEPS = $(shell find ./src -name "*.h")
|
||||
SRC = $(shell find ./src -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)
|
105
archive/xyz2shc/optimize_cg.cpp
Normal file
105
archive/xyz2shc/optimize_cg.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "xyz2shc.h"
|
||||
|
||||
void XYZ2SHC::Optimize_CG(){
|
||||
int i,j;
|
||||
double data_object_func, PartB_m,gradient_m,gradient_m1,dTAd,ak,beta_k1;
|
||||
_1dArray gradientk, dk, Adk, Adk_part, predict_field;
|
||||
//reserve vector
|
||||
gradientk.resize(shc_num_); dk.resize(shc_num_);
|
||||
Adk.resize(shc_num_); Adk_part.resize(obs_num_);
|
||||
predict_field.resize(obs_num_);
|
||||
|
||||
#pragma omp parallel for private(i,j) schedule(guided)
|
||||
for (i = 0; i < obs_num_; i++){
|
||||
Adk_part[i] = 0.0;
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
Adk_part[i] += c_kernel_[i][j]*shc_value_[j];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma omp parallel for private(i,j) schedule(guided)
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
Adk[j] = 0.0;
|
||||
for (i = 0; i < obs_num_; i++){
|
||||
Adk[i] += c_kernel_[i][j]*wdTwd_[i]*Adk_part[i];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma omp parallel for private(i) schedule(guided)
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
gradientk[j] = Adk[j] - PartB_[j];
|
||||
dk[j] = -1.0*gradientk[j];
|
||||
}
|
||||
|
||||
PartB_m = 0.0;
|
||||
gradient_m = 0.0;
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
PartB_m += PartB_[j]*PartB_[j];
|
||||
gradient_m += gradientk[i]*gradientk[i];
|
||||
}
|
||||
|
||||
for (int time = 0; time < iter_times_; time++){
|
||||
//计算数据拟合差函数
|
||||
#pragma omp parallel for private(i,j) schedule(guided)
|
||||
for (i = 0; i < obs_num_; i++){
|
||||
predict_field[i] = 0.0;
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
predict_field[i] += c_kernel_[i][j] * shc_value_[j];
|
||||
}
|
||||
}
|
||||
|
||||
data_object_func = 0.0;
|
||||
for (i = 0; i < obs_num_; i++){
|
||||
data_object_func += pow((obs_point_[i].val - predict_field[i])/obs_point_[i].dev,2);
|
||||
}
|
||||
data_object_func /= obs_num_;
|
||||
|
||||
if (data_object_func <= 1.0){
|
||||
cout << "Iteration limit rearched!\tconvergence value = " << data_object_func << endl;
|
||||
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl;
|
||||
break;
|
||||
}
|
||||
else{
|
||||
cout << "Iteration times: " << time << "\tconvergence value = " << data_object_func << endl;
|
||||
MOVEUP(1);
|
||||
cout << CLEARLINE;
|
||||
}
|
||||
|
||||
#pragma omp parallel for private(i,j) schedule(guided)
|
||||
for (i = 0; i < obs_num_; i++){
|
||||
Adk_part[i] = 0.0;
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
Adk_part[i] += c_kernel_[i][j]*dk[j];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma omp parallel for private(i,j) schedule(guided)
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
Adk[j] = 0.0;
|
||||
for (i = 0; i < obs_num_; i++){
|
||||
Adk[i] += c_kernel_[i][j]*wdTwd_[i]*Adk_part[i];
|
||||
}
|
||||
}
|
||||
|
||||
dTAd = 0.0;
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
dTAd += dk[j]*Adk[j];
|
||||
}
|
||||
|
||||
ak = gradient_m/dTAd;
|
||||
|
||||
gradient_m1 = 0;
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
shc_value_[j] += ak*dk[j];
|
||||
gradientk[j] += ak*Adk[j];
|
||||
gradient_m1 += gradientk[j]*gradientk[j];
|
||||
}
|
||||
|
||||
beta_k1 = gradient_m1/gradient_m;
|
||||
gradient_m = gradient_m1;
|
||||
for (j = 0; j < shc_num_; j++){
|
||||
dk[j] = beta_k1*dk[j] - gradientk[j];
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
20
archive/xyz2shc/out_shc.cpp
Normal file
20
archive/xyz2shc/out_shc.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "xyz2shc.h"
|
||||
|
||||
int XYZ2SHC::OutShc(char* filename){
|
||||
int start_index,flow_index;
|
||||
|
||||
ofstream outfile;
|
||||
if (open_outfile(outfile,filename)) return -1;
|
||||
|
||||
for (int i = 0; i < shc_order_+1; i++){
|
||||
//按列优先排列将球谐系数输出 由列号计算数组中的起始位置
|
||||
start_index = int(0.5*i*(2*shc_order_+3-i));
|
||||
for (int j = 0; j < shc_order_+1; j++){
|
||||
//根据行号计算偏移量
|
||||
flow_index = j - i;
|
||||
outfile << j << " " << i << setprecision(16) << " " << shc_value_[start_index+flow_index] << " " << shc_value_[start_index+flow_index+half_shc_num_] << endl;
|
||||
}
|
||||
}
|
||||
outfile.close();
|
||||
return 0;
|
||||
}
|
114
archive/xyz2shc/progressBar.cpp
Normal file
114
archive/xyz2shc/progressBar.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
//#ifdef _WINDOWS
|
||||
//#include <windows.h>
|
||||
//#else
|
||||
//#include <sys/ioctl.h>
|
||||
//#endif
|
||||
|
||||
#include "progressBar.h"
|
||||
|
||||
ProgressBar::ProgressBar() {}
|
||||
|
||||
ProgressBar::ProgressBar(unsigned long n_, const char* description_, std::ostream& out_){
|
||||
|
||||
n = n_;
|
||||
frequency_update = n_;
|
||||
description = description_;
|
||||
out = &out_;
|
||||
|
||||
unit_bar = "\u2588";
|
||||
unit_space = "-";
|
||||
desc_width = std::strlen(description); // character width of description field
|
||||
|
||||
}
|
||||
|
||||
void ProgressBar::SetFrequencyUpdate(unsigned long frequency_update_){
|
||||
|
||||
if(frequency_update_ > n){
|
||||
frequency_update = n; // prevents crash if freq_updates_ > n_
|
||||
}
|
||||
else{
|
||||
frequency_update = frequency_update_;
|
||||
}
|
||||
}
|
||||
|
||||
void ProgressBar::SetStyle(const char* unit_bar_, const char* unit_space_){
|
||||
|
||||
unit_bar = unit_bar_;
|
||||
unit_space = unit_space_;
|
||||
}
|
||||
|
||||
int ProgressBar::GetConsoleWidth(){
|
||||
|
||||
int width;
|
||||
|
||||
#ifdef _WINDOWS
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
|
||||
width = csbi.srWindow.Right - csbi.srWindow.Left;
|
||||
#else
|
||||
struct winsize win;
|
||||
//注意!当我们使用pipe here-doc等通道获取程序参数时无法正确的获取窗口大小 此时我们将使用预定值
|
||||
if (ioctl(0, TIOCGWINSZ, &win) != -1)
|
||||
width = win.ws_col;
|
||||
else width = 100;
|
||||
#endif
|
||||
|
||||
return width;
|
||||
}
|
||||
|
||||
int ProgressBar::GetBarLength(){
|
||||
|
||||
// get console width and according adjust the length of the progress bar
|
||||
|
||||
int bar_length = static_cast<int>((GetConsoleWidth() - desc_width - CHARACTER_WIDTH_PERCENTAGE) / 2.);
|
||||
|
||||
return bar_length;
|
||||
}
|
||||
|
||||
void ProgressBar::ClearBarField(){
|
||||
|
||||
for(int i=0;i<GetConsoleWidth();++i){
|
||||
*out << " ";
|
||||
}
|
||||
*out << "\r" << std::flush;
|
||||
}
|
||||
|
||||
void ProgressBar::Progressed(unsigned long idx_)
|
||||
{
|
||||
try{
|
||||
if(idx_ > n) throw idx_;
|
||||
|
||||
// determines whether to update the progress bar from frequency_update
|
||||
if ((idx_ != n-1) && ((idx_+1) % (n/frequency_update) != 0)) return;
|
||||
|
||||
// calculate the size of the progress bar
|
||||
int bar_size = GetBarLength();
|
||||
|
||||
// calculate percentage of progress
|
||||
double progress_percent = idx_* TOTAL_PERCENTAGE/(n-1);
|
||||
|
||||
// calculate the percentage value of a unit bar
|
||||
double percent_per_unit_bar = TOTAL_PERCENTAGE/bar_size;
|
||||
|
||||
// display progress bar
|
||||
*out << " " << description << " |";
|
||||
|
||||
for(int bar_length=0;bar_length<=bar_size-1;++bar_length){
|
||||
if(bar_length*percent_per_unit_bar<progress_percent){
|
||||
*out << unit_bar;
|
||||
}
|
||||
else{
|
||||
*out << unit_space;
|
||||
}
|
||||
}
|
||||
|
||||
if(idx_ == n-1)
|
||||
*out << "|" << std::setw(CHARACTER_WIDTH_PERCENTAGE + 1) << std::setprecision(1) << std::fixed << progress_percent << "%\r" << std::flush << std::endl;
|
||||
else *out << "|" << std::setw(CHARACTER_WIDTH_PERCENTAGE + 1) << std::setprecision(1) << std::fixed << progress_percent << "%\r" << std::flush;
|
||||
}
|
||||
catch(unsigned long e){
|
||||
ClearBarField();
|
||||
std::cerr << "PROGRESS_BAR_EXCEPTION: _idx (" << e << ") went out of bounds, greater than n (" << n << ")." << std::endl << std::flush;
|
||||
}
|
||||
|
||||
}
|
41
archive/xyz2shc/progressBar.h
Normal file
41
archive/xyz2shc/progressBar.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef _PROGRESS_BAR_
|
||||
#define _PROGRESS_BAR_
|
||||
#include <sys/ioctl.h>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
#define TOTAL_PERCENTAGE 100.0
|
||||
#define CHARACTER_WIDTH_PERCENTAGE 4
|
||||
|
||||
class ProgressBar
|
||||
{
|
||||
public:
|
||||
|
||||
ProgressBar();
|
||||
ProgressBar(unsigned long n_, const char *description_="", std::ostream& out_=std::cerr);
|
||||
|
||||
void SetFrequencyUpdate(unsigned long frequency_update_);
|
||||
void SetStyle(const char* unit_bar_, const char* unit_space_);
|
||||
|
||||
void Progressed(unsigned long idx_);
|
||||
|
||||
private:
|
||||
|
||||
unsigned long n;
|
||||
unsigned int desc_width;
|
||||
unsigned long frequency_update;
|
||||
std::ostream* out;
|
||||
|
||||
const char *description;
|
||||
const char *unit_bar;
|
||||
const char *unit_space;
|
||||
|
||||
void ClearBarField();
|
||||
int GetConsoleWidth();
|
||||
int GetBarLength();
|
||||
|
||||
};
|
||||
#endif
|
68
archive/xyz2shc/read_xyz.cpp
Normal file
68
archive/xyz2shc/read_xyz.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "xyz2shc.h"
|
||||
|
||||
int XYZ2SHC::ReadXyz(char* file_para,double given_dev){
|
||||
char filename[1024];
|
||||
int order[4];
|
||||
|
||||
//同时读取文件名和读入列
|
||||
if (5 != sscanf(file_para,"%[^+]+d%d,%d,%d,%d",filename,&order[0],&order[1],&order[2],&order[3])){
|
||||
if (4 != sscanf(file_para,"%[^+]+d%d,%d,%d",filename,&order[0],&order[1],&order[2])){
|
||||
//直接将file_para赋值给filename
|
||||
strcpy(filename,file_para);
|
||||
order[0] = 0;
|
||||
order[1] = 1;
|
||||
order[2] = 2;
|
||||
order[3] = -1;
|
||||
}
|
||||
else order[3] = -1;
|
||||
}
|
||||
|
||||
ifstream infile;
|
||||
if (open_infile(infile,filename)) return -1;
|
||||
|
||||
double temp_d;
|
||||
_1dArray temp_row;
|
||||
obspoint temp_obs;
|
||||
string temp_str;
|
||||
stringstream temp_ss;
|
||||
|
||||
temp_row.reserve(100);
|
||||
while (getline(infile,temp_str)){
|
||||
if (*(temp_str.begin()) == '#')
|
||||
continue;
|
||||
|
||||
if (!temp_row.empty())
|
||||
temp_row.clear();
|
||||
|
||||
temp_ss = str2ss(temp_str);
|
||||
while (temp_ss >> temp_d)
|
||||
temp_row.push_back(temp_d);
|
||||
|
||||
temp_obs.phi = temp_row[order[0]];
|
||||
//注意经度范围为0~360
|
||||
if (temp_obs.phi < 0){
|
||||
temp_obs.phi += 360.0;
|
||||
}
|
||||
|
||||
temp_obs.theta = temp_row[order[1]];
|
||||
//注意讲纬度转换为余纬度
|
||||
temp_obs.theta = 90.0 - temp_obs.theta;
|
||||
|
||||
temp_obs.rad = BDL_MAX; //注意我们这里暂时不需要半径信息
|
||||
temp_obs.val = temp_row[order[2]];
|
||||
|
||||
//默认读入数据不包含dev
|
||||
if (order[3] == -1) temp_obs.dev = given_dev;
|
||||
else temp_obs.dev = temp_row[order[3]];
|
||||
|
||||
obs_point_.push_back(temp_obs);
|
||||
}
|
||||
infile.close();
|
||||
|
||||
obs_num_ = obs_point_.size();
|
||||
if (obs_num_ == 0){
|
||||
cerr << BOLDRED << "error ==> " << RESET << "fail to read observations: " << file_para << endl;
|
||||
return -1;
|
||||
}
|
||||
else return 0;
|
||||
}
|
31
archive/xyz2shc/xyz2shc.h
Normal file
31
archive/xyz2shc/xyz2shc.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef _XYZ2SHC_H
|
||||
#define _XYZ2SHC_H
|
||||
#include "head_func.h"
|
||||
#include "progressBar.h"
|
||||
|
||||
class XYZ2SHC
|
||||
{
|
||||
public:
|
||||
XYZ2SHC(){}
|
||||
~XYZ2SHC(){}
|
||||
int ReadXyz(char*,double);
|
||||
int InitShc(int);
|
||||
int OutShc(char*);
|
||||
void CalWd();
|
||||
void CalCKernel();
|
||||
void CalPartB();
|
||||
void Optimize_CG();
|
||||
private:
|
||||
int obs_num_,shc_order_,shc_num_,half_shc_num_;
|
||||
int iter_times_;
|
||||
|
||||
obspointArray obs_point_;
|
||||
_1dArray obs_value_;
|
||||
_1dArray shc_value_;
|
||||
_1dArray wdTwd_;
|
||||
_1dArray PartB_;
|
||||
_2dArray Anm_;
|
||||
_2dArray Bnm_;
|
||||
_2dArray c_kernel_;
|
||||
};
|
||||
#endif
|
Reference in New Issue
Block a user