Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/utils/trixi2tec.jl
2055 views
1
using Trixi
2
3
"""
4
trixi2tec(u, semi, filename; title=basename(filename), solution_variables=cons2cons)
5
6
Convert a Trixi.jl solution array `u` for a given semidiscretization `semi` into a point-based
7
Tecplot ASCII file and store it as `filename`. Instead of manually extracting `u` and `semi`, you
8
can also just pass `sol` instead, i.e., the usual variable name used in Trixi.jl's elixirs for the
9
solution data.
10
11
You can optionally pass a `title` (by default the file portion of the filename is used). Further,
12
you can pass a different conversion function to `solution_variables` to write out variables in
13
non-conservative form.
14
15
### Example
16
```julia
17
julia> using Trixi
18
19
julia> trixi_include(default_example())
20
21
julia> trixi2tec(sol, "mydata.tec")
22
23
julia> trixi2tec(sol, "mydata_primitive.tec", solution_variables=cons2prim)
24
```
25
26
!!! warning "Experimental implementation"
27
This only works for the `TreeMesh`, the `StructuredMesh`, the `UnstructuredMesh2D`,
28
and the `P4estMesh`. In particular, it does not work for the `DGMulti` solver using the
29
`DGMultiMesh`.
30
31
!!! warning "Experimental implementation"
32
This is an experimental feature and *not* part of the official Trixi.jl API. Specifically,
33
this function may change (or even be removed) in future releases without warning.
34
"""
35
function trixi2tec(u, semi, filename; title = basename(filename),
36
solution_variables = cons2cons)
37
# Extract fundamental building blocks and auxiliary data
38
mesh, equations, solver, cache = Trixi.mesh_equations_solver_cache(semi)
39
@unpack node_coordinates = cache.elements
40
41
# Collect variable names and size information
42
ndims = Trixi.ndims(semi)
43
if ndims == 1
44
variables = ["x"]
45
ndofs_x = size(u, 2)
46
indices = CartesianIndices((ndofs_x,))
47
zone_info = "ZONE I=$ndofs_x, F=POINT\n"
48
elseif ndims == 2
49
variables = ["x", "y"]
50
ndofs_x = size(u, 2)
51
ndofs_y = size(u, 3)
52
indices = CartesianIndices((ndofs_x, ndofs_y))
53
zone_info = "ZONE I=$ndofs_x, J=$ndofs_y, F=POINT\n"
54
elseif ndims == 3
55
variables = ["x", "y", "z"]
56
ndofs_x = size(u, 2)
57
ndofs_y = size(u, 3)
58
ndofs_z = size(u, 4)
59
indices = CartesianIndices((ndofs_x, ndofs_y, ndofs_z))
60
zone_info = "ZONE I=$ndofs_x, J=$ndofs_y, K=$ndofs_z, F=POINT\n"
61
else
62
error("Unsupported number of dimensions (must be 1, 2, or 3)")
63
end
64
push!(variables, Trixi.varnames(solution_variables, equations)...)
65
variables_list = join(variables, "\", \"")
66
67
# Write tec file
68
open(filename, "w") do io
69
write(io, """TITLE = "$title"\n""")
70
write(io, """VARIABLES = "$variables_list"\n""")
71
for element in eachelement(solver, cache)
72
write(io, zone_info)
73
for ci in indices
74
node_coords = Trixi.get_node_coords(node_coordinates, equations, solver, ci,
75
element)
76
node_vars = solution_variables(Trixi.get_node_vars(u, equations, solver, ci,
77
element), equations)
78
print(io, join(node_coords, " "))
79
write(io, " ")
80
print(io, join(node_vars, " "))
81
write(io, "\n")
82
end # k, j, i
83
end # element
84
end
85
end
86
87
# Convenience function to allow calling `trixi2tec` with the `sol` variable
88
function trixi2tec(sol, filename; kwargs...)
89
semi = sol.prob.p
90
u_ode = sol.u[end]
91
trixi2tec(u_ode, semi, filename; kwargs...)
92
end
93
94
# Convenience function to allow calling `trixi2tec` with, e.g., the initial condition
95
function trixi2tec(u_ode::Vector{<:Real}, semi, filename; kwargs...)
96
u = Trixi.wrap_array_native(u_ode, semi)
97
trixi2tec(u, semi, filename; kwargs...)
98
end
99
100