CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

[Question]Mesh generating code

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 7, 2023, 14:03
Default [Question]Mesh generating code
  #1
New Member
 
Yuji
Join Date: May 2023
Posts: 1
Rep Power: 0
Yuji is on a distinguished road
I am making a code which generates simple mesh with C++.
I can generate the simple mesh with ASCII but cannot do it with Binary.
Can you teach me how to get Binary data?
I attach a figure of mesh data of ASCII with paraview and code.
Best,
Yuji



#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int main() {
int jmax, kmax, lmax, idomein; // nufile_binaryer of grid points in xi, eta and zeta direction
double xl, yl, zl; // horizontal, vertical, depth computational dommain
double dnx, dny, dnz, dx, dy, dz; // 1/dnx = dx (grid spacing)
double unit = 1.0;
int index;
int ibinary = 0;

// boundary nufile_binaryer
int bcfluid = 0, bcnonslip = 1, bcmovingwall = 2, bcinflow = 3, bcoutflow = 4, bcdammy = -1;

// parameters set
// jmax = 201;
// kmax = 101;
// lmax = 1;
xl = 3.0;
yl = 1.0;
zl = 0.0;
dnx = 50.0;
dny = 100.0;
dnz = 1.0;
dx = unit / dnx;
dy = unit / dny;
dz = unit / dnz;
printf("Grid Space: dx = %lf, dy = %lf, dz = %lf", dx, dy, dz);
jmax = dnx * xl + 1;
kmax = dny * yl + 1;
lmax = dnz * zl + 1;

idomein = jmax * kmax * lmax;
vector<double> x(idomein, 0.0);
vector<double> y(idomein, 0.0);
vector<double> z(idomein, 0.0);
vector<int> bc(idomein, 10);
printf("Nufile_binaryer of Grid Poits: jmax = %d, kmax = %d, lmax = %d, total = %d", jmax, kmax, lmax, idomein);

for (int l = 0, index = 0; l <= lmax - 1; l++) {
for (int k = 0; k <= kmax - 1; k++) {
for (int j = 0; j <= jmax - 1; j++, index++) {
x[index] = dx * double(j);
y[index] = dy * double(k);
z[index] = dz * double(l);
// boundary condition and fluid region
if ((l == 0 && j == 0) || (l == 0 && j == jmax - 1) || (l == 0 && k == 0) || (l == 0 && k == kmax - 1)) {
bc[index] = bcdammy;
} else if (l == 0 && j == 1) {
bc[index] = bcinflow;
} else if ((l == 0 && j == jmax - 2) || (l == 0 && k == kmax - 2)) {
bc[index] = bcoutflow;
} else if (l == 0 && k == 1) {
bc[index] = bcnonslip;
} else {
bc[index] = bcfluid;
}
}
}
}

// if (ibinary == 0) {
ofstream file("grid.dat");
if (!file.is_open()) {
cerr << "failed to open grid.dat" << endl;
return -1;
}
// ascii
file << "TITLE = " mesh data "" << endl;
file << "VARIABLES = "X", "Y", "Z", "BC" " << endl;
file << "ZONE T = "GRID", I=" << jmax << ", J=" << kmax << ", K=" << lmax << ", F=POINT" << endl;
// for (int index = 0; index <= idomein; index++) {
for (int l = 0, index = 0; l <= lmax - 1; l++) {
for (int k = 0; k <= kmax - 1; k++) {
for (int j = 0; j <= jmax - 1; j++, index++) {
file << x[index] << " " << y[index] << " " << z[index] << " " << bc[index] << endl;
}
}
}
file.close();
//}
//} else {
// binary
ofstream file_binary("grid_binary.dat", ios::out | ios::binary);
if (!file_binary.is_open()) {
cerr << "failed to open grid.plt" << endl;
return -1;
}
file_binary << "TITLE = "mesh data"" << endl;
file_binary << "VARIABLES = "X", "Y", "Z", "BC" " << endl;
file_binary << "ZONE T = "GRID", I=" << jmax << ", J=" << kmax << ", K=" << lmax << ", F=POINT" << endl;
// file.write((const char*)&jmax, sizeof(int));
// file.write((const char*)&kmax, sizeof(int));
// file.write((const char*)&lmax, sizeof(int));
file_binary.write((const char*)x.data(), idomein * sizeof(double));
file_binary.write((const char*)y.data(), idomein * sizeof(double));
file_binary.write((const char*)z.data(), idomein * sizeof(double));
file_binary.write((const char*)bc.data(), idomein * sizeof(int));

file_binary.close();
// }
}
Attached Images
File Type: jpg Screenshot 2023-05-08 015443.jpg (94.4 KB, 12 views)
Yuji is offline   Reply With Quote

Reply

Tags
binary file, c++ code, mesh 3d, paraview 4.2


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make CFD code architechture flexible enough for future modifications? aerosayan Main CFD Forum 6 September 3, 2021 05:35
Fortran->Assembly : How to remove redundant non-vectorized code? aerosayan Main CFD Forum 6 November 20, 2020 06:37
UDF code for heat generating source valerhain Fluent UDF and Scheme Programming 13 November 18, 2015 15:24
The FOAM Documentation Project - SHUT-DOWN holger_marschall OpenFOAM 242 March 7, 2013 13:30
Small 3-D code Zdravko Stojanovic Main CFD Forum 2 July 19, 2010 11:11


All times are GMT -4. The time now is 04:32.