-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomf2matlab.m
217 lines (197 loc) · 7.63 KB
/
omf2matlab.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
function [data]=omf2matlab(fileToRead)
% The function imports vector file archives from oommf [1], mumax3 [2]
% Boris [3] into Matlab arrays.
%
% The file is inspired by H. Corte and his function oommf2matlab.m
% https://mathworks.com/matlabcentral/fileexchange/44187-oommf-vector-data-file-to-matlab-array
% https://thebrickinthesky.wordpress.com/?s=OOMMF
% ChatGPT [4] was used also.
%
% The main modification: fileToRead is processed with fileread function,
% not line-by-line. It works much faster for large files (100+Mb)
%
% Oommf vector files must be writen with the output Specifications "text %g"
% instead of the default "binary 4" option.
% And the type of grid must be rectangular.
% In Boris use ns.saveovf2mag('text', output_file, bufferCommand=True)
% https://groups.google.com/g/boris-computational-spintronics/c/B4YLoLqqsvQ
%
% Vector files will be imported into the object "data" which will have this
% fields (if exist in fileToRead):
% datax: component x of vector on data file
% datay: component y of vector on data file
% dataz: component z of vector on data file
% time: total simulation time
% xmin: minimum x value
% xnodes: number of nodes used along x
% xmax: maximum x value
% ymin: minimum y value
% ynodes: number of nodes used along y
% ymax: maximum y value
% zmin: minimum z value
% znodes: number of nodes used along z
% zmax: maximum z value
% positionx: x positions of vectors
% positiony: y positions of vectors
% positionz: z positions of vectors
% The number of fields could be extended easily
%
% Example: Plot 2D images from all files in directory:
%
% %% read file names
% dir_name = './DirName/';
% file_extention = '*.ovf'; % .omf is also good
% f_n = dir([dir_name file_extention]);
% %% sort files by date
% [~,idx] = sort([f_n.datenum]);
% f_n = f_n(idx);
% fileNames = {f_n.name};
% %%
% mult = 1e6; % switch dimentions to microns
% for ff = 1:length(fileNames)
% dataOMF = oommf2matlab_fileread([dir_name fileNames{ff}]);
% Xvector = mult* linspace(dataOMF.xmin, dataOMF.xmax, dataOMF.xnodes);
% Yvector = mult* linspace(dataOMF.ymin, dataOMF.ymax, dataOMF.ynodes);
% Magn_z = dataOMF.dataz
% imagesc(Xvector, Yvector, Magn_z');
% drawnow;
% end;
%
% References:
% [1] OOMMF: Object Oriented MicroMagnetic Framework, NIST,
% http://math.nist.gov/oommf/
% [2] mumax3, a GPU-accelerated micromagnetic simulation
% https://mumax.github.io/
% [3] Boris Computational Spintronics,
% Multi-physics magnetisation dynamics and spin transport simulations,
% http://www.boris-spintronics.uk/
% [4] ChatGPT https://chat.openai.com/
%
% This function was written by N. Khokhlov
% https://www.scopus.com/authid/detail.uri?authorId=57213491467
% https://publons.com/researcher/2337219/nikolai-e-khokhlov/
% https://scholar.google.com/citations?user=YKEZQLIAAAAJ&hl=en
% https://www.researchgate.net/profile/N-Khokhlov
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%We open the data file and start reading lines. The first lines are the
%header with information about the simulation. On this version not all the
%information is stracted, but is quite easy to do
file_content = fileread(fileToRead);
%%%% get total simulation time %%%%
pattern = '# Desc: Total simulation time:\s*([-+]?[\d.]+(?:e[-+]?\d+)?)';
% expression \s*([-+]?[\d.]+(?:e[-+]?\d+)?) returns all numerical values in
% scientific format until the end of the line
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.time = str2double(matches{1}{1});
end
%%%% Build the grid %%%%
% xmin
pattern = '# xmin:\s*([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.xmin = str2double(matches{1}{1});
end
%ymin
pattern = '# ymin:\s*([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.ymin = str2double(matches{1}{1});
end
%zmin
pattern = '# zmin:\s*([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.zmin = str2double(matches{1}{1});
end
% xmax
pattern = '# xmax:\s*([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.xmax = str2double(matches{1}{1});
end
%ymax
pattern = '# ymax:\s([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.ymax = str2double(matches{1}{1});
end
%zmax
pattern = '# zmax:\s([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.zmax = str2double(matches{1}{1});
end
%xnodes
pattern = '# xnodes:\s([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.xnodes = str2double(matches{1}{1});
end
%ynodes
pattern = '# ynodes:\s([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.ynodes = str2double(matches{1}{1});
end
%znodes
pattern = '# znodes:\s([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.znodes = str2double(matches{1}{1});
end
%xstepsize
pattern = '# xstepsize:\s([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.xstepsize = str2double(matches{1}{1});
end
%ystepsize
pattern = '# ystepsize:\s([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.ystepsize = str2double(matches{1}{1});
end
%zstepsize
pattern = '# zstepsize:\s([-+]?[\d.]+(?:e[-+]?\d+)?)';
matches = regexp(file_content, pattern, 'tokens');
if ~isempty(matches)
data.zstepsize = str2double(matches{1}{1});
end
%We have already the data about the grid, size and number of nodes,
%thus we can create the arrays containing the coordinates of the points on the
%grid. It is necesary here that the simulation grid is rectangular.
x=linspace(data.xmin, data.xmax, data.xnodes);
y=linspace(data.ymin, data.ymax, data.ynodes);
z=linspace(data.zmin, data.zmax, data.znodes);
[X,Y,Z] = meshgrid(x,y,z);
data.datax =0.*permute(X,[2,1,3]);%Gives the proper size to datax
data.datay =0.*permute(Y,[2,1,3]);%Gives the proper size to datay
data.dataz =0.*permute(Z,[2,1,3]);%Gives the proper size to dataz
data.positionx =permute(X,[2,1,3]);
data.positiony =permute(Y,[2,1,3]);
data.positionz =permute(Z,[2,1,3]);
%%%% bulding 2d arrays of components x,y,z %%%%
% Search the data between "# Begin: data text" and "# End: data text"
start_pattern = '# Begin: Data Text';
end_pattern = '# End: Data Text';
start_idx = regexp(file_content, start_pattern, 'end');
end_idx = regexp(file_content, end_pattern, 'start');
if isempty(start_idx) && isempty(end_idx)
start_pattern = '# Begin: data text';
end_pattern = '# End: data text';
start_idx = regexp(file_content, start_pattern, 'end');
end_idx = regexp(file_content, end_pattern, 'start');
end
if ~isempty(start_idx) && ~isempty(end_idx)
% get the data between the strings
data_section = file_content(start_idx+1:end_idx-1);
s=textscan(data_section,'%f \t%f \t%f');
S=cell2mat(s);
data.datax(:)=S(:,1);
data.datay(:)=S(:,2);
data.dataz(:)=S(:,3);
end
end