Tensor Network Theory Library  Beta release 1.2.1
A library of routines for performing TNT-based operations
 All Data Structures Functions Variables Groups Pages
tntNc2mat.m
1 %======================================================================
2 %> @ingroup matscripts
3 %> Converts a NetCDF initialisation or output file to a MATLAB
4 %> initialisation or output file.
5 %>
6 %> The converted file will be saved in the current directory.
7 %>
8 %> @param ncname Path to the .nc file to convert to MATLAB format
9 %> @param matoutput Name of the MATLAB output file (should end in .mat)
10 %>
11 %> @retval No return value, but saves a MATLAB file.
12 %======================================================================
13 
14 function tntNc2mat(ncname, matoutput)
15 %#ok<*NASGU>
16 %#ok<*ASGLU>
17 %#ok<*AGROW>
18 exten = '';
19 pref = pwd;
20 matoutput = [pref '/' matoutput];
21 
22 ncid = netcdf.open(ncname,'NOWRITE');
23 
24 globval = netcdf.getConstant('NC_GLOBAL');
25 % ----- Reading the system information ----- %
26 tntLibVersion = tntgetAtt(ncid,globval,'tntLibVersion');
27 tntLibType = tntgetAtt(ncid,globval,'tntLibType');
28 gid = netcdf.inqNcid(ncid,'system');
29 tntSystem_nc = struct;
30 tntSystem_nc.sysnum = tntgetAtt(gid,globval,'sysnum');
31 tntSystem_nc.symm_type = tntgetAtt(gid,globval,'symm_type');
32 tntSystem_nc.symm_num_qn = tntgetAtt(gid,globval,'symm_num_qn');
33 tntSystem_nc.zero_tol = tntgetAtt(gid,globval,'zero_tol');
34 tntSystem_nc.abs_trunc_tol = tntgetAtt(gid,globval,'abs_trunc_tol');
35 tntSystem_nc.rel_trunc_tol = tntgetAtt(gid,globval,'rel_trunc_tol');
36 tntSystem_nc.trunc_err_tol = tntgetAtt(gid,globval,'trunc_err_tol');
37 tntSystem_nc.svdtype = tntgetAtt(gid,globval,'svdtype');
38 tntSystem_nc.maxeigiter = tntgetAtt(gid,globval,'maxeigiter');
39 tntSystem_nc.reshape_reuse = tntgetAtt(gid,globval,'reshape_reuse');
40 tntSystem_nc.trunc_err_func = tntgetAtt(gid,globval,'trunc_err_func');
41 basisopid = netcdf.inqGrps(gid);
42 if (~isempty(basisopid))
43  tntSystem_nc.basisOp = tntNcGetNode(basisopid);
44 else
45  tntSystem_nc.basisOp = [];
46 end
47 tntSystem = tntSystem_nc;
48 tntFileInfo = ['NetCDF file ',ncname,' converted to MATLAB format'];
49 save(matoutput, 'tntSystem', 'tntLibType', 'tntLibVersion', 'tntFileInfo');
50 
51 % ----- Reading parameters in the output file ------
52 gid = netcdf.inqNcid(ncid,'parameters');
53 varids = netcdf.inqGrps(gid);
54 for j = 1:length(varids)
55  [varname, vardata] = tntNcGetData(varids(j));
56  varname = [varname exten];
57  eval([varname,' = transpose(vardata);']);
58  save(matoutput,varname,'-append');
59  disp(['Converted parameter ',varname]);
60 end
61 
62 % ----- Reading arrays in the output file ------
63 gid = netcdf.inqNcid(ncid,'arrays');
64 varids = netcdf.inqGrps(gid);
65 for j = 1:length(varids)
66  [varname, vardata] = tntNcGetData(varids(j));
67  varname = [varname exten];
68  eval([varname,' = vardata;']);
69  save(matoutput, varname, '-append');
70  disp(['Converted array ',varname]);
71 end
72 
73 % ----- Reading strings in the output file ------
74 gid = netcdf.inqNcid(ncid,'strings');
75 varids = netcdf.inqVarIDs(gid);
76 for j = 1:length(varids)
77  varname = netcdf.inqVar(gid,varids(j));
78  vardata = tntgetAtt(gid,varids(j),'value');
79  varname = [varname exten];
80  eval([varname,' = vardata;']);
81  save(matoutput,varname,'-append');
82  disp(['Converted string ',varname]);
83 end
84 
85 % ----- Reading nodes in the output file ------
86 gid = netcdf.inqNcid(ncid,'nodes');
87 varids = netcdf.inqGrps(gid);
88 for j = 1:length(varids)
89  varname = netcdf.inqGrpName(varids(j));
90  nodestruct = tntNcGetNode(varids(j));
91  varname = [varname exten];
92  eval([varname,' = nodestruct;']);
93  save(matoutput, varname, '-append');
94  disp(['Converted node ',varname]);
95 end
96 
97 % ----- Reading nodes arrays in the output file ------
98 gid = netcdf.inqNcid(ncid,'node arrays');
99 varids = netcdf.inqGrps(gid);
100 for j = 1:length(varids)
101  varname = netcdf.inqGrpName(varids(j));
102  nodeids = netcdf.inqGrps(varids(j));
103  nodestruct = tntNcGetNode(nodeids);
104  varname = [varname exten];
105  eval([varname,' = nodestruct;']);
106  save(matoutput, varname, '-append');
107  disp(['Converted node array ',varname]);
108 end
109 
110 %----- Reading expectation value operators in the output file ------
111 gid = netcdf.inqNcid(ncid,'exops');
112 varids = netcdf.inqGrps(gid);
113 for j = 1:length(varids)
114  varname = netcdf.inqGrpName(varids(j));
115  exop = struct;
116 
117  gid = netcdf.inqNcid(varids(j),'os_operators');
118  ops = netcdf.inqGrps(gid);
119  exop.os_operators = tntNcGetNode(ops);
120  exop.os_labels = cell(1, length(ops));
121  for o = 1:length(ops)
122  exop.os_labels{o} = netcdf.inqGrpName(ops(o));
123  end
124 
125  gid = netcdf.inqNcid(varids(j),'nn_operators');
126  ops = netcdf.inqGrps(gid);
127  exop.nn_operators = tntNcGetNode(ops);
128  exop.nn_labels = cell(1, length(ops)/2);
129  for o = 2:2:length(ops)
130  exop.nn_labels{o/2} = netcdf.inqGrpName(ops(o));
131  exop.nn_labels{o/2} = exop.nn_labels{o/2}(1:end-1);
132  end
133 
134  gid = netcdf.inqNcid(varids(j),'cs_operators');
135  ops = netcdf.inqGrps(gid);
136  exop.cs_operators = tntNcGetNode(ops);
137  exop.cs_labels = cell(1, length(ops)/2);
138  for o = 2:2:length(ops)
139  exop.cs_labels{o/2} = netcdf.inqGrpName(ops(o));
140  exop.cs_labels{o/2} = exop.cs_labels{o/2}(1:end-1);
141  end
142 
143  gid = netcdf.inqNcid(varids(j),'ap_operators');
144  ops = netcdf.inqGrps(gid);
145  exop.ap_operators = tntNcGetNode(ops);
146  exop.ap_labels = cell(1, length(ops)/2);
147  for o = 2:2:length(ops)
148  exop.ap_labels{o/2} = netcdf.inqGrpName(ops(o));
149  exop.ap_labels{o/2} = exop.ap_labels{o/2}(1:end-1);
150  end
151  varname = [varname exten];
152  eval([varname,' = exop;']);
153  save(matoutput, varname, '-append');
154  disp(['Converted exop ',varname]);
155 end
156 
157 % ----- Reading networks in the output file ------
158 gid = netcdf.inqNcid(ncid,'networks');
159 varids = netcdf.inqGrps(gid);
160 for j = 1:length(varids)
161  varname = netcdf.inqGrpName(varids(j));
162  nodeids = netcdf.inqGrps(netcdf.inqNcid(varids(j),'nodes'));
163  conngid = netcdf.inqNcid(varids(j),'connections');
164  connid = netcdf.inqVarID(conngid,'conn_nodes');
165  networkstruct.nodes = tntNcGetNode(nodeids);
166  numnodes = length(networkstruct.nodes);
167  dimid = netcdf.inqDimID(conngid,'connecting legs');
168  [~, dimlen] = netcdf.inqDim(conngid,dimid);
169  networkstruct.connections = cell(numnodes);
170  for m = 1:numnodes
171  for n = m:numnodes
172  str = netcdf.getVar(conngid, connid,[0,m-1,n-1],[dimlen,1,1]);
173  networkstruct.connections{m,n} = str(isstrprop(str, 'alphanum'))';
174  end
175  end
176  networkstruct.start = tntgetAtt(conngid, connid, 'start');
177  networkstruct.start_leg = tntgetAtt(conngid, connid, 'start_leg');
178  networkstruct.end = tntgetAtt(conngid, connid, 'end');
179  networkstruct.end_leg = tntgetAtt(conngid, connid, 'end_leg');
180  varname = [varname exten];
181  eval([varname,' = networkstruct;']);
182  save(matoutput, varname, '-append');
183  disp(['Converted network ',varname]);
184 end
185 
186 netcdf.close(ncid);
187 end
188 
189 function [varname,vardata] = tntNcGetData(varid)
190  varname = netcdf.inqGrpName(varid);
191  if (length(netcdf.inqVarIDs(varid)) > 0)
192  vardata = netcdf.getVar(varid,netcdf.inqVarID(varid,'real_part'));
193  if (length(netcdf.inqVarIDs(varid)) > 1)
194  vardata = vardata + 1i*netcdf.getVar(varid,netcdf.inqVarID(varid,'imaginary_part'));
195  end
196  else
197  vardata = [];
198  end
199 
200  sz = size(vardata);
201  if (length(sz) == 3 && sz(2) == 1)
202  vardata = reshape(vardata, [sz(1) sz(3)]);
203  end
204 end
205 
206 function nodestruct = tntNcGetNode(nodeids)
207 
208 nodestruct = struct;
209 
210 for k = 1:length(nodeids)
211  globval = netcdf.getConstant('NC_GLOBAL');
212 
213  numlegs = tntgetAtt(nodeids(k),globval,'num_legs');
214  nodestruct(k).ids = tntgetAtt(nodeids(k),globval,'ids');
215 
216  nodestruct(k).indices = cell(1,numlegs);
217  for l=1:numlegs
218  nodestruct(k).indices{l} = tntgetAtt(nodeids(k),globval,['leg_',num2str(l-1),'_indices']);
219  end
220 
221  tensorid = netcdf.inqNcid(nodeids(k),'tensor');
222  nodestruct(k).tensor.elems_type = tntgetAtt(tensorid,globval,'elems_type');
223  nodestruct(k).tensor.dims = tntgetAtt(tensorid,globval,'dims');
224  nodestruct(k).tensor.qn_info.qn_dir = tntgetAtt(tensorid,globval,'qn_dir');
225  nodestruct(k).tensor.qn_info.qn_index = cell(1,numlegs);
226  qnsymmnum = tntgetAtt(tensorid,globval,'qn_symmnum');
227  if (qnsymmnum && any(nodestruct(k).tensor.qn_info.qn_dir))
228  for i=1:length(nodestruct(k).tensor.qn_info.qn_dir)
229  nodestruct(k).tensor.qn_info.qn_index{i} = tntgetAtt(tensorid,globval,['qn_index_',num2str(i-1)]);
230  nodestruct(k).tensor.qn_info.qn_index{i} = reshape(nodestruct(k).tensor.qn_info.qn_index{i}, [qnsymmnum, nodestruct(k).tensor.dims(i)]);
231  end
232  end
233  elemsid = netcdf.inqNcid(tensorid,'elems');
234 
235  if (strcmp(nodestruct(k).tensor.elems_type,'values'))
236  elems.vals = tntNcGetTensorValues(elemsid, 'vals');
237  elseif (strcmp(nodestruct(k).tensor.elems_type,'blocks'))
238 
239  numblocks = tntgetAtt(elemsid,globval,'numblocks');
240  elems.vals = cell(1,numblocks);
241  for b = 1:numblocks
242  elems.vals{b} = tntNcGetTensorValues(elemsid, ['block_' num2str(b-1)]);
243  end
244  elems.qn_tot = reshape(tntgetAtt(elemsid,globval,'qn_tot'),[qnsymmnum,numblocks]);
245  elems.rowlegs = tntgetAtt(elemsid,globval,'rowlegs');
246  elems.collegs = tntgetAtt(elemsid,globval,'collegs');
247  elems.indmapr = tntgetAtt(elemsid,globval,'indmapr');
248  elems.indmapc = tntgetAtt(elemsid,globval,'indmapc');
249  elems.indmapr = reshape(elems.indmapr,[2,length(elems.indmapr)/2]);
250  elems.indmapc = reshape(elems.indmapc,[2,length(elems.indmapc)/2]);
251  end
252  nodestruct(k).tensor.elems = elems;
253 end
254 
255 end
256 
257 function elemvals = tntNcGetTensorValues(elemsid, varname)
258 
259 valsid = netcdf.inqVarID(elemsid,varname);
260 rowsize = tntgetAtt(elemsid,valsid,'rowsize');
261 colsize = tntgetAtt(elemsid,valsid,'colsize');
262 comp = tntgetAtt(elemsid,valsid,'comp');
263 elemvals = netcdf.getVar(elemsid,valsid,[0 0 0],[comp+1 rowsize colsize]);
264 if (comp)
265  elemvals = elemvals(1,:,:) + 1i*elemvals(2,:,:);
266 end
267 elemvals = reshape(elemvals,[rowsize colsize]);
268 
269 end
270 
271 function convertedval = tntgetAtt(gid,varid,varname)
272  convertedval = netcdf.getAtt(gid,varid,varname);
273 
274  if (any(isinteger(convertedval)))
275  convertedval = double(convertedval);
276  end
277 end
function tntNc2mat(in ncname, in matoutput)