-
Notifications
You must be signed in to change notification settings - Fork 0
/
dimensionalisablefield.c
316 lines (276 loc) · 10.3 KB
/
dimensionalisablefield.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <petscdmcomposite.h>
#include "dimensionalisablefield.h"
#include "ctx.h"
PetscErrorCode DimensionalisableFieldCreate(DimensionalisableField *pf, DM dm, PetscScalar *scalings, PetscBool isScaled)
{
PetscErrorCode ierr;
PetscInt i;
DMType dmtype;
PetscBool isComposite;
DimensionalisableField f;
PetscFunctionBeginUser;
ierr = PetscMalloc1(1,pf);CHKERRQ(ierr);
f = *pf;
f->dm = dm;
ierr = DMGetType(f->dm,&dmtype);CHKERRQ(ierr);
ierr = PetscStrcmp(dmtype,DMCOMPOSITE,&isComposite);CHKERRQ(ierr);
if (isComposite) {
ierr = DMCompositeGetNumberDM(f->dm,&(f->numDomains));CHKERRQ(ierr);
} else {
f->numDomains = 1;
}
ierr = PetscMalloc1(f->numDomains,&f->scaling);CHKERRQ(ierr);
for (i=0; i<f->numDomains; ++i) {
f->scaling[i] = scalings[i];
}
f->scaled = isScaled;
ierr = DMCreateGlobalVector(f->dm,&f->vecGlobal);CHKERRQ(ierr);
f->name = "Unnamed DimensionalisableField";
ierr = PetscMalloc1(f->numDomains,&f->subdomainNames);CHKERRQ(ierr);
ierr = PetscMalloc1(f->numDomains,&f->subdomainUnits);CHKERRQ(ierr);
for (i=0; i<f->numDomains; ++i) {
f->subdomainNames[i] = "Unnamed Subdomain";
f->subdomainUnits[i] = "Unknown Units";
}
PetscFunctionReturn(0);
}
PetscErrorCode DimensionalisableFieldDestroy(DimensionalisableField *pf)
{
PetscErrorCode ierr;
DimensionalisableField f;
PetscFunctionBeginUser;
f = *pf;
if (f->vecGlobal) {ierr = VecDestroy(&f->vecGlobal);CHKERRQ(ierr);}
ierr = PetscFree(f->scaling);CHKERRQ(ierr);
ierr = PetscFree(f->subdomainNames);CHKERRQ(ierr);
ierr = PetscFree(f->subdomainUnits);CHKERRQ(ierr);
ierr = PetscFree(*pf);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
PetscErrorCode DimensionalisableFieldDuplicate(DimensionalisableField f,DimensionalisableField *pfNew)
{
PetscErrorCode ierr;
DimensionalisableField fNew;
PetscInt i;
PetscFunctionBeginUser;
ierr = DimensionalisableFieldCreate(pfNew,f->dm,f->scaling,f->scaled);CHKERRQ(ierr);
fNew = *pfNew;
ierr = DimensionalisableFieldSetName(fNew,f->name);CHKERRQ(ierr);
for (i=0; i<fNew->numDomains; ++i) {
ierr = DimensionalisableFieldSetSubdomainName(fNew,i,f->subdomainNames[i]);CHKERRQ(ierr);
ierr = DimensionalisableFieldSetSubdomainUnits(fNew,i,f->subdomainUnits[i]);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
/*
Get the global vector. Note that this is created and destroyed along with the
DimensionalisableField object, hence this just returns the Vec object (which
is a pointer). Contrast to DimensionalisableFieldCreateLocalVector()
Thus, you do NOT need to destroy this vector.
*/
PetscErrorCode DimensionalisableFieldGetGlobalVec(DimensionalisableField f,Vec *v)
{
PetscFunctionBeginUser;
*v = f->vecGlobal;
PetscFunctionReturn(0);
}
PetscErrorCode DimensionalisableFieldGetScaling(DimensionalisableField f,PetscInt *numDomains, PetscScalar *scaling)
{
PetscInt i;
PetscFunctionBeginUser;
*numDomains = f->numDomains;
for (i=0; i<f->numDomains; ++i) {
scaling[i] = f->scaling[i];
}
PetscFunctionReturn(0);
}
/*
Create a local vector. You should destroy this yourself.
*/
PetscErrorCode DimensionalisableFieldCreateLocalVec(DimensionalisableField f,Vec *pv)
{
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = DMCreateLocalVector(f->dm,pv);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
static PetscErrorCode DimensionalisableFieldScaleOrUnscale(DimensionalisableField f,PetscBool forward)
{
PetscErrorCode ierr;
PetscInt i;
Vec *subVecs;
PetscFunctionBeginUser;
if (f->scaled == forward) {
ierr = PetscPrintf(PETSC_COMM_WORLD,"Warning: attempting to scale/unscale an already-scaled/unscaled field. Doing nothing.\n");CHKERRQ(ierr);
} else {
f->scaled = forward;
if (f->numDomains == 1) {
if (forward) {
ierr = VecScale(f->vecGlobal,*f->scaling);CHKERRQ(ierr);
} else {
ierr = VecScale(f->vecGlobal,1.0/(*f->scaling));CHKERRQ(ierr);
}
} else {
ierr = PetscMalloc1(f->numDomains,&subVecs);CHKERRQ(ierr);
ierr = DMCompositeGetAccessArray(f->dm,f->vecGlobal,f->numDomains,NULL,subVecs);CHKERRQ(ierr);
for (i=0; i<f->numDomains; ++i) {
if (forward) {
ierr = VecScale(subVecs[i],f->scaling[i]);CHKERRQ(ierr);
} else {
ierr = VecScale(subVecs[i],1.0/(f->scaling[i]));CHKERRQ(ierr);
}
}
ierr = DMCompositeRestoreAccessArray(f->dm,f->vecGlobal,f->numDomains,NULL,subVecs);CHKERRQ(ierr);
ierr = PetscFree(subVecs);CHKERRQ(ierr);
}
}
PetscFunctionReturn(0);
}
PetscErrorCode DimensionalisableFieldScale(DimensionalisableField f)
{
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = DimensionalisableFieldScaleOrUnscale(f,PETSC_TRUE);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
PetscErrorCode DimensionalisableFieldUnscale(DimensionalisableField f)
{
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = DimensionalisableFieldScaleOrUnscale(f,PETSC_FALSE);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
/* Helper function to convert a scalar value to an appropriate string */
PetscErrorCode valueToString(PetscScalar val,char *str,int maxLength)
{
PetscErrorCode ierr;
#if PETSC_USE_REAL___FLOAT128
ierr = quadmath_snprintf(str,maxLength,"%32.32Qg",val);
if (ierr >= maxLength || ierr < 0) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_LIB,"quadmath_snprintf() failed");
#else
ierr = PetscSNPrintf(str,maxLength,"%16.16g",val);CHKERRQ(ierr);
#endif
return 0;
}
#define FORMAT_STRING_SIZE 64
PetscErrorCode DimensionalisableFieldToJSON(DimensionalisableField const f,cJSON **pjson)
{
PetscErrorCode ierr;
Vec vec;
PetscInt vecSize,i,d;
cJSON *json,*values,*subdomainArray;
const PetscScalar *arr;
Vec *subVecs;
PetscFunctionBeginUser;
ierr = DimensionalisableFieldGetGlobalVec(f,&vec);CHKERRQ(ierr);
*pjson = cJSON_CreateObject();
json = *pjson;
cJSON_AddItemToObject(json,"name",cJSON_CreateString(f->name));
cJSON_AddItemToObject(json,"subdomains",cJSON_CreateNumber(f->numDomains));
if (f->numDomains > 1) {
subdomainArray = cJSON_CreateArray();
ierr = PetscMalloc1(f->numDomains,&subVecs);CHKERRQ(ierr);
ierr = DMCompositeGetAccessArray(f->dm,vec,f->numDomains,NULL,subVecs);CHKERRQ(ierr);
} else {
if (f->subdomainUnits[0]) {
cJSON_AddItemToObject(json,"units",cJSON_CreateString(f->subdomainUnits[0]));
}
subdomainArray = NULL;
}
for (d=0; d<f->numDomains; ++d){
cJSON *curr;
Vec vecCurr;
if (f->numDomains > 1) {
curr = cJSON_CreateObject();
vecCurr = subVecs[d];
cJSON_AddItemToObject(curr,"subdomain",cJSON_CreateNumber(d));
if (f->subdomainNames[d]) {
cJSON_AddItemToObject(curr,"description",cJSON_CreateString(f->subdomainNames[d]));
}
if (f->subdomainUnits[d]) {
cJSON_AddItemToObject(curr,"units",cJSON_CreateString(f->subdomainUnits[d]));
}
} else {
curr = json;
vecCurr = vec;
}
ierr = VecGetSize(vecCurr,&vecSize);CHKERRQ(ierr);
cJSON_AddItemToObject(curr,"size",cJSON_CreateNumber(vecSize));
{
char str[FORMAT_STRING_SIZE];
ierr = valueToString(f->scaling[d],str,FORMAT_STRING_SIZE);CHKERRQ(ierr);
cJSON_AddItemToObject(curr,"scaling",cJSON_CreateString(str));
}
cJSON_AddItemToObject(curr,"scaled",f->scaled? cJSON_CreateString("true") : cJSON_CreateString("false"));
/* Store vector values as STRINGS to ensure precision we want */
/* Current implementation assumes single rank */
{
PetscMPIInt size;
ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);
if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Not implemented in parallel");
}
ierr = VecGetArrayRead(vecCurr,&arr);CHKERRQ(ierr);
values = cJSON_CreateArray();
{
for (i=0; i<vecSize; ++i) {
char str[FORMAT_STRING_SIZE];
ierr = valueToString(arr[i],str,FORMAT_STRING_SIZE);CHKERRQ(ierr);
cJSON_AddItemToArray(values,cJSON_CreateString(str));
}
}
ierr = VecRestoreArrayRead(vecCurr,&arr);CHKERRQ(ierr);
cJSON_AddItemToObject(curr,"values",values);
if (f->numDomains > 1) {
cJSON_AddItemToArray(subdomainArray,curr);
}
}
if (f->numDomains > 1 ) {
cJSON_AddItemToObject(json,"subdomain data",subdomainArray);
ierr = DMCompositeRestoreAccessArray(f->dm,vec,f->numDomains,NULL,subVecs);CHKERRQ(ierr);
ierr = PetscFree(subVecs);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
#undef FORMAT_STRING_SIZE
PetscErrorCode DimensionalisableFieldSetName(DimensionalisableField f,const char *name)
{
PetscFunctionBeginUser;
f->name = name;
PetscFunctionReturn(0);
}
PetscErrorCode DimensionalisableFieldSetUnits(DimensionalisableField f,const char *units)
{
PetscFunctionBeginUser;
if (f->numDomains != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Not supported for multiple-subdomain DimensionalisableField objects. Use DimensionalisableFieldSetSubdomainUnits");
f->subdomainUnits[0] = units;
PetscFunctionReturn(0);
}
PetscErrorCode DimensionalisableFieldSetSubdomainName(DimensionalisableField f,PetscInt subdomain,const char *name)
{
PetscFunctionBeginUser;
f->subdomainNames[subdomain] = name;
PetscFunctionReturn(0);
}
PetscErrorCode DimensionalisableFieldSetSubdomainUnits(DimensionalisableField f,PetscInt subdomain,const char *units)
{
PetscFunctionBeginUser;
f->subdomainUnits[subdomain] = units;
PetscFunctionReturn(0);
}
PetscErrorCode JSON_add_single_value_to_object( DM dm, PetscScalar scaling, const char *name, const char *units, const PetscScalar value, cJSON *data )
{
PetscErrorCode ierr;
cJSON *item;
DimensionalisableField dfield;
PetscFunctionBeginUser;
ierr = DimensionalisableFieldCreate(&dfield,dm,&scaling,PETSC_FALSE);CHKERRQ(ierr);
ierr = DimensionalisableFieldSetName(dfield,name);CHKERRQ(ierr);
ierr = DimensionalisableFieldSetUnits(dfield,units);CHKERRQ(ierr);
ierr = VecSetValue(dfield->vecGlobal,0,value,INSERT_VALUES);CHKERRQ(ierr);
ierr = VecAssemblyBegin(dfield->vecGlobal);CHKERRQ(ierr);
ierr = VecAssemblyEnd(dfield->vecGlobal);CHKERRQ(ierr);
ierr = DimensionalisableFieldToJSON(dfield,&item);CHKERRQ(ierr);
cJSON_AddItemToObject(data,name,item);
ierr = DimensionalisableFieldDestroy(&dfield);CHKERRQ(ierr);
PetscFunctionReturn(0);
}