-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmatlab2ensightgolddeformingmesh.m
313 lines (274 loc) · 12.2 KB
/
matlab2ensightgolddeformingmesh.m
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
%Exports a mesh to Ensight Gold format
function matlab2ensightgolddeformingmesh(pathname,filename,filesuffix,x,y,z,timesteps)
global NO
% [rotatedx, rotatedy] = rotateendorings(x,y);
% x(:,:,:) = rotatedx;
% y(:,:,:) = rotatedy;
%Fix timeframes without segmentation issues:
%----------------------------------------------------------------------
isnanendo=isnan(sum(x(:,:,1),1));
tfvector=(1:size(x,2));
isnotnanendo_indice = tfvector(~isnanendo);
isnanendo_indice = tfvector(isnanendo)';
if isempty(isnotnanendo_indice)
mymsgbox('Error: No segmentation detected. Export aborted.');
return;
end
if ~isempty(isnanendo_indice)
%Copy closest segmentation to timeframes missing segmentation:
[~, closestendo] = min(abs(bsxfun(@minus,isnanendo_indice,isnotnanendo_indice)),[],2);
x(:,isnanendo_indice,:) = x(:,isnotnanendo_indice(closestendo),:);
y(:,isnanendo_indice,:) = y(:,isnotnanendo_indice(closestendo),:);
end
%if singleslice: duplicate it.
if ((numel(z)==1) && (size(x,3)==1) && (size(y,3)==1))
z=[z,1];
x = x(:,:,ones(2,1));
y = y(:,:,ones(2,1));
end
%----------------------------------------------------------------------
numsteps = length(timesteps); %Number of time steps
% ---- Create Case file
%Create and open the file
disp('Creating case file.');
casefilefid = fopen([pathname filesep filename filesuffix '.case'],'w');
if isequal(casefilefid,-1)
error(sprintf('Could not open %s for output.',[pathname filesep filename '.case']));
end;
%Write FORMAT
fprintf(casefilefid,'FORMAT\n\n');
fprintf(casefilefid,'type:\tensight gold\n\n');
%Write GEOMETRY section
if numsteps>1
wildcard = '****';
else
wildcard = '';
end;
fprintf(casefilefid,'GEOMETRY\n\n');
fprintf(casefilefid,'model:\t\t1\t%s%s.geo%s\n\n', filename, filesuffix, wildcard);
%Write TIME section
fprintf(casefilefid,'TIME\n');
fprintf(casefilefid,'time set:\t\t1\tModel\n'); %default 1
fprintf(casefilefid,'number of steps:\t%d\n',numsteps);
fprintf(casefilefid,'filename start number:\t1\n'); %default 1
fprintf(casefilefid,'filename increment:\t1\n'); %default 1
fprintf(casefilefid,'time values:\t%12.5e ',timesteps(1));
if numsteps>1
for loop=2:numsteps
fprintf(casefilefid,'%12.5e ',timesteps(loop)); %default 1
if mod(loop,4)==0
fprintf(casefilefid,'\n');
end;
end;
fprintf(casefilefid,'\n');
end;
%Close file
fclose(casefilefid);
%%% ---- Create geometry file
for timeframe = 1:numsteps
newname = sprintf('%s%s%s%s.geo%04d',pathname,filesep,filename,filesuffix,timeframe);
geofilefid = fopen(newname,'w'); % geofilefid = fopen(newname,'w');
if isequal(geofilefid,-1)
error(sprintf('Could not open %s for output.',newname));
end;
% Print Header
fprintf(geofilefid, '%s\n', '3D and 2D parts');
fprintf(geofilefid, '%s\n', sprintf('Exported by %s', mfilename));
fprintf(geofilefid, '%s\n', 'node id given');
fprintf(geofilefid, '%s\n', 'element id given');
%Start with the part
fprintf(geofilefid, '%s\n', 'part');
%Write part number
fprintf(geofilefid, ' 1\n');
%Write description
fprintf(geofilefid,'description for part1\n');
fprintf(geofilefid,'coordinates\n');
%Write number of nodes
nodes = 0;
for nodecoordinates = 1:size(x, 3)
if not(isnan(x(1, timeframe, nodecoordinates)))
nodes = nodes+size(x, 1);
end;
end;
fprintf(geofilefid,'%10d\n', nodes);
% write node ids and set up lookup structure
nodeindexlookup = java.util.Hashtable;
nodeid = 1;
for currentcolumn = 1:size(x, 3)
nodeindexlookup.put(currentcolumn, java.util.Hashtable);
for currentrow = 1:size(x, 1)
if not(isnan(x(currentrow, timeframe, currentcolumn)))
nodeindexlookup.get(currentcolumn).put(currentrow, nodeid);
fprintf(geofilefid,'%10d\n', nodeid);
nodeid=nodeid+1;
end;
end;
end;
% write x node coordinates
for currentcolumn = 1:size(x, 3)
for currentrow = 1:size(x, 1)
if not(isnan(x(currentrow, timeframe, currentcolumn)))
coords = calcfunctions('xyz2rlapfh', NO, x(currentrow, timeframe, currentcolumn), y(currentrow, timeframe, currentcolumn), z(currentcolumn));
expstring = getcrossplatformexponential(coords(1)/1000);
fprintf(geofilefid,'%s\n', expstring);
end;
end;
end;
% write y node coordinates
for currentcolumn = 1:size(x, 3)
for currentrow = 1:size(x, 1)
if not(isnan(x(currentrow, timeframe, currentcolumn)))
coords = calcfunctions('xyz2rlapfh', NO,x(currentrow, timeframe, currentcolumn), y(currentrow, timeframe, currentcolumn), z(currentcolumn));
expstring = getcrossplatformexponential(coords(2)/1000);
fprintf(geofilefid,'%s\n', expstring);
end;
end;
end;
% write z node coordinates
for currentcolumn = 1:size(x, 3)
for currentrow = 1:size(x, 1)
if not(isnan(x(currentrow, timeframe, currentcolumn)))
coords = calcfunctions('xyz2rlapfh', NO,x(currentrow, timeframe, currentcolumn), y(currentrow, timeframe, currentcolumn), z(currentcolumn));
expstring = getcrossplatformexponential(coords(3)/1000);
fprintf(geofilefid,'%s\n', expstring);
end;
end;
end;
% write number of quads
% |NaN|*|*|NaN|
% |NaN|*|*|NaN| <------- example with 5 quads
% |NaN|*|*|NaN| every node that has a node on the
% |NaN|*|*|NaN| next column and one on the next row
% |NaN|*|*|NaN| is a quad
% |NaN|*|*|NaN|
quads = 0;
for currentcolumn = 1:size(x, 3)-1 % go through all columns untill the second to last one
for currentrow = 1:size(x, 1)-1
nextrow = not(isnan(x(currentrow+1, timeframe, currentcolumn)));
nextcolumn = not(isnan(x(currentrow, timeframe, currentcolumn+1)));
if nextrow && nextcolumn
quads=quads+1;
end;
end;
% count the quad between the beginning and end as well
if nextrow && nextcolumn
quads=quads+1;
end;
end;
fprintf(geofilefid,'quad4\n');
fprintf(geofilefid,'%10d\n', quads);
for quadid=1:quads
fprintf(geofilefid,'%10d\n', quadid);
end;
% write the quad topology
for currentcolumn = 1:size(x, 3)-1 % go through all columns untill the second to last one
for currentrow = 1:size(x, 1)-1
nextrow = not(isnan(x(currentrow+1, timeframe, currentcolumn)));
nextcolumn = not(isnan(x(currentrow, timeframe, currentcolumn+1)));
if nextrow && nextcolumn
fprintf(geofilefid,'%10d', nodeindexlookup.get(currentcolumn).get(currentrow));
fprintf(geofilefid,'%10d', nodeindexlookup.get(currentcolumn+1).get(currentrow));
fprintf(geofilefid,'%10d', nodeindexlookup.get(currentcolumn+1).get(currentrow+1));
fprintf(geofilefid,'%10d\n', nodeindexlookup.get(currentcolumn).get(currentrow+1));
end;
end;
% add the quad between the last and first row as well
if nextrow && nextcolumn
fprintf(geofilefid,'%10d', nodeindexlookup.get(currentcolumn).get(size(x, 1)));
fprintf(geofilefid,'%10d', nodeindexlookup.get(currentcolumn+1).get(size(x, 1)));
fprintf(geofilefid,'%10d', nodeindexlookup.get(currentcolumn+1).get(1));
fprintf(geofilefid,'%10d\n', nodeindexlookup.get(currentcolumn).get(1));
end;
end;
% Close geo file
fclose(geofilefid);
end;
function returnedExponentialString = getcrossplatformexponential(number)
if(number >= 0)
returnedExponentialString = sprintf('%12.5e', number);
else
returnedExponentialString = sprintf('%12.4e', number);
end;
%if ispc
% returnedExponentialString = strrep(returnedExponentialString, 'e+0','e+');
% returnedExponentialString = strrep(returnedExponentialString, 'e-0','e-');
%end;
function [rotatedx, rotatedy] = rotatering(referencex, referencey, xring, yring)
rotatedx = zeros(size(xring));
rotatedy = zeros(size(yring));
smallestdistance = sqrt((referencex-xring(1))^2+(referencey-yring(1))^2);
smallestposition = 1;
for distanceloopcounter = 2:size(xring)
newdistance = sqrt((referencex-xring(distanceloopcounter))^2+(referencey-yring(distanceloopcounter))^2);
if(newdistance < smallestdistance)
smallestdistance = newdistance;
smallestposition = distanceloopcounter;
end;
end;
writtenelements = 1;
for i = smallestposition:size(xring)
rotatedx(writtenelements) = xring(i);
rotatedy(writtenelements) = yring(i);
writtenelements = writtenelements+1;
end;
if smallestposition ~= 1
for i = 1:(smallestposition-1)
rotatedx(writtenelements) = xring(i);
rotatedy(writtenelements) = yring(i);
writtenelements = writtenelements+1;
end;
end;
function [outdatax, outdatay] = rotateendorings(indatax, indatay)
outdatax = zeros(size(indatax));
outdatax(:,:,:) = NaN;
outdatay = zeros(size(indatay));
outdatay(:,:,:) = NaN;
% Go through the entire input dataset and rotate the endo rings so
% they are all oriented the same
for currenttime = 1:size(indatax, 2)
% Find the first column that is not full of NaNs.
% The first element of this column will be our reference position
for referencecolumn = 1:size(indatax, 3)
if not(isnan(indatax(1, currenttime, referencecolumn)))
referencex = indatax(1, currenttime, referencecolumn);
referencey = indatay(1, currenttime, referencecolumn);
break;
end;
end;
% Rotate the next column ring after first column and then the next after that
for currentcolumn = (referencecolumn+1):size(indatax, 3)
if not(isnan(indatax(1, currenttime, currentcolumn)))
[rotatedringx, rotatedringy] = rotatering(referencex, referencey, indatax(:,currenttime, currentcolumn), indatay(:,currenttime, currentcolumn));
outdatax(:,currenttime, currentcolumn) = rotatedringx;
outdatay(:,currenttime, currentcolumn) = rotatedringy;
referencex = outdatax(1, currenttime, currentcolumn);
referencey = outdatay(1, currenttime, currentcolumn);
end;
end;
end;
function [outdatax, outdatay] = rotateendorings2(indatax, indatay)
outdatax = nan(size(indatax));
outdatay = nan(size(indatay));
% Go through the entire input dataset and rotate the endo rings so
% they are all oriented the same
for currenttime = 1:size(indatax, 2)
% Find the first column that is not full of NaNs.
% The first element of this column will be our reference position
for referencecolumn = 1:size(indatax, 3)
if not(isnan(indatax(1, currenttime, referencecolumn)))
referencex = indatax(1, currenttime, referencecolumn);
referencey = indatay(1, currenttime, referencecolumn);
break;
end;
end;
% Rotate the next column ring after first column and then the next after that
for currentcolumn = (referencecolumn+1):size(indatax, 3)
if not(isnan(indatax(1, currenttime, currentcolumn)))
[rotatedringx, rotatedringy] = rotatering(referencex, referencey, indatax(:,currenttime, currentcolumn), indatay(:,currenttime, currentcolumn));
outdatax(:,currenttime, currentcolumn) = rotatedringx;
outdatay(:,currenttime, currentcolumn) = rotatedringy;
referencex = outdatax(1, currenttime, currentcolumn);
referencey = outdatay(1, currenttime, currentcolumn);
end;
end;
end;