19 static void dVecRead(std::vector<double> &v,
int n, FILE *fp,
bool binary,
25 if(!fread(&v[0],
sizeof(
double), n, fp))
Msg::Error(
"Read error");
29 for(
int i = 0; i < n; i++) {
30 if(fscanf(fp,
"%lf", &v[i]) != 1) {
38 static void cVecRead(std::vector<char> &v,
int n, FILE *fp,
bool binary,
39 int swap,
bool oldStyle)
44 if(!fread(&v[0],
sizeof(
char), n, fp))
Msg::Error(
"Read error");
49 for(
int i = 0; i < n; i++) {
50 if(fscanf(fp,
"%c", &v[i]) != 1) {
54 if(v[i] ==
'^') v[i] =
'\0';
58 for(
int i = 0; i < n; i++) {
59 char c = (char)fgetc(fp);
72 static void dVecWrite(std::vector<double> &v, FILE *fp,
bool binary)
76 fwrite(&v[0],
sizeof(
double), v.size(), fp);
78 for(
unsigned i = 0; i < v.size(); i++) fprintf(fp,
" %.16g", v[i]);
81 static void cVecWrite(std::vector<char> &v, FILE *fp,
bool binary)
85 fwrite(&v[0],
sizeof(
char), v.size(), fp);
87 for(
unsigned i = 0; i < v.size(); i++) fputc(v[i], fp);
95 int NbSL2 = 0, NbVL2 = 0, NbTL2 = 0, NbST2 = 0, NbVT2 = 0, NbTT2 = 0;
96 int NbSQ2 = 0, NbVQ2 = 0, NbTQ2 = 0, NbSS2 = 0, NbVS2 = 0, NbTS2 = 0;
97 int NbSH2 = 0, NbVH2 = 0, NbTH2 = 0, NbSI2 = 0, NbVI2 = 0, NbTI2 = 0;
98 int NbSY2 = 0, NbVY2 = 0, NbTY2 = 0;
99 std::vector<double> SL2, VL2, TL2, ST2, VT2, TT2;
100 std::vector<double> SQ2, VQ2, TQ2, SS2, VS2, TS2;
101 std::vector<double> SH2, VH2, TH2, SI2, VI2, TI2;
102 std::vector<double> SY2, VY2, TY2;
105 Msg::Debug(
"Detected post-processing view format <= 1.0");
106 if(fscanf(fp,
"%s %d %d %d %d %d %d %d %d %d %d %d %d %d\n", name,
114 else if(version == 1.1) {
115 Msg::Debug(
"Detected post-processing view format 1.1");
116 if(fscanf(fp,
"%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
124 else if(version == 1.2 || version == 1.3) {
125 Msg::Debug(
"Detected post-processing view format %g", version);
127 "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d "
128 "%d %d %d %d %d %d %d %d %d %d %d %d %d\n",
137 else if(version == 1.4) {
138 Msg::Debug(
"Detected post-processing view format 1.4");
140 "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d "
141 "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d "
142 "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
146 &NbSL2, &NbVL2, &NbTL2, &NbST2, &NbVT2, &NbTT2, &NbSQ2, &NbVQ2,
147 &NbTQ2, &NbSS2, &NbVS2, &NbTS2, &NbSH2, &NbVH2, &NbTH2, &NbSI2,
148 &NbVI2, &NbTI2, &NbSY2, &NbVY2, &NbTY2, &
NbT2, &t2l, &
NbT3,
155 Msg::Error(
"Unknown post-processing file format (version %g)", version);
159 for(
int i = 0; i < (int)strlen(name); i++)
160 if(name[i] ==
'^') name[i] =
' ';
165 if(!fread(&testone,
sizeof(
int), 1, fp)) {
170 Msg::Info(
"Swapping bytes from binary file");
314 "SP(%d/%d) VP(%d/%d) TP(%d/%d) "
315 "SL(%d/%d) VL(%d/%d) TL(%d/%d) "
316 "ST(%d/%d) VT(%d/%d) TT(%d/%d) "
317 "SQ(%d/%d) VQ(%d/%d) TQ(%d/%d) "
318 "SS(%d/%d) VS(%d/%d) TS(%d/%d) "
319 "SH(%d/%d) VH(%d/%d) TH(%d/%d) "
320 "SI(%d/%d) VI(%d/%d) TI(%d/%d) "
321 "SY(%d/%d) VY(%d/%d) TY(%d/%d) "
322 "T2(%d/%d/%d) T3(%d/%d/%d) ",
339 if(list.size() > 1) {
340 fprintf(fp,
"TIME{");
341 for(std::size_t i = 0; i < list.size(); i++) {
342 if(i) fprintf(fp,
",");
343 fprintf(fp,
"%.16g", list[i]);
350 std::vector<double> &list)
353 int n = list.size() / nb;
354 for(std::size_t i = 0; i < list.size(); i += n) {
355 double *x = &list[i];
356 double *y = &list[i + nbnod];
357 double *
z = &list[i + 2 * nbnod];
358 fprintf(fp,
"%s(", str);
359 for(
int j = 0; j < nbnod; j++) {
360 if(j) fprintf(fp,
",");
361 fprintf(fp,
"%.16g,%.16g,%.16g", x[j], y[j],
z[j]);
364 for(
int j = 3 * nbnod; j < n; j++) {
365 if(j - 3 * nbnod) fprintf(fp,
",");
366 fprintf(fp,
"%.16g", list[i + j]);
373 static void writeTextPOS(FILE *fp,
int nbc,
int nb, std::vector<double> &TD,
374 std::vector<char> &TC)
376 if(!nb || (nbc != 4 && nbc != 5))
return;
377 for(std::size_t j = 0; j < TD.size(); j += nbc) {
379 double y = TD[j + 1];
380 double z = (nbc == 5) ? TD[j + 2] : 0.;
381 double style = TD[j + nbc - 2];
383 fprintf(fp,
"T2(%g,%g,%g){", x, y, style);
385 fprintf(fp,
"T3(%g,%g,%g,%g){", x, y,
z, style);
386 double start = TD[j + nbc - 1];
388 if(j + nbc * 2 - 1 < TD.size())
389 end = TD[j + nbc * 2 - 1];
393 while(l < end - start) {
394 char *str = &TC[(int)start + l];
395 if(l) fprintf(fp,
",");
396 fprintf(fp,
"\"%s\"", str);
397 l += strlen(str) + 1;
404 bool parsed,
bool append)
408 "Writing adapted dataset (will only export current time step)");
414 "Cannot export datasets with interpolation matrices in old POS format: "
415 "consider using the new mesh-based format instead, or select 'Adapt "
416 "post-processing data' before exporting");
420 FILE *fp =
Fopen(fileName.c_str(),
421 append ? (binary ?
"ab" :
"a") : (binary ?
"wb" :
"w"));
423 Msg::Error(
"Unable to open file '%s'", fileName.c_str());
427 if(!parsed && !append) {
428 fprintf(fp,
"$PostFormat /* Gmsh 1.3, %s */\n",
429 binary ?
"binary" :
"ascii");
430 fprintf(fp,
"1.3 %d %d\n", binary, (
int)
sizeof(
double));
431 fprintf(fp,
"$EndPostFormat\n");
435 for(std::size_t i = 0; i < str.size(); i++)
436 if(str[i] ==
' ') str[i] =
'^';
439 fprintf(fp,
"$View /* %s */\n",
getName().c_str());
441 fprintf(fp,
"noname ");
443 fprintf(fp,
"%s ", str.c_str());
445 "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d "
446 "%d %d %d %d %d %d %d %d %d %d %d %d\n",
448 NbTT,
NbSQ,
NbVQ,
NbTQ,
NbSS,
NbVS,
NbTS,
NbSH,
NbVH,
NbTH,
NbSI,
453 if(!fwrite(&one,
sizeof(
int), 1, fp)) {
489 fprintf(fp,
"$EndView\n");
492 fprintf(fp,
"View \"%s\" {\n",
getName().c_str());
528 std::vector<MVertex *> &nodes)
531 int nb = list.size() / nbelm;
532 for(std::size_t i = 0; i < list.size(); i += nb) {
533 double *x = &list[i];
534 double *y = &list[i + nbnod];
535 double *
z = &list[i + 2 * nbnod];
536 for(
int j = 0; j < nbnod; j++)
537 nodes.push_back(
new MVertex(x[j], y[j],
z[j]));
555 int type, std::map<MVertex *, nodeData> *vertexData)
612 Msg::Warning(
"Discarding elements of type (%d nodes)", nbnod);
616 int nb = list.size() / nbelm;
617 for(std::size_t i = 0; i < list.size(); i += nb) {
618 double *x = &list[i];
619 double *y = &list[i + nbnod];
620 double *
z = &list[i + 2 * nbnod];
621 std::vector<MVertex *> verts(nbnod);
622 for(
int j = 0; j < nbnod; j++) {
623 verts[j] = pos.
find(x[j], y[j],
z[j]);
625 (*vertexData)[verts[j]] =
nodeData(nbnod, j, &list[i + 3 * nbnod]);
628 elements.push_back(e);
633 bool binary,
bool saveMesh,
bool multipleView,
634 int partitionNum,
bool saveInterpolationMatrices,
635 bool forceNodeData,
bool forceElementData)
639 "Writing adapted dataset (will only export current time step)");
643 FILE *fp =
Fopen(fileName.c_str(),
"w");
645 Msg::Error(
"Unable to open file '%s'", fileName.c_str());
652 std::vector<MVertex *> vertices;
653 std::vector<MElement *> elements;
655 int numComponents = 9;
656 for(
int i = 0; i < 24; i++) {
657 std::vector<double> *list =
nullptr;
658 int *numEle =
nullptr, numNodes, numComp;
659 _getRawData(i, &list, &numEle, &numComp, &numNodes);
660 if(*numEle) numComponents = std::min(numComponents, numComp);
664 std::vector<MVertex *> unique;
665 for(std::size_t i = 0; i < vertices.size(); i++) {
666 if(!pos.
insert(vertices[i])) unique.push_back(vertices[i]);
670 std::map<MVertex *, nodeData> vertexData;
672 for(
int i = 0; i < 24; i++) {
673 std::vector<double> *list =
nullptr;
674 int *numEle =
nullptr, numComp, numNodes;
675 int typ =
_getRawData(i, &list, &numEle, &numComp, &numNodes);
677 forceNodeData ? &vertexData :
nullptr);
681 for(std::size_t i = 0; i < unique.size(); i++) unique[i]->setIndex(++num);
684 Msg::Warning(
"Mesh-based export of list-based datasets not available with "
685 "MSH %g: using MSH 2.2",
688 fprintf(fp,
"$MeshFormat\n2.2 0 8\n$EndMeshFormat\n");
691 fprintf(fp,
"$Nodes\n");
692 fprintf(fp,
"%d\n", (
int)unique.size());
693 for(std::size_t i = 0; i < unique.size(); i++) {
695 fprintf(fp,
"%ld %.16g %.16g %.16g\n", v->
getIndex(), v->
x(), v->
y(),
698 fprintf(fp,
"$EndNodes\n");
700 fprintf(fp,
"$Elements\n");
701 fprintf(fp,
"%d\n", (
int)elements.size());
702 for(std::size_t i = 0; i < elements.size(); i++) {
703 elements[i]->writeMSH2(fp, 2.2,
false, i + 1);
705 fprintf(fp,
"$EndElements\n");
709 !forceNodeData && !forceElementData) {
710 fprintf(fp,
"$InterpolationScheme\n");
711 fprintf(fp,
"\"INTERPOLATION_SCHEME\"\n");
714 if(it->second.size() >= 2) {
715 fprintf(fp,
"%d\n2\n", it->first);
716 for(
int mat = 0; mat < 2; mat++) {
717 int m = it->second[mat]->size1(), n = it->second[mat]->size2();
718 fprintf(fp,
"%d %d\n", m, n);
719 for(
int i = 0; i < m; i++) {
720 for(
int j = 0; j < n; j++)
721 fprintf(fp,
"%.16g ", it->second[mat]->get(i, j));
727 fprintf(fp,
"$EndInterpolationScheme\n");
732 fprintf(fp,
"$NodeData\n");
733 else if(forceElementData)
734 fprintf(fp,
"$ElementData\n");
736 fprintf(fp,
"$ElementNodeData\n");
738 !forceNodeData && !forceElementData)
739 fprintf(fp,
"2\n\"%s\"\n\"INTERPOLATION_SCHEME\"\n",
getName().c_str());
741 fprintf(fp,
"1\n\"%s\"\n",
getName().c_str());
742 fprintf(fp,
"1\n%.16g\n",
getTime(ts));
743 int size = forceNodeData ? (int)unique.size() : (int)elements.size();
745 fprintf(fp,
"4\n%d\n%d\n%d\n%d\n", ts, numComponents, size, partitionNum);
747 fprintf(fp,
"3\n%d\n%d\n%d\n", ts, numComponents, size);
750 for(std::size_t i = 0; i < unique.size(); i++) {
753 int nbnod = vertexData[v].nbnod;
754 int nod = vertexData[v].nod;
755 double *d = vertexData[v].data;
756 for(
int j = 0; j < numComponents; j++)
757 fprintf(fp,
" %.16g",
758 d[numComponents * nbnod * ts + numComponents * nod + j]);
761 fprintf(fp,
"$EndNodeData\n");
765 for(
int i = 0; i < 24; i++) {
766 std::vector<double> *list =
nullptr;
767 int *numEle =
nullptr, numComp, numNodes;
768 int typ =
_getRawData(i, &list, &numEle, &numComp, &numNodes);
772 int nb = list->size() / *numEle;
773 for(std::size_t i = 0; i < list->size(); i += nb) {
774 double *v = &(*list)[i + 3 * numNodes];
775 if(forceElementData) {
776 fprintf(fp,
"%d", ++n);
777 for(
int j = 0; j < numComponents; j++)
778 fprintf(fp,
" %.16g", v[numComponents *
mult * ts + j]);
781 fprintf(fp,
"%d %d", ++n,
mult);
782 for(
int j = 0; j < numComponents *
mult; j++)
783 fprintf(fp,
" %.16g", v[numComponents *
mult * ts + j]);
790 fprintf(fp,
"$EndElementData\n");
792 fprintf(fp,
"$EndElementNodeData\n");
802 for(
int i = 0; i < 24; i++) {
803 std::vector<double> *list =
nullptr;
804 int *nbe =
nullptr, nbc, nbn;
815 if(index < 0 || index >= 24) {
819 std::vector<double> *list =
nullptr;
820 int *nbe =
nullptr, nbc, nbn;
829 for(
int i = 0; i < 24; i++) {
830 std::vector<double> *list =
nullptr;
831 int *nbe =
nullptr, nbc, nbn;