rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
init_values.m
1 function U0 = init_values(X,Y, params)
2 %function U0 = init_values([X],[Y], params)
3 %
4 % function constructing the initial values of the convection diffusion
5 % problem in the specified points and parameters
6 %
7 % 'blobs': u_0 = beta*exp(-gamma*((x-0.25).^2+(y-0.5).^2))* Xi(B_r) ...
8 % + (1-beta)*exp(-gamma*((x-0.25).^2+(y-0.7).^2))* Xi(B_r)
9 % 'homogeneous' u_0 = beta
10 % 'as_dirichlet' : evaluate dirichlet-data-function at time 0
11 % 'waveproduct' : product of two sinus waves in both coordinate directions
12 % 'grey_image' : load grey value image as initial data
13 % 'function_ptr' : init function given by function
14 % pointer to complete evaluation:
15 % params.init_values_ptr
16 % Function must
17 % allow call U = f(X,Y,params), where X, Y are
18 % vectors of the same size. Return is a vector of
19 % values in the corresponding points.
20 % 'decomp_function_ptr' : init function given by function
21 % pointer to components and coefficients:
22 % params.init_values_coefficients_ptr
23 % Function must
24 % allow call U = f([],[],params)
25 % Return is a vector of length Q with (parameter
26 % dependent) coefficients in U.
27 % params.init_values_components_ptr
28 % Functions must
29 % allow call U = f(X,Y,params), where X, Y are
30 % vectors of the same size.
31 % Return of components is a cell array of vectors of
32 % the same size as X and Y with the point values
33 % of the components. Linear combination of
34 % components by coefficients then yields the
35 % complete evaluation.
36 %
37 % required fields of params:
38 % name_init_values : 'blobs', 'homogeneous', 'wave', 'leftright',
39 % 'as_dirichlet'
40 %
41 % if name_init_values == 'gradient_box'
42 % c_init_up : constant value on upper border of box
43 % (unit square)
44 % c_init_lo : constant value on lower border of box
45 %
46 % if name_init_values == 'blobs'
47 % radius : radius of blob-cutoff circle
48 % gamma : spread of initial-value gaussian
49 % beta : value between 0 and 1 weighting two gauss-blobs
50 %
51 % if name_init_values == 'homogeneous'
52 % c_init : constant value in field
53 %
54 % if name_init_values == 'wave'
55 % a wave of values between 0 and c_init
56 % c_init* 0.5 * (sin(freq_x * x + freq_y * y) + 1)
57 % c_init : maximum value in field
58 % freq_x : frequency in x-direction
59 % freq_y : frequency in y-direction
60 %
61 % if name_init_values == 'waveproduct'
62 % a product of axis-dependent waves of values between
63 % c_init_min and c_init_max
64 % c_init_max : maximum value in field
65 % c_init_min : minimum value in field
66 % c_init_freq_x : frequency in x-direction
67 % c_init_freq_y : frequency in y-direction
68 % c_init_phase_x : phase shift
69 % c_init_phase_y : phase shift
70 %
71 % if name_init_values == 'leftright'
72 % c_init_left : constant value in field left of border
73 % c_init_right : constant value in field left of border
74 % c_init_middle : x-coordinate separation
75 %
76 % if name_init_values == 'as_dirichlet'
77 % parameters as required by dirichlet function
78 %
79 % if name_init_values == 'grey_image'
80 % a grey value image is loaded mapping 0 to 0 and 255 to c_init
81 % with bilinear interpolation
82 % c_init : parameter weighting the components
83 % c_init_filename : name of image file
84 % c_init_xmin : image is mapped to specified rectangle
85 % c_init_xmax : image is mapped to specified rectangle
86 % c_init_ymin : image is mapped to specified rectangle
87 % c_init_ymax : image is mapped to specified rectangle
88 % c_init_xdivisions : vector of divisions, i.e intervals,
89 % e.g. borders of letters in an image
90 % determines number of components. c_init = 0
91 % => full weight to leftmost part of image
92 % c_init = 1 => full weight to complete image
93 %
94 % Function supports affine decomposition, i.e. different operation modes
95 % guided by optional field affine_decomp_mode in params. See also the
96 % contents.txt for general explanation
97 %
98 % optional fields of params:
99 % mu_names : names of fields to be regarded as parameters in vector mu
100 % affine_decomp_mode: operation mode of the function
101 % 'none' (default): no parameter dependence or decomposition is
102 % performed. output is as described above.
103 % 'components': For each output argument a cell array of output
104 % arguments is returned representing the q-th component
105 % independent of the parameters given in mu_names
106 % 'coefficients': For each output argument a cell array of output
107 % arguments is returned representing the q-th coefficient
108 % dependent of the parameters given in mu_names
109 %
110 % in 'coefficients' mode, the parameters in brackets are empty
111 
112 % Bernard Haasdonk 5.10.2005
113 
114 decomp_mode = params.decomp_mode;
115 % flag indicating whether the computation respected the decomposition
116 respected_decomp_mode = 0;
117 
118 if isequal(params.name_init_values,'gradient_box')
119  % definition of the box
120  width = 0.2;
121  xmid = 0.45;
122  ymid = 0.55;
123  height = 0.7;
124  ymin = ymid-height/2;
125  ymax = ymid+ height/2;
126  if decomp_mode == 0
127  BX = abs(X(:)-xmid);
128  BY = abs(Y(:)-ymid);
129  I = (BX<width/2) & (BY<height/2);
130  % y = ymin => 0 + c_init_lo
131  % y = ymax => c_init_up + 0
132  U0 = (params.c_init_up * (Y(:)-ymin) + ...
133  + params.c_init_lo * (ymax-Y(:))) .* I / (ymax-ymin);
134  elseif decomp_mode == 1
135  % two components
136  BX = abs(X(:)-xmid);
137  BY = abs(Y(:)-ymid);
138  I = (BX<width/2) & (BY<height/2);
139  U0{1} = (Y(:)-ymin) .* I / (ymax-ymin);
140  U0{2} = (ymax-Y(:)) .* I / (ymax-ymin);
141  else % decomp_mode == 2
142  U0 = [params.c_init_up, params.c_init_lo];
143  end;
144  respected_decomp_mode = 1;
145 elseif isequal(params.name_init_values,'blobs')
146  B1 = ((X(:)-0.25).^2+(Y(:)-0.5).^2);
147  B2 = ((X(:)-0.25).^2+(Y(:)-0.7).^2);
148  I1 = (B1<params.radius.^2);
149  I2 = (B2<params.radius.^2);
150  if decomp_mode == 0
151  U0 = params.beta*exp(-params.gamma*B1).*I1 ...
152  + (1-params.beta)* exp(-params.gamma*B2).*I2;
153  elseif decomp_mode == 1
154  if ismember('gamma',params.mu_names) || ...
155  ismember('radius',params.mu_names)
156  error('affine decomp with respect to mu_names not possible!');
157  end;
158  % two components
159  U0{1} = exp(-params.gamma*B1).*I1;
160  U0{2} = exp(-params.gamma*B2).*I2;
161  else % decomp_mode == 2
162  U0 = [params.beta, 1-params.beta];
163  end;
164  respected_decomp_mode = 1;
165 elseif isequal(params.name_init_values,'homogeneous')
166  if decomp_mode == 0
167  U0 = params.c_init * ones(length(X),1);
168  elseif decomp_mode == 1
169  U0 = { ones(length(X),1) };
170  else % decomp_mode == 2
171  U0 = params.c_init;
172  end;
173  respected_decomp_mode = 1;
174 elseif isequal(params.name_init_values,'wave')
175  if decomp_mode == 0
176  U0 = params.c_init * 0.5 * (sin(params.freq_x * X(:) + ...
177  params.freq_y * Y(:) ) + 1 );
178  elseif decomp_mode == 1
179  % single component independent of mu_names
180  if ismember('freq_x',params.mu_names) || ...
181  ismember('freq.y',params.mu_names)
182  error('affine decomp with respect to mu_names not possible!');
183  end;
184  U0 = {0.5 * (sin(params.freq_x * X(:) + ...
185  params.freq_y * Y(:) ) + 1 )};
186  else % decomp_mode == 2
187  U0 = params.c_init;
188  end;
189  respected_decomp_mode = 1;
190 
191  elseif isequal(params.name_init_values,'transformed_blobs')
192 % [X_trans, Y_trans] = geometry_transformation(X,Y,params);
193 % B = cell(1,5);
194 % I = cell(1,5);
195 % B{1} = ((X_trans(:) - 0.5).^2 +(Y_trans(:)-0.7).^2);
196  if any(ismember(fieldnames(params), 'blob_height'))
197  h = params.blob_height;
198  else
199  h = 0.15;
200  end
201  B{1} = ((X(:) - 0.5).^2 +(Y(:)-0.7).^2);
202  B{2} = ((X(:) - 0.8).^2 +(Y(:)-0.9).^2);
203  B{3} = ((X(:) - 0.8).^2 +(Y(:)-0.2).^2);
204  B{4} = ((X(:) - 0.2).^2 +(Y(:)-0.2).^2);
205  B{5} = ((X(:) - 0.2).^2 +(Y(:)-0.9).^2);
206  B{6} = ((X(:) - 0.4).^2 +(Y(:)-0.4).^2);
207  B{7} = ((X(:) - 0.6).^2 +(Y(:)-0.6).^2);
208 
209  I=cell(1,7);
210  % p(mu) = mu*(1-x)
211  % p_mu = spline_select(params);
212  ztrans = Y.*(1-X);
213  for i=1:7
214  I{i} = (B{i} < 0.05.^2);
215  end
216  if decomp_mode == 0
217  U0 = h .* I{1} + params.c_init * ones(length(X),1) + params.gravity * (Y + params.hill_height*ztrans);
218  for i = 2:7
219  U0 = U0 + h .* exp(-B{i}) .* I{i};
220  end
221  elseif decomp_mode == 1
222  U0 = cell(1,8);
223  U0{1} = I{1};
224  for i = 2:7
225  U0{i} = I{i};
226  end
227  U0{8} = ones(length(X),1);
228  if params.gravity ~= 0
229  U0{9} = Y;
230  U0{10} = ztrans;
231  end
232  else
233  U0 = [ h, h, h, h, h, h, h, params.c_init];
234  U0 = [ U0, params.gravity, params.gravity * params.hill_height ];
235  end
236  respected_decomp_mode = 1;
237 elseif isequal(params.name_init_values,'waveproduct')
238  if (decomp_mode == 0) || (decomp_mode ==1)
239  Uinit = params.c_init_min + ...
240  (params.c_init_max-params.c_init_min) * ...
241  0.5 * (sin_sym(params.c_init_freq_x * X(:) + params.c_init_phase_x) ...
242  .* sin_sym(params.c_init_freq_y * Y(:) + params.c_init_phase_y) ...
243  + 1);
244  end;
245 % if ismember('c_init_freq_x',params.mu_names) | ...
246 % ismember('c_init_freq_y',params.mu_names) | ...
247 % ismember('c_init_min',params.mu_names) | ...
248 % ismember('c_init_max',params.mu_names) | ...
249 % ismember('c_init_phase_x',params.mu_names) | ...
250 % ismember('c_init_phase_y',params.mu_names)
251 % error('affine decomp with respect to mu_names not possible!');
252 % end;
253  switch decomp_mode
254  case 0
255  U0 = Uinit;
256  case 1
257  U0 = {Uinit};
258  case 2
259  U0 = 1;
260  end;
261  respected_decomp_mode = 1;
262 
263 elseif isequal(params.name_init_values,'leftright')
264  i = (X <= params.init_middle);
265  U0 = params.c_init_left * i + (1-i) * params.c_init_right;
266 elseif isequal(params.name_init_values,'as_dirichlet')
267  U0 = dirichlet_values(X,Y,params);
268  % if as_dirichlet not decomposable => error there and not here.
269  respected_decomp_mode = 1;
270 
271 elseif isequal(params.name_init_values,'grey_image')
272  ncomp = 1+length(params.c_init_xdivisions);
273  if decomp_mode ~= 2
274  % determine components and perform linear combination
275  img = imread(params.c_init_filename)/255;
276  %erstmal ohne bilineare Interpolation:
277  XI = (X(:)-params.c_init_xmin)/(params.c_init_xmax- ...
278  params.c_init_xmin)* size(img,2);
279  XI = max(XI,0);
280  XI = min(XI,size(img,2));
281  XI = ceil(XI);
282  fi = find(XI==0);
283  if ~isempty(fi)
284  XI(fi)= 1;
285  end;
286  YI = (Y(:)-params.c_init_ymin)/(params.c_init_ymax- ...
287  params.c_init_ymin)* size(img,1);
288  YI = max(YI,0);
289  YI = min(YI,size(img,1));
290  % reflect y coordinate
291  YI = size(img,1)-YI;
292  YI = ceil(YI);
293  fi = find(YI==0);
294  if ~isempty(fi)
295  YI(fi)= 1;
296  end;
297 
298  ind = sub2ind(size(img),YI,XI);
299  I = img(ind);
300 
301  U0comp = cell(ncomp,1);
302  xdivs = [0;params.c_init_xdivisions(:);params.c_init_xmax];
303  for i = 1:ncomp
304  U0 = zeros(length(X(:)),1);
305  fi = find( (X(:)<xdivs(i+1)) & (X(:)>=xdivs(i)));
306  if ~isempty(fi)
307  U0(fi) = I(fi);
308  end;
309  U0comp{i} = U0;
310  end;
311  end;
312 
313  % determine coefficients:
314  %coeff = zeros(ncomp,1);
315  coeff = (0:(ncomp-1))/ncomp;
316  coeff = (params.c_init - coeff)*ncomp;
317  coeff = min(coeff,1);
318  coeff = max(coeff,0);
319 
320  % now return the correct quantity
321  if decomp_mode == 2
322  U0 = coeff;
323  elseif decomp_mode == 0
324  U0 = lincomb_sequence(U0comp,coeff);
325  elseif decomp_mode == 1
326  U0 = U0comp;
327  end;
328  respected_decomp_mode = 1;
329 
330 elseif isequal(params.name_init_values,'decomp_function_ptr')
331  if decomp_mode == 2
332  U0 = params.init_values_coefficients_ptr([],[],params);
333  elseif decomp_mode == 1; % components
334  U0 = params.init_values_components_ptr(X,Y,params);
335  else % decomp_mode = 0, complete
336  Ucoefficients = params.init_values_coefficients_ptr([],[],params);
337  Ucomponents = params.init_values_components_ptr(X,Y,params);
338  U0 = lincomb_sequence(Ucomponents,Ucoefficients);
339  end;
340  respected_decomp_mode = 1;
341 
342 elseif isequal(params.name_init_values,'function_ptr')
343  U0 = params.init_values_ptr(X,Y,params);
344  respected_decomp_mode = 0;
345 
346 else
347  error('unknown name_init_values');
348 end;
349 
350 if decomp_mode>0 && respected_decomp_mode==0
351  error('function does not support affine decomposition!');
352 end;
353 
354 % TO BE ADJUSTED TO NEW SYNTAX
355 %| \docupdate
function Udirichlet = dirichlet_values(model, X, Y)
UDIRICHLET = DIRICHLET_VALUES([X],[Y], MODEL) Examples dirichlet_values([0,1,2],[1,1,1],struct(name_dirichlet_values, homogeneous, ... c_dir, 1)) dirichlet_values([0:0.1:1],[0],struct(name_dirichlet_values, xstripes, ... c_dir, [0 1 2], ... dir_borders, [0.3 0.6])) dirichlet_values([0:0.1:1],[0],struct(name_dirichlet_values, box, ... c_dir, 1, ... dir_box_xrange, [0.3 0.6], ... dir_box_yrange, [-0.1 0.1]))