Repository navigation
Expand file tree
/
Copy pathwrite_coefficients.c
More file actions
196 lines (182 loc) · 5.26 KB
/
Copy pathwrite_coefficients.c
File metadata and controls
196 lines (182 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* part of the shansyn spherical harmonics package, see COPYRIGHT for license */
/* $Id: write_coefficients.c,v 1.8 2009/04/16 00:55:29 becker Exp becker $ */
#include "shansyn.h"
void write_nonzero_coefficients(COMP_PRECISION **a,
COMP_PRECISION **b,int lmax,
COMP_PRECISION sfac,
int print_lmax,
FILE *out, int nexp,
int one_column)
{
int l,m,i,os;
if(print_lmax == 1) /* short format */
fprintf(out,"%i\n",lmax);
else if(print_lmax == 2) /* long format */
fprintf(out,"%i %i %g %i %i %i\n",lmax,0,0.,1,nexp,0);
for(l=0;l <= lmax;l++)
for(m=0;m <= l;m++){
os = POSLM(l,m);
if(m == 0){
/* don't write the zero B term for m == 0 */
for(i=0;i < nexp;i++)
fprintf(out,ASCII_DATA_FORMAT,*(a[i]+os));
}else{
for(i=0;i < nexp;i++){
if(one_column){
fprintf(out,ASCII_DATA_FORMAT,*(a[i]+os));fprintf(out,"\n");
fprintf(out,ASCII_DATA_FORMAT,*(b[i]+os));
}else{
fprintf(out,COEFF_DATA_FORMAT,*(a[i]+os),*(b[i]+os));
}
}
}
fprintf(out,"\n");
}
}
void write_coefficients(COMP_PRECISION **a,
COMP_PRECISION **b,int lmax,
COMP_PRECISION sfac,int print_lmax,
FILE *out, int nexp)
{
int l,m,i,os;
/* print header */
if(print_lmax == 1)
fprintf(out,"%i\n",lmax);
else if(print_lmax == 2) /* long format */
fprintf(out,"%i %i %g %i %i %i\n",lmax,0,0.,1,nexp,0);
for(l=0;l<=lmax;l++)
for(m=0;m<=l;m++){
os = POSLM(l,m);
for(i=0;i < nexp;i++){
if((*(a[i]+os) != 0)||(*(b[i]+os)!=0))
fprintf(out,COEFF_DATA_FORMAT,*(a[i]+os)*sfac,*(b[i]+os)*sfac);
else
fprintf(out," 0 0");
}
fprintf(out,"\n");
}
}
#define HSTR_SIZE 200
void write_vector_coefficients(COMP_PRECISION **amp,
COMP_PRECISION **amt,
COMP_PRECISION **bmp,
COMP_PRECISION **bmt,
int lmax,
COMP_PRECISION sfac,
int print_lmax,
FILE *out, int nexp)
{
int l,m,i,os;
char sout[HSTR_SIZE+1],buffer[HSTR_SIZE+1];
/* make the output string */
for(i=0;i<4;i++)
if(i==0){
sprintf(sout,"%s",ASCII_DATA_FORMAT);
}
else{
sprintf(buffer,"%s %s",sout,ASCII_DATA_FORMAT);
strncpy(sout,buffer,HSTR_SIZE);
}
/* print a header? */
if(print_lmax == 1)
fprintf(out,"%i\n",lmax);
else if(print_lmax == 2) /* long format */
fprintf(out,"%i %i %g %i %i %i\n",lmax,0,0.,1,nexp*2,0);
for(l=0;l <= lmax;l++)
for(m=0;m <= l;m++){
os = POSLM(l,m);
for(i=0;i < nexp;i++){ /* 1 3 2 4 */
fprintf(out,sout,
*(amp[i]+os)*sfac,*(bmp[i]+os)*sfac,
*(amt[i]+os)*sfac,*(bmt[i]+os)*sfac);
}
fprintf(out,"\n");
}
}
void write_model(struct mod *model,COMP_PRECISION fac,
int nexp,FILE *out)
{
int i;
switch(model[0].radial_type){
case DISCRETE:{
printf("%i\n",model[0].n);// number of layers
for(i=0;i < model[0].n;i++){
printf("%.8e\n",model[0].d[i]);// depth
write_model_layer(model,i,fac,TRUE,nexp,out);
}
break;
}
case CHEBYSHEV:{
printf("%i\n",-model[0].n);// use negative order as layer number
printf("%.8e %.8e\n",model[0].dmin,model[0].dmax);// boundaries
for(i=0;i<model[0].n;i++){// coefficients
write_model_layer(model,i,fac,TRUE,nexp,out);
}
break;
}
case SPLINES:{
printf("%i\n",model[0].n+1000);// use order + 1000 as layer number
printf("%.8e %.8e\n",model[0].dmin,model[0].dmax);// boundaries
for(i=0;i<model[0].n;i++){// coefficients
write_model_layer(model,i,fac,TRUE,nexp,out);
}
break;
}
case HC_TYPE:{
for(i=0;i < model[0].n;i++){
fprintf(stdout,"%i %i %.8e %i %i %i\n",
model[0].lmax,i,model[0].d[i],model[0].n,1,0);
write_model_layer(model,i,fac,TRUE,nexp,out);
}
break;
}
default:{
fprintf(stderr,"write_model: type %i undefined\n",
model[0].radial_type);
exit(-1);
}}
}
void write_model_layer(struct mod *model, int layer,
COMP_PRECISION fac,int write_lmax,
int nexp, FILE *out)
{
COMP_PRECISION **a,**b,**amp,**amt,**bmp,**bmt;
int j;
/* */
a=(COMP_PRECISION **)malloc(sizeof(COMP_PRECISION *)*nexp);
b=(COMP_PRECISION **)malloc(sizeof(COMP_PRECISION *)*nexp);
amt=(COMP_PRECISION **)malloc(sizeof(COMP_PRECISION *)*nexp);
amp=(COMP_PRECISION **)malloc(sizeof(COMP_PRECISION *)*nexp);
bmt=(COMP_PRECISION **)malloc(sizeof(COMP_PRECISION *)*nexp);
bmp=(COMP_PRECISION **)malloc(sizeof(COMP_PRECISION *)*nexp);
for(j=0;j < nexp;j++){
if(!model[j].vector_field){
a[j] = model[j].a[layer];
b[j] = model[j].b[layer];
}else{
amp[j] = model[j].amp[layer];
bmp[j] = model[j].bmp[layer];
amt[j] = model[j].amt[layer];
bmt[j] = model[j].bmt[layer];
}
}
if(model[0].is_gsh){ /* GSH type */
for(j=0;j < nexp;j++)
write_gsh_coeff_set((model[j].is_gsh-1), model[j].lmax,
a[j],b[j],amp[j],amt[j],
bmp[j],bmt[j],fac,out);
}else{ /* regular */
if(model[0].vector_field){
// coefficients
write_vector_coefficients(amp,amt,bmp,bmt,
model[0].lmax,fac,
write_lmax,out,nexp);
}else{
write_coefficients(a,b,model[0].lmax,fac,
write_lmax,out,nexp);
}
}
free(a);free(b);
free(amp);free(bmp);
free(amt);free(bmt);
}