summaryrefslogtreecommitdiff
path: root/lib/core/c++/write_vtk.hpp
blob: 9d190159f319c7bc486111b952715a319a161620 (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#pragma once

#include <forstio/error.hpp>

#include <forstio/codec/data.hpp>
#include <forstio/codec/data_math.hpp>

#include "descriptor.hpp"
#include "flatten.hpp"

#include <fstream>
#include <filesystem>

namespace kel {
namespace lbm {
namespace impl {

template<typename CellFieldSchema>
struct lbm_vtk_writer {
};

template<typename T, uint64_t D>
struct lbm_vtk_writer<sch::Primitive<T,D>> {
	static saw::error_or<void> apply_header(std::ostream& vtk_file, std::string_view name){
		vtk_file<<"SCALARS "<<name<<" float\n";
		vtk_file<<"LOOKUP_TABLE default\n";
		return saw::make_void();
	}
	static saw::error_or<void> apply(std::ostream& vtk_file, const saw::data<sch::Primitive<T,D>>& field){
		vtk_file<<field.get()<<"\n";
		return saw::make_void();
	}
};

template<typename T, uint64_t D>
struct lbm_vtk_writer<sch::FixedArray<T,D>> {
	static saw::error_or<void> apply_header(std::ostream& vtk_file, std::string_view name){
		vtk_file<<"VECTORS "<<name<<" float\n";
		return saw::make_void();
	}

	static saw::error_or<void> apply(std::ostream& vtk_file, const saw::data<sch::FixedArray<T,D>>& field){
		saw::data<sch::FixedArray<sch::UInt64,D>> index;
		for(saw::data<sch::UInt64> it{0}; it.get() < D; ++it){
			index.at({0u}).set(0u);
		}

		// vtk_file<<"VECTORS "<<name<<" float\n";
		for(uint64_t i = 0u; i < D; ++i){
			if(i > 0){
				vtk_file<<" ";
			}
			vtk_file<<field.at({i}).get();
		}
		for(uint64_t i = D; i < 3; ++i){
			vtk_file<<" 0";
		}
		vtk_file<<"\n";
		return saw::make_void();
	}
};

template<typename Sch>
struct lbm_vtk_struct_field_writer;

template<typename T, uint64_t... D>
struct lbm_vtk_struct_field_writer<sch::FixedArray<T,D...>> {
	template<uint64_t d>
	static saw::error_or<void> apply_d(std::ostream& vtk_file, const saw::data<sch::FixedArray<T,D...>>& field, saw::data<sch::FixedArray<sch::UInt64,sizeof...(D)>>& index){

		if constexpr ( d < sizeof...(D)){
			for(index.at({d}) = 0u; index.at({d}) < field.get_dims().at({d}); ++index.at({d})){
				auto eov = apply_d<d+1u>(vtk_file, field, index);
			}
		}else{
			auto eov = lbm_vtk_writer<T>::apply(vtk_file, field.at(index));
			if(eov.is_error()) return eov;


		}
		return saw::make_void();
	}

	static saw::error_or<void> apply(std::ostream& vtk_file, const saw::data<sch::FixedArray<T,D...>>& field, std::string_view name){
		{
			auto eov = lbm_vtk_writer<T>::apply_header(vtk_file,name);
			if(eov.is_error()){
				return eov;
			}
		}
		saw::data<sch::FixedArray<sch::UInt64,sizeof...(D)>> index;
		for(saw::data<sch::UInt64> it{0}; it.get() < sizeof...(D); ++it){
			index.at({0u}).set(0u);
		}

		{
			auto eov = apply_d<0u>(vtk_file, field, index);
			if(eov.is_error()){
				return eov;
			}
		}

		return saw::make_void();
	}
};

template<typename T, uint64_t D>
struct lbm_vtk_writer<sch::Vector<T,D>> {
	static saw::error_or<void> apply_header(std::ostream& vtk_file, std::string_view name){
		vtk_file<<"VECTORS "<<name<<" float\n";
		return saw::make_void();
	}
	static saw::error_or<void> apply(std::ostream& vtk_file, const saw::data<sch::Vector<T,D>>& field){
		static_assert(D > 0, "Non-dimensionality is bad for velocity.");
		static_assert(D <= 3, "4th dimension as well. Mostly due to vtk.");

		// vtk_file<<"VECTORS "<<name<<" float\n";
		for(uint64_t i = 0u; i < D; ++i){
			if(i > 0){
				vtk_file<<" ";
			}
			vtk_file<<field.at({{i}}).get();
		}
		for(uint64_t i = D; i < 3; ++i){
			vtk_file<<" 0";
		}
		vtk_file<<"\n";
		return saw::make_void();
	}
};

template<typename... MemberT, saw::string_literal... Keys, uint64_t... Dims>
struct lbm_vtk_writer<sch::Struct<sch::Member<sch::FixedArray<MemberT,Dims...>,Keys>...>> final  {
	template<uint64_t i>
	static saw::error_or<void> iterate_i(std::ostream& vtk_file,
		const saw::data<sch::Struct<sch::Member<StructT,Keys>...>>& field){

		if constexpr ( i < sizeof...(MemberT) ) {
			using MT = typename saw::paramter_pack_type<i,sch::Member<StrucT,Keys>...>::type;
			{
				auto eov = lbm_vtk_struct_field_writer<typename MT::Type>::apply(vtk_file,field.template get<MT::KeyLiteral>(), MT::KeyLiteral.view());
				if(eov.is_error()){
					return eov;
				}
			}

			return iterate_i<i+1u>(vtk_file, field);
		}

		return saw::make_void();
	}


	static saw::error_or<void> apply(std::ostream& vtk_file,
		const saw::data<sch::Array<sch::Struct<sch::Member<StructT,StructN>...>, Dim>>& field){
		
		vtk_file
			<<"# vtk DataFile Version 3.0\n"
			<<"LBM File\n"
			<<"ASCII\n"
			<<"DATASET STRUCTURED_POINTS\n"
			<<"SPACING 1.0 1.0 1.0\n"
			<<"ORIGIN 0.0 0.0 0.0\n"
		;
		
		auto meta = field.get_dims();
		saw::data<sch::UInt64> pd_size{1u};
		// DIMENSIONS

		{
			vtk_file << "DIMENSIONS";
			pd_size.set(saw::ct_multiply<uint64_t,Dims...>::value);
			
			static_assert(saw::ct_multiply<uint64_t,Dims...>::value > 0u, "Invalid Dim size resulting in length 0u");

			for(saw::data<sch::UInt64> i{0u}; i.get() < sizeof...(Dims); ++i){
				pd_size = pd_size * meta.at(i);
				vtk_file << " " << meta.at(i).get(); 
			}
			for(saw::data<sch::UInt64> i{Dim}; i.get() < 3u; ++i){
				vtk_file << " 1";
			}

			vtk_file << "\n";
		}
		if constexpr (sizeof...(MemberT) > 0u){
			// POINT DATA
			{
				vtk_file << "POINT_DATA " << pd_size.get() <<"\n";
			}

			// HEADER TO BODY
			{
				vtk_file << "\n";
			}
		}
		
		return iterate_i<0u>(vtk_file, field);
	}
};

template<uint64_t Dim, typename... StructT, saw::string_literal... StructN>
struct lbm_vtk_writer<sch::Array<sch::Struct<sch::Member<StructT,StructN>...> , Dim>> {

	template<uint64_t i, uint64_t Dep>
	static saw::error_or<void> write_i_iterate_d(std::ostream& vtk_file, const saw::data<sch::Array<sch::Struct<sch::Member<StructT,StructN>...>,Dim>>& field, saw::data<sch::FixedArray<sch::UInt64,Dim>>& index){
		constexpr auto Lit = saw::parameter_key_pack_type<i, StructN...>::literal;
		using Type = typename saw::parameter_pack_type<i,StructT...>::type;

		if constexpr (Dep == 0u){
			return lbm_vtk_writer<Type>::apply(vtk_file, field.at(index).template get<Lit>());
		} else {
			// Dep < Dim // I hope
			static_assert(Dep > 0u, "Don't fall into this case");
			for(index.at({Dep-1u}) = 0; index.at({Dep-1u}) < field.get_dims().at({Dep-1u}); ++index.at({Dep-1u})){
				auto eov = write_i_iterate_d<i,Dep-1u>(vtk_file, field, index);
				if(eov.is_error()){
					return eov;
				}
			}
		}
		return saw::make_void();
	}

	template<uint64_t i>
	static saw::error_or<void> write_i(std::ostream& vtk_file, const saw::data<sch::Array<sch::Struct<sch::Member<StructT,StructN>...>,Dim>>& field){

		// auto meta = field.get_dims();
		saw::data<sch::FixedArray<sch::UInt64,Dim>> index;
		for(saw::data<sch::UInt64> it{0}; it.get() < Dim; ++it){
			index.at({0u}).set(0u);
		}
		// vtk_file write?
		// Data header
		{

			auto eov = lbm_vtk_writer<typename saw::parameter_pack_type<i,StructT...>::type>::apply_header(vtk_file, saw::parameter_key_pack_type<i, StructN...>::literal.view());
			if(eov.is_error()){
				return eov;
			}

		}
		return write_i_iterate_d<i,Dim>(vtk_file, field, index);
	}

	template<uint64_t i>
	static saw::error_or<void> iterate_i(std::ostream& vtk_file, const saw::data<sch::Array<sch::Struct<sch::Member<StructT,StructN>...>, Dim>>& field){
		// constexpr auto Lit = saw::parameter_key_pack_type<i, StructN...>::literal;
		{
			auto eov = write_i<i>(vtk_file, field);
			if(eov.is_error()){
				return eov;
			}
		}
		if constexpr ( (i+1u) < sizeof...(StructT) ){
			return iterate_i<i+1u>(vtk_file, field);
		}
		return saw::make_void();
	}

	static saw::error_or<void> apply(std::ostream& vtk_file,
		const saw::data<sch::Array<sch::Struct<sch::Member<StructT,StructN>...>, Dim>>& field){

		vtk_file
			<<"# vtk DataFile Version 3.0\n"
			<<"LBM File\n"
			<<"ASCII\n"
			<<"DATASET STRUCTURED_POINTS\n"
			<<"SPACING 1.0 1.0 1.0\n"
			<<"ORIGIN 0.0 0.0 0.0\n"
		;
		
		auto meta = field.get_dims();
		saw::data<sch::UInt64> pd_size{1u};
		// DIMENSIONS

		{
			vtk_file << "DIMENSIONS";
			for(saw::data<sch::UInt64> i{0u}; i.get() < Dim; ++i){
				pd_size = pd_size * meta.at(i);
				vtk_file << " " << meta.at(i).get(); 
			}
			for(saw::data<sch::UInt64> i{Dim}; i.get() < 3u; ++i){
				vtk_file << " 1";
			}

			vtk_file << "\n";
		}

		if constexpr (sizeof...(StructT) > 0u){
			// POINT DATA
			{
				vtk_file << "POINT_DATA " << pd_size.get() <<"\n";
			}

			// HEADER TO BODY
			{
				vtk_file << "\n";
			}

			return iterate_i<0u>(vtk_file, field);
		}

		return saw::make_void();
	}
};

template<typename StructT, uint64_t Dim>
struct lbm_vtk_writer_raw {
	template<uint64_t i, uint64_t Dep>
	static saw::error_or<void> write_i_iterate_d(std::ostream& vtk_file, const saw::data<StructT>* field, const saw::data<sch::FixedArray<sch::UInt64,Dim>>& meta, saw::data<sch::FixedArray<sch::UInt64,Dim>>& index){
		constexpr auto Lit = saw::schema_type_at_index<i, StructT>::Type::KeyLiteral;
		using Type = typename saw::schema_type_at_index<i, StructT>::Type::ValueType;

		if constexpr (Dep == 0u){
			auto flat_index = flatten_index<sch::UInt64,Dim>::apply(index, meta);
			return lbm_vtk_writer<Type>::apply(vtk_file, field[flat_index.get()].template get<Lit>());
		} else {
			// Dep < Dim // I hope
			static_assert(Dep > 0u, "Don't fall into this case");
			for(index.at({Dep-1u}) = 0; index.at({Dep-1u}) < meta.at({Dep-1u}); ++index.at({Dep-1u})){
				auto eov = write_i_iterate_d<i,Dep-1u>(vtk_file, field, meta, index);
				if(eov.is_error()){
					return eov;
				}
			}
		}
		return saw::make_void();
	}

	template<uint64_t i>
	static saw::error_or<void> write_i(std::ostream& vtk_file, const saw::data<StructT>* field, const saw::data<sch::FixedArray<sch::UInt64,Dim>>& meta){
		constexpr auto Lit = saw::schema_type_at_index<i, StructT>::Type::KeyLiteral;

		// auto meta = field.get_dims();
		saw::data<sch::FixedArray<sch::UInt64,Dim>> index;
		for(saw::data<sch::UInt64> it{0}; it.get() < Dim; ++it){
			index.at({0u}).set(0u);
		}
		// vtk_file write?
		// Data header
		{

			auto eov = lbm_vtk_writer<typename saw::schema_type_at_index<i,StructT>::Type::ValueType>::apply_header(vtk_file, Lit.view());
			if(eov.is_error()){
				return eov;
			}

		}
		return write_i_iterate_d<i,Dim>(vtk_file, field, meta, index);
	}
	template<uint64_t i>
	static saw::error_or<void> iterate_i(std::ostream& vtk_file, const saw::data<StructT>* field,
			const saw::data<sch::FixedArray<sch::UInt64,Dim>>& meta){
		{
			auto eov = write_i<i>(vtk_file, field, meta);
			if(eov.is_error()){
				return eov;
			}
		}
		if constexpr ( (i+1u) < saw::schema_width<StructT>::value ){
			return iterate_i<i+1u>(vtk_file, field, meta);
		}
		return saw::make_void();
	}

	static saw::error_or<void> apply(std::ostream& vtk_file, const saw::data<StructT>* field,
			const saw::data<sch::FixedArray<sch::UInt64,Dim>>& meta){

		vtk_file
			<<"# vtk DataFile Version 3.0\n"
			<<"LBM File\n"
			<<"ASCII\n"
			<<"DATASET STRUCTURED_POINTS\n"
			<<"SPACING 1.0 1.0 1.0\n"
			<<"ORIGIN 0.0 0.0 0.0\n"
		;
		
		saw::data<sch::UInt64> pd_size{1u};
		// DIMENSIONS

		{
			vtk_file << "DIMENSIONS";
			for(saw::data<sch::UInt64> i{0u}; i.get() < Dim; ++i){
				pd_size = pd_size * meta.at(i);
				vtk_file << " " << meta.at(i).get(); 
			}
			for(saw::data<sch::UInt64> i{Dim}; i.get() < 3u; ++i){
				vtk_file << " 1";
			}

			vtk_file << "\n";
		}

		if constexpr (saw::schema_width<StructT>::value > 0u){
			// POINT DATA
			{
				vtk_file << "POINT_DATA " << pd_size.get() <<"\n";
			}

			// HEADER TO BODY
			{
				vtk_file << "\n";
			}

			return iterate_i<0u>(vtk_file, field,meta);
		}

		return saw::make_void();
	}
};
}

template<typename Sch>
saw::error_or<void> write_vtk_file(const std::filesystem::path& file_name, const saw::data<Sch>& field){

	std::ofstream vtk_file{file_name};

	if( not vtk_file.is_open() ){
		return saw::make_error<saw::err::critical>("Could not open file.");
	}

	auto eov = impl::lbm_vtk_writer<Sch>::apply(vtk_file, field);
	return eov;
}

template<typename StructInRaw, uint64_t D>
saw::error_or<void> write_vtk_file(
		const std::filesystem::path& file_name,
		const saw::data<StructInRaw>* field, 
		const saw::data<sch::FixedArray<sch::UInt64,D>>& meta
	){
	std::ofstream vtk_file{file_name};

	if( not vtk_file.is_open() ){
		return saw::make_error<saw::err::critical>("Could not open file.");
	}

	auto eov = impl::lbm_vtk_writer_raw<StructInRaw,D>::apply(vtk_file, field, meta);
	return eov;
}
}
}