rbmatlab 0.10.01
datafunc/init_values.m
00001 function U0 = init_values(X,Y, params)
00002 %function U0 = init_values([X],[Y], params)
00003 %
00004 % function constructing the initial values of the convection diffusion
00005 % problem in the specified points and parameters
00006 %
00007 % 'blobs':   u_0 = beta*exp(-gamma*((x-0.25).^2+(y-0.5).^2))* Xi(B_r) ...
00008 %                  + (1-beta)*exp(-gamma*((x-0.25).^2+(y-0.7).^2))* Xi(B_r)
00009 % 'homogeneous' u_0 = beta
00010 % 'as_dirichlet' : evaluate dirichlet-data-function at time 0  
00011 % 'waveproduct'  : product of two sinus waves in both coordinate directions
00012 % 'grey_image'   : load grey value image as initial data
00013 % 'function_ptr' : init function given by function
00014 %                  pointer to complete evaluation:
00015 %                      params.init_values_ptr
00016 %                  Function must
00017 %                  allow call U = f(X,Y,params), where X, Y are
00018 %                  vectors of the same size. Return is a vector of
00019 %                  values in the corresponding points.
00020 % 'decomp_function_ptr' : init function given by function
00021 %                  pointer to components and coefficients: 
00022 %                      params.init_values_coefficients_ptr 
00023 %                  Function must
00024 %                  allow call U = f([],[],params)
00025 %                  Return is a vector of length Q with (parameter
00026 %                  dependent) coefficients in U.
00027 %                      params.init_values_components_ptr 
00028 %                  Functions must
00029 %                  allow call U = f(X,Y,params), where X, Y are
00030 %                  vectors of the same size. 
00031 %                  Return of components is a cell array of vectors of
00032 %                  the same size as X and Y with the point values
00033 %                  of the components. Linear combination of
00034 %                  components by coefficients then yields the
00035 %                  complete evaluation.
00036 %
00037 % required fields of params:
00038 % name_init_values     : 'blobs', 'homogeneous', 'wave', 'leftright', 
00039 %                        'as_dirichlet'
00040 %
00041 % if name_init_values == 'gradient_box'
00042 %   c_init_up       : constant value on upper border of box
00043 %                     (unit square)
00044 %   c_init_lo       : constant value on lower border of box
00045 %
00046 % if name_init_values == 'blobs'
00047 % radius             : radius of blob-cutoff circle
00048 % gamma              : spread of initial-value gaussian
00049 % beta               : value between 0 and 1 weighting two gauss-blobs
00050 %
00051 % if name_init_values == 'homogeneous'
00052 % c_init             : constant value in field
00053 %
00054 % if name_init_values == 'wave'
00055 % a wave of values between 0 and c_init 
00056 %           c_init* 0.5 * (sin(freq_x * x + freq_y * y) + 1)
00057 % c_init             : maximum value in field
00058 % freq_x            : frequency in x-direction
00059 % freq_y            : frequency in y-direction
00060 %
00061 % if name_init_values == 'waveproduct'
00062 % a product of axis-dependent waves of values between 
00063 %           c_init_min and c_init_max 
00064 % c_init_max             : maximum value in field
00065 % c_init_min             : minimum value in field
00066 % c_init_freq_x           : frequency in x-direction
00067 % c_init_freq_y           : frequency in y-direction
00068 % c_init_phase_x          : phase shift
00069 % c_init_phase_y          : phase shift
00070 %
00071 % if name_init_values == 'leftright'
00072 % c_init_left      : constant value in field left of border
00073 % c_init_right     : constant value in field left of border
00074 % c_init_middle    : x-coordinate separation  
00075 %
00076 % if name_init_values == 'as_dirichlet'
00077 %          parameters as required by dirichlet function
00078 %
00079 % if name_init_values == 'grey_image'
00080 %          a grey value image is loaded mapping 0 to 0 and 255 to c_init
00081 %          with bilinear interpolation 
00082 % c_init             : parameter weighting the components
00083 % c_init_filename    : name of image file
00084 % c_init_xmin        : image is mapped to specified rectangle 
00085 % c_init_xmax        : image is mapped to specified rectangle 
00086 % c_init_ymin        : image is mapped to specified rectangle 
00087 % c_init_ymax        : image is mapped to specified rectangle 
00088 % c_init_xdivisions  : vector of divisions, i.e intervals,
00089 %                      e.g. borders of letters in an image
00090 %                      determines number of components. c_init = 0
00091 %                      => full weight to leftmost part of image
00092 %                      c_init = 1 => full weight to complete image
00093 %
00094 % Function supports affine decomposition, i.e. different operation modes
00095 % guided by optional field affine_decomp_mode in params. See also the 
00096 % contents.txt for general explanation
00097 %
00098 % optional fields of params:
00099 %   mu_names : names of fields to be regarded as parameters in vector mu
00100 %   affine_decomp_mode: operation mode of the function
00101 %     'none' (default): no parameter dependence or decomposition is 
00102 %                performed. output is as described above.
00103 %     'components': For each output argument a cell array of output
00104 %                 arguments is returned representing the q-th component
00105 %                 independent of the parameters given in mu_names  
00106 %     'coefficients': For each output argument a cell array of output
00107 %                 arguments is returned representing the q-th coefficient
00108 %                 dependent of the parameters given in mu_names  
00109 %
00110 % in 'coefficients' mode, the parameters in brackets are empty
00111 
00112 % Bernard Haasdonk 5.10.2005
00113 
00114 decomp_mode = params.decomp_mode;
00115 % flag indicating whether the computation respected the decomposition
00116 respected_decomp_mode = 0;
00117 
00118 if isequal(params.name_init_values,'gradient_box')
00119   % definition of the box
00120   width = 0.2;
00121   xmid = 0.45;
00122   ymid = 0.55;
00123   height = 0.7;
00124   ymin = ymid-height/2;
00125   ymax = ymid+ height/2;
00126   if decomp_mode == 0 
00127     BX = abs(X(:)-xmid);
00128     BY = abs(Y(:)-ymid);
00129     I = (BX<width/2) & (BY<height/2);
00130     % y = ymin =>  0 + c_init_lo
00131     % y = ymax =>  c_init_up + 0
00132     U0 = (params.c_init_up * (Y(:)-ymin) + ...
00133           + params.c_init_lo * (ymax-Y(:))) .* I / (ymax-ymin);
00134   elseif decomp_mode == 1 
00135     % two components
00136     BX = abs(X(:)-xmid);
00137     BY = abs(Y(:)-ymid);
00138     I = (BX<width/2) & (BY<height/2);
00139     U0{1} = (Y(:)-ymin) .* I / (ymax-ymin);
00140     U0{2} = (ymax-Y(:)) .* I / (ymax-ymin);
00141   else % decomp_mode == 2 
00142     U0 = [params.c_init_up, params.c_init_lo];
00143   end;  
00144   respected_decomp_mode = 1;
00145 elseif isequal(params.name_init_values,'blobs')
00146   B1 = ((X(:)-0.25).^2+(Y(:)-0.5).^2);
00147   B2 = ((X(:)-0.25).^2+(Y(:)-0.7).^2);
00148   I1 = (B1<params.radius.^2);
00149   I2 = (B2<params.radius.^2);
00150   if decomp_mode == 0 
00151     U0 = params.beta*exp(-params.gamma*B1).*I1 ...
00152          + (1-params.beta)* exp(-params.gamma*B2).*I2;
00153   elseif decomp_mode == 1 
00154     if ismember('gamma',params.mu_names) || ...
00155           ismember('radius',params.mu_names)
00156       error('affine decomp with respect to mu_names not possible!');
00157     end;
00158     % two components
00159     U0{1} = exp(-params.gamma*B1).*I1;
00160     U0{2} = exp(-params.gamma*B2).*I2;      
00161   else % decomp_mode == 2 
00162     U0 = [params.beta, 1-params.beta];
00163   end;  
00164   respected_decomp_mode = 1;
00165 elseif isequal(params.name_init_values,'homogeneous')
00166     if decomp_mode == 0
00167         U0 = params.c_init * ones(length(X),1);
00168     elseif decomp_mode == 1
00169         U0 = { ones(length(X),1) };
00170     else % decomp_mode == 2
00171         U0 = params.c_init;
00172     end;
00173     respected_decomp_mode = 1;
00174 elseif isequal(params.name_init_values,'wave')
00175   if decomp_mode == 0
00176     U0 = params.c_init * 0.5 * (sin(params.freq_x * X(:) + ...
00177                                     params.freq_y * Y(:)  ) + 1 );
00178   elseif decomp_mode == 1 
00179     % single component independent of mu_names
00180     if ismember('freq_x',params.mu_names) || ...
00181           ismember('freq.y',params.mu_names)
00182       error('affine decomp with respect to mu_names not possible!');
00183     end;
00184     U0 = {0.5 * (sin(params.freq_x * X(:) + ...
00185                      params.freq_y * Y(:)  ) + 1 )};      
00186   else % decomp_mode == 2 
00187     U0 = params.c_init;
00188   end;
00189   respected_decomp_mode = 1;
00190   
00191   elseif isequal(params.name_init_values,'transformed_blobs')
00192 %    [X_trans, Y_trans] = geometry_transformation(X,Y,params);
00193 %    B = cell(1,5);
00194 %    I = cell(1,5);
00195 %    B{1} = ((X_trans(:) - 0.5).^2 +(Y_trans(:)-0.7).^2);
00196     if any(ismember(fieldnames(params), 'blob_height'))
00197       h = params.blob_height;
00198     else
00199       h = 0.15;
00200     end
00201     B{1} = ((X(:) - 0.5).^2 +(Y(:)-0.7).^2);
00202     B{2} = ((X(:) - 0.8).^2 +(Y(:)-0.9).^2);
00203     B{3} = ((X(:) - 0.8).^2 +(Y(:)-0.2).^2);
00204     B{4} = ((X(:) - 0.2).^2 +(Y(:)-0.2).^2);
00205     B{5} = ((X(:) - 0.2).^2 +(Y(:)-0.9).^2);
00206     B{6} = ((X(:) - 0.4).^2 +(Y(:)-0.4).^2);
00207     B{7} = ((X(:) - 0.6).^2 +(Y(:)-0.6).^2);
00208                     
00209     I=cell(1,7);
00210     % p(mu) = mu*(1-x)
00211     % p_mu = spline_select(params);
00212     ztrans = Y.*(1-X);
00213     for i=1:7
00214         I{i} = (B{i} < 0.05.^2);
00215     end
00216     if decomp_mode == 0
00217       U0 = h .* I{1} + params.c_init * ones(length(X),1) + params.gravity * (Y + params.hill_height*ztrans);
00218       for i = 2:7
00219         U0 = U0 + h .* exp(-B{i}) .* I{i};
00220       end
00221     elseif decomp_mode == 1
00222       U0 = cell(1,8);
00223       U0{1} = I{1};
00224         for i = 2:7
00225         U0{i} = I{i};
00226         end
00227       U0{8} = ones(length(X),1);
00228       if params.gravity ~= 0
00229         U0{9} = Y;
00230         U0{10} = ztrans;
00231       end
00232     else
00233       U0 = [ h, h, h, h, h, h, h, params.c_init];
00234       U0 = [ U0, params.gravity, params.gravity * params.hill_height ];
00235     end
00236     respected_decomp_mode = 1;
00237 elseif isequal(params.name_init_values,'waveproduct')
00238   if (decomp_mode == 0) || (decomp_mode ==1)
00239     Uinit = params.c_init_min + ...
00240             (params.c_init_max-params.c_init_min) * ...
00241             0.5 * (sin_sym(params.c_init_freq_x * X(:) +  params.c_init_phase_x) ...
00242                    .* sin_sym(params.c_init_freq_y * Y(:) +  params.c_init_phase_y) ...
00243                    + 1);
00244   end;
00245 %  if ismember('c_init_freq_x',params.mu_names) | ...
00246 %       ismember('c_init_freq_y',params.mu_names) | ...
00247 %       ismember('c_init_min',params.mu_names) | ...
00248 %       ismember('c_init_max',params.mu_names) | ...
00249 %       ismember('c_init_phase_x',params.mu_names) | ...
00250 %       ismember('c_init_phase_y',params.mu_names) 
00251 %    error('affine decomp with respect to mu_names not possible!');
00252 %  end;
00253   switch decomp_mode 
00254    case 0
00255     U0 = Uinit;
00256    case 1
00257     U0 = {Uinit};
00258    case 2
00259     U0 = 1;
00260   end;
00261   respected_decomp_mode = 1;
00262   
00263 elseif isequal(params.name_init_values,'leftright')
00264   i = (X <= params.init_middle);
00265   U0 = params.c_init_left * i + (1-i) * params.c_init_right;
00266 elseif isequal(params.name_init_values,'as_dirichlet')
00267   U0 = dirichlet_values(X,Y,params);
00268   % if as_dirichlet not decomposable => error there and not here.
00269   respected_decomp_mode = 1;
00270 
00271 elseif isequal(params.name_init_values,'grey_image')
00272   ncomp = 1+length(params.c_init_xdivisions);
00273   if decomp_mode ~= 2
00274     % determine components and perform linear combination
00275     img = imread(params.c_init_filename)/255;
00276     %erstmal ohne bilineare Interpolation:
00277     XI = (X(:)-params.c_init_xmin)/(params.c_init_xmax- ...
00278                                     params.c_init_xmin)* size(img,2);
00279     XI = max(XI,0);
00280     XI = min(XI,size(img,2));
00281     XI = ceil(XI);
00282     fi = find(XI==0);
00283     if ~isempty(fi)
00284       XI(fi)= 1;
00285     end;    
00286     YI = (Y(:)-params.c_init_ymin)/(params.c_init_ymax- ...
00287                                     params.c_init_ymin)* size(img,1);
00288     YI = max(YI,0);
00289     YI = min(YI,size(img,1));
00290     % reflect y coordinate
00291     YI = size(img,1)-YI;
00292     YI = ceil(YI);
00293     fi = find(YI==0);
00294     if ~isempty(fi)
00295       YI(fi)= 1;
00296     end;
00297     
00298     ind = sub2ind(size(img),YI,XI);
00299     I = img(ind);
00300     
00301     U0comp = cell(ncomp,1);
00302     xdivs = [0;params.c_init_xdivisions(:);params.c_init_xmax];
00303     for i = 1:ncomp
00304       U0 = zeros(length(X(:)),1);
00305       fi = find( (X(:)<xdivs(i+1)) & (X(:)>=xdivs(i)));
00306       if ~isempty(fi)
00307         U0(fi) = I(fi);
00308       end;
00309       U0comp{i} = U0;
00310     end;
00311   end;
00312     
00313   % determine coefficients:
00314   %coeff = zeros(ncomp,1);
00315   coeff = (0:(ncomp-1))/ncomp;
00316   coeff = (params.c_init - coeff)*ncomp;
00317   coeff = min(coeff,1);
00318   coeff = max(coeff,0);
00319   
00320   % now return the correct quantity
00321   if decomp_mode == 2 
00322     U0 = coeff;
00323   elseif decomp_mode == 0 
00324     U0 = lincomb_sequence(U0comp,coeff);
00325   elseif decomp_mode == 1 
00326     U0 = U0comp;
00327   end;
00328   respected_decomp_mode = 1;
00329   
00330 elseif isequal(params.name_init_values,'decomp_function_ptr')
00331   if decomp_mode == 2
00332     U0 = params.init_values_coefficients_ptr([],[],params);
00333   elseif decomp_mode == 1; % components
00334     U0 = params.init_values_components_ptr(X,Y,params);
00335   else % decomp_mode = 0, complete
00336     Ucoefficients = params.init_values_coefficients_ptr([],[],params);
00337     Ucomponents = params.init_values_components_ptr(X,Y,params); 
00338     U0 = lincomb_sequence(Ucomponents,Ucoefficients);     
00339   end;    
00340   respected_decomp_mode = 1;
00341   
00342 elseif isequal(params.name_init_values,'function_ptr')
00343   U0 = params.init_values_ptr(X,Y,params);
00344   respected_decomp_mode = 0;  
00345 
00346 else
00347   error('unknown name_init_values');
00348 end;
00349 
00350 if decomp_mode>0 && respected_decomp_mode==0
00351   error('function does not support affine decomposition!');
00352 end;
00353 
00354 % TO BE ADJUSTED TO NEW SYNTAX
00355 %| \docupdate 
All Classes Namespaces Files Functions Variables