|
@@ -0,0 +1,337 @@
|
|
1
|
+
|
|
2
|
+ * Adds second input to each
|
|
3
|
+ * element of first input
|
|
4
|
+ * a = MyMEXFunction(a,b);
|
|
5
|
+*/
|
|
6
|
+
|
|
7
|
+#include "mex.hpp"
|
|
8
|
+#include "mexAdapter.hpp"
|
|
9
|
+#include "MatlabDataArray.hpp"
|
|
10
|
+#include "LemmaCore"
|
|
11
|
+#include "FDEM1D"
|
|
12
|
+#include "timer.h"
|
|
13
|
+#include <Eigen/Core>
|
|
14
|
+
|
|
15
|
+#if defined(__clang__)
|
|
16
|
+
|
|
17
|
+ const char* compiler = "clang";
|
|
18
|
+ const char* ver = __VERSION__;
|
|
19
|
+#elif defined(__ICC) || defined(__INTEL_COMPILER)
|
|
20
|
+
|
|
21
|
+ const char* compiler = "icpc";
|
|
22
|
+ const char* ver = __VERSION__;
|
|
23
|
+#elif defined(__GNUC__) || defined(__GNUG__)
|
|
24
|
+
|
|
25
|
+ const char* compiler = "gcc (GCC) ";
|
|
26
|
+ const char* ver = __VERSION__;
|
|
27
|
+#elif defined(_MSC_VER)
|
|
28
|
+
|
|
29
|
+ const char* compiler = "msvc ";
|
|
30
|
+ const int ver = _MSC_FULL_VER;
|
|
31
|
+#elif defined(__PGI)
|
|
32
|
+
|
|
33
|
+ const char* compiler = "pgc";
|
|
34
|
+#endif
|
|
35
|
+
|
|
36
|
+using namespace Lemma;
|
|
37
|
+using namespace matlab::data;
|
|
38
|
+using matlab::mex::ArgumentList;
|
|
39
|
+
|
|
40
|
+std::vector<Real> readinpfile(const std::string& fname);
|
|
41
|
+std::vector<std::string> readinpfile2(const std::string& fname);
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+class MexFunction : public matlab::mex::Function {
|
|
45
|
+
|
|
46
|
+public:
|
|
47
|
+
|
|
48
|
+ void operator()(ArgumentList outputs, ArgumentList inputs) {
|
|
49
|
+
|
|
50
|
+ const char *buildString = __DATE__ ", " __TIME__;
|
|
51
|
+ std::cout
|
|
52
|
+ << "===========================================================================\n"
|
|
53
|
+ << "Lemma " << LEMMA_VERSION << "\n"
|
|
54
|
+ << "[" << compiler << " " << ver << " " << buildString << "]\n"
|
|
55
|
+ << "This program is part of Lemma, a geophysical modelling and inversion API. \n"
|
|
56
|
+ << " This Source Code Form is subject to the terms of the Mozilla Public\n"
|
|
57
|
+ << " License, v. 2.0. If a copy of the MPL was not distributed with this\n"
|
|
58
|
+ << " file, You can obtain one at http://mozilla.org/MPL/2.0/. \n"
|
|
59
|
+ << "Copyright (C) 2018 Lemma Software \n"
|
|
60
|
+ << "More information may be found at: https://lemmasoftware.org\n"
|
|
61
|
+ << " info@lemmasoftware.org\n"
|
|
62
|
+ << "===========================================================================\n\n"
|
|
63
|
+ << "Hantenna calculates the harmonic H field from polygonal wire loop sources\n";
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+ if (inputs.size() < 4) {
|
|
70
|
+ std::cout << "usage: hantenna(trans.inp cond.inp points.inp config.inp) \n";
|
|
71
|
+ ArrayFactory ef;
|
|
72
|
+
|
|
73
|
+ outputs[0] = ef.createCharArray("Insufficient inputs");
|
|
74
|
+ return;
|
|
75
|
+
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ #ifdef LEMMAUSEOMP
|
|
79
|
+ std::cout << "OpenMP is using " << omp_get_max_threads() << " threads" << std::endl;
|
|
80
|
+ #endif
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+ std::vector<Real> Trans = readinpfile(std::string(inputs[1]));
|
|
84
|
+ std::vector<Real> CondMod = readinpfile(std::string(inputs[2]));
|
|
85
|
+ std::vector<Real> Points = readinpfile(std::string(inputs[3]));
|
|
86
|
+ std::vector<std::string> config = readinpfile2(std::string(inputs[4]));
|
|
87
|
+ */
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+ matlab::data::CharArray charVector2 = inputs[0];
|
|
91
|
+ std::string TransFile = charVector2.toAscii();
|
|
92
|
+
|
|
93
|
+ std::vector<Real> Trans = readinpfile( "trans.inp" );
|
|
94
|
+ std::vector<std::string> config = readinpfile2("config.inp");
|
|
95
|
+ std::vector<Real> CondMod = readinpfile("cond.inp");
|
|
96
|
+ std::vector<Real> Points = readinpfile("points.inp");
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+ auto trans = PolygonalWireAntenna::NewSP();
|
|
101
|
+ trans->SetNumberOfPoints((int)(Trans[0]));
|
|
102
|
+ int ip=1;
|
|
103
|
+ for ( ; ip<=(int)(Trans[0])*2; ip+=2) {
|
|
104
|
+ trans->SetPoint(ip/2, Vector3r (Trans[ip], Trans[ip+1], -1e-3));
|
|
105
|
+
|
|
106
|
+ }
|
|
107
|
+ trans->SetNumberOfFrequencies(1);
|
|
108
|
+ trans->SetFrequency(0, Trans[ip]);
|
|
109
|
+ trans->SetCurrent(Trans[ip+1]);
|
|
110
|
+ trans->SetMinDipoleRatio(atof(config[1].c_str()));
|
|
111
|
+ trans->SetMinDipoleMoment(atof(config[2].c_str()));
|
|
112
|
+ trans->SetMaxDipoleMoment(atof(config[3].c_str()));
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+ auto receivers = FieldPoints::NewSP();
|
|
117
|
+ int nx = (int)Points[0];
|
|
118
|
+ int ny = (int)Points[1];
|
|
119
|
+ int nz = (int)Points[2];
|
|
120
|
+ Real ox = Points[3];
|
|
121
|
+ Real oy = Points[4];
|
|
122
|
+ Real oz = Points[5];
|
|
123
|
+ Vector3r loc;
|
|
124
|
+ VectorXr dx(nx-1);
|
|
125
|
+ VectorXr dy(ny-1);
|
|
126
|
+ VectorXr dz(nz-1);
|
|
127
|
+ ip = 6;
|
|
128
|
+ int ir = 0;
|
|
129
|
+ for ( ; ip <6+nx-1; ++ip) {
|
|
130
|
+ dx[ir] = Points[ip];
|
|
131
|
+ ++ir;
|
|
132
|
+ }
|
|
133
|
+ ir = 0;
|
|
134
|
+ for ( ; ip <6+ny-1+nx-1; ++ip) {
|
|
135
|
+ dy[ir] = Points[ip];
|
|
136
|
+ ++ir;
|
|
137
|
+ }
|
|
138
|
+ ir = 0;
|
|
139
|
+ for ( ; ip <6+nz-1+ny-1+nx-1; ++ip) {
|
|
140
|
+ dz[ir] = Points[ip];
|
|
141
|
+ ++ir;
|
|
142
|
+ }
|
|
143
|
+ receivers->SetNumberOfPoints(nx*ny*nz);
|
|
144
|
+ ir = 0;
|
|
145
|
+ Real pz = oz;
|
|
146
|
+ for (int iz=0; iz<nz; ++iz) {
|
|
147
|
+ Real py = oy;
|
|
148
|
+ for (int iy=0; iy<ny; ++iy) {
|
|
149
|
+ Real px = ox;
|
|
150
|
+ for (int ix=0; ix<nx; ++ix) {
|
|
151
|
+ loc << px, py, pz;
|
|
152
|
+ receivers->SetLocation(ir, loc);
|
|
153
|
+ if (ix < nx-1) px += dx[ix];
|
|
154
|
+ ++ ir;
|
|
155
|
+ }
|
|
156
|
+ if (iy<ny-1) py += dy[iy];
|
|
157
|
+ }
|
|
158
|
+ if (iz<nz-1) pz += dz[iz];
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+ auto earth = LayeredEarthEM::NewSP();
|
|
164
|
+ VectorXcr sigma;
|
|
165
|
+ VectorXr thick;
|
|
166
|
+ earth->SetNumberOfLayers(static_cast<int>(CondMod[0])+1);
|
|
167
|
+ sigma.resize(static_cast<int>(CondMod[0])+1); sigma(0) = 0;
|
|
168
|
+ thick.resize(static_cast<int>(CondMod[0])-1);
|
|
169
|
+ int ilay=1;
|
|
170
|
+ for ( ; ilay/2<CondMod[0]-1; ilay+=2) {
|
|
171
|
+ sigma(ilay/2+1) = 1./CondMod[ilay];
|
|
172
|
+ thick(ilay/2) = CondMod[ilay+1];
|
|
173
|
+ }
|
|
174
|
+ sigma(ilay/2+1) = 1./ CondMod[ilay];
|
|
175
|
+ earth->SetLayerConductivity(sigma);
|
|
176
|
+ if (thick.size() > 0) earth->SetLayerThickness(thick);
|
|
177
|
+
|
|
178
|
+ auto EmEarth = EMEarth1D::NewSP();
|
|
179
|
+ EmEarth->AttachWireAntenna(trans);
|
|
180
|
+ EmEarth->AttachLayeredEarthEM(earth);
|
|
181
|
+ EmEarth->AttachFieldPoints(receivers);
|
|
182
|
+ EmEarth->SetFieldsToCalculate(H);
|
|
183
|
+ EmEarth->SetHankelTransformMethod(string2Enum<HANKELTRANSFORMTYPE>(config[0]));
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+ jsw_timer timer;
|
|
188
|
+ timer.begin();
|
|
189
|
+ clock_t launch = clock();
|
|
190
|
+ EmEarth->CalculateWireAntennaFields(false);
|
|
191
|
+ Real paTime = timer.end();
|
|
192
|
+
|
|
193
|
+ std::cout << "\n\n===========================================\ncalc. real time: " << paTime/60. << "\t[m]\n";
|
|
194
|
+ std::cout << "calc. user time: " << (clock()-launch)/CLOCKS_PER_SEC/60. << "\t[CPU m]"
|
|
195
|
+ << std::endl;
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+ ArrayFactory f;
|
|
203
|
+ StructArray S = f.createStructArray({ 3,1 }, { "Component", "Data", "Units" });
|
|
204
|
+
|
|
205
|
+ S[0]["Component"] = f.createCharArray("H_x");
|
|
206
|
+ S[0]["Units"] = f.createCharArray("A/m");
|
|
207
|
+ Eigen::Array<Complex, Eigen::Dynamic, 1> Hx = receivers->GetHfield(0).row(0).array();
|
|
208
|
+ std::vector<Complex> Hxv = std::vector<Complex>(Hx.begin(), Hx.end());
|
|
209
|
+ matlab::data::TypedArray<Complex> Hxm = f.createArray<Complex>({1, Hxv.size()}, Hxv.data(), Hxv.data()+Hxv.size());
|
|
210
|
+ S[0]["Data"] = std::move(Hxm);
|
|
211
|
+
|
|
212
|
+ S[1]["Component"] = f.createCharArray("H_y");
|
|
213
|
+ S[1]["Units"] = f.createCharArray("A/m");
|
|
214
|
+ Eigen::Array<Complex, Eigen::Dynamic, 1> Hy = receivers->GetHfield(0).row(1).array();
|
|
215
|
+ std::vector<Complex> Hyv = std::vector<Complex>(Hy.begin(), Hy.end());
|
|
216
|
+ matlab::data::TypedArray<Complex> Hym = f.createArray<Complex>({1, Hyv.size()}, Hyv.data(), Hyv.data()+Hyv.size());
|
|
217
|
+ S[1]["Data"] = std::move(Hym);
|
|
218
|
+
|
|
219
|
+ S[2]["Component"] = f.createCharArray("H_z");
|
|
220
|
+ S[2]["Units"] = f.createCharArray("A/m");
|
|
221
|
+ Eigen::Array<Complex, Eigen::Dynamic, 1> Hz = receivers->GetHfield(0).row(2).array();
|
|
222
|
+ std::vector<Complex> Hzv = std::vector<Complex>(Hz.begin(), Hz.end());
|
|
223
|
+ matlab::data::TypedArray<Complex> Hzm = f.createArray<Complex>({1, Hzv.size()}, Hzv.data(), Hzv.data()+Hzv.size());
|
|
224
|
+ S[2]["Data"] = std::move(Hzm);
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+ outputs[0] = std::move(S);
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+ std::fstream hrep("hfield.yaml", std::ios::out);
|
|
233
|
+ std::fstream hreal("hfield.dat", std::ios::out);
|
|
234
|
+
|
|
235
|
+ hrep << *EmEarth << std::endl;
|
|
236
|
+ hrep.close();
|
|
237
|
+
|
|
238
|
+ hreal << "// Right hand coordinate system, z is positive down\n";
|
|
239
|
+ hreal << "// x[m]\ty[m]\tz[m]\tRe(Hx[A/m])\tRe(Hy[A/m])\tRe(Hz[A/m])\tIm(Hx)\tIm(Hy)\tIm(Hz)\n";
|
|
240
|
+ hreal.precision(8);
|
|
241
|
+ int i=0;
|
|
242
|
+ for (int iz=0; iz<nz; ++iz) {
|
|
243
|
+ for (int iy=0; iy<ny; ++iy) {
|
|
244
|
+ for (int ix=0; ix<nx; ++ix) {
|
|
245
|
+ hreal << receivers->GetLocation(i).transpose() << "\t";
|
|
246
|
+ hreal << receivers->GetHfield(0, i).transpose() << "\n";
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+ ++i;
|
|
250
|
+ }
|
|
251
|
+ }
|
|
252
|
+ }
|
|
253
|
+ hreal.close();
|
|
254
|
+ */
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+ const double offSet = inputs[0][0];
|
|
259
|
+ TypedArray<double> doubleArray = std::move(inputs[1]);
|
|
260
|
+ for (auto& elem : doubleArray) {
|
|
261
|
+ elem += offSet;
|
|
262
|
+ }
|
|
263
|
+ outputs[0] = doubleArray;
|
|
264
|
+ */
|
|
265
|
+ }
|
|
266
|
+
|
|
267
|
+ void checkArguments(ArgumentList outputs, ArgumentList inputs) {
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+ std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+ ArrayFactory factory;
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+ if (inputs[0].getType() != ArrayType::CHAR ||
|
|
277
|
+ inputs[0].getNumberOfElements() != 1)
|
|
278
|
+ {
|
|
279
|
+ matlabPtr->feval(u"error",
|
|
280
|
+ 0,
|
|
281
|
+ std::vector<Array>({ factory.createScalar("First input must be the name of a trans.inp file") }));
|
|
282
|
+ }
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+ if (inputs[1].getType() != ArrayType::DOUBLE ||
|
|
286
|
+ inputs[1].getType() == ArrayType::COMPLEX_DOUBLE)
|
|
287
|
+ {
|
|
288
|
+ matlabPtr->feval(u"error",
|
|
289
|
+ 0,
|
|
290
|
+ std::vector<Array>({ factory.createScalar("Input must be double array") }));
|
|
291
|
+ }
|
|
292
|
+
|
|
293
|
+ if (outputs.size() > 1) {
|
|
294
|
+ matlabPtr->feval(u"error",
|
|
295
|
+ 0,
|
|
296
|
+ std::vector<Array>({ factory.createScalar("Only one output is returned") }));
|
|
297
|
+ }
|
|
298
|
+ }
|
|
299
|
+};
|
|
300
|
+
|
|
301
|
+std::vector<Real> readinpfile(const std::string& fname) {
|
|
302
|
+ std::string buf;
|
|
303
|
+ char dump[255];
|
|
304
|
+ std::vector<Real> vals;
|
|
305
|
+ std::fstream input(fname.c_str(), std::ios::in);
|
|
306
|
+ if (input.fail()) {
|
|
307
|
+ std::cerr << "Input file " << fname << " failed to open\n";
|
|
308
|
+ exit(EXIT_FAILURE);
|
|
309
|
+ }
|
|
310
|
+ while (input >> buf) {
|
|
311
|
+ if (buf.substr(0,2) == "//") {
|
|
312
|
+ input.getline(dump, 255);
|
|
313
|
+ } else {
|
|
314
|
+ vals.push_back( atof(buf.c_str() ));
|
|
315
|
+ }
|
|
316
|
+ }
|
|
317
|
+ return vals;
|
|
318
|
+}
|
|
319
|
+
|
|
320
|
+std::vector<std::string> readinpfile2(const std::string& fname) {
|
|
321
|
+ std::string buf;
|
|
322
|
+ char dump[255];
|
|
323
|
+ std::vector<std::string> vals;
|
|
324
|
+ std::fstream input(fname.c_str(), std::ios::in);
|
|
325
|
+ if (input.fail()) {
|
|
326
|
+ std::cerr << "Input file " << fname << " failed to open\n";
|
|
327
|
+ exit(EXIT_FAILURE);
|
|
328
|
+ }
|
|
329
|
+ while (input >> buf) {
|
|
330
|
+ if (buf.substr(0,2) == "//") {
|
|
331
|
+ input.getline(dump, 255);
|
|
332
|
+ } else {
|
|
333
|
+ vals.push_back( std::string(buf.c_str() ));
|
|
334
|
+ }
|
|
335
|
+ }
|
|
336
|
+ return vals;
|
|
337
|
+}
|