source: issm/trunk/src/mobile/native/Main.cpp@ 15396

Last change on this file since 15396 was 14712, checked in by Eric.Larour, 12 years ago

CHG: better units

File size: 5.9 KB
Line 
1#include "../../c/issm.h"
2#include <cstddef>
3#include <stdio.h>
4
5//Android specific header includes:
6#include <jni.h>
7#include <android/log.h>
8#include <android/log.h>
9
10//iOS specific header includes:
11
12namespace gov_nasa_jpl_issm
13{
14 /*Global variables{{{*/
15 FemModel *fm;
16 double* xyz; /*keep vertices information here*/
17 /*}}}*/
18 jint Initialize(JNIEnv *env, jclass clazz, jstring jsolution_type, jstring jabsfile, jstring jrelfile) /*{{{*/
19 {
20
21 /*arguments to constructor: */
22 int argc; //arguments to constructor.
23 char** argv = NULL;
24 COMM communicator = 1; //fake communicator for constructor
25 const char* issmname = "issm.exe";
26 char *solution_type = NULL;
27 char *absfile = NULL;
28 char *relfile = NULL;
29
30 /*log:*/
31 __android_log_print(ANDROID_LOG_INFO, "Native","Initializing FemModel");
32
33 /*retrieve from java machine: */
34 solution_type = (char*)env->GetStringUTFChars(jsolution_type,0);
35 absfile = (char*)env->GetStringUTFChars(jabsfile,0);
36 relfile = (char*)env->GetStringUTFChars(jrelfile,0);
37
38 /*creat arguments to call constructor for FemModel: */
39 argc=4;
40 argv=(char**)malloc(argc*sizeof(char*));
41 argv[0]=(char*)issmname;
42 argv[1]=solution_type;
43 argv[2]=absfile;
44 argv[3]=relfile;
45
46 /*call Model constructor passing in infile as File Descriptor parameter.*/
47 fm = new FemModel(argc,argv,communicator);
48
49 /*we'll need the toolkits activated right away to use matrices: */
50 ToolkitsOptionsFromAnalysis(fm->parameters,NoneAnalysisEnum);
51
52 /*release strings: */
53 env->ReleaseStringUTFChars(jsolution_type, solution_type); //must realease the char*
54 env->ReleaseStringUTFChars(jabsfile, absfile); //must realease the char*
55 env->ReleaseStringUTFChars(jrelfile, relfile); //must realease the char*
56
57 /*figure out size of solution: */
58 __android_log_print(ANDROID_LOG_INFO, "Native","Number of elements");
59 jint size = (jint) fm->elements->NumberOfElements();
60
61 /*retrieve vertices x,y and z coordinates: */
62 __android_log_print(ANDROID_LOG_INFO, "Native","Retrieving vertices");
63 xyz=fm->vertices->ToXYZ();
64
65 /*log: */
66 __android_log_print(ANDROID_LOG_INFO, "Native","Done Initializing FemModel");
67
68 return size;
69
70 }
71 /*}}}*/
72 void Solve(JNIEnv *env, jclass clazz , jdouble alpha, jdouble temperature, jobject buf){ /*{{{*/
73
74 int i,count;
75 double x1,y1,z1,vel1;
76 double x2,y2,z2,vel2;
77 double x3,y3,z3,vel3;
78 int v1,v2,v3,eid;
79 Patch* patch=NULL;
80
81 /*log:*/
82 __android_log_print(ANDROID_LOG_INFO, "Native","Solving ");
83
84 /*retrieve buffer: */
85 jdouble *dBuf = (jdouble *)env->GetDirectBufferAddress(buf);
86
87 /*reset basal friction to what it was before: */
88 __android_log_print(ANDROID_LOG_INFO, "Native","alpha %g ",alpha);
89 /*reset basal temperature to what it was before: */
90 __android_log_print(ANDROID_LOG_INFO, "Native","temperature %g ",temperature);
91
92 InputDuplicatex(fm->elements,fm->nodes,fm->vertices,fm->loads,fm->materials,fm->parameters,AndroidFrictionCoefficientEnum,FrictionCoefficientEnum);
93
94 /*now scale friction by alpha: */
95 InputScalex(fm->elements,fm->nodes,fm->vertices,fm->loads,fm->materials,fm->parameters,FrictionCoefficientEnum,alpha/100);
96
97 /*solve: */
98 fm -> Solve();
99
100 /*retrieve results: */
101 __android_log_print(ANDROID_LOG_INFO, "Native","Retrieving results ");
102 fm->elements->ProcessResultsUnits();
103 patch=fm->elements->ResultsToPatch();
104
105 /*sort out the velocities: */
106 for(i=0;i<patch->numrows;i++){
107 if ((patch->values[i*patch->numcols+0])==VelEnum){
108
109 /*Each row of the Patch object is made of the following information:
110 - the result enum_type,
111 - the step and time,
112 - the id of the element,
113 - the interpolation type,
114 - the vertices ids,
115 - and the values at the nodes (could be different from the vertices)
116 */
117 eid=(int)patch->values[i*patch->numcols+3]-1;
118 v1=(int)patch->values[i*patch->numcols+5];
119 x1=xyz[3*(v1-1)+0]; y1=xyz[3*(v1-1)+1]; z1=xyz[3*(v1-1)+2];
120
121 v2=(int)patch->values[i*patch->numcols+6];
122 x2=xyz[3*(v2-1)+0]; y2=xyz[3*(v2-1)+1]; z2=xyz[3*(v2-1)+2];
123
124 v3=(int)patch->values[i*patch->numcols+7];
125 x3=xyz[3*(v3-1)+0]; y3=xyz[3*(v3-1)+1]; z3=xyz[3*(v3-1)+2];
126
127 vel1=patch->values[i*patch->numcols+8];
128 vel2=patch->values[i*patch->numcols+9];
129 vel3=patch->values[i*patch->numcols+10];
130
131 /*plug into dBuf: */
132 /*vertex 1: */
133 dBuf[12*eid+0]=x1;
134 dBuf[12*eid+1]=y1;
135 dBuf[12*eid+2]=z1;
136
137 /*vertex 2: */
138 dBuf[12*eid+3]=x2;
139 dBuf[12*eid+4]=y2;
140 dBuf[12*eid+5]=z2;
141
142 /*vertex 3: */
143 dBuf[12*eid+6]=x3;
144 dBuf[12*eid+7]=y3;
145 dBuf[12*eid+8]=z3;
146
147 /*values at 3 vertices: */
148 dBuf[12*eid+9]=vel1;
149 dBuf[12*eid+10]=vel2;
150 dBuf[12*eid+11]=vel3;
151
152 }
153 }
154
155 /*for(i=0;i<148;i++){
156 __android_log_print(ANDROID_LOG_INFO, "Native","%g %g %g | %g %g %g | %g %g %g | %g %g %g\n",
157 dBuf[12*i+0],dBuf[12*i+1],dBuf[12*i+2],
158 dBuf[12*i+3],dBuf[12*i+4],dBuf[12*i+5],
159 dBuf[12*i+6],dBuf[12*i+7],dBuf[12*i+8],
160 dBuf[12*i+9],dBuf[12*i+10],dBuf[12*i+11]);
161 }*/
162
163 /*delete temporary data:*/
164 delete patch;
165
166 }/*}}}*/
167 static JNINativeMethod method_table[] = /*{{{*/
168 {
169 {"createISSMModel" ,"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I" , (void *) Initialize},
170 {"solveISSMModel", "(DDLjava/nio/DoubleBuffer;)V", (void *) Solve}
171 };
172 /*}}}*/
173}
174
175using namespace gov_nasa_jpl_issm;
176extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) /*{{{*/
177{
178 JNIEnv* env;
179 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
180 return -1;
181 }
182 else
183 {
184 jclass clazz = env->FindClass("gov/nasa/jpl/issm/IssmJni");
185 if(clazz)
186 {
187 env->RegisterNatives(clazz, method_table, 3);
188 env->DeleteLocalRef(clazz);
189 return JNI_VERSION_1_6;
190 }
191 else return -1;
192 }
193}
194/*}}}*/
Note: See TracBrowser for help on using the repository browser.