forked from forefireAPI/firefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJLibForeFire.cpp
216 lines (161 loc) · 6.02 KB
/
JLibForeFire.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Copyright (C) 2012 ForeFire Team, SPE, UniversitŽ de Corse.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 US
*/
#ifdef _JNI_IMPLEMENTATION
#include <stdio.h>
#include "JLibForeFire.h"
#include "Command.h"
#include <string.h>
using namespace std;
using namespace libforefire;
//static istream JavaInput;
Command* executor = 0;
/*
* Class: fprop_coupling_CCoupling
* Method: executeCommand
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_fprop_coupling_CCoupling_executeCommand (JNIEnv *penv, jclass jc, jstring jstr)
{
const char * msg = penv->GetStringUTFChars(jstr,0);
if ( executor == 0 ) {
executor = new Command();
}
ostringstream stringOut;
executor->setOstringstream(&stringOut);
string smsg(msg);
executor->ExecuteCommand(smsg);
penv->ReleaseStringUTFChars(jstr,msg);
const string& tmpString(stringOut.str());
const char* const tmpchar = tmpString.c_str();
return penv->NewStringUTF(tmpchar);
}
/*
* Class: fprop_coupling_CCoupling
* Method: getMatrix
* Signature: (Ljava/lang/String;)[[F
*/
JNIEXPORT jobjectArray JNICALL Java_fprop_coupling_CCoupling_getMatrix (JNIEnv *penv, jclass jc, jstring jstr)
{
int i,j,k;
jboolean isCopy;
const char * msg = penv->GetStringUTFChars(jstr,0);
string smsg(msg);
if ( executor == 0 ) {
executor = new Command();
}
/* Testing to see if the wanted matrix is a flux */
FluxLayer<double>* myFluxLayer = executor->getDomain()->getFluxLayer(msg);
if ( myFluxLayer ){
//executor->getDomain()->getDataBroker()->computeActiveSurfacesFlux(40000);
FFArray<double>* srcD;
// getting the pointer
myFluxLayer->getMatrix(&srcD, executor->getTime());
// myFluxLayer->getInstantaneousFlux(&srcD, executor->getTime());
if (srcD == 0) return NULL;
int nx = srcD->getDim("x");
int ny = srcD->getDim("y");
int nz = srcD->getDim("z");
jdoubleArray elemProto = penv->NewDoubleArray(nz);
jdoubleArray elem = penv->NewDoubleArray(nz);
jobjectArray row = penv->NewObjectArray(ny,penv->GetObjectClass(elem), NULL);
jobjectArray resultDD = penv->NewObjectArray(nx,penv->GetObjectClass(row), NULL);
penv->DeleteLocalRef(row);
row = NULL;
penv->DeleteLocalRef(elem);
elem = NULL;
for (i = 0; i < nx; i++) {
jobjectArray row = penv->NewObjectArray(ny,penv->GetObjectClass(elemProto), NULL);
for (j = 0; j < ny; j++) {
elem = penv->NewDoubleArray(nz);
jdouble* elemElems = penv->GetDoubleArrayElements(elem, &isCopy);
for (k = 0; k < nz; k++) {
elemElems[k] = (*srcD)(i,j,k);
}
penv->SetDoubleArrayRegion( elem, 0, nz, elemElems);
penv->SetObjectArrayElement(row, j, elem);
if (isCopy == JNI_TRUE) {
penv->ReleaseDoubleArrayElements( elem, elemElems, JNI_ABORT);
}
penv->DeleteLocalRef(elem);
elem = NULL;
}
penv->SetObjectArrayElement(resultDD, i, row);
penv->DeleteLocalRef(row);
row = NULL;
}
penv->DeleteLocalRef(elemProto);
elemProto = NULL;
return resultDD;
} else {
/* If not, treating it as a generic data layer */
DataLayer<double>* myLayer = executor->getDomain()->getDataLayer(msg);
if ( myLayer ){
FFArray<double>* srcD;
// getting the pointer
myLayer->getMatrix(&srcD, executor->getTime());
if (srcD == 0) return NULL;
int nx = srcD->getDim("x");
int ny = srcD->getDim("y");
int nz = 1;// srcD->getDim("z");
jdoubleArray elemProto = penv->NewDoubleArray(nz);
jdoubleArray elem = penv->NewDoubleArray(nz);
jobjectArray row = penv->NewObjectArray(ny,penv->GetObjectClass(elem), NULL);
jobjectArray resultDD = penv->NewObjectArray(nx,penv->GetObjectClass(row), NULL);
penv->DeleteLocalRef(row);
row = NULL;
penv->DeleteLocalRef(elem);
elem = NULL;
double dx = (executor->getDomain()->getNECorner().getX() - executor->getDomain()->getSWCorner().getX())/nx;
double dy = (executor->getDomain()->getNECorner().getY() - executor->getDomain()->getSWCorner().getY())/ny;
FFPoint p = FFPoint(executor->getDomain()->getSWCorner().getX(),executor->getDomain()->getSWCorner().getY(),0);
for (i = 0; i < nx; i++) {
jobjectArray row = penv->NewObjectArray(ny,penv->GetObjectClass(elemProto), NULL);
for (j = 0; j < ny; j++) {
elem = penv->NewDoubleArray(nz);
jdouble* elemElems = penv->GetDoubleArrayElements(elem, &isCopy);
for (k = 0; k < nz; k++) {
elemElems[k] = myLayer->getValueAt(p,0);//(*srcD)(i,j,k);
}
penv->SetDoubleArrayRegion( elem, 0, nz, elemElems);
penv->SetObjectArrayElement(row, j, elem);
if (isCopy == JNI_TRUE) {
penv->ReleaseDoubleArrayElements( elem, elemElems, JNI_ABORT);
}
p.y += dy;
penv->DeleteLocalRef(elem);
elem = NULL;
}
penv->SetObjectArrayElement(resultDD, i, row);
p.x += dx;
p.y = executor->getDomain()->getSWCorner().getY();
penv->DeleteLocalRef(row);
row = NULL;
}
penv->DeleteLocalRef(elemProto);
elemProto = NULL;
return resultDD;
}
return NULL;
}
}
/*
* Class: fprop_coupling_CCoupling
* Method: setMatrix
* Signature: (Ljava/lang/String;[[F)V
*/
JNIEXPORT void JNICALL Java_fprop_coupling_CCoupling_setMatrix
(JNIEnv *penv, jobject caller, jstring key, jobjectArray values){
}
#endif