pax_global_header00006660000000000000000000000064142664176460014532gustar00rootroot0000000000000052 comment=022f699bc2b27be03dc74fa8d09432f528e5c765 bim-1.1.6/000077500000000000000000000000001426641764600123065ustar00rootroot00000000000000bim-1.1.6/.hgignore000066400000000000000000000014231426641764600141110ustar00rootroot00000000000000syntax: regexp # The recurrent (^|/) idiom in the regexps below should be understood # to mean "at any directory" while the ^ idiom means "from the # project's top-level directory". (^|/).*\.dvi$ (^|/).*\.pdf$ (^|/).*\.o$ (^|/).*\.oct$ (^|/).*\.octlink$ (^|/)octave-core$ (^|/)octave-workspace$ (^|/).*\.tar\.gz$ ## Our Makefile target ^target/ ## Files generated automatically by autoconf and the configure script ^src/aclocal\.m4$ ^src/configure$ ^src/autom4te\.cache($|/) ^src/config\.log$ ^src/config\.status$ ^src/Makefile$ ^src/.*\.m$ # e.g. doc/faq/OctaveFAQ.info # doc/interpreter/octave.info-4 ^doc/.*\.info(-\d)?$ ^doc/\w*/stamp-vti$ ^doc/\w*/version\.texi$ # Emacs tools create these (^|/)TAGS$ (^|/)semantic.cache$ # Other text editors often create these (^|/)~.* bim-1.1.6/COPYING000066400000000000000000000430771426641764600133540ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. bim-1.1.6/DESCRIPTION000066400000000000000000000005661426641764600140230ustar00rootroot00000000000000Name: bim Version: 1.1.6 Date: 2022-07-22 Author: Carlo de Falco, Culpo Massimiliano, Matteo Porro, Emanuela Abbate Maintainer: Carlo de Falco Title: PDE Solver using a Finite Element/Finite Volume approach Description: Package for solving Diffusion Advection Reaction (DAR) Partial Differential Equations Depends: octave (>= 3.8.0), fpl, msh Autoload: no License: GPLv2+ bim-1.1.6/INDEX000066400000000000000000000022661426641764600131060ustar00rootroot00000000000000BIM >> BIM - Diffusion Advection Reaction PDE Solver Matrix assembly bim1a_advection_diffusion bim1a_advection_upwind bim1a_axisymmetric_advection_diffusion bim1a_axisymmetric_advection_upwind bim2a_advection_diffusion bim2a_advection_upwind bim2a_axisymmetric_advection_diffusion bim2a_axisymmetric_advection_upwind bim3a_advection_diffusion bim3a_osc_advection_diffusion bim1a_laplacian bim1a_axisymmetric_laplacian bim2a_laplacian bim2a_axisymmetric_laplacian bim3a_laplacian bim3a_osc_laplacian bim1a_reaction bim1a_axisymmetric_reaction bim2a_reaction bim2a_axisymmetric_reaction bim3a_reaction bim1a_rhs bim1a_axisymmetric_rhs bim2a_rhs bim2a_axisymmetric_rhs bim3a_rhs bim2a_boundary_mass bim2a_axisymmetric_boundary_mass bim3a_boundary_mass Pre-processing and Post-processing computations bim2c_mesh_properties bim3c_mesh_properties bim2c_unknowns_on_side bim3c_unknowns_on_faces bim2c_pde_gradient bim3c_pde_gradient bim2c_global_flux bim3c_global_flux bim1c_elem_to_nodes bim2c_tri_to_nodes bim3c_tri_to_nodes bim2c_intrp bim3c_intrp bim1c_norm bim2c_norm bim3c_norm Utilities bimu_bernoulli bimu_logm bim-1.1.6/NEWS000066400000000000000000000036431426641764600130130ustar00rootroot00000000000000Summary of important user-visible changes for bim 1.1.6: ------------------------------------------------------------------- ** Avoid complex conjugate when changing array shape in bim2a_reaction and bim2a_rhs. Summary of important user-visible changes for bim 1.1.5: ------------------------------------------------------------------- ** Improvement of the functions for stiffness matrix assembly in 2D axisymmetric configuration. Summary of important user-visible changes for bim 1.1.4: ------------------------------------------------------------------- ** Added new functions for cylindrical coordinates in axisymmetric configuration. ** Fixed some bugs in 1d upwind discretization. Summary of important user-visible changes for bim 1.1.3: ------------------------------------------------------------------- ** Added new projection functions. ** A function for the generation of 3D boundary matrices has been added that can be used in the implementation of Robin boundary condition and interface conditions. ** Added a set of functions for the computation of inf, L2 and H1 norms of piecewise constant and elementwise constant functions. Summary of important user-visible changes for bim 1.1.2: ------------------------------------------------------------------- ** Fixed a bug in 1d upwind discretization. ** Added function to compute gradient of a 3d scalar field. Summary of important user-visible changes for bim 1.1.1: ------------------------------------------------------------------- ** Added new functions to perform interpolation at arbitrary nodes. Summary of important user-visible changes for bim 1.1.0: ------------------------------------------------------------------- ** Added new functions implementing the Orthogonal Subdomain Collocation method in 3D. ** More functions for 3d problems were added. ** Demostrations of how to use the package are now in the wiki. bim-1.1.6/doc/000077500000000000000000000000001426641764600130535ustar00rootroot00000000000000bim-1.1.6/doc/fiume.geo000066400000000000000000000011061426641764600146520ustar00rootroot00000000000000Point (1) = {0, 0, 0, 0.1}; Point (2) = {1, 1, 0, 0.1}; Point (3) = {1, 0.9, 0, 0.1}; Point (4) = {0, 0.1, 0, 0.1}; Point (5) = {0.3,0.1,-0,0.1}; Point (6) = {0.4,0.4,-0,0.1}; Point (7) = {0.5,0.6,0,0.1}; Point (8) = {0.6,0.9,0,0.1}; Point (9) = {0.8,0.8,0,0.1}; Point (10) = {0.2,0.2,-0,0.1}; Point (11) = {0.3,0.5,0,0.1}; Point (12) = {0.4,0.7,0,0.1}; Point (13) = {0.5,1,0,0.1}; Point (14) = {0.8,0.9,0,0.1}; Line (1) = {3, 2}; Line (2) = {4, 1}; CatmullRom(3) = {1,5,6,7,8,9,3}; CatmullRom(4) = {4,10,11,12,13,14,2}; Line Loop(15) = {3,1,-4,2}; Plane Surface(16) = {15}; bim-1.1.6/doc/tutorial.html000066400000000000000000000060361426641764600156110ustar00rootroot00000000000000

This is a short example on how to use bim to solve a DAR problem.
The data for this example can be found in the doc directory inside the bim installation directory.

Create the mesh and precompute the mesh properties
The geometry of the domain was created using gmsh and is stored in the file fiume.geo

[mesh] = msh2m_gmsh("fiume","scale",1,"clscale",.1);
[mesh] = bim2c_mesh_properties(mesh);

Construct an initial guess
For a linear problem only the values at boundary nodes are actually relevant

xu     = mesh.p(1,:).';
yu     = mesh.p(2,:).';
nelems = columns(mesh.t);
nnodes = columns(mesh.p);
uin    = 3*xu;

Set the coefficients for the problem: -div ( \alpha \gamma ( \eta \nabla u - \beta u ) )+ \delta \zeta u = f g

epsilon = .1;
alfa    = ones(nelems,1);
gamma   = ones(nnodes,1);
eta     = epsilon*ones(nnodes,1);
beta    = xu+yu;
delta   = ones(nelems,1);
zeta    = ones(nnodes,1);
f       = ones(nelems,1);
g       = ones(nnodes,1);

Construct the discretized operators
AdvDiff = bim2a_advection_diffusion(mesh,alfa,gamma,eta,beta);
Mass    = bim2a_reaction(mesh,delta,zeta);
b       = bim2a_rhs(mesh,f,g);
A       = AdvDiff + Mass;

To Apply Boundary Conditions, partition LHS and RHS
The tags of the sides are assigned by gmsh

Dlist = bim2c_unknowns_on_side(mesh, [8 18]); 	   ## DIRICHLET NODES LIST
Nlist = bim2c_unknowns_on_side(mesh, [23 24]); 	   ## NEUMANN NODES LIST
Nlist = setdiff(Nlist,Dlist);
Fn    = zeros(length(Nlist),1);           	   ## PRESCRIBED NEUMANN FLUXES
Ilist = setdiff(1:length(uin),union(Dlist,Nlist)); ## INTERNAL NODES LIST

Add = A(Dlist,Dlist);
Adn = A(Dlist,Nlist); ## shoud be all zeros hopefully!!
Adi = A(Dlist,Ilist); 

And = A(Nlist,Dlist); ## shoud be all zeros hopefully!!
Ann = A(Nlist,Nlist);
Ani = A(Nlist,Ilist); 

Aid = A(Ilist,Dlist);
Ain = A(Ilist,Nlist); 
Aii = A(Ilist,Ilist); 

bd = b(Dlist);
bn = b(Nlist); 
bi = b(Ilist); 

ud = uin(Dlist);
un = uin(Nlist); 
ui = uin(Ilist); 

Solve for the displacements

temp = [Ann Ani ; Ain Aii ] \ [ Fn+bn-And*ud ; bi-Aid*ud];
un   = temp(1:length(un));
ui   = temp(length(un)+1:end);
u(Dlist) = ud;
u(Ilist) = ui;
u(Nlist) = un;

Compute the fluxes through Dirichlet sides

Fd = Add * ud + Adi * ui + Adn*un - bd;

Compute the gradient of the solution

[gx, gy] = bim2c_pde_gradient(mesh,u);

Compute the internal Advection-Diffusion flux

[jxglob,jyglob] = bim2c_global_flux(mesh,u,alfa,gamma,eta,beta);

Save data for later visualization

fpl_dx_write_field("dxdata",mesh,[gx; gy]',"Gradient",1,2,1);
fpl_vtk_write_field ("vtkdata", mesh, {}, {[gx; gy]', "Gradient"}, 1);

bim-1.1.6/inst/000077500000000000000000000000001426641764600132635ustar00rootroot00000000000000bim-1.1.6/inst/bim1a_advection_diffusion.m000066400000000000000000000077171426641764600205500ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{A}]} = @ ## bim1a_advection_diffusion(@var{mesh},@var{alpha},@var{gamma},@var{eta},@var{beta}) ## ## Build the Scharfetter-Gummel stabilized stiffness matrix for a ## diffusion-advection problem. ## ## The equation taken into account is: ## ## - div (@var{alpha} * @var{gamma} (@var{eta} grad (u) - @var{beta} u)) = f ## ## where @var{alpha} is an element-wise constant scalar function, ## @var{eta} and @var{gamma} are piecewise linear conforming scalar ## functions, @var{beta} is an element-wise constant vector function. ## ## Instead of passing the vector field @var{beta} directly one can pass ## a piecewise linear conforming scalar function @var{phi} as the last ## input. In such case @var{beta} = grad @var{phi} is assumed. ## ## If @var{phi} is a single scalar value @var{beta} is assumed to be 0 ## in the whole domain. ## ## @seealso{bim1a_rhs, bim1a_reaction, bim1a_laplacian, bim2a_advection_diffusion} ## @end deftypefn function A = bim1a_advection_diffusion (x,alpha,gamma,eta,beta) ## Check input if (nargin != 5) error ("bim1a_advection_diffusion: wrong number of input parameters."); elseif (! isvector (x)) error ("bim1a_advection_diffusion: first argument is not a valid vector."); endif nnodes = numel (x); nelem = nnodes - 1; ## Turn scalar input to a vector of appropriate size if (isscalar (alpha)) alpha = alpha * ones (nelem, 1); endif if (isscalar (gamma)) gamma = gamma * ones (nnodes, 1); endif if (isscalar (eta)) eta = eta * ones (nnodes, 1); endif if (! (isvector (alpha) && isvector (gamma) && isvector (eta))) error ("bim1a_advection_diffusion: coefficients are not valid vectors."); elseif (numel (alpha) != nelem) error ("bim1a_advection_diffusion: length of alpha is not equal to the number of elements."); elseif (numel (gamma) != nnodes) error ("bim1a_advection_diffusion: length of gamma is not equal to the number of nodes."); elseif (numel (eta) != nnodes) error ("bim1a_advection_diffusion: length of eta is not equal to the number of nodes."); endif areak = reshape (diff (x), [], 1); if (numel (beta) == 1) vk = 0; elseif (numel (beta) == nelem) vk = beta .* areak; elseif (numel (beta) == nnodes) vk = diff (beta); else error ("bim1a_advection_diffusion: coefficient beta has wrong dimensions."); endif gammaetak = bimu_logm ((gamma .* eta)(1:end-1), (gamma .* eta)(2:end)); veta = diff (eta); etak = bimu_logm (eta(1:end-1), eta(2:end)); ck = alpha .* gammaetak .* etak ./ areak; [bpk, bmk] = bimu_bernoulli ((vk - veta) ./ etak); dm1 = [-(ck.*bmk); NaN]; dp1 = [NaN; -(ck.*bpk)]; d0 = [(ck(1).*bmk(1)); ((ck.*bmk)(2:end) + (ck.*bpk)(1:end-1)); (ck(end).*bpk(end))]; A = spdiags([dm1, d0, dp1],-1:1,nnodes,nnodes); endfunction %!test %! x = linspace(0,1,101); %! A = bim1a_advection_diffusion(x,1,1,1,0); %! alpha = ones(100,1); %! gamma = ones(101,1); %! eta = gamma; %! B = bim1a_advection_diffusion(x,alpha,gamma,eta,0); %! assert(A,B) bim-1.1.6/inst/bim1a_advection_upwind.m000066400000000000000000000060751426641764600200640ustar00rootroot00000000000000## Copyright (C) 2010-2014 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo Porro ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{A}]} = bim1a_advection_upwind (@var{mesh}, @var{beta}) ## ## Build the UW stabilized stiffness matrix for an advection problem. ## ## The equation taken into account is: ## ## (@var{beta} u)' = f ## ## where @var{beta} is an element-wise constant. ## ## Instead of passing the vector field @var{beta} directly one can pass ## a piecewise linear conforming scalar function @var{phi} as the last ## input. In such case @var{beta} = grad @var{phi} is assumed. ## ## If @var{phi} is a single scalar value @var{beta} is assumed to be 0 ## in the whole domain. ## ## @seealso{bim1a_rhs, bim1a_reaction, bim1a_laplacian, bim2a_advection_diffusion} ## @end deftypefn function A = bim1a_advection_upwind (x, beta) ## Check input if nargin != 2 error("bim1a_advection_upwind: wrong number of input parameters."); endif nnodes = length(x); nelem = nnodes-1; if (length(beta) == 1) vk = zeros(nelem,1); elseif (length(beta) == nelem) vk = beta; elseif (length(beta) == nnodes) vk = diff(beta); else error("bim1a_advection_upwind: coefficient beta has wrong dimensions."); endif bmk = (vk+abs(vk))/2; bpk = -(vk-abs(vk))/2; dm1 = [-bmk; NaN]; dp1 = [NaN; -bpk]; d0 = [bmk(1); bmk(2:end) + bpk(1:end-1); bpk(end)]; A = spdiags([dm1, d0, dp1],-1:1,nnodes,nnodes); endfunction %!test %! n = 200; %! mesh = linspace(0,1,n+1)'; %! uex = @(r) - r.^2 + 1; %! Nnodes = numel(mesh); %! Nelements = Nnodes-1; %! D = 1; v = 1; sigma = 0; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D*v*ones(Nelements,1); %! delta = ones(Nelements,1); %! zeta = sigma*ones(Nnodes,1); %! f = @(r) 2*D - 2*v.*r + sigma*uex(r); %! rhs = bim1a_rhs(mesh, ones(Nelements,1), f(mesh)); %! S = bim1a_laplacian(mesh,alpha,gamma); %! A = bim1a_advection_upwind(mesh, beta); %! R = bim1a_reaction(mesh, delta, zeta); %! S += (A+R); %! u = zeros(Nnodes,1); u([1 end]) = uex(mesh([1 end])); %! u(2:end-1) = S(2:end-1,2:end-1)\(rhs(2:end-1) - S(2:end-1,[1 end])*u([1 end])); %! assert(u,uex(mesh),1e-3) bim-1.1.6/inst/bim1a_axisymmetric_advection_diffusion.m000066400000000000000000000151561426641764600233420ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo Porro ## author: Emanuela Abbate ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{A}]} = @ ## bim1a_axisymmetric_advection_diffusion(@var{mesh},@var{alpha},@var{gamma},@var{eta},@var{beta}) ## ## Build the Scharfetter-Gummel stabilized stiffness matrix for a ## diffusion-advection problem in cylindrical coordinates with axisymmetric ## configuration. Rotational symmetry is assumed with respect to be the vertical ## axis r=0. Only grids that DO NOT contain r=0 are admissible. ## ##@example ##@group ## | |-------| OK |-------| | OK |--|-----| NO! ## r=0 r=0 r=0 #@end group #@end example ## ## The equation taken into account is: ## ## - 1/r * d/dr (@var{alpha} * @var{gamma} (@var{eta} du/dr - @var{beta} u)) = f ## ## where @var{alpha} is an element-wise constant scalar function, ## @var{eta} and @var{gamma} are piecewise linear conforming scalar ## functions, @var{beta} is an element-wise constant vector function. ## ## Instead of passing the vector field @var{beta} directly one can pass ## a piecewise linear conforming scalar function @var{phi} as the last ## input. In such case @var{beta} = grad @var{phi} is assumed. ## ## If @var{phi} is a single scalar value @var{beta} is assumed to be 0 ## in the whole domain. ## ## @seealso{bim1a_axisymmetric_rhs, bim1a_axisymmetric_reaction, ## bim1a_axisymmetric_laplacian, bim2a_axisymmetric_advection_diffusion} ## @end deftypefn function A = bim1a_axisymmetric_advection_diffusion (x,alpha,gamma,eta,beta) ## Check input if nargin != 5 error("bim1a_axisymmetric_advection_diffusion: wrong number of input parameters."); elseif !isvector(x) error("bim1a_axisymmetric_advection_diffusion: first argument is not a valid vector."); endif nnodes = length(x); nelem = nnodes-1; ## Turn scalar input to a vector of appropriate size if isscalar(alpha) alpha = alpha*ones(nelem,1); endif if isscalar(gamma) gamma = gamma*ones(nnodes,1); endif if isscalar(eta) eta = eta*ones(nnodes,1); endif if !( isvector(alpha) && isvector(gamma) && isvector(eta) ) error("bim1a_axisymmetric_advection_diffusion: coefficients are not valid vectors."); elseif (length(alpha) != nelem) error("bim1a_axisymmetric_advection_diffusion: length of alpha is not equal to the number of elements."); elseif (length(gamma) != nnodes) error("bim1a_axisymmetric_advection_diffusion: length of gamma is not equal to the number of nodes."); elseif (length(eta) != nnodes) error("bim1a_axisymmetric_advection_diffusion: length of eta is not equal to the number of nodes."); endif areak = reshape(diff(x),[],1); cm = reshape((x(1:end-1)+x(2:end))/2,[],1); if (length(beta) == 1) vk = 0; elseif (length(beta) == nelem) vk = beta .* areak; elseif (length(beta) == nnodes) vk = diff(beta); else error("bim1a_axisymmetric_advection_diffusion: coefficient beta has wrong dimensions."); endif gammaetak = bimu_logm ( (gamma.*eta)(1:end-1), (gamma.*eta)(2:end)); veta = diff(eta); etak = bimu_logm ( eta(1:end-1), eta(2:end)); ck = alpha .* gammaetak .* etak ./ areak .* abs(cm); [bpk, bmk] = bimu_bernoulli( (vk - veta)./etak); dm1 = [-(ck.*bmk); NaN]; dp1 = [NaN; -(ck.*bpk)]; d0 = [(ck(1).*bmk(1)); ((ck.*bmk)(2:end) + (ck.*bpk)(1:end-1)); (ck(end).*bpk(end))]; A = spdiags([dm1, d0, dp1],-1:1,nnodes,nnodes); endfunction %!test %! n = 3; %! mesh = linspace(1,2,n+1)'; %! uex = @(r) exp(r); %! duexdr = @(r) uex(r); %! d2uexdr2 = @(r) uex(r); %! Nnodes = numel(mesh); %! Nelements = Nnodes-1; %! D = 1; v = 1; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D*v*ones(Nelements,1); %! f = @(r) -D./r.*duexdr(r) - D.*d2uexdr2(r) ... %! + v./r .* uex(r) + v * duexdr(r); %! rhs = bim1a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh)); %! S = bim1a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); u([1,end]) = uex(mesh([1 end])); %! u(2:end-1) = S(2:end-1,2:end-1)\(rhs(2:end-1) - S(2:end-1,[1 end])*u([1 end])); %! assert(u,uex(mesh),1e-7) %!test %! n = 100; %! mesh = linspace(0,1,n+1)'; %! cm = (mesh(1:end-1) + mesh(2:end))/2; %! uex = @(r) - r.^2 + 1; %! Nnodes = numel(mesh); %! Nelements = Nnodes-1; %! D = 1; v = 0; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = v; %! f = @(r) 4*D; %! rhs = bim1a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh)); %! S = bim1a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); u(end) = uex(mesh(end)); %! u(1:end-1) = S(1:end-1,1:end-1)\(rhs(1:end-1) - S(1:end-1,end)*u(end)); %! assert(u,uex(mesh),1e-3) %!test %! n = 100; %! mesh = linspace(0,1,n+1)'; %! cm = (mesh(1:end-1) + mesh(2:end))/2; %! uex = @(r) - r.^2 + 1; %! Nnodes = numel(mesh); %! Nelements = Nnodes-1; %! D = 1; v = cm; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D*v; %! f = @(r) 4*D + 2 - 4*r.^2; %! rhs = bim1a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh)); %! S = bim1a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); u(end) = uex(mesh(end)); %! u(1:end-1) = S(1:end-1,1:end-1)\(rhs(1:end-1) - S(1:end-1,end)*u(end)); %! assert(u,uex(mesh),1e-3) %!test %! x = linspace(0,1,101); %! A = bim1a_axisymmetric_advection_diffusion(x,1,1,1,0); %! alpha = ones(100,1); %! gamma = ones(101,1); %! eta = gamma; %! B = bim1a_axisymmetric_advection_diffusion(x,alpha,gamma,eta,0); %! assert(A,B) bim-1.1.6/inst/bim1a_axisymmetric_advection_upwind.m000066400000000000000000000065711426641764600226630ustar00rootroot00000000000000## Copyright (C) 2010-2014 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo porro ## author: Emanuela Abbate ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{A}]} = bim1a_axisymmetric_advection_upwind (@var{mesh}, @var{beta}) ## ## Build the Upwind stabilized stiffness matrix for an advection problem ## in cylindrical coordinates with axisymmetric configuration. ## ## The equation taken into account is: ## ## 1/r * (r * @var{beta} u)' = f ## ## where @var{beta} is an element-wise constant. ## ## Instead of passing the vector field @var{beta} directly one can pass ## a piecewise linear conforming scalar function @var{phi} as the last ## input. In such case @var{beta} = grad @var{phi} is assumed. ## ## If @var{phi} is a single scalar value @var{beta} is assumed to be 0 ## in the whole domain. ## ## @seealso{bim1a_axisymmetric_advection_diffusion, bim1a_axisymmetric_rhs, ## bim1a_axisymmetric_reaction, bim1a_axisymmetric_laplacian} ## @end deftypefn function A = bim1a_axisymmetric_advection_upwind (x, beta) ## Check input if nargin != 2 error("bim1a_axisymmetric_advection_upwind: wrong number of input parameters."); endif nnodes = length(x); nelem = nnodes-1; cm = reshape((x(1:end-1)+x(2:end))/2,[],1) if (length(beta) == 1) vk = 0;#zeros(nelem,1); elseif (length(beta) == nelem) vk = beta; elseif (length(beta) == nnodes) vk = diff(beta); else error("bim1a_axisymmetric_advection_upwind: coefficient beta has wrong dimensions."); endif bmk = (vk+abs(vk))/2 .* abs(cm); bpk = -(vk-abs(vk))/2 .* abs(cm); dm1 = [-bmk; NaN]; dp1 = [NaN; -bpk]; d0 = [bmk(1); bmk(2:end) + bpk(1:end-1); bpk(end)]; A = spdiags([dm1, d0, dp1],-1:1,nnodes,nnodes); endfunction %!test %! nn = 20; %! mesh = linspace(1,2,nn+1)'; %! D = 1; v = 0; sigma = 0; %! uex = @(r) exp(r); %! duexdr = @(r) uex(r); %! d2uexdr2 = @(r) uex(r); %! f = @(r,z) -D./r.*duexdr(r) - D.*d2uexdr2(r) ... %! + v./r .* uex(r) + v * duexdr(r) ... %! + sigma * uex(r); %! uex_left = uex(mesh(1)); uex_right = uex(mesh(end)); %! Ar = bim1a_axisymmetric_laplacian (mesh, D, 1); %! Adv = bim1a_axisymmetric_advection_upwind (mesh, v*ones(nn,1)); %! R = bim1a_axisymmetric_reaction (mesh, sigma, 1); %! M = Ar + Adv + R; %! M(1,:) *= 0; M(1,1) = 1; %! M(end,:) *= 0; M(end, end) = 1; %! rhs = bim1a_axisymmetric_rhs (mesh, 1, f(mesh)); %! rhs(1) = uex_left; rhs(end) = uex_right; %! uh = M \ rhs; %! assert(uh, uex(mesh), 1e-3); bim-1.1.6/inst/bim1a_axisymmetric_laplacian.m000066400000000000000000000047011426641764600212360ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo Porro ## author: Emanuela Abbate ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {@var{A}} = bim1a_axisymmetric_laplacian (@var{mesh},@var{epsilon},@var{kappa}) ## ## Build the standard finite element stiffness matrix for a diffusion ## problem in cylindrical coordinates with axisymmetric configuration. ## Rotational symmetry is assumed with respect to be the vertical ## axis r=0. Only grids that DO NOT contain r=0 are admissible. ## ##@example ##@group ## | |-------| OK |--|-----| NO! ## r=0 r=0 #@end group #@end example ## ## The equation taken into account is: ## ## - 1/r * (r * @var{epsilon} * @var{kappa} ( u' ))' = f ## ## where @var{epsilon} is an element-wise constant scalar function, ## while @var{kappa} is a piecewise linear conforming scalar function. ## ## @seealso{bim1a_axisymmetric_rhs, bim1a_axisymmetric_reaction, ## bim1a_axisymmetric_advection_diffusion, bim2a_laplacian, bim3a_laplacian} ## @end deftypefn function [A] = bim1a_axisymmetric_laplacian(mesh,epsilon,kappa) ## Check input if nargin != 3 error("bim1a_axisymmetric_laplacian: wrong number of input parameters."); elseif !isvector(mesh) error("bim1a_axisymmetric_laplacian: first argument is not a valid vector."); endif ## Input-type check inside bim1a_axisymmetric_advection_diffusion nnodes = length(mesh); nelem = nnodes - 1; A = bim1a_axisymmetric_advection_diffusion (mesh, epsilon, kappa, ones(nnodes,1), 0); endfunction bim-1.1.6/inst/bim1a_axisymmetric_reaction.m000066400000000000000000000107271426641764600211230ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo Porro ## author: Emanuela Abbate ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{C}]} = @ ## bim1a_axisymmetric_reaction(@var{mesh},@var{delta},@var{zeta}) ## ## Build the lumped finite element mass matrix for a diffusion ## problem in cylindrical coordinates with axisymmetric configuration. ## ## The equation taken into account is: ## ## @var{delta} * @var{zeta} * u = f ## ## where @var{delta} is an element-wise constant scalar function, while ## @var{zeta} is a piecewise linear conforming scalar function. ## ## @seealso{bim1a_axisymmetric_rhs, bim1a_axisymmetric_advection_diffusion, bim1a_axisymmetric_laplacian, ## bim2a_reaction, bim3a_reaction} ## @end deftypefn function [C] = bim1a_axisymmetric_reaction(mesh,delta,zeta) ## Check input if nargin != 3 error("bim1a_axisymmetric_reaction: wrong number of input parameters."); elseif !isvector(mesh) error("bim1a_axisymmetric_reaction: first argument is not a valid vector."); endif mesh = reshape(mesh,[],1); nnodes = length(mesh); nelems = nnodes-1; ## Turn scalar input to a vector of appropriate size if isscalar(delta) delta = delta*ones(nelems,1); endif if isscalar(zeta) zeta = zeta*ones(nnodes,1); endif if !( isvector(delta) && isvector(zeta) ) error("bim1a_axisymmetric_reaction: coefficients are not valid vectors."); elseif length(delta) != nelems error("bim1a_axisymmetric_reaction: length of delta is not equal to the number of elements."); elseif length(zeta) != nnodes error("bim1a_axisymmetric_reaction: length of zeta is not equal to the number of nodes."); endif h = (mesh(2:end)-mesh(1:end-1)).*delta; d0 = zeta.*[h(1)/2; (h(1:end-1)+h(2:end))/2; h(end)/2]; C = spdiags(d0.*abs(mesh), 0, nnodes,nnodes); endfunction %!test %! n = 100; %! mesh = linspace(0,1,n+1)'; %! cm = (mesh(1:end-1) + mesh(2:end))/2; %! uex = @(r) - r.^2 + 1; %! Nnodes = numel(mesh); %! Nelements = Nnodes-1; %! D = 1; v = cm; sigma = 1; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 0; %! delta = ones(Nelements,1); %! zeta = sigma*ones(Nnodes,1); %! f = @(r) 4*D + sigma*uex(r); %! rhs = bim1a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh)); %! S = bim1a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! R = bim1a_axisymmetric_reaction(mesh, delta, zeta); %! S += R; %! u = zeros(Nnodes,1); u(end) = uex(mesh(end)); %! u(1:end-1) = S(1:end-1,1:end-1)\(rhs(1:end-1) - S(1:end-1,end)*u(end)); %! assert(u,uex(mesh),1e-3) %!test %! n = 100; %! mesh = linspace(0,1,n+1)'; %! cm = (mesh(1:end-1) + mesh(2:end))/2; %! uex = @(r) - r.^2 + 1; %! Nnodes = numel(mesh); %! Nelements = Nnodes-1; %! D = 1; v = cm; sigma = 1; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D*v; %! delta = ones(Nelements,1); %! zeta = sigma*ones(Nnodes,1); %! f = @(r) 4*D + 2 - 4*r.^2 + sigma*uex(r); %! rhs = bim1a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh)); %! S = bim1a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! R = bim1a_axisymmetric_reaction(mesh, delta, zeta); %! S += R; %! u = zeros(Nnodes,1); u(end) = uex(mesh(end)); %! u(1:end-1) = S(1:end-1,1:end-1)\(rhs(1:end-1) - S(1:end-1,end)*u(end)); %! assert(u,uex(mesh),1e-3) %!test %! x = linspace(0,1,101); %! A = bim1a_axisymmetric_reaction(x,1,1); %! delta = ones(100,1); %! zeta = ones(101,1); %! B = bim1a_axisymmetric_reaction(x,delta,zeta); %! assert(A,B) bim-1.1.6/inst/bim1a_axisymmetric_rhs.m000066400000000000000000000052261426641764600201110ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo Porro ## author: Emanuela Abbate ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{b}]} = @ ## bim1a_rhs(@var{mesh},@var{f}, @var{g}) ## ## Build the finite element right-hand side of a diffusion problem ## employing mass-lumping. ## ## The equation taken into account is: ## ## @var{delta} * u = f * g ## ## where @var{f} is an element-wise constant scalar function, while ## @var{g} is a piecewise linear conforming scalar function. ## ## @seealso{bim1a_reaction, bim1a_advection_diffusion, bim1a_laplacian, ## bim2a_reaction, bim3a_reaction} ## @end deftypefn function b = bim1a_axisymmetric_rhs(mesh,f,g) ## Check input if nargin != 3 error("bim1a_rad_rhs: wrong number of input parameters."); elseif !isvector(mesh) error("bim1a_rad_rhs: first argument is not a valid vector."); endif mesh = reshape(mesh,[],1); nnodes = length(mesh); nelem = nnodes-1; ## Turn scalar input to a vector of appropriate size if isscalar(f) f = f*ones(nelem,1); endif if isscalar(g) g = g*ones(nnodes,1); endif if !( isvector(f) && isvector(g) ) error("bim1a_rad_rhs: coefficients are not valid vectors."); elseif (length(f) != nelem && length(f) != 1) error("bim1a_rad_rhs: length of f is not equal to the number of elements."); elseif (length(g) != nnodes && length(g) != 1) error("bim1a_rad_rhs: length of g is not equal to the number of nodes."); endif h = (mesh(2:end)-mesh(1:end-1)).*f; b = g.*[h(1)/2; (h(1:end-1)+h(2:end))/2; h(end)/2] .* abs(mesh); endfunction %!test %! x = linspace(0,1,101); %! A = bim1a_axisymmetric_rhs(x,1,1); %! delta = ones(100,1); %! zeta = ones(101,1); %! B = bim1a_axisymmetric_rhs(x,delta,zeta); %! assert(A,B) bim-1.1.6/inst/bim1a_laplacian.m000066400000000000000000000036321426641764600164420ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {@var{A}} = bim1a_laplacian (@var{mesh},@var{epsilon},@var{kappa}) ## ## Build the standard finite element stiffness matrix for a diffusion ## problem. ## ## The equation taken into account is: ## ## - (@var{epsilon} * @var{kappa} ( u' ))' = f ## ## where @var{epsilon} is an element-wise constant scalar function, ## while @var{kappa} is a piecewise linear conforming scalar function. ## ## @seealso{bim1a_rhs, bim1a_reaction, bim1a_advection_diffusion, ## bim2a_laplacian, bim3a_laplacian} ## @end deftypefn function [A] = bim1a_laplacian(mesh,epsilon,kappa) ## Check input if nargin != 3 error("bim1a_laplacian: wrong number of input parameters."); elseif !isvector(mesh) error("bim1a_laplacian: first argument is not a valid vector."); endif ## Input-type check inside bim1a_advection_diffusion nnodes = numel (mesh); nelem = nnodes - 1; A = bim1a_advection_diffusion (mesh, epsilon, kappa, ones(nnodes,1), 0); endfunction bim-1.1.6/inst/bim1a_reaction.m000066400000000000000000000051371426641764600163240ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{C}]} = @ ## bim1a_reaction(@var{mesh},@var{delta},@var{zeta}) ## ## Build the lumped finite element mass matrix for a diffusion ## problem. ## ## The equation taken into account is: ## ## @var{delta} * @var{zeta} * u = f ## ## where @var{delta} is an element-wise constant scalar function, while ## @var{zeta} is a piecewise linear conforming scalar function. ## ## @seealso{bim1a_rhs, bim1a_advection_diffusion, bim1a_laplacian, ## bim2a_reaction, bim3a_reaction} ## @end deftypefn function [C] = bim1a_reaction(mesh,delta,zeta) ## Check input if nargin != 3 error("bim1a_reaction: wrong number of input parameters."); elseif !isvector(mesh) error("bim1a_reaction: first argument is not a valid vector."); endif mesh = reshape (mesh, [], 1); nnodes = numel (mesh); nelems = nnodes - 1; ## Turn scalar input to a vector of appropriate size if isscalar(delta) delta = delta*ones(nelems,1); endif if isscalar(zeta) zeta = zeta*ones(nnodes,1); endif if !( isvector(delta) && isvector(zeta) ) error("bim1a_reaction: coefficients are not valid vectors."); elseif numel (delta) != nelems error("bim1a_reaction: length of delta is not equal to the number of elements."); elseif numel (zeta) != nnodes error("bim1a_reaction: length of zeta is not equal to the number of nodes."); endif h = (mesh(2:end)-mesh(1:end-1)).*delta; d0 = zeta.*[h(1)/2; (h(1:end-1)+h(2:end))/2; h(end)/2]; C = spdiags(d0, 0, nnodes,nnodes); endfunction %!test %! x = linspace(0,1,101); %! A = bim1a_reaction(x,1,1); %! delta = ones(100,1); %! zeta = ones(101,1); %! B = bim1a_reaction(x,delta,zeta); %! assert(A,B) bim-1.1.6/inst/bim1a_rhs.m000066400000000000000000000047511426641764600153150ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{b}]} = @ ## bim1a_rhs(@var{mesh},@var{f}, @var{g}) ## ## Build the finite element right-hand side of a diffusion problem ## employing mass-lumping. ## ## The equation taken into account is: ## ## @var{delta} * u = f * g ## ## where @var{f} is an element-wise constant scalar function, while ## @var{g} is a piecewise linear conforming scalar function. ## ## @seealso{bim1a_reaction, bim1a_advection_diffusion, bim1a_laplacian, ## bim2a_reaction, bim3a_reaction} ## @end deftypefn function b = bim1a_rhs(mesh,f,g) ## Check input if nargin != 3 error("bim1a_rhs: wrong number of input parameters."); elseif !isvector(mesh) error("bim1a_rhs: first argument is not a valid vector."); endif mesh = reshape(mesh,[],1); nnodes = numel (mesh); nelem = nnodes-1; ## Turn scalar input to a vector of appropriate size if isscalar(f) f = f*ones(nelem,1); endif if isscalar(g) g = g*ones(nnodes,1); endif if !( isvector(f) && isvector(g) ) error("bim1a_rhs: coefficients are not valid vectors."); elseif (numel (f) != nelem && numel (f) != 1) error("bim1a_rhs: length of f is not equal to the number of elements."); elseif (numel (g) != nnodes && numel (g) != 1) error("bim1a_rhs: length of g is not equal to the number of nodes."); endif h = (mesh(2:end)-mesh(1:end-1)).*f; b = g.*[h(1)/2; (h(1:end-1)+h(2:end))/2; h(end)/2]; endfunction %!test %! x = linspace(0,1,101); %! A = bim1a_rhs(x,1,1); %! delta = ones(100,1); %! zeta = ones(101,1); %! B = bim1a_rhs(x,delta,zeta); %! assert(A,B) bim-1.1.6/inst/bim1c_elem_to_nodes.m000066400000000000000000000054121426641764600173320ustar00rootroot00000000000000## Copyright (C) 2013 Carlo de Falco ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 3 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, see ## . ## -*- texinfo -*- ## ## @deftypefn {Function File} {@var{u_nod}} = bim1c_elem_to_nodes (@var{mesh}, @var{u_el}) ## @deftypefnx {Function File} {@var{u_nod}} = bim1c_elem_to_nodes (@var{m_el}, @var{u_el}) ## @deftypefnx {Function File} {[@var{u_nod}, @var{m_el}]} = bim1c_elem_to_nodes ( ... ) ## ## Compute interpolated values at nodes @var{u_nod} given values at element mid-points @var{u_el}. ## If called with more than one output, also return the interpolation matrix @var{m_el} such that ## @code{u_nod = m_el * u_el}. ## If repeatedly performing interpolation on the same mesh the matrix @var{m_el} obtained by a previous call ## to @code{bim1c_elem_to_nodes} may be passed as input to avoid unnecessary computations. ## ## @end deftypefn ## Author: Carlo de Falco ## Author: Matteo Porro ## Created: 2013-11-04 function [u_nod, m_el] = bim1c_elem_to_nodes (m, u_el) if (nargout > 1 ) if (isvector (m)) nel = numel (m) - 1; nnod = numel (m); m_el = spalloc (nnod, nel, 2 * nel); h = diff (m); for iel = 1:nel m_el([iel, iel+1], iel) = h(iel); endfor m_el = diag (sum (m_el, 2)) \ m_el; elseif (ismatrix (m)) m_el = m; else error (["bim1c_elem_to_nodes: first input ", ... "parameter is of incorrect type"]); endif u_nod = m_el * u_el; else if (isvector (m)) rhs = bim1a_rhs (m, u_el, 1); mass = bim1a_reaction (m, 1, 1); u_nod = full (mass \ rhs); elseif (ismatrix (m)) u_nod = m * u_el; else error (["bim1c_elem_to_nodes: first input ", ... "parameter is of incorrect type"]); endif endif endfunction %!test %! n = 10; msh = linspace (0, 1, n+1); %! nel = n; %! nnod = n+1; %! u_el = randn (nel, 1); %! un1 = bim1c_elem_to_nodes (msh, u_el); %! [un2, m] = bim1c_elem_to_nodes (msh, u_el); %! un3 = bim1c_elem_to_nodes (m, u_el); %! [un4, m] = bim1c_elem_to_nodes (m, u_el); %! assert (un1, un2, 1e-10) %! assert (un1, un3, 1e-10) %! assert (un1, un4, 1e-10) bim-1.1.6/inst/bim1c_norm.m000066400000000000000000000072261426641764600154760ustar00rootroot00000000000000## Copyright (C) 2006-2013 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Matteo Porro ## -*- texinfo -*- ## ## @deftypefn {Function File} {[@var{norm_u}]} = @ ## bim1c_norm(@var{mesh},@var{u},@var{norm_type}) ## ## Compute the @var{norm_type}-norm of function @var{u} on the domain described ## by the triangular grid @var{mesh}. ## ## The input function @var{u} can be either a piecewise linear conforming scalar ## function or an elementwise constant scalar or vector function. ## ## The string parameter @var{norm_type} can be one among 'L2', 'H1' and 'inf'. ## ## Should the input function be piecewise constant, the H1 norm will not be ## computed and the function will return an error message. ## ## @seealso{bim2c_norm, bim3c_norm} ## ## @end deftypefn function [norm_u] = bim1c_norm (m, u, norm_type) ## Check input if (nargin != 3) error ("bim1c_norm: wrong number of input parameters."); elseif (! isvector (m)) error ("bim1c_norm: first input is not a valid mesh."); endif nnodes = numel (m); nel = numel (m) - 1; if (isrow (u)) u = u'; endif if (isrow (m)) m = m'; endif if ((numel (u) != nnodes) && (numel (u) != nel)) error ("bim1c_norm: numel(u) != nnodes and numel(u) != nel."); endif if (! (strcmp (norm_type, 'L2') || strcmp (norm_type, 'inf') || strcmp (norm_type, 'H1'))) error ("bim1c_norm: invalid norm type parameter."); endif if (strcmp (norm_type,'inf')) norm_u = max (abs (u)); else if (numel (u) == nnodes) M = __mass_matrix__ (m); if (strcmp (norm_type, 'H1')) A = bim1a_laplacian (m, 1, 1); M += A; endif norm_u = sqrt(u' * M * u); else if (strcmp (norm_type, 'H1')) error (["bim1c_norm: cannot compute the ", ... "H1 norm of an elementwise constant function."]); endif norm_u = diff(m)' * u.^2; norm_u = sqrt (norm_u); endif endif endfunction function M = __mass_matrix__ (m) nnodes = numel(m); h = diff(m); d0 = 1/3*[h(1); h(1:end-1)+h(2:end); h(end)]; d1 = [0; 1/6*h]; dm1 = [1/6*h; 0]; M = spdiags([dm1 d0 d1], -1:1, nnodes, nnodes); endfunction %!test %!shared L, V, m %! L = rand (1); V = rand (1); m = linspace (0,1,5).^2; m *= L; %! u = V * ones (size (m))'; %! uinf = bim1c_norm (m, u, 'inf'); %! uL2 = bim1c_norm (m, u, 'L2'); %! uH1 = bim1c_norm (m, u, 'H1'); %! assert ([uinf, uL2, uH1], [V, V*sqrt(L), V*sqrt(L)], 1e-12); %!test %! u = V * m'; %! uinf = bim1c_norm (m, u, 'inf'); %! uL2 = bim1c_norm (m, u, 'L2'); %! uH1 = bim1c_norm (m, u, 'H1'); %! assert ([uinf, uL2, uH1], %! [L*V, V*sqrt(L^3/3), V*sqrt(L^3/3 + L)], %! 1e-12); %!test %! u = V * ones (size (diff (m)))'; %! uinf = bim1c_norm (m, u, 'inf'); %! uL2 = bim1c_norm (m, u, 'L2'); %! assert ([uinf, uL2], [V, V*sqrt(L)], 1e-12); bim-1.1.6/inst/bim2a_advection_diffusion.m000066400000000000000000000275211426641764600205440ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{A}]} = @ ## bim2a_advection_diffusion(@var{mesh},@var{alpha},@var{gamma},@var{eta},@var{beta}) ## ## Build the Scharfetter-Gummel stabilized stiffness matrix for a ## diffusion-advection problem. ## ## The equation taken into account is: ## ## - div (@var{alpha} * @var{gamma} (@var{eta} grad (u) - @var{beta} u )) = f ## ## where @var{alpha} is an element-wise constant scalar function, ## @var{eta} and @var{gamma} are piecewise linear conforming scalar ## functions, @var{beta} is an element-wise constant vector function. ## ## Instead of passing the vector field @var{beta} directly one can pass ## a piecewise linear conforming scalar function @var{phi} as the last ## input. In such case @var{beta} = grad @var{phi} is assumed. ## ## If @var{phi} is a single scalar value @var{beta} is assumed to be 0 ## in the whole domain. ## ## Example: ## @example ## mesh = msh2m_structured_mesh([0:1/3:1],[0:1/3:1],1,1:4); ## mesh = bim2c_mesh_properties(mesh); ## x = mesh.p(1,:)'; ## ## Dnodes = bim2c_unknowns_on_side(mesh,[2,4]); ## Nnodes = columns(mesh.p); ## Nelements = columns(mesh.t); ## Varnodes = setdiff(1:Nnodes,Dnodes); ## ## alpha = ones(Nelements,1); ## eta = .1*ones(Nnodes,1); ## beta = [ones(1,Nelements);zeros(1,Nelements)]; ## gamma = ones(Nnodes,1); ## f = bim2a_rhs(mesh,ones(Nnodes,1),ones(Nelements,1)); ## ## S = bim2a_advection_diffusion(mesh,alpha,gamma,eta,beta); ## u = zeros(Nnodes,1); ## uex = x - (exp(10*x)-1)/(exp(10)-1); ## ## u(Varnodes) = S(Varnodes,Varnodes)\f(Varnodes); ## ## assert(u,uex,1e-7) ## @end example ## ## @seealso{bim2a_rhs, bim2a_reaction, bim2c_mesh_properties} ## @end deftypefn function [A] = bim2a_advection_diffusion (mesh, alpha, gamma, eta, beta) ## Check input if nargin != 5 error("bim2a_advection_diffusion: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_advection_diffusion: first input is not a valid mesh structure."); endif nnodes = columns(mesh.p); nelem = columns(mesh.t); ## Turn scalar input to a vector of appropriate size if isscalar(alpha) alpha = alpha*ones(nelem,1); endif if isscalar(gamma) gamma = gamma*ones(nnodes,1); endif if isscalar(eta) eta = eta*ones(nnodes,1); endif if !( isvector(alpha) && isvector(gamma) && isvector(eta) ) error("bim2a_advection_diffusion: coefficients are not valid vectors."); elseif (numel (alpha) != nelem) error("bim2a_advection_diffusion: length of alpha is not equal to the number of elements."); elseif (numel (gamma) != nnodes) error("bim2a_advection_diffusion: length of gamma is not equal to the number of nodes."); elseif (numel (eta) != nnodes) error("bim2a_advection_diffusion: length of eta is not equal to the number of nodes."); endif alphaareak = reshape (alpha.*mesh.area,1,1,nelem); shg = mesh.shg(:,:,:); ## Build local Laplacian matrix Lloc = zeros(3,3,nelem); for inode = 1:3 for jnode = 1:3 ginode(inode,jnode,:) = mesh.t(inode,:); gjnode(inode,jnode,:) = mesh.t(jnode,:); Lloc(inode,jnode,:) = sum( shg(:,inode,:) .* shg(:,jnode,:),1) .* alphaareak; endfor endfor x = mesh.p(1,:); x = x(mesh.t(1:3,:)); y = mesh.p(2,:); y = y(mesh.t(1:3,:)); if all(size(beta)==1) v12 = 0; v23 = 0; v31 = 0; elseif all(size(beta)==[2,nelem]) v12 = beta(1,:) .* (x(2,:)-x(1,:)) + beta(2,:) .* (y(2,:)-y(1,:)); v23 = beta(1,:) .* (x(3,:)-x(2,:)) + beta(2,:) .* (y(3,:)-y(2,:)); v31 = beta(1,:) .* (x(1,:)-x(3,:)) + beta(2,:) .* (y(1,:)-y(3,:)); elseif all(size(beta)==[nnodes,1]) betaloc = beta(mesh.t(1:3,:)); v12 = betaloc(2,:)-betaloc(1,:); v23 = betaloc(3,:)-betaloc(2,:); v31 = betaloc(1,:)-betaloc(3,:); else error("bim2a_advection_diffusion: coefficient beta has wrong dimensions."); endif etaloc = eta(mesh.t(1:3,:)); eta12 = etaloc(2,:) - etaloc(1,:); eta23 = etaloc(3,:) - etaloc(2,:); eta31 = etaloc(1,:) - etaloc(3,:); etalocm1 = bimu_logm(etaloc(2,:),etaloc(3,:)); etalocm2 = bimu_logm(etaloc(3,:),etaloc(1,:)); etalocm3 = bimu_logm(etaloc(1,:),etaloc(2,:)); gammaloc = gamma(mesh.t(1:3,:)); geloc = gammaloc.*etaloc; gelocm1 = bimu_logm (geloc(2,:), geloc(3,:)); gelocm2 = bimu_logm (geloc(3,:), geloc(1,:)); gelocm3 = bimu_logm (geloc(1,:), geloc(2,:)); [bp12,bm12] = bimu_bernoulli ((v12 - eta12) ./ etalocm3); [bp23,bm23] = bimu_bernoulli ((v23 - eta23) ./ etalocm1); [bp31,bm31] = bimu_bernoulli ((v31 - eta31) ./ etalocm2); bp12 = reshape(gelocm3.*etalocm3.*bp12,1,1,nelem).*Lloc(1,2,:); bm12 = reshape(gelocm3.*etalocm3.*bm12,1,1,nelem).*Lloc(1,2,:); bp23 = reshape(gelocm1.*etalocm1.*bp23,1,1,nelem).*Lloc(2,3,:); bm23 = reshape(gelocm1.*etalocm1.*bm23,1,1,nelem).*Lloc(2,3,:); bp31 = reshape(gelocm2.*etalocm2.*bp31,1,1,nelem).*Lloc(3,1,:); bm31 = reshape(gelocm2.*etalocm2.*bm31,1,1,nelem).*Lloc(3,1,:); Sloc(1,1,:) = (-bm12-bp31)./reshape(etaloc(1,:),1,1,nelem); Sloc(1,2,:) = bp12./reshape(etaloc(2,:),1,1,nelem); Sloc(1,3,:) = bm31./reshape(etaloc(3,:),1,1,nelem); Sloc(2,1,:) = bm12./reshape(etaloc(1,:),1,1,nelem); Sloc(2,2,:) = (-bp12-bm23)./reshape(etaloc(2,:),1,1,nelem); Sloc(2,3,:) = bp23./reshape(etaloc(3,:),1,1,nelem); Sloc(3,1,:) = bp31./reshape(etaloc(1,:),1,1,nelem); Sloc(3,2,:) = bm23./reshape(etaloc(2,:),1,1,nelem); Sloc(3,3,:) = (-bm31-bp23)./reshape(etaloc(3,:),1,1,nelem); A = sparse(ginode(:),gjnode(:),Sloc(:)); endfunction %!test %! [mesh] = msh2m_structured_mesh([0:1/3:1],[0:1/3:1],1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! x = mesh.p(1,:)'; %! Dnodes = bim2c_unknowns_on_side(mesh,[2,4]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! alpha = ones(Nelements,1); %! eta = .1*ones(Nnodes,1); %! beta = [ones(1,Nelements);zeros(1,Nelements)]; %! gamma = ones(Nnodes,1); %! f = bim2a_rhs(mesh,ones(Nelements,1),ones(Nnodes,1)); %! S = bim2a_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); %! u(Varnodes) = S(Varnodes,Varnodes)\f(Varnodes); %! uex = x - (exp(10*x)-1)/(exp(10)-1); %! assert(u,uex,1e-7) %!test %! [mesh] = msh2m_structured_mesh([0:1/3:1],[0:1/3:1],1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! x = mesh.p(1,:)'; %! Dnodes = bim2c_unknowns_on_side(mesh,[2,4]); %! Nnodes = columns(mesh.p); Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! alpha = ones(Nelements,1); %! eta = .1*ones(Nnodes,1); %! beta = x; %! gamma = ones(Nnodes,1); %! f = bim2a_rhs(mesh,ones(Nelements,1),ones(Nnodes,1)); %! S = bim2a_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); %! u(Varnodes) = S(Varnodes,Varnodes)\f(Varnodes); %! uex = x - (exp(10*x)-1)/(exp(10)-1); %! assert(u,uex,1e-7) %!test %! [mesh] = msh2m_structured_mesh([0:1/3:1],[0:1/3:1],1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! x = mesh.p(1,:)'; %! Dnodes = bim2c_unknowns_on_side(mesh,[2,4]); %! Nnodes = columns(mesh.p); Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! alpha = 10*ones(Nelements,1); %! eta = .01*ones(Nnodes,1); %! beta = x/10; %! gamma = ones(Nnodes,1); %! f = bim2a_rhs(mesh,ones(Nelements,1),ones(Nnodes,1)); %! S = bim2a_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); %! u(Varnodes) = S(Varnodes,Varnodes)\f(Varnodes); %! uex = x - (exp(10*x)-1)/(exp(10)-1); %! assert(u,uex,1e-7) %!test %! [mesh] = msh2m_structured_mesh([0:1/3:1],[0:1/3:1],1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! x = mesh.p(1,:)'; %! Dnodes = bim2c_unknowns_on_side(mesh,[2,4]); %! Nnodes = columns(mesh.p); Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! alpha = 10*ones(Nelements,1); eta = .001*ones(Nnodes,1); %! beta = x/100; %! gamma = 10*ones(Nnodes,1); %! f = bim2a_rhs(mesh,ones(Nelements,1),ones(Nnodes,1)); %! S = bim2a_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); %! u(Varnodes) = S(Varnodes,Varnodes)\f(Varnodes); %! uex = x - (exp(10*x)-1)/(exp(10)-1); %! assert(u,uex,1e-7) %!test %! [mesh] = msh2m_structured_mesh([0:1/1e3:1],[0:1/2:1],1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! x = mesh.p(1,:)'; %! Dnodes = bim2c_unknowns_on_side(mesh,[2,4]); %! Nnodes = columns(mesh.p); Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! alpha = 3*ones(Nelements,1); eta = x+1; %! beta = [ones(1,Nelements);zeros(1,Nelements)]; %! gamma = 2*x; %! ff = 2*(6*x.^2+6*x) - (6*x+6).*(1-2*x)+6*(x-x.^2); %! f = bim2a_rhs(mesh,ones(Nelements,1),ff); %! S = bim2a_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); %! u(Varnodes) = S(Varnodes,Varnodes)\f(Varnodes); %! uex = x - x.^2; %! assert(u,uex,5e-3) %!test %! [mesh] = msh2m_structured_mesh([0:1/1e3:1],[0:1/2:1],1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! x = mesh.p(1,:)'; %! Dnodes = bim2c_unknowns_on_side(mesh,[2,4]); %! Nnodes = columns(mesh.p); Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! alpha = ones(Nelements,1); eta = ones(Nnodes,1); %! beta = 0; %! gamma = x+1; %! ff = 4*x+1; %! f = bim2a_rhs(mesh,ones(Nelements,1),ff); %! S = bim2a_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); %! u(Varnodes) = S(Varnodes,Varnodes)\f(Varnodes); %! uex = x - x.^2; %! assert(u,uex,1e-7) %!test %! [mesh] = msh2m_structured_mesh([0:.1:1],[0:.1:1],1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! x = mesh.p(1,:)';y = mesh.p(2,:)'; %! Dnodes = bim2c_unknowns_on_side(mesh,[1:4]); %! Nnodes = columns(mesh.p); Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! alpha = ones(Nelements,1); diff = 1e-2; eta=diff*ones(Nnodes,1); %! beta =[ones(1,Nelements);ones(1,Nelements)]; %! gamma = x*0+1; %! ux = y.*(1-exp((y-1)/diff)) .* (1-exp((x-1)/diff)-x.*exp((x-1)/diff)/diff); %! uy = x.*(1-exp((x-1)/diff)) .* (1-exp((y-1)/diff)-y.*exp((y-1)/diff)/diff); %! uxx = y.*(1-exp((y-1)/diff)) .* (-2*exp((x-1)/diff)/diff-x.*exp((x-1)/diff)/(diff^2)); %! uyy = x.*(1-exp((x-1)/diff)) .* (-2*exp((y-1)/diff)/diff-y.*exp((y-1)/diff)/(diff^2)); %! ff = -diff*(uxx+uyy)+ux+uy; %! f = bim2a_rhs(mesh,ones(Nelements,1),ff); %! S = bim2a_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); %! u(Varnodes) = S(Varnodes,Varnodes)\f(Varnodes); %! uex = x.*y.*(1-exp((x-1)/diff)).*(1-exp((y-1)/diff)); %! assert(u,uex,1e-7) %!test %! [mesh] = msh2m_structured_mesh([0:.1:1],[0:.1:1],1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! x = mesh.p(1,:)'; y = mesh.p(2,:)'; %! Dnodes = bim2c_unknowns_on_side(mesh,[1:4]); %! Nnodes = columns(mesh.p); Nelements = columns(mesh.t); %! alpha = ones(Nelements,1); eta=ones(Nnodes,1); %! beta = 0; %! gamma = ones(Nnodes,1); %! A = bim2a_advection_diffusion(mesh,1,1,1,0); %! B = bim2a_advection_diffusion(mesh,alpha,gamma,eta,beta); %! assert(A,B) bim-1.1.6/inst/bim2a_advection_upwind.m000066400000000000000000000075321426641764600200640ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{A}]} = @ ## bim2a_advection_upwind (@var{mesh}, @var{beta}) ## ## Build the UW stabilized stiffness matrix for an advection problem. ## ## The equation taken into account is: ## ## div (@var{beta} u) = f ## ## where @var{beta} is an element-wise constant vector function. ## ## Instead of passing the vector field @var{beta} directly one can pass ## a piecewise linear conforming scalar function @var{phi} as the last ## input. In such case @var{beta} = grad @var{phi} is assumed. ## ## If @var{phi} is a single scalar value @var{beta} is assumed to be 0 ## in the whole domain. ## ## @seealso{bim2a_rhs, bim2a_reaction, bim2c_mesh_properties} ## @end deftypefn function A = bim2a_advection_upwind (mesh, beta) ## Check input if nargin != 2 error("bim2a_advection_upwind: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_advection_upwind: first input is not a valid mesh structure."); endif nnodes = columns(mesh.p); nelem = columns(mesh.t); alphaareak = reshape (mesh.area, 1, 1, nelem); shg = mesh.shg(:,:,:); ## Build local Laplacian matrix Lloc = zeros(3,3,nelem); for inode = 1:3 for jnode = 1:3 ginode(inode,jnode,:) = mesh.t(inode,:); gjnode(inode,jnode,:) = mesh.t(jnode,:); Lloc(inode,jnode,:) = sum( shg(:,inode,:) .* shg(:,jnode,:),1) .* alphaareak; endfor endfor x = mesh.p(1,:); x = x(mesh.t(1:3,:)); y = mesh.p(2,:); y = y(mesh.t(1:3,:)); if all(size(beta)==1) v12 = 0; v23 = 0; v31 = 0; elseif all(size(beta)==[2,nelem]) v12 = beta(1,:) .* (x(2,:)-x(1,:)) + beta(2,:) .* (y(2,:)-y(1,:)); v23 = beta(1,:) .* (x(3,:)-x(2,:)) + beta(2,:) .* (y(3,:)-y(2,:)); v31 = beta(1,:) .* (x(1,:)-x(3,:)) + beta(2,:) .* (y(1,:)-y(3,:)); elseif all(size(beta)==[nnodes,1]) betaloc = beta(mesh.t(1:3,:)); v12 = betaloc(2,:)-betaloc(1,:); v23 = betaloc(3,:)-betaloc(2,:); v31 = betaloc(1,:)-betaloc(3,:); else error("bim2a_advection_upwind: coefficient beta has wrong dimensions."); endif [bp12, bm12] = deal (- (v12 - abs (v12))/2, (v12 + abs (v12))/2); [bp23, bm23] = deal (- (v23 - abs (v23))/2, (v23 + abs (v23))/2); [bp31, bm31] = deal (- (v31 - abs (v31))/2, (v31 + abs (v31))/2); bp12 = reshape(bp12,1,1,nelem).*Lloc(1,2,:); bm12 = reshape(bm12,1,1,nelem).*Lloc(1,2,:); bp23 = reshape(bp23,1,1,nelem).*Lloc(2,3,:); bm23 = reshape(bm23,1,1,nelem).*Lloc(2,3,:); bp31 = reshape(bp31,1,1,nelem).*Lloc(3,1,:); bm31 = reshape(bm31,1,1,nelem).*Lloc(3,1,:); Sloc(1,1,:) = (-bm12-bp31); Sloc(1,2,:) = bp12; Sloc(1,3,:) = bm31; Sloc(2,1,:) = bm12; Sloc(2,2,:) = (-bp12-bm23); Sloc(2,3,:) = bp23; Sloc(3,1,:) = bp31; Sloc(3,2,:) = bm23; Sloc(3,3,:) = (-bm31-bp23); A = sparse(ginode(:), gjnode(:), Sloc(:)); endfunction bim-1.1.6/inst/bim2a_axisymmetric_advection_diffusion.m000066400000000000000000000340661426641764600233440ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo porro ## author: Emanuela Abbate ## -*- texinfo -*- ## @deftypefn {Function File} @ ## {[@var{A}]} = @ ## bim2a_axisymmetric_advection_diffusion(@var{mesh},@var{alpha},@var{gamma},@var{eta},@var{beta}) ## ## Build the Scharfetter-Gummel stabilized stiffness matrix for a ## diffusion-advection problem in cylindrical coordinates with axisymmetric ## configuration. Rotational symmetry is assumed with respect to be the vertical ## axis r=0. Only plane geometries that DO NOT intersect the symmetry axis ## are admitted. ## ##@example ##@group ## | ____ _|____ ## | | \ \ | | ## z | | \ OK \| | NO! ## | |______\ |\___| ## | r | #@end group #@end example ## ## The equation taken into account is: ## ## 1/r * d(r * Fr)/dr + dFz/dz = f ## ## with ## ## F = [Fr, Fz]' = - @var{alpha} * @var{gamma} ( @var{eta} grad (u) - @var{beta} u ) ## ## where @var{alpha} is an element-wise constant scalar function, ## @var{eta} and @var{gamma} are piecewise linear conforming scalar ## functions, @var{beta} is an element-wise constant vector function. ## ## Instead of passing the vector field @var{beta} directly, one can pass ## a piecewise linear conforming scalar function @var{phi} as the last ## input. In such case @var{beta} = grad @var{phi} is assumed. ## ## If @var{phi} is a single scalar value @var{beta} is assumed to be 0 ## in the whole domain. ## ## @seealso{bim2a_axisymmetric_rhs, bim2a_axisymmetric_reaction, ## bim2a_advection_diffusion, bim2c_mesh_properties} ## @end deftypefn function [A] = bim2a_axisymmetric_advection_diffusion (mesh, alpha, gamma, eta, beta) ## Check input if nargin != 5 error("bim2a_axisymmetric_advection_diffusion: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_axisymmetric_advection_diffusion: first input is not a valid mesh structure."); elseif !(all(mesh.p(1,:) >= 0) || all(mesh.p(1,:) <= 0)) error("bim2a_axisymmetric_advection_diffusion: the input mesh cannot intersect the rotation axis r=0."); endif nnodes = columns(mesh.p); nelem = columns(mesh.t); ## Turn scalar input to a vector of appropriate size if isscalar(alpha) alpha = alpha*ones(nelem,1); endif if isscalar(gamma) gamma = gamma*ones(nnodes,1); endif if isscalar(eta) eta = eta*ones(nnodes,1); endif if !( isvector(alpha) && isvector(gamma) && isvector(eta) ) error("bim2a_axisymmetric_advection_diffusion: coefficients are not valid vectors."); elseif length(alpha) != nelem error("bim2a_axisymmetric_advection_diffusion: length of alpha is not equal to the number of elements."); elseif length(gamma) != nnodes error("bim2a_axisymmetric_advection_diffusion: length of gamma is not equal to the number of nodes."); elseif length(eta) != nnodes error("bim2a_axisymmetric_advection_diffusion: length of eta is not equal to the number of nodes."); endif x = abs(mesh.p(1,:)); x = x(mesh.t(1:3,:)); y = mesh.p(2,:); y = y(mesh.t(1:3,:)); alphaareak = reshape (alpha.*mesh.area,1,1,nelem); shg = mesh.shg(:,:,:); ## Build local Laplacian matrix Lloc = zeros(3,3,nelem); for inode = 1:3 for jnode = 1:3 ginode(inode,jnode,:) = mesh.t(inode,:); gjnode(inode,jnode,:) = mesh.t(jnode,:); Lloc(inode,jnode,:) = sum( shg(:,inode,:) .* shg(:,jnode,:),1) .* alphaareak; endfor endfor if all(size(beta)==1) v12 = 0; v23 = 0; v31 = 0; elseif all(size(beta)==[2,nelem]) v12 = beta(1,:) .* (x(2,:)-x(1,:)) + beta(2,:) .* (y(2,:)-y(1,:)); v23 = beta(1,:) .* (x(3,:)-x(2,:)) + beta(2,:) .* (y(3,:)-y(2,:)); v31 = beta(1,:) .* (x(1,:)-x(3,:)) + beta(2,:) .* (y(1,:)-y(3,:)); elseif all(size(beta)==[nnodes,1]) betaloc = beta(mesh.t(1:3,:)); v12 = betaloc(2,:)-betaloc(1,:); v23 = betaloc(3,:)-betaloc(2,:); v31 = betaloc(1,:)-betaloc(3,:); else error("bim2a_axisymmetric_advection_diffusion: coefficient beta has wrong dimensions."); endif etaloc = eta(mesh.t(1:3,:)); eta12 = etaloc(2,:) - etaloc(1,:); eta23 = etaloc(3,:) - etaloc(2,:); eta31 = etaloc(1,:) - etaloc(3,:); r12 = (x(2,:) + x(1,:)) / 2; r23 = (x(3,:) + x(2,:)) / 2; r31 = (x(1,:) + x(3,:)) / 2; etalocm1 = bimu_logm(etaloc(2,:),etaloc(3,:)); etalocm2 = bimu_logm(etaloc(3,:),etaloc(1,:)); etalocm3 = bimu_logm(etaloc(1,:),etaloc(2,:)); gammaloc = gamma(mesh.t(1:3,:)); geloc = gammaloc.*etaloc; gelocm1 = bimu_logm (geloc(2,:), geloc(3,:)); gelocm2 = bimu_logm (geloc(3,:), geloc(1,:)); gelocm3 = bimu_logm (geloc(1,:), geloc(2,:)); [bp12,bm12] = bimu_bernoulli ((v12 - eta12) ./ etalocm3); [bp23,bm23] = bimu_bernoulli ((v23 - eta23) ./ etalocm1); [bp31,bm31] = bimu_bernoulli ((v31 - eta31) ./ etalocm2); bp12 = reshape(r12.*gelocm3.*etalocm3.*bp12,1,1,nelem).*Lloc(1,2,:); bm12 = reshape(r12.*gelocm3.*etalocm3.*bm12,1,1,nelem).*Lloc(1,2,:); bp23 = reshape(r23.*gelocm1.*etalocm1.*bp23,1,1,nelem).*Lloc(2,3,:); bm23 = reshape(r23.*gelocm1.*etalocm1.*bm23,1,1,nelem).*Lloc(2,3,:); bp31 = reshape(r31.*gelocm2.*etalocm2.*bp31,1,1,nelem).*Lloc(3,1,:); bm31 = reshape(r31.*gelocm2.*etalocm2.*bm31,1,1,nelem).*Lloc(3,1,:); Sloc(1,1,:) = (-bm12-bp31)./reshape(etaloc(1,:),1,1,nelem); Sloc(1,2,:) = bp12./reshape(etaloc(2,:),1,1,nelem); Sloc(1,3,:) = bm31./reshape(etaloc(3,:),1,1,nelem); Sloc(2,1,:) = bm12./reshape(etaloc(1,:),1,1,nelem); Sloc(2,2,:) = (-bp12-bm23)./reshape(etaloc(2,:),1,1,nelem); Sloc(2,3,:) = bp23./reshape(etaloc(3,:),1,1,nelem); Sloc(3,1,:) = bp31./reshape(etaloc(1,:),1,1,nelem); Sloc(3,2,:) = bm23./reshape(etaloc(2,:),1,1,nelem); Sloc(3,3,:) = (-bm31-bp23)./reshape(etaloc(3,:),1,1,nelem); A = sparse(ginode(:),gjnode(:),Sloc(:)); endfunction %!test %! n = 3; %! [mesh] = msh2m_structured_mesh(linspace(1,2,n+1),linspace(0,1,n+1),1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! uex = @(r,z) exp(r); %! duexdr = @(r,z) uex(r,z); %! d2uexdr2 = @(r,z) uex(r,z); %! duexdz = @(r,z) 0*uex(r,z); %! d2uexdz2 = @(r,z) 0*uex(r,z); %! Dnodes = bim2c_unknowns_on_side(mesh,[2,4]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! D = 1; vr = 1; vz = 0; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D*[vr*ones(1,Nelements); vz*ones(1,Nelements)]; %! f = @(r,z) -D./r.*duexdr(r,z) - D.*d2uexdr2(r,z) ... %! + vr./r .* uex(r,z) + vr * duexdr(r,z) ... %! - D.*d2uexdz2(r,z) + vz * duexdz(r,z); %! rhs = bim2a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh.p(1,:), mesh.p(2,:))); %! S = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); u(Dnodes) = uex(mesh.p(1,Dnodes), mesh.p(2,Dnodes)); %! u(Varnodes) = S(Varnodes,Varnodes)\(rhs(Varnodes) - S(Varnodes,Dnodes)*u(Dnodes)); %! assert(u,uex(mesh.p(1,:), mesh.p(2,:))',1e-7) %!test %! n = 20; %! [mesh] = msh2m_structured_mesh(linspace(1,2,n+1),linspace(0,1,n+1),1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! uex = @(r,z) exp(r) .* exp(1-z); %! duexdr = @(r,z) uex(r,z); %! d2uexdr2 = @(r,z) uex(r,z); %! duexdz = @(r,z) -uex(r,z); %! d2uexdz2 = @(r,z) uex(r,z); %! Dnodes = bim2c_unknowns_on_side(mesh,[1,2,3,4]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! D = 1; vr = 1; vz = 1; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D*[vr*ones(1,Nelements); vz*ones(1,Nelements)]; %! f = @(r,z) -D./r.*duexdr(r,z) - D.*d2uexdr2(r,z) ... %! + vr./r .* uex(r,z) + vr * duexdr(r,z) ... %! - D.*d2uexdz2(r,z) + vz * duexdz(r,z); %! rhs = bim2a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh.p(1,:), mesh.p(2,:))); %! S = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); u(Dnodes) = uex(mesh.p(1,Dnodes), mesh.p(2,Dnodes)); %! u(Varnodes) = S(Varnodes,Varnodes)\(rhs(Varnodes) - S(Varnodes,Dnodes)*u(Dnodes)); %! assert(u,uex(mesh.p(1,:), mesh.p(2,:))',1e-3) %!test %! n = 10; %! [mesh] = msh2m_structured_mesh(linspace(1,2,n+1),linspace(0,1,n+1),1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! uex = @(r,z) exp(r) .* exp(1-z); %! duexdr = @(r,z) uex(r,z); %! d2uexdr2 = @(r,z) uex(r,z); %! duexdz = @(r,z) -uex(r,z); %! d2uexdz2 = @(r,z) uex(r,z); %! Dnodes = bim2c_unknowns_on_side(mesh,[1,2,3,4]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! D = 1; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D * mesh.p(1,:)'; %! f = @(r,z) -D./r.*duexdr(r,z) - D.*d2uexdr2(r,z) ... %! + 1./r .* uex(r,z) + duexdr(r,z) ... %! - D.*d2uexdz2(r,z); %! rhs = bim2a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh.p(1,:), mesh.p(2,:))); %! S = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); u(Dnodes) = uex(mesh.p(1,Dnodes), mesh.p(2,Dnodes)); %! u(Varnodes) = S(Varnodes,Varnodes)\(rhs(Varnodes) - S(Varnodes,Dnodes)*u(Dnodes)); %! assert(u,uex(mesh.p(1,:), mesh.p(2,:))',1e-3) %!test %! n = 10; %! [mesh] = msh2m_structured_mesh(linspace(1,2,n+1),linspace(0,1,n+1),1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! uex = @(r,z) exp(r) .* exp(1-z); %! duexdr = @(r,z) uex(r,z); %! d2uexdr2 = @(r,z) uex(r,z); %! duexdz = @(r,z) -uex(r,z); %! d2uexdz2 = @(r,z) uex(r,z); %! Dnodes = bim2c_unknowns_on_side(mesh,[1,2,3,4]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! D = 1; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D * 1/2*(mesh.p(1,:)').^2; %! f = @(r,z) 1./r.*(1+r).*(r-D) .* uex(r,z); %! rhs = bim2a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh.p(1,:), mesh.p(2,:))); %! S = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); u(Dnodes) = uex(mesh.p(1,Dnodes), mesh.p(2,Dnodes)); %! u(Varnodes) = S(Varnodes,Varnodes)\(rhs(Varnodes) - S(Varnodes,Dnodes)*u(Dnodes)); %! assert(u,uex(mesh.p(1,:), mesh.p(2,:))',1e-3) %!test %! n = 3; %! [mesh] = msh2m_structured_mesh(linspace(-2,-1,n+1),linspace(0,1,n+1),1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! uex = @(r,z) exp(r); %! duexdr = @(r,z) uex(r,z); %! d2uexdr2 = @(r,z) uex(r,z); %! duexdz = @(r,z) 0*uex(r,z); %! d2uexdz2 = @(r,z) 0*uex(r,z); %! Dnodes = bim2c_unknowns_on_side(mesh,[2,4]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! D = 1; vr = 1; vz = 0; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D*[vr*ones(1,Nelements); vz*ones(1,Nelements)]; %! f = @(r,z) -D./r.*duexdr(r,z) - D.*d2uexdr2(r,z) ... %! + vr./r .* uex(r,z) + vr * duexdr(r,z) ... %! - D.*d2uexdz2(r,z) + vz * duexdz(r,z); %! rhs = bim2a_axisymmetric_rhs(mesh, ones(Nelements,1), f(abs(mesh.p(1,:)), mesh.p(2,:))); %! S = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); u(Dnodes) = uex(abs(mesh.p(1,Dnodes)), mesh.p(2,Dnodes)); %! u(Varnodes) = S(Varnodes,Varnodes)\(rhs(Varnodes) - S(Varnodes,Dnodes)*u(Dnodes)); %! assert(u,uex(abs(mesh.p(1,:)), mesh.p(2,:))',1e-7) %!test %! n = 10; %! [mesh] = msh2m_structured_mesh(linspace(-2,-1,n+1),linspace(0,1,n+1),1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! uex = @(r,z) exp(r) .* exp(1-z); %! duexdr = @(r,z) uex(r,z); %! d2uexdr2 = @(r,z) uex(r,z); %! duexdz = @(r,z) -uex(r,z); %! d2uexdz2 = @(r,z) uex(r,z); %! Dnodes = bim2c_unknowns_on_side(mesh,[1,2,3,4]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! D = 1; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D * 1/2*(mesh.p(1,:)').^2; %! f = @(r,z) 1./r.*(1+r).*(r-D) .* uex(r,z); %! rhs = bim2a_axisymmetric_rhs(mesh, ones(Nelements,1), f(abs(mesh.p(1,:)), mesh.p(2,:))); %! S = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! u = zeros(Nnodes,1); u(Dnodes) = uex(abs(mesh.p(1,Dnodes)), mesh.p(2,Dnodes)); %! u(Varnodes) = S(Varnodes,Varnodes)\(rhs(Varnodes) - S(Varnodes,Dnodes)*u(Dnodes)); %! assert(u,uex(abs(mesh.p(1,:)), mesh.p(2,:))',1e-3) %!test %! [mesh] = msh2m_structured_mesh([0:.1:1],[0:.1:1],1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! x = mesh.p(1,:)'; y = mesh.p(2,:)'; %! Dnodes = bim2c_unknowns_on_side(mesh,[1:4]); %! Nnodes = columns(mesh.p); Nelements = columns(mesh.t); %! alpha = ones(Nelements,1); eta=ones(Nnodes,1); %! beta = 0; %! gamma = ones(Nnodes,1); %! A = bim2a_axisymmetric_advection_diffusion(mesh,1,1,1,0); %! B = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! assert(A,B) bim-1.1.6/inst/bim2a_axisymmetric_advection_upwind.m000066400000000000000000000107601426641764600226570ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo porro ## author: Emanuela Abbate ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{A}]} = @ ## bim2a_axisymmetric_advection_upwind (@var{mesh}, @var{beta}) ## ## Build the Upwind stabilized stiffness matrix for an advection problem ## in cylindrical coordinates with axisymmetric configuration. ## ## The equation taken into account is: ## ## 1/r * d/dr (r * @var{beta}_r u) + d/dz (@var{beta}_z u) = f ## ## where @var{beta} is an element-wise constant vector function. ## ## Instead of passing the vector field @var{beta} directly one can pass ## a piecewise linear conforming scalar function @var{phi} as the last ## input. In such case @var{beta} = grad @var{phi} is assumed. ## ## If @var{phi} is a single scalar value @var{beta} is assumed to be 0 ## in the whole domain. ## ## @seealso{bim2a_axisymmetric_rhs, bim2a_axisymmetric_reaction, ## bim2a_axisymmetric_advection_diffusion, bim2c_mesh_properties} ## @end deftypefn function A = bim2a_axisymmetric_advection_upwind (mesh, beta) ## Check input if nargin != 2 error("bim2a_axisymmetric_advection_upwind: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_axisymmetric_advection_upwind: first input is not a valid mesh structure."); elseif !(all(mesh.p(1,:) >= 0) || all(mesh.p(1,:) <= 0)) error("bim2a_axisymmetric_advection_upwind: the input mesh cannot intersect the rotation axis r=0."); endif nnodes = columns(mesh.p); nelem = columns(mesh.t); x = abs (mesh.p(1,:)); x = x(mesh.t(1:3,:)); y = mesh.p(2,:); y = y(mesh.t(1:3,:)); alphaareak = reshape (mesh.area, 1, 1, nelem); shg = mesh.shg(:,:,:); ## Build local Laplacian matrix Lloc = zeros(3,3,nelem); for inode = 1:3 for jnode = 1:3 ginode(inode,jnode,:) = mesh.t(inode,:); gjnode(inode,jnode,:) = mesh.t(jnode,:); Lloc(inode,jnode,:) = sum( shg(:,inode,:) .* shg(:,jnode,:),1) .* alphaareak; endfor endfor if all(size(beta)==1) v12 = 0; v23 = 0; v31 = 0; elseif all(size(beta)==[2,nelem]) v12 = beta(1,:) .* (x(2,:)-x(1,:)) + beta(2,:) .* (y(2,:)-y(1,:)); v23 = beta(1,:) .* (x(3,:)-x(2,:)) + beta(2,:) .* (y(3,:)-y(2,:)); v31 = beta(1,:) .* (x(1,:)-x(3,:)) + beta(2,:) .* (y(1,:)-y(3,:)); elseif all(size(beta)==[nnodes,1]) betaloc = beta(mesh.t(1:3,:)); v12 = betaloc(2,:)-betaloc(1,:); v23 = betaloc(3,:)-betaloc(2,:); v31 = betaloc(1,:)-betaloc(3,:); else error("bim2a_axisymmetric_advection_upwind: coefficient beta has wrong dimensions."); endif [bp12, bm12] = deal (- (v12 - abs (v12))/2, (v12 + abs (v12))/2); [bp23, bm23] = deal (- (v23 - abs (v23))/2, (v23 + abs (v23))/2); [bp31, bm31] = deal (- (v31 - abs (v31))/2, (v31 + abs (v31))/2); r12 = (x(2,:) + x(1,:)) / 2; r23 = (x(3,:) + x(2,:)) / 2; r31 = (x(1,:) + x(3,:)) / 2; bp12 = reshape(r12 .* bp12,1,1,nelem).*Lloc(1,2,:); bm12 = reshape(r12 .* bm12,1,1,nelem).*Lloc(1,2,:); bp23 = reshape(r23 .* bp23,1,1,nelem).*Lloc(2,3,:); bm23 = reshape(r23 .* bm23,1,1,nelem).*Lloc(2,3,:); bp31 = reshape(r31 .* bp31,1,1,nelem).*Lloc(3,1,:); bm31 = reshape(r31 .* bm31,1,1,nelem).*Lloc(3,1,:); Sloc(1,1,:) = (-bm12-bp31); Sloc(1,2,:) = bp12; Sloc(1,3,:) = bm31; Sloc(2,1,:) = bm12; Sloc(2,2,:) = (-bp12-bm23); Sloc(2,3,:) = bp23; Sloc(3,1,:) = bp31; Sloc(3,2,:) = bm23; Sloc(3,3,:) = (-bm31-bp23); A = sparse(ginode(:), gjnode(:), Sloc(:)); endfunction bim-1.1.6/inst/bim2a_axisymmetric_boundary_mass.m000066400000000000000000000144731426641764600221700ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo porro ## author: Emanuela Abbate ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{M}]} = @ ## bim2a_axisymmetric_boundary_mass(@var{mesh},@var{sidelist},@var{nodelist}) ## ## Build the lumped boundary mass matrix needed to apply Robin and Neumann ## boundary conditions in a problem in cylindrical coordinates with ## axisymmetric configuration. ## ## The vector @var{sidelist} contains the list of the side edges ## contributing to the mass matrix. ## ## The optional argument @var{nodelist} contains the list of the ## degrees of freedom on the boundary. ## ## @seealso{bim2a_axisymmetric_rhs, bim2a_axisymmetric_advection_diffusion, ## bim2a_axisymmetric_laplacian, bim2a_axisymmetric_reaction, bim2a_boundary_mass} ## @end deftypefn function [M] = bim2a_axisymmetric_boundary_mass(mesh,sidelist,nodelist) ## Check input if (nargin > 3) error ("bim2a_axisymmetric_boundary_mass: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_axisymmetric_boundary_mass: first input is not a valid mesh structure."); elseif !( isvector(sidelist) && isnumeric(sidelist) ) error("bim2a_axisymmetric_boundary_mass: second input is not a valid numeric vector."); elseif !(all(mesh.p(1,:) >= 0) || all(mesh.p(1,:) <= 0)) error("bim2a_axisymmetric_boundary_mass: the input mesh cannot intersect the rotation axis r=0."); endif if (nargin < 3) [nodelist] = bim2c_unknowns_on_side(mesh,sidelist); endif r = abs (mesh.p(1,nodelist)); edges = []; for ie = sidelist edges = [ edges, mesh.e([1:2 5],mesh.e(5,:)==ie)]; endfor l = sqrt((mesh.p(1,edges(1,:))-mesh.p(1,edges(2,:))).^2 + (mesh.p(2,edges(1,:))-mesh.p(2,edges(2,:))).^2); dd = zeros(size(nodelist)); for in = 1:length(nodelist) dd (in) = ( sum(r(in).*l(edges(1,:)==nodelist(in))) ... + sum(r(in).*l(edges(2,:)==nodelist(in))) )/2; endfor M = sparse(diag(dd)); endfunction %!test %! n = 3; %! [mesh] = msh2m_structured_mesh(linspace(1,2,n+1),linspace(0,1,n+1),1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! uex = @(r,z) exp(r); %! duexdr = @(r,z) uex(r,z); %! d2uexdr2 = @(r,z) uex(r,z); %! duexdz = @(r,z) 0*uex(r,z); %! d2uexdz2 = @(r,z) 0*uex(r,z); %! Rnodesr = bim2c_unknowns_on_side(mesh,[2]); %! Rnodesl = bim2c_unknowns_on_side(mesh,[4]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! D = 1; vr = 1; vz = 0; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D*[vr*ones(1,Nelements); vz*ones(1,Nelements)]; %! f = @(r,z) -D./r.*duexdr(r,z) - D.*d2uexdr2(r,z) ... %! + vr./r .* uex(r,z) + vr * duexdr(r,z) ... %! - D.*d2uexdz2(r,z) + vz * duexdz(r,z); %! gr = @(r,z) uex(r,z) - 1 * (-D*duexdr(r,z) + vr*uex(r,z)); %! gl = @(r,z) uex(r,z) - (-1) * (-D*duexdr(r,z) + vr*uex(r,z)); %! rhs = bim2a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh.p(1,:), mesh.p(2,:))); %! S = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! Mr = bim2a_axisymmetric_boundary_mass(mesh,2); Ml = bim2a_axisymmetric_boundary_mass(mesh,4); %! S(Rnodesr,Rnodesr) += Mr; %! rhs(Rnodesr) += diag(Mr) .* gr(mesh.p(1,Rnodesr), mesh.p(2,Rnodesr))'; %! S(Rnodesl,Rnodesl) += Ml; %! rhs(Rnodesl) += diag(Ml) .* gl(mesh.p(1,Rnodesl), mesh.p(2,Rnodesl))'; %! u = S\rhs; %! assert(u,uex(mesh.p(1,:), mesh.p(2,:))',1e-7) %!test %! n = 10; %! [mesh] = msh2m_structured_mesh(linspace(1,2,n+1),linspace(0,1,n+1),1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! uex = @(r,z) exp(r); %! duexdr = @(r,z) uex(r,z); %! d2uexdr2 = @(r,z) uex(r,z); %! duexdz = @(r,z) 0*uex(r,z); %! d2uexdz2 = @(r,z) 0*uex(r,z); %! Rnodesr = bim2c_unknowns_on_side(mesh,[2]); %! Rnodesl = bim2c_unknowns_on_side(mesh,[4]); %! Rnodesb = bim2c_unknowns_on_side(mesh,[1]); %! Rnodest = bim2c_unknowns_on_side(mesh,[3]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! D = 1; vr = 1; vz = 0; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! beta = 1/D*[vr*ones(1,Nelements); vz*ones(1,Nelements)]; %! f = @(r,z) -D./r.*duexdr(r,z) - D.*d2uexdr2(r,z) ... %! + vr./r .* uex(r,z) + vr * duexdr(r,z) ... %! - D.*d2uexdz2(r,z) + vz * duexdz(r,z); %! gr = @(r,z) uex(r,z) - 1 * (-D*duexdr(r,z) + vr*uex(r,z)); %! gl = @(r,z) uex(r,z) - (-1) * (-D*duexdr(r,z) + vr*uex(r,z)); %! gb = @(r,z) uex(r,z) - (-1) * (-D*duexdz(r,z) + vz*uex(r,z)); %! gt = @(r,z) uex(r,z) - 1 * (-D*duexdz(r,z) + vz*uex(r,z)); %! rhs = bim2a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh.p(1,:), mesh.p(2,:))); %! S = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! Mr = bim2a_axisymmetric_boundary_mass(mesh,2); Ml = bim2a_axisymmetric_boundary_mass(mesh,4); %! Mb = bim2a_axisymmetric_boundary_mass(mesh,1); Mt = bim2a_axisymmetric_boundary_mass(mesh,3); %! S(Rnodesr,Rnodesr) += Mr; %! rhs(Rnodesr) += diag(Mr) .* gr(mesh.p(1,Rnodesr), mesh.p(2,Rnodesr))'; %! S(Rnodesl,Rnodesl) += Ml; %! rhs(Rnodesl) += diag(Ml) .* gl(mesh.p(1,Rnodesl), mesh.p(2,Rnodesl))'; %! S(Rnodesb,Rnodesb) += Mb; %! rhs(Rnodesb) += diag(Mb) .* gb(mesh.p(1,Rnodesb), mesh.p(2,Rnodesb))'; %! S(Rnodest,Rnodest) += Mt; %! rhs(Rnodest) += diag(Mt) .* gt(mesh.p(1,Rnodest), mesh.p(2,Rnodest))'; %! u = S\rhs; %! assert(u,uex(mesh.p(1,:), mesh.p(2,:))',1e-7) bim-1.1.6/inst/bim2a_axisymmetric_laplacian.m000066400000000000000000000053241426641764600212410ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo porro ## author: Emanuela Abbate ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {@var{A}} = bim2a_axisymmetric_laplacian (@var{mesh},@var{epsilon},@var{kappa}) ## ## Build the standard finite element stiffness matrix for a diffusion ## problem in cylindrical coordinates with axisymmetric configuration. ## Rotational symmetry is assumed with respect to be the vertical axis r=0. ## Only plane geometries that DO NOT intersect the symmetry axis are admitted. ## ##@example ##@group ## | ____ _|____ ## | | \ \ | | ## z | | \ OK \| | NO! ## | |______\ |\___| ## | r | #@end group #@end example ## ## The equation taken into account is: ## ## 1/r * d(r * Fr)/dr + dFz/dz = f ## ## with ## ## F = [Fr, Fz]' = - @var{epsilon} * @var{kappa} grad (u) ## ## where @var{epsilon} is an element-wise constant scalar function, ## while @var{kappa} is a piecewise linear conforming scalar function. ## ## @seealso{bim2a_axisymmetric_rhs, bim2a_axisymmetric_reaction, ## bim2a_axisymmetric_advection_diffusion, bim2a_laplacian, bim1a_laplacian, ## bim3a_laplacian} ## @end deftypefn function [A] = bim2a_axisymmetric_laplacian(mesh,epsilon,kappa) ## Check input if nargin != 3 error("bim2a_axisymmetric_laplacian: wrong number of input parameters."); elseif !(all(mesh.p(1,:) >= 0) || all(mesh.p(1,:) <= 0)) error("bim2a_axisymmetric_laplacian: the input mesh cannot intersect the rotation axis r=0."); endif ## Input check inside bim2a_axisymmetric_advection_diffusion nnodes = columns(mesh.p); nelem = columns(mesh.t); A = bim2a_axisymmetric_advection_diffusion (mesh,epsilon,kappa,ones(nnodes,1),0); endfunction bim-1.1.6/inst/bim2a_axisymmetric_reaction.m000066400000000000000000000122021426641764600211120ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo porro ## author: Emanuela Abbate ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{C}]} = @ ## bim2a_axisymmetric_reaction(@var{mesh},@var{delta},@var{zeta}) ## ## Build the lumped finite element mass matrix for a diffusion ## problem in cylindrical coordinates with axisymmetric configuration. ## ## The equation taken into account is: ## ## @var{delta} * @var{zeta} * u = f ## ## where @var{delta} is an element-wise constant scalar function, while ## @var{zeta} is a piecewise linear conforming scalar function. ## ## @seealso{bim2a_rhs, bim2a_axisymmetric_advection_diffusion, ## bim2a_axisymmetric_laplacian, bim2a_reaction, bim1a_reaction, bim3a_reaction} ## @end deftypefn function [C] = bim2a_axisymmetric_reaction(mesh,delta,zeta) ## Check input if nargin != 3 error("bim2a_axisymmetric_reaction: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_axisymmetric_reaction: first input is not a valid mesh structure."); elseif !(all(mesh.p(1,:) >= 0) || all(mesh.p(1,:) <= 0)) error("bim2a_axisymmetric_reaction: the input mesh cannot intersect the rotation axis r=0."); endif nnodes = size(mesh.p,2); nelem = size(mesh.t,2); r = abs (mesh.p(1,:)); ## Turn scalar input to a vector of appropriate size if isscalar(delta) delta = delta*ones(nelem,1); endif if isscalar(zeta) zeta = zeta*ones(nnodes,1); endif if !( isvector(delta) && isvector(zeta) ) error("bim2a_axisymmetric_reaction: coefficients are not valid vectors."); elseif length(delta) != nelem error("bim2a_axisymmetric_reaction: length of alpha is not equal to the number of elements."); elseif length(zeta) != nnodes error("bim2a_axisymmetric_reaction: length of gamma is not equal to the number of nodes."); endif wjacdet = mesh.wjacdet(:,:); coeff = zeta(mesh.t(1:3,:)); coeffe = delta(:); ## Local matrix Blocmat = zeros(3,nelem); for inode = 1:3 Blocmat(inode,:) = coeffe'.*coeff(inode,:).*wjacdet(inode,:) .* r(mesh.t(inode,:)); endfor gnode = (mesh.t(1:3,:)); ## Global matrix C = sparse(gnode(:),gnode(:),Blocmat(:)); endfunction %!shared mesh,delta,zeta,nnodes,nelem % x = y = linspace(0,1,4); % [mesh] = msh2m_structured_mesh(x,y,1,1:4); % [mesh] = bim2c_mesh_properties(mesh); % nnodes = columns(mesh.p); % nelem = columns(mesh.t); % delta = ones(columns(mesh.t),1); % zeta = ones(columns(mesh.p),1); %!test % [C] = bim2a_axisymmetric_reaction(mesh,delta,zeta); % assert(size(C),[nnodes, nnodes]); %!test % [C1] = bim2a_axisymmetric_reaction(mesh,3*delta,zeta); % [C2] = bim2a_axisymmetric_reaction(mesh,delta,3*zeta); % assert(C1,C2); %!test % [C1] = bim2a_axisymmetric_reaction(mesh,3*delta,zeta); % [C2] = bim2a_axisymmetric_reaction(mesh,3,1); % assert(C1,C2); %!test %! n = 20; %! [mesh] = msh2m_structured_mesh(linspace(1,2,n+1),linspace(0,1,n+1),1,1:4); %! mesh = bim2c_mesh_properties(mesh); %! uex = @(r,z) exp(r) .* exp(1-z); %! duexdr = @(r,z) uex(r,z); %! d2uexdr2 = @(r,z) uex(r,z); %! duexdz = @(r,z) -uex(r,z); %! d2uexdz2 = @(r,z) uex(r,z); %! Dnodes = bim2c_unknowns_on_side(mesh,[1,2,3,4]); %! Nnodes = columns(mesh.p); %! Nelements = columns(mesh.t); %! Varnodes = setdiff(1:Nnodes,Dnodes); %! D = 1; vr = 1; vz = 1; sigma = 1; %! alpha = D*ones(Nelements,1); %! gamma = ones(Nnodes,1); %! eta = ones(Nnodes,1); %! delta = sigma*ones(columns(mesh.t),1); %! zeta = ones(columns(mesh.p),1); %! beta = 1/D*[vr*ones(1,Nelements); vz*ones(1,Nelements)]; %! f = @(r,z) -D./r.*duexdr(r,z) - D.*d2uexdr2(r,z) ... %! + vr./r .* uex(r,z) + vr * duexdr(r,z) ... %! - D.*d2uexdz2(r,z) + vz * duexdz(r,z) ... %! + sigma * uex(r,z); %! rhs = bim2a_axisymmetric_rhs(mesh, ones(Nelements,1), f(mesh.p(1,:), mesh.p(2,:))); %! S = bim2a_axisymmetric_advection_diffusion(mesh,alpha,gamma,eta,beta); %! C = bim2a_axisymmetric_reaction(mesh,delta,zeta); %! S += C; %! u = zeros(Nnodes,1); u(Dnodes) = uex(mesh.p(1,Dnodes), mesh.p(2,Dnodes)); %! u(Varnodes) = S(Varnodes,Varnodes)\(rhs(Varnodes) - S(Varnodes,Dnodes)*u(Dnodes)); %! assert(u,uex(mesh.p(1,:), mesh.p(2,:))',1e-3) bim-1.1.6/inst/bim2a_axisymmetric_rhs.m000066400000000000000000000071441426641764600201130ustar00rootroot00000000000000## Copyright (C) 2006-2014 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## author: Matteo porro ## author: Emanuela Abbate ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{b}]} = @ ## bim2a_axisymmetric_rhs(@var{mesh},@var{f},@var{g}) ## ## Build the finite element right-hand side of a diffusion problem ## in cylindrical coordinates with axisymmetric configuration ## employing mass-lumping. ## ## The equation taken into account is: ## ## @var{delta} * u = f * g ## ## where @var{f} is an element-wise constant scalar function, while ## @var{g} is a piecewise linear conforming scalar function. ## ## @seealso{bim2a_axisymmetric_reaction, bim2a_axisymmetric_advection_diffusion, ## bim2a_axisymmetric_laplacian, bim1a_axisymmetric_rhs} ## @end deftypefn function b = bim2a_axisymmetric_rhs(mesh,f,g) ## Check input if (nargin != 3) error("bim2a_axisymmetric_rhs: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_axisymmetric_rhs: first input is not a valid mesh structure."); elseif !(all(mesh.p(1,:) >= 0) || all(mesh.p(1,:) <= 0)) error("bim2a_axisymmetric_rhs: the input mesh cannot intersect the rotation axis r=0."); endif nnodes = columns(mesh.p); nelem = columns(mesh.t); r = abs (mesh.p(1,:)); ## Turn scalar input to a vector of appropriate size if isscalar(f) f = f*ones(nelem,1); endif if isscalar(g) g = g*ones(nnodes,1); endif if !( isvector(f) && isvector(g) ) error("bim2a_axisymmetric_rhs: coefficients are not valid vectors."); elseif length(f) != nelem error("bim2a_axisymmetric_rhs: length of f is not equal to the number of elements."); elseif length(g) != nnodes error("bim2a_axisymmetric_rhs: length of g is not equal to the number of nodes."); endif g = g(mesh.t(1:3,:)); wjacdet = mesh.wjacdet; ## Build local matrix Blocmat = zeros(3,nelem); for inode = 1:3 Blocmat(inode,:) = f'.*g(inode,:).*wjacdet(inode,:) .* r(mesh.t(inode,:)); endfor gnode = (mesh.t(1:3,:)); ## Assemble global matrix b = sparse(gnode(:),1,Blocmat(:)); endfunction %!shared mesh,f,g,nnodes,nelem % x = y = linspace(0,1,4); % [mesh] = msh2m_structured_mesh(x,y,1,1:4); % [mesh] = bim2c_mesh_properties(mesh); % nnodes = columns(mesh.p); % nelem = columns(mesh.t); % g = ones(columns(mesh.t),1); % f = ones(columns(mesh.p),1); %!test % [b] = bim2a_axisymmetric_rhs(mesh,f,g); % assert(size(b),[nnodes, 1]); %!test % [b1] = bim2a_axisymmetric_rhs(mesh,3*f,g); % [b2] = bim2a_axisymmetric_rhs(mesh,f,3*g); % assert(b1,b2); %!test % [b1] = bim2a_axisymmetric_rhs(mesh,3*f,g); % [b2] = bim2a_axisymmetric_rhs(mesh,3,1); % assert(b1,b2); bim-1.1.6/inst/bim2a_boundary_mass.m000066400000000000000000000046131426641764600173650ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{M}]} = @ ## bim2a_boundary_mass(@var{mesh},@var{sidelist},@var{nodelist}) ## ## Build the lumped boundary mass matrix needed to apply Robin boundary ## conditions. ## ## The vector @var{sidelist} contains the list of the side edges ## contributing to the mass matrix. ## ## The optional argument @var{nodelist} contains the list of the ## degrees of freedom on the boundary. ## ## @seealso{bim2a_rhs, bim2a_advection_diffusion, bim2a_laplacian, ## bim2a_reaction} ## @end deftypefn function [M] = bim2a_boundary_mass(mesh,sidelist,nodelist) ## Check input if nargin > 3 error("bim2a_boundary_mass: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_boundary_mass: first input is not a valid mesh structure."); elseif !( isvector(sidelist) && isnumeric(sidelist) ) error("bim2a_boundary_mass: second input is not a valid numeric vector."); endif if nargin<3 [nodelist] = bim2c_unknowns_on_side(mesh,sidelist); endif edges = []; for ie = sidelist edges = [ edges, mesh.e([1:2 5],mesh.e(5,:)==ie)]; endfor l = sqrt((mesh.p(1,edges(1,:))-mesh.p(1,edges(2,:))).^2 + (mesh.p(2,edges(1,:))-mesh.p(2,edges(2,:))).^2); dd = zeros(size(nodelist)); for in = 1:numel (nodelist) dd (in) = (sum(l(edges(1,:)==nodelist(in)))+sum(l(edges(2,:)==nodelist(in))))/2; endfor M = sparse(diag(dd)); endfunction bim-1.1.6/inst/bim2a_laplacian.m000066400000000000000000000034701426641764600164430ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {@var{A}} = bim2a_laplacian (@var{mesh},@var{epsilon},@var{kappa}) ## ## Build the standard finite element stiffness matrix for a diffusion ## problem. ## ## The equation taken into account is: ## ## - div (@var{epsilon} * @var{kappa} grad (u)) = f ## ## where @var{epsilon} is an element-wise constant scalar function, ## while @var{kappa} is a piecewise linear conforming scalar function. ## ## @seealso{bim2a_rhs, bim2a_reaction, bim2a_advection_diffusion, bim1a_laplacian, bim3a_laplacian} ## @end deftypefn function [A] = bim2a_laplacian(mesh,epsilon,kappa) ## Check input if nargin != 3 error("bim2a_laplacian: wrong number of input parameters."); endif ## Input check inside bim2a_advection_diffusion nnodes = columns(mesh.p); nelem = columns(mesh.t); A = bim2a_advection_diffusion (mesh,epsilon,kappa,ones(nnodes,1),0); endfunctionbim-1.1.6/inst/bim2a_reaction.m000066400000000000000000000063311426641764600163220ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{C}]} = @ ## bim2a_reaction(@var{mesh},@var{delta},@var{zeta}) ## ## Build the lumped finite element mass matrix for a diffusion ## problem. ## ## The equation taken into account is: ## ## @var{delta} * @var{zeta} * u = f ## ## where @var{delta} is an element-wise constant scalar function, while ## @var{zeta} is a piecewise linear conforming scalar function. ## ## @seealso{bim2a_rhs, bim2a_advection_diffusion, bim2a_laplacian, ## bim1a_reaction, bim3a_reaction} ## @end deftypefn function [C] = bim2a_reaction(mesh,delta,zeta) ## Check input if nargin != 3 error("bim2a_reaction: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_reaction: first input is not a valid mesh structure."); endif nnodes = size(mesh.p,2); nelem = size(mesh.t,2); ## Turn scalar input to a vector of appropriate size if isscalar(delta) delta = delta*ones(nelem,1); endif if isscalar(zeta) zeta = zeta*ones(nnodes,1); endif if !( isvector(delta) && isvector(zeta) ) error("bim2a_reaction: coefficients are not valid vectors."); elseif (numel (delta) != nelem) error("bim2a_: length of alpha is not equal to the number of elements."); elseif (numel (zeta) != nnodes) error("bim2a_: length of gamma is not equal to the number of nodes."); endif wjacdet = mesh.wjacdet(:,:); coeff = zeta(mesh.t(1:3,:)); coeffe = delta(:); ## Local matrix Blocmat = zeros(3,nelem); for inode = 1:3 Blocmat(inode,:) = coeffe.'.*coeff(inode,:).*wjacdet(inode,:); endfor gnode = (mesh.t(1:3,:)); ## Global matrix C = sparse(gnode(:),gnode(:),Blocmat(:)); endfunction %!shared mesh,delta,zeta,nnodes,nelem % x = y = linspace(0,1,4); % [mesh] = msh2m_structured_mesh(x,y,,1:4; % [mesh] = bim2c_mesh_properties(mesh); % nnodes = columns(mesh.p); % nelem = columns(mesh.t); % delta = ones(columns(mesh.t),1); % zeta = ones(columns(mesh.p),1); %!test % [C] = bim2a_reaction(mesh,delta,zeta); % assert(size(C),[nnodes, nnodes]); %!test % [C1] = bim2a_reaction(mesh,3*delta,zeta); % [C2] = bim2a_reaction(mesh,delta,3*zeta); % assert(C1,C2); %!test % [C1] = bim2a_reaction(mesh,3*delta,zeta); % [C2] = bim2a_reaction(mesh,3,1); % assert(C1,C2); bim-1.1.6/inst/bim2a_rhs.m000066400000000000000000000060531426641764600153130ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{b}]} = @ ## bim2a_rhs(@var{mesh},@var{f},@var{g}) ## ## Build the finite element right-hand side of a diffusion problem ## employing mass-lumping. ## ## The equation taken into account is: ## ## @var{delta} * u = f * g ## ## where @var{f} is an element-wise constant scalar function, while ## @var{g} is a piecewise linear conforming scalar function. ## ## @seealso{bim2a_reaction, bim2a_advection_diffusion, bim2a_laplacian, ## bim1a_reaction, bim3a_reaction} ## @end deftypefn function b = bim2a_rhs(mesh,f,g) ## Check input if nargin != 3 error("bim2a_rhs: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2a_rhs: first input is not a valid mesh structure."); endif nnodes = columns(mesh.p); nelem = columns(mesh.t); ## Turn scalar input to a vector of appropriate size if isscalar(f) f = f*ones(nelem,1); endif if isscalar(g) g = g*ones(nnodes,1); endif if !( isvector(f) && isvector(g) ) error("bim2a_rhs: coefficients are not valid vectors."); elseif (numel (f) != nelem) error("bim2a_rhs: length of f is not equal to the number of elements."); elseif (numel (g) != nnodes) error("bim2a_rhs: length of g is not equal to the number of nodes."); endif g = g(mesh.t(1:3,:)); wjacdet = mesh.wjacdet; ## Build local matrix Blocmat=zeros(3,nelem); for inode=1:3 Blocmat(inode,:) = f.' .* g(inode,:) .* wjacdet(inode,:); endfor gnode=(mesh.t(1:3,:)); ## Assemble global matrix b = sparse(gnode(:),1,Blocmat(:)); endfunction %!shared mesh,f,g,nnodes,nelem % x = y = linspace(0,1,4); % [mesh] = msh2m_structured_mesh(x,y,1,1:4); % [mesh] = bim2c_mesh_properties(mesh); % nnodes = columns(mesh.p); % nelem = columns(mesh.t); % g = ones(columns(mesh.t),1); % f = ones(columns(mesh.p),1); %!test % [b] = bim2a_rhs(mesh,f,g); % assert(size(b),[nnodes, 1]); %!test % [b1] = bim2a_rhs(mesh,3*f,g); % [b2] = bim2a_rhs(mesh,f,3*g); % assert(b1,b2); %!test % [b1] = bim2a_rhs(mesh,3*f,g); % [b2] = bim2a_rhs(mesh,3,1); % assert(b1,b2); bim-1.1.6/inst/bim2c_global_flux.m000066400000000000000000000134531426641764600170210ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{jx},@var{jy}]} = @ ## bim2c_global_flux(@var{mesh},@var{u},@var{alpha},@var{gamma},@var{eta},@var{beta}) ## ## Compute the flux associated with the Scharfetter-Gummel approximation ## of the scalar field @var{u}. ## ## The vector field is defined as: ## ## J(@var{u}) = @var{alpha}* @var{gamma} * (@var{eta} * grad @var{u} - @var{beta} * @var{u})) ## ## where @var{alpha} is an element-wise constant scalar function, ## @var{eta} and @var{gamma} are piecewise linear conforming scalar ## functions, while @var{beta} is element-wise constant vector function. ## ## J(@var{u}) is an element-wise constant vector function. ## ## Instead of passing the vector field @var{beta} directly one can pass ## a piecewise linear conforming scalar function @var{phi} as the last ## input. In such case @var{beta} = grad @var{phi} is assumed. If ## @var{phi} is a single scalar value @var{beta} is assumed to be 0 in ## the whole domain. ## ## @seealso{bim2c_pde_gradient,bim2a_advection_diffusion} ## @end deftypefn function [jx, jy] = bim2c_global_flux(mesh,u,alpha,gamma,eta,beta) ## Check input if nargin != 6 error("bim2c_global_flux: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2c_global_flux: first input is not a valid mesh structure."); endif nnodes = columns(mesh.p); nelem = columns(mesh.t); if !( isvector(u) && isvector(alpha) && isvector(gamma) && isvector(eta) ) error("bim2c_global_flux: coefficients are not valid vectors."); elseif (numel (u) != nnodes) error("bim2c_global_flux: length of u is not equal to the number of nodes."); elseif (numel (alpha) != nelem) error("bim2c_global_flux: length of alpha is not equal to the number of elements."); elseif (numel (gamma) != nnodes) error("bim2c_global_flux: length of gamma is not equal to the number of nodes."); elseif (numel (eta) != nnodes) error("bim2c_global_flux: length of eta is not equal to the number of nodes."); endif nelem = columns(mesh.t); nnodes = columns(mesh.p); uloc = u(mesh.t(1:3,:)); shgx = reshape(mesh.shg(1,:,:),3,nelem); shgy = reshape(mesh.shg(2,:,:),3,nelem); x = reshape(mesh.p(1,mesh.t(1:3,:)),3,[]); dx = [ (x(3,:)-x(2,:)) ; (x(1,:)-x(3,:)) ; (x(2,:)-x(1,:)) ]; y = reshape(mesh.p(2,mesh.t(1:3,:)),3,[]); dy = [ (y(3,:)-y(2,:)) ; (y(1,:) -y(3,:)) ; (y(2,:) -y(1,:)) ]; if all(size(beta)==1) v12=0;v23=0;v31=0; elseif all(size(beta)==[2,nelem]) v23 = beta(1,:) .* dx(1,:) + beta(2,:) .* dy(1,:); v31 = beta(1,:) .* dx(2,:) + beta(2,:) .* dy(2,:); v12 = beta(1,:) .* dx(3,:) + beta(2,:) .* dy(3,:); elseif all(size(beta)==[nnodes,1]) betaloc = beta(mesh.t(1:3,:)); v23 = betaloc(3,:)-betaloc(2,:); v31 = betaloc(1,:)-betaloc(3,:); v12 = betaloc(2,:)-betaloc(1,:); else error("bim2c_global_flux: coefficient beta has wrong dimensions."); endif etaloc = eta(mesh.t(1:3,:)); eta23 = etaloc(3,:)-etaloc(2,:); eta31 = etaloc(1,:)-etaloc(3,:); eta12 = etaloc(2,:)-etaloc(1,:); etalocm1 = bimu_logm(etaloc(2,:),etaloc(3,:)); etalocm2 = bimu_logm(etaloc(3,:),etaloc(1,:)); etalocm3 = bimu_logm(etaloc(1,:),etaloc(2,:)); gammaloc = gamma(mesh.t(1:3,:)); geloc = gammaloc.*etaloc; gelocm1 = bimu_logm(geloc(2,:),geloc(3,:)); gelocm2 = bimu_logm(geloc(3,:),geloc(1,:)); gelocm3 = bimu_logm(geloc(1,:),geloc(2,:)); [bp23,bm23] = bimu_bernoulli( (v23 - eta23)./etalocm1); [bp31,bm31] = bimu_bernoulli( (v31 - eta31)./etalocm2); [bp12,bm12] = bimu_bernoulli( (v12 - eta12)./etalocm3); gfigfj = [ shgx(3,:) .* shgx(2,:) + shgy(3,:) .* shgy(2,:) ; shgx(1,:) .* shgx(3,:) + shgy(1,:) .* shgy(3,:) ; shgx(2,:) .* shgx(1,:) + shgy(2,:) .* shgy(1,:) ]; jx = - alpha' .* ( gelocm1 .* etalocm1 .* dx(1,:) .* ... gfigfj(1,:) .* ... ( bp23 .* uloc(3,:)./etaloc(3,:) -... bm23 .* uloc(2,:)./etaloc(2,:)) +... %% 1 gelocm2 .* etalocm2 .* dx(2,:) .* ... gfigfj(2,:) .* ... (bp31 .* uloc(1,:)./etaloc(1,:) -... bm31 .* uloc(3,:)./etaloc(3,:)) +... %% 2 gelocm3 .* etalocm3 .* dx(3,:) .* ... gfigfj(3,:) .* ... (bp12 .* uloc(2,:)./etaloc(2,:) -... bm12 .* uloc(1,:)./etaloc(1,:)) ... %% 3 ); jy = - alpha' .* ( gelocm1 .* etalocm1 .* dy(1,:) .* ... gfigfj(1,:) .* ... ( bp23 .* uloc(3,:)./etaloc(3,:) -... bm23 .* uloc(2,:)./etaloc(2,:)) +... %% 1 gelocm2 .* etalocm2 .* dy(2,:) .* ... gfigfj(2,:) .* ... (bp31 .* uloc(1,:)./etaloc(1,:) -... bm31 .* uloc(3,:)./etaloc(3,:)) +... %% 2 gelocm3 .* etalocm3 .* dy(3,:) .* ... gfigfj(3,:) .* ... (bp12 .* uloc(2,:)./etaloc(2,:) -... bm12 .* uloc(1,:)./etaloc(1,:)) ... %% 3 ); endfunction bim-1.1.6/inst/bim2c_intrp.m000066400000000000000000000041611426641764600156530ustar00rootroot00000000000000## Copyright (C) 2011, 2012 Carlo de Falco ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 3 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, see ## . ## -*- texinfo -*- ## ## @deftypefn {Function File} {@var{data}} = bim2c_intrp (@var{msh}, @var{n_data}, @var{e_data}, @var{points}) ## ## Compute interpolated values of multicomponent node centered field @var{n_data} and/or ## cell centered field @var{n_data} at an arbitrary set of points whose coordinates are given in the ## n_by_2 matrix @var{points}. ## ## @end deftypefn ## Author: Carlo de Falco ## Created: 2012-10-01 function data = bim2c_intrp (msh, n_data, e_data, p) %% for each point, find the enclosing tetrahedron [t_list, b_list] = tsearchn (msh.p.', msh.t(1:3, :)', p); %% only keep points within tetrahedra invalid = isnan (t_list); t_list = t_list (! invalid); ntl = numel (t_list); b_list = b_list(! invalid, :); points(invalid,:) = []; data = []; if (! isempty (n_data)) data = cat (1, data, squeeze ( sum (reshape (n_data(msh.t(1:3, t_list), :), [3, ntl, (columns (n_data))]) .* repmat (b_list.', [1, 1, (columns (n_data))]), 1))); endif if (! isempty (e_data)) data = cat (1, data, e_data(t_list, :)); endif endfunction %!test %! msh = bim2c_mesh_properties (msh2m_structured_mesh (linspace (0, 1, 11), linspace (0, 1, 13), 1, 1:4)); %! x = y = linspace (0, 1, 100).'; %! u = msh.p(1, :).'; %! ui = bim2c_intrp (msh, u, [], [x, y]); %! assert (ui, linspace (0, 1, 100), 10*eps); bim-1.1.6/inst/bim2c_mesh_properties.m000066400000000000000000000040701426641764600177260ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{omesh}]} = @ ## bim2c_mesh_properties(@var{imesh}) ## ## Compute the properties of @var{imesh} needed by BIM method and append ## them to @var{omesh} as fields. ## ## @seealso{bim2a_reaction, bim2a_advection_diffusion, bim2a_rhs, ## bim2a_laplacian, bim2a_boundary_mass} ## @end deftypefn function [omesh] = bim2c_mesh_properties(imesh) ## Check input if nargin != 1 error("bim2c_mesh_properties: wrong number of input parameters."); elseif !(isstruct(imesh) && isfield(imesh,"p") && isfield (imesh,"t") && isfield(imesh,"e")) error("bim2c_mesh_properties: first input is not a valid mesh structure."); endif omesh = imesh; [omesh.wjacdet,omesh.area,omesh.shg] = ... msh2m_geometrical_properties(imesh,"wjacdet","area","shg"); endfunction %!shared mesh % x = y = linspace(0,1,4); % [mesh] = msh2m_structured_mesh(x,y,1,1:4); % [mesh] = bim2c_mesh_properties(mesh); %!test % tmp = msh2m_geometrical_properties(mesh,"wjacdet"); % assert(mesh.wjacdet,tmp); %!test % tmp = msh2m_geometrical_properties(mesh,"shg"); % assert(mesh.shg,tmp); %!test % assert(mesh.area,sum(mesh.wjacdet,1));bim-1.1.6/inst/bim2c_norm.m000066400000000000000000000107741426641764600155010ustar00rootroot00000000000000## Copyright (C) 2006-2013 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Matteo Porro ## -*- texinfo -*- ## ## @deftypefn {Function File} {[@var{norm_u}]} = @ ## bim2c_norm(@var{mesh},@var{u},@var{norm_type}) ## ## Compute the @var{norm_type}-norm of function @var{u} on the domain described ## by the triangular grid @var{mesh}. ## ## The input function @var{u} can be either a piecewise linear conforming scalar ## function or an elementwise constant scalar or vector function. ## ## The string parameter @var{norm_type} can be one among 'L2', 'H1' and 'inf'. ## ## Should the input function be piecewise constant, the H1 norm will not be ## computed and the function will return an error message. ## ## For the numerical integration of the L2 norm the second order middle point ## quadrature rule is used. ## ## @seealso{bim1c_norm, bim3c_norm} ## ## @end deftypefn function [norm_u] = bim2c_norm (m, u, norm_type) ## Check input if (nargin != 3) error ("bim2c_norm: wrong number of input parameters."); elseif (! (isstruct (m) && isfield (m,"p") && isfield (m, "t") && isfield (m, "e"))) error ("bim2c_norm: first input is not a valid mesh structure."); endif nnodes = columns (m.p); nel = columns (m.t); if (isequal (size (u), [2, nel])) u = u'; endif if ((numel (u) != nnodes) && (rows (u) != nel)) error ("bim2c_norm: numel(u) != nnodes and rows(u) != nel."); endif if (! (strcmp (norm_type,'L2') || strcmp (norm_type,'inf') || strcmp (norm_type,'H1'))) error ("bim2c_norm: invalid norm type parameter."); endif if (strcmp (norm_type,'inf')) norm_u = max (abs (u(:))); else if (numel (u) == nnodes) M = __mass_matrix__ (m); if (strcmp (norm_type, 'H1')) A = bim2a_laplacian (m, 1, 1); M += A; endif norm_u = sqrt(u' * M * u); else if (strcmp (norm_type, 'H1')) error (["bim2c_norm: cannot compute the H1 norm ", ... "of an elementwise constant function."]); endif norm_u = m.area' * (norm (u, 2, 'rows').^2); norm_u = sqrt (norm_u); endif endif endfunction function M = __mass_matrix__ (mesh) t = mesh.t; nnodes = columns (mesh.p); nelem = columns (t); ## Local contributions Mref = 1/12 * [2 1 1; 1 2 1; 1 1 2]; area = reshape (mesh.area, 1, 1, nelem); ## Computation for inode = 1:3 for jnode = 1:3 ginode(inode,jnode,:) = t(inode,:); gjnode(inode,jnode,:) = t(jnode,:); endfor endfor Mloc = area .* Mref; ## assemble global matrix M = sparse (ginode(:), gjnode(:), Mloc(:), nnodes, nnodes); endfunction %!test %!shared L, V, x, y, m %! L = rand (1); V = rand (1); x = linspace (0,L,4); y = x; %! m = msh2m_structured_mesh (x,y,1,1:4); %! m.area = msh2m_geometrical_properties (m, 'area'); %! m.shg = msh2m_geometrical_properties (m, 'shg'); %! u = V * ones (columns(m.p),1); %! uinf = bim2c_norm (m, u, 'inf'); %! uL2 = bim2c_norm (m, u, 'L2'); %! uH1 = bim2c_norm (m, u, 'H1'); %! assert ([uinf, uL2, uH1], [V, V*L, V*L], 1e-12); %!test %! u = V * (m.p(1,:) + 2*m.p(2,:))'; %! uinf = bim2c_norm (m, u, 'inf'); %! uL2 = bim2c_norm (m, u, 'L2'); %! uH1 = bim2c_norm (m, u, 'H1'); %! assert ([uinf, uL2, uH1], %! [3*L*V, V*L^2*sqrt(8/3), V*sqrt(8/3*L^4 + 5*L^2)], %! 1e-12); %!test %! u = V * ones (columns(m.t),1); %! uinf = bim2c_norm (m, u, 'inf'); %! uL2 = bim2c_norm (m, u, 'L2'); %! assert ([uinf, uL2], [V, V*L], 1e-12); %!test %! u = V * ones (columns(m.t),1); %! uvect = [u, 2*u]; %! uinf = bim2c_norm (m, uvect, 'inf'); %! uL2 = bim2c_norm (m, uvect, 'L2'); %! assert ([uinf, uL2], [2*V, V*L*sqrt(5)], 1e-12); bim-1.1.6/inst/bim2c_pde_gradient.m000066400000000000000000000034321426641764600171440ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## ## @deftypefn {Function File} {[@var{gx},@var{gy}]} = @ ## bim2c_pde_gradient(@var{mesh},@var{u}) ## ## Compute the gradient of the piecewise linear conforming scalar ## function @var{u}. ## ## @seealso{bim2c_global_flux} ## @end deftypefn function [gx, gy] = bim2c_pde_gradient(mesh,u) ## Check input if nargin != 2 error("bim2c_pde_gradient: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2c_pde_gradient: first input is not a valid mesh structure."); endif nnodes = columns(mesh.p); if (numel (u) != nnodes) error("bim2c_pde_gradient: length(u) != nnodes."); endif shgx = reshape(mesh.shg(1,:,:),3,[]); gx = sum(shgx.*u(mesh.t(1:3,:)),1); shgy = reshape(mesh.shg(2,:,:),3,[]); gy = sum(shgy.*u(mesh.t(1:3,:)),1); endfunction bim-1.1.6/inst/bim2c_tri_to_nodes.m000066400000000000000000000052021426641764600172040ustar00rootroot00000000000000## Copyright (C) 2011 Carlo de Falco ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 3 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, see ## . ## -*- texinfo -*- ## ## @deftypefn {Function File} {@var{u_nod}} = bim2c_tri_to_nodes (@var{mesh}, @var{u_tri}) ## @deftypefnx {Function File} {@var{u_nod}} = bim2c_tri_to_nodes (@var{m_tri}, @var{u_tri}) ## @deftypefnx {Function File} {[@var{u_nod}, @var{m_tri}]} = bim2c_tri_to_nodes ( ... ) ## ## Compute interpolated values at triangle nodes @var{u_nod} given values at triangle mid-points @var{u_tri}. ## If called with more than one output, also return the interpolation matrix @var{m_tri} such that ## @code{u_nod = m_tri * u_tri}. ## If repeatedly performing interpolation on the same mesh the matrix @var{m_tri} obtained by a previous call ## to @code{bim2c_tri_to_nodes} may be passed as input to avoid unnecessary computations. ## ## @end deftypefn ## Author: Carlo de Falco ## Created: 2011-03-07 function [u_nod, m_tri] = bim2c_tri_to_nodes (m, u_tri) if (nargout > 1) if (isstruct (m)) nel = columns (m.t); nnod = columns (m.p); ii = m.t(1:3, :); jj = repmat (1:nel, 3, 1); vv = repmat (m.area(:)', 3, 1) / 3; m_tri = bim2a_reaction (m, 1, 1) \ sparse (ii, jj, vv, nnod, nel); elseif (ismatrix (m)) m_tri = m; else error ("bim2c_tri_to_nodes: first input parameter is of incorrect type"); endif u_nod = m_tri * u_tri; else if (isstruct (m)) rhs = bim2a_rhs (m, u_tri, 1); mass = bim2a_reaction (m, 1, 1); u_nod = full (mass \ rhs); elseif (ismatrix (m)) u_nod = m * u_tri; else error ("bim2c_tri_to_nodes: first input parameter is of incorrect type"); endif endif endfunction %!test %! msh = bim2c_mesh_properties (msh2m_structured_mesh (linspace (0, 1, 3), linspace (0, 1, 3), 1, 1:4, "random")); %! nel = columns (msh.t); %! nnod = columns (msh.p); %! u_tri = randn (nel, 1); %! un1 = bim2c_tri_to_nodes (msh, u_tri); %! [un2, m] = bim2c_tri_to_nodes (msh, u_tri); %! assert (un1, un2, 1e-10) bim-1.1.6/inst/bim2c_unknowns_on_side.m000066400000000000000000000034031426641764600200770ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{nodelist}]} = @ ## bim2c_unknowns_on_side(@var{mesh},@var{sidelist}) ## ## Return the list of the mesh nodes that lie on the geometrical sides ## specified in @var{sidelist}. ## ## @seealso{bim3c_unknown_on_faces, bim2c_pde_gradient, ## bim2c_global_flux} ## @end deftypefn function [nodelist] = bim2c_unknowns_on_side(mesh, sidelist) ## Check input if nargin != 2 error("bim2c_unknowns_on_side: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim2c_unknowns_on_side: first input is not a valid mesh structure."); elseif !isnumeric(sidelist) error("bim2c_unknowns_on_side: second input is not a valid numeric vector."); endif [nodelist] = msh2m_nodes_on_sides(mesh,sidelist); endfunction bim-1.1.6/inst/bim3a_advection_diffusion.m000066400000000000000000000072221426641764600205410ustar00rootroot00000000000000## Copyright (C) 2010 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{A}]} = @ ## bim3a_advection_diffusion (@var{mesh}, @var{alpha}, @var{v}) ## ## Build the Scharfetter-Gummel stabilized stiffness matrix for a ## diffusion-advection problem. ## ## The equation taken into account is: ## ## - div (@var{alpha} ( grad (u) - grad (@var{v}) u)) = f ## ## where @var{v} is a piecewise linear continuous scalar ## functions and @var{alpha} is a piecewise constant scalar function. ## ## @seealso{bim3a_rhs, bim3a_reaction, bim3a_laplacian, bim3c_mesh_properties} ## @end deftypefn function SG = bim3a_advection_diffusion (mesh, acoeff, v) t = mesh.t; nnodes = columns (mesh.p); nelem = columns(t); ## Local contributions Lloc = zeros(4,4,nelem); epsilonareak = reshape (acoeff .* mesh.area', 1, 1, nelem); shg = mesh.shg(:,:,:); ## Computation for inode = 1:4 for jnode = 1:4 ginode(inode,jnode,:) = t(inode,:); gjnode(inode,jnode,:) = t(jnode,:); Lloc(inode,jnode,:) = ... sum( shg(:,inode,:) .* shg(:,jnode,:), 1) .* epsilonareak; endfor endfor vloc = v(t(1:4, :)); [bp12,bm12] = bimu_bernoulli (vloc(2,:)-vloc(1,:)); [bp13,bm13] = bimu_bernoulli (vloc(3,:)-vloc(1,:)); [bp14,bm14] = bimu_bernoulli (vloc(4,:)-vloc(1,:)); [bp23,bm23] = bimu_bernoulli (vloc(3,:)-vloc(2,:)); [bp24,bm24] = bimu_bernoulli (vloc(4,:)-vloc(2,:)); [bp34,bm34] = bimu_bernoulli (vloc(4,:)-vloc(3,:)); bp12 = reshape (bp12, 1, 1, nelem) .* Lloc(1,2,:); bm12 = reshape (bm12, 1, 1, nelem) .* Lloc(1,2,:); bp13 = reshape (bp13, 1, 1, nelem) .* Lloc(1,3,:); bm13 = reshape (bm13, 1, 1, nelem) .* Lloc(1,3,:); bp14 = reshape (bp14, 1, 1, nelem) .* Lloc(1,4,:); bm14 = reshape (bm14, 1, 1, nelem) .* Lloc(1,4,:); bp23 = reshape (bp23, 1, 1, nelem) .* Lloc(2,3,:); bm23 = reshape (bm23, 1, 1, nelem) .* Lloc(2,3,:); bp24 = reshape (bp24, 1, 1, nelem) .* Lloc(2,4,:); bm24 = reshape (bm24, 1, 1, nelem) .* Lloc(2,4,:); bp34 = reshape (bp34, 1, 1, nelem) .* Lloc(3,4,:); bm34 = reshape (bm34, 1, 1, nelem) .* Lloc(3,4,:); ## SGloc=[... ## -bm12-bm13-bm14,bp12 ,bp13 ,bp14 ## bm12 ,-bp12-bm23-bm24 ,bp23 ,bp24 ## bm13 ,bm23 ,-bp13-bp23-bm34,bp34 ## bm14 ,bm24 ,bm34 ,-bp14-bp24-bp34... ## ]; Sloc(1,1,:) = -bm12-bm13-bm14; Sloc(1,2,:) = bp12; Sloc(1,3,:) = bp13; Sloc(1,4,:) = bp14; Sloc(2,1,:) = bm12; Sloc(2,2,:) = -bp12-bm23-bm24; Sloc(2,3,:) = bp23; Sloc(2,4,:) = bp24; Sloc(3,1,:) = bm13; Sloc(3,2,:) = bm23; Sloc(3,3,:) = -bp13-bp23-bm34; Sloc(3,4,:) = bp34; Sloc(4,1,:) = bm14; Sloc(4,2,:) = bm24; Sloc(4,3,:) = bm34; Sloc(4,4,:) = -bp14-bp24-bp34; ## assemble global matrix SG = sparse(ginode(:),gjnode(:),Sloc(:), nnodes, nnodes); endfunction bim-1.1.6/inst/bim3a_boundary_mass.m000066400000000000000000000047751426641764600173770ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Matteo Porro ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{M}]} = @ ## bim3a_boundary_mass(@var{mesh},@var{facelist},@var{nodelist}) ## ## Build the lumped boundary mass matrix needed to apply Robin boundary ## conditions. ## ## The vector @var{facelist} contains the list of the faces contributing ## to the mass matrix. ## ## The optional argument @var{nodelist} contains the list of the ## degrees of freedom on the boundary. ## ## @seealso{bim3a_rhs, bim3a_advection_diffusion, bim3a_laplacian, ## bim3a_reaction, bim2a_boundary_mass} ## @end deftypefn function [M] = bim3a_boundary_mass (mesh, facelist, nodelist) ## Check input if (nargin > 3) error ("bim3a_boundary_mass: wrong number of input parameters."); elseif (! ((isstruct (mesh)) && (isfield (mesh, "p")) && (isfield (mesh, "t")) && isfield(mesh, "e"))) error (["bim3a_boundary_mass: first input", ... " is not a valid mesh structure."]); elseif (! ((isvector (facelist)) && (isnumeric (facelist)))) error (["bim3a_boundary_mass: second ", ... "input is not a valid numeric vector."]); endif if (nargin < 3) [nodelist] = bim3c_unknowns_on_faces (mesh, facelist); endif p = mesh.p; t = []; for ie = facelist t = [t, mesh.e([1:3 10], mesh.e(10,:) == ie)]; endfor area = 1/2 * norm (cross (p(:,t(2,:))-p(:,t(1,:)), p(:,t(3,:))-p(:,t(1,:))), 2, 'columns'); dd = zeros (size (nodelist)); for in = 1:numel (nodelist) dd (in) = 1/3 * sum (area (any (t(1:3,:) == nodelist(in)))); endfor M = sparse (diag (dd)); endfunction bim-1.1.6/inst/bim3a_laplacian.m000066400000000000000000000067071426641764600164520ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} @ ## {@var{A}} = bim3a_laplacian (@var{mesh}, @var{epsilon}, @var{kappa}) ## ## Build the standard finite element stiffness matrix for a diffusion ## problem. ## ## The equation taken into account is: ## ## - (@var{epsilon} * @var{kappa} ( u' ))' = f ## ## where @var{epsilon} is an element-wise constant scalar function, ## while @var{kappa} is a piecewise linear conforming scalar function. ## ## @seealso{bim3a_rhs, bim3a_reaction, bim2a_laplacian, bim3a_laplacian} ## @end deftypefn function [A] = bim3a_laplacian (mesh,epsilon,kappa) ## Check input if nargin != 3 error("bim3a_laplacian: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim3a_laplacian: first input is not a valid mesh structure."); endif p = mesh.p; t = mesh.t; nnodes = columns(p); nelem = columns(t); ## Turn scalar input to a vector of appropriate size if isscalar(epsilon) epsilon = epsilon * ones(nelem,1); endif if isscalar(kappa) kappa = kappa*ones(nnodes,1); endif if !( isvector(epsilon) && isvector(kappa) ) error("bim3a_laplacian: coefficients are not valid vectors."); elseif (numel (epsilon) != nelem) error("bim3a_laplacian: length of epsilon is not equal to the number of elements."); elseif (numel (kappa) != nnodes) error("bim2a_laplacian: length of kappa is not equal to the number of nodes."); endif ## Local contributions Lloc = zeros(4,4,nelem); epsilonareak = reshape (epsilon .* mesh.area',1,1,nelem); shg = mesh.shg(:,:,:); ## Computation for inode = 1:4 for jnode = 1:4 ginode(inode,jnode,:) = mesh.t(inode,:); gjnode(inode,jnode,:) = mesh.t(jnode,:); Lloc(inode,jnode,:) = sum( kappa(inode) * shg(:,inode,:) .* shg(:,jnode,:),1) .* epsilonareak; endfor endfor ## Assembly A = sparse(ginode(:),gjnode(:),Lloc(:)); endfunction %!shared mesh,epsilon,kappa,nnodes,nelem % x = y = z = linspace(0,1,4); % [mesh] = msh3m_structured_mesh(x,y,z,1,1:6); % [mesh] = bim3c_mesh_properties(mesh); % nnodes = columns(mesh.p); % nelem = columns(mesh.t); % epsilon = ones(columns(mesh.t),1); % kappa = ones(columns(mesh.p),1); %!test % [A] = bim3a_laplacian(mesh,epsilon,kappa); % assert(size(A),[nnodes, nnodes]); %!test % [A1] = bim3a_laplacian(mesh,3*epsilon,kappa); % [A2] = bim3a_laplacian(mesh,epsilon,3*kappa); % assert(A1,A2); %!test % [A1] = bim3a_laplacian(mesh,epsilon,kappa); % [A2] = bim3a_laplacian(mesh,1,1); % assert(A1,A2); bim-1.1.6/inst/bim3a_osc_advection_diffusion.m000066400000000000000000000266771426641764600214240ustar00rootroot00000000000000## Copyright (C) 2012 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{A}]} = bim3a_osc_advection_diffusion (@var{mesh}, @var{alpha}, @var{v}) ## ## Build the Scharfetter-Gummel stabilized OSC stiffness ## matrix for a diffusion-advection problem. ## ## For details on the Orthogonal Subdomain Collocation (OSC) method ## see: M.Putti and C.Cordes, SIAM J.SCI.COMPUT. Vol.19(4), pp.1154-1168, 1998. ## ## The equation taken into account is: ## ## - div (@var{alpha} ( grad (u) - grad (@var{v}) u)) = f ## ## where @var{v} is a piecewise linear continuous scalar ## functions and @var{alpha} is a piecewise constant scalar function. ## ## @seealso{bim3a_rhs, bim3a_osc_laplacian, bim3a_reaction, bim3a_laplacian, bim3c_mesh_properties} ## @end deftypefn function M = bim3a_osc_advection_diffusion (msh, epsilon, v) ## Check input if (nargin != 3) print_usage (); elseif (! (isstruct (msh) && isfield (msh, "p") && isfield (msh, "t") && isfield (msh, "e"))) error (["bim3a_laplacian: first input ", ... "is not a valid msh structure"]); endif nnodes = columns (msh.p); nelem = columns (msh.t); ## Turn scalar input to a vector of appropriate size if (isscalar (epsilon)) epsilon = epsilon * ones(nelem, 1); endif if (! isvector (epsilon)) error ("bim3a_laplacian: coefficient is not a vector"); elseif (numel (epsilon) != nelem) error (["bim3a_laplacian: length of epsilon is ", ... "not equal to the number of mesh elements"]); endif ## Avoid warnings for broadcasting warning ("off", "Octave:broadcast", "local") ## Local contributions Lloc = __osc_local_laplacian__ (msh.p, msh.t, msh.shg, epsilon, msh.area, nnodes, nelem); ## Stabilization if (isscalar (v)) v = zeros (nelem, 1); endif vloc = v(msh.t(1:4, :)); [bp12, bm12] = bimu_bernoulli (vloc(2,:)-vloc(1,:)); [bp13, bm13] = bimu_bernoulli (vloc(3,:)-vloc(1,:)); [bp14, bm14] = bimu_bernoulli (vloc(4,:)-vloc(1,:)); [bp23, bm23] = bimu_bernoulli (vloc(3,:)-vloc(2,:)); [bp24, bm24] = bimu_bernoulli (vloc(4,:)-vloc(2,:)); [bp34, bm34] = bimu_bernoulli (vloc(4,:)-vloc(3,:)); bp12 = reshape (bp12, 1, 1, nelem) .* Lloc(1,2,:); bm12 = reshape (bm12, 1, 1, nelem) .* Lloc(1,2,:); bp13 = reshape (bp13, 1, 1, nelem) .* Lloc(1,3,:); bm13 = reshape (bm13, 1, 1, nelem) .* Lloc(1,3,:); bp14 = reshape (bp14, 1, 1, nelem) .* Lloc(1,4,:); bm14 = reshape (bm14, 1, 1, nelem) .* Lloc(1,4,:); bp23 = reshape (bp23, 1, 1, nelem) .* Lloc(2,3,:); bm23 = reshape (bm23, 1, 1, nelem) .* Lloc(2,3,:); bp24 = reshape (bp24, 1, 1, nelem) .* Lloc(2,4,:); bm24 = reshape (bm24, 1, 1, nelem) .* Lloc(2,4,:); bp34 = reshape (bp34, 1, 1, nelem) .* Lloc(3,4,:); bm34 = reshape (bm34, 1, 1, nelem) .* Lloc(3,4,:); Sloc(1,1,:) = -bm12-bm13-bm14; Sloc(1,2,:) = bp12; Sloc(1,3,:) = bp13; Sloc(1,4,:) = bp14; Sloc(2,1,:) = bm12; Sloc(2,2,:) = -bp12-bm23-bm24; Sloc(2,3,:) = bp23; Sloc(2,4,:) = bp24; Sloc(3,1,:) = bm13; Sloc(3,2,:) = bm23; Sloc(3,3,:) = -bp13-bp23-bm34; Sloc(3,4,:) = bp34; Sloc(4,1,:) = bm14; Sloc(4,2,:) = bm24; Sloc(4,3,:) = bm34; Sloc(4,4,:) = -bp14-bp24-bp34; ## Assembly for inode = 1:4 for jnode = 1:4 ginode(inode, jnode,:) = msh.t(inode, :); gjnode(inode, jnode,:) = msh.t(jnode, :); endfor endfor M = sparse (ginode(:), gjnode(:), Sloc(:), nnodes, nnodes); endfunction %!shared msh, epsilon, M, nnodes, nelem, x, y, z %!test %! msh = bim3c_mesh_properties (msh3m_structured_mesh (0:5, 0:5, 0:5, 1, 1:6)); %! x = msh.p (1, :).'; %! y = msh.p (2, :).'; %! z = msh.p (3, :).'; %! u = ones (size (x)); %! M = bim3a_osc_advection_diffusion (msh, 1, 0); %! assert (M * u, zeros (size (u)), eps * 100) %!test %! u = x; %! bnd = bim3c_unknowns_on_faces (msh, [1, 2]); %! int = setdiff (1:columns (msh.p), bnd); %! assert (M(int, int) * u(int), -M(int, bnd) * u(bnd), 100 * eps) %!test %! u = y; %! bnd = bim3c_unknowns_on_faces (msh, [3, 4]); %! int = setdiff (1:columns (msh.p), bnd); %! assert (M(int, int) * u(int), -M(int, bnd) * u(bnd), 100 * eps) %!test %! u = z; %! bnd = bim3c_unknowns_on_faces (msh, [5, 6]); %! int = setdiff (1:columns (msh.p), bnd); %! assert (M(int, int) * u(int), -M(int, bnd) * u(bnd), 100 * eps) %!test %! u = z; %! bnd = bim3c_unknowns_on_faces (msh, [5, 6]); %! int = setdiff (1:columns (msh.p), bnd); %! M = bim3a_osc_advection_diffusion (msh, pi, 0); %! assert (M(int, int) * u(int), -M(int, bnd) * u(bnd), 100 * eps) %!test %! M = bim3a_osc_advection_diffusion (msh, 1, x); %! assert (norm (sum (M, 1), inf), 0, eps * 100) %!test %! M = bim3a_osc_advection_diffusion (msh, 1, y); %! assert (norm (sum (M, 1), inf), 0, eps * 100) %!test %! M = bim3a_osc_advection_diffusion (msh, 1, z); %! assert (norm (sum (M, 1), inf), 0, eps * 100) %!demo %! gmsh_input = [["Point(1) = {0, 0, 0, .1}; \n"], ... %! ["Point(2) = {1, 0, 0, .1}; \n"], ... %! ["Point(3) = {0, -.3, 0, .1}; \n"], ... %! ["Point(4) = {0, +.3, 0, .1}; \n"], ... %! ["Point(5) = {1, -.3, 0, .1}; \n"], ... %! ["Point(6) = {1, 0.3, 0, .1}; \n"], ... %! ["Point(7) = {0, 0, -.3, .1}; \n"], ... %! ["Point(8) = {0, 0, +.3, .1}; \n"], ... %! ["Point(9) = {1, 0, -.3, .1}; \n"], ... %! ["Point(10) = {1, 0, 0.3, .1}; \n"], ... %! ["Circle(1) = {4, 1, 7}; \n"], ... %! ["Circle(2) = {7, 1, 3}; \n"], ... %! ["Circle(3) = {3, 1, 8}; \n"], ... %! ["Circle(4) = {8, 1, 4}; \n"], ... %! ["Circle(5) = {6, 2, 9}; \n"], ... %! ["Circle(6) = {9, 2, 5}; \n"], ... %! ["Circle(7) = {5, 2, 10}; \n"], ... %! ["Circle(8) = {10, 2, 6}; \n"], ... %! ["Line(9) = {4, 6}; \n"], ... %! ["Line(10) = {3, 5}; \n"], ... %! ["Line(11) = {8, 10}; \n"], ... %! ["Line(12) = {7, 9}; \n"], ... %! ["Line Loop(13) = {4, 1, 2, 3}; \n"], ... %! ["Plane Surface(14) = {13}; \n"], ... %! ["Line Loop(15) = {5, 6, 7, 8}; \n"], ... %! ["Plane Surface(16) = {15}; \n"], ... %! ["Line Loop(17) = {9, -8, -11, 4}; \n"], ... %! ["Ruled Surface(18) = {17}; \n"], ... %! ["Line Loop(19) = {12, -5, -9, 1}; \n"], ... %! ["Ruled Surface(20) = {19}; \n"], ... %! ["Line Loop(21) = {12, 6, -10, -2}; \n"], ... %! ["Ruled Surface(22) = {21}; \n"], ... %! ["Line Loop(23) = {11, -7, -10, 3}; \n"], ... %! ["Ruled Surface(24) = {23}; \n"], ... %! ["Surface Loop(25) = {18, 20, 22, 16, 24, 14}; \n"], ... %! ["Volume(26) = {25}; \n"]]; %! fname = tmpnam (); %! [fid, msg] = fopen (strcat (fname, ".geo"), "w"); %! if (fid < 0); error (msg); endif %! fputs (fid, gmsh_input); %! fclose (fid); %! msh = bim3c_mesh_properties (msh3m_gmsh (fname, "clscale", ".25")); %! x = msh.p (1, :).'; %! u = x; %! bnd = bim3c_unknowns_on_faces (msh, [14, 16]); %! int = setdiff (1:columns (msh.p), bnd); %! Mosc = bim3a_osc_advection_diffusion (msh, 1, msh.p(1,:)'*0); %! Mgal = bim3a_advection_diffusion (msh, 1, msh.p(1,:)'*0); %! u(int) = Mosc(int, int) \ ( - Mosc(int, bnd) * u(bnd)); %! uosc = u; %! u(int) = Mgal(int, int) \ ( - Mgal(int, bnd) * u(bnd)); %! ugal = u; %! fname_out = tmpnam (); %! printf ("saving results to %s \n", strcat (fname_out, ".vtu")); %! fpl_vtk_raw_write_field (fname_out, msh, {uosc, "u_osc"; ugal, "u_galerkin"}, {}); %! unlink (fname); %!demo %! gmsh_input = [["Point(1) = {0, 0, 0, .1}; \n"], ... %! ["Point(2) = {1, 0, 0, .1}; \n"], ... %! ["Point(3) = {0, -.3, 0, .1}; \n"], ... %! ["Point(4) = {0, +.3, 0, .1}; \n"], ... %! ["Point(5) = {1, -.3, 0, .1}; \n"], ... %! ["Point(6) = {1, 0.3, 0, .1}; \n"], ... %! ["Point(7) = {0, 0, -.3, .1}; \n"], ... %! ["Point(8) = {0, 0, +.3, .1}; \n"], ... %! ["Point(9) = {1, 0, -.3, .1}; \n"], ... %! ["Point(10) = {1, 0, 0.3, .1}; \n"], ... %! ["Circle(1) = {4, 1, 7}; \n"], ... %! ["Circle(2) = {7, 1, 3}; \n"], ... %! ["Circle(3) = {3, 1, 8}; \n"], ... %! ["Circle(4) = {8, 1, 4}; \n"], ... %! ["Circle(5) = {6, 2, 9}; \n"], ... %! ["Circle(6) = {9, 2, 5}; \n"], ... %! ["Circle(7) = {5, 2, 10}; \n"], ... %! ["Circle(8) = {10, 2, 6}; \n"], ... %! ["Line(9) = {4, 6}; \n"], ... %! ["Line(10) = {3, 5}; \n"], ... %! ["Line(11) = {8, 10}; \n"], ... %! ["Line(12) = {7, 9}; \n"], ... %! ["Line Loop(13) = {4, 1, 2, 3}; \n"], ... %! ["Plane Surface(14) = {13}; \n"], ... %! ["Line Loop(15) = {5, 6, 7, 8}; \n"], ... %! ["Plane Surface(16) = {15}; \n"], ... %! ["Line Loop(17) = {9, -8, -11, 4}; \n"], ... %! ["Ruled Surface(18) = {17}; \n"], ... %! ["Line Loop(19) = {12, -5, -9, 1}; \n"], ... %! ["Ruled Surface(20) = {19}; \n"], ... %! ["Line Loop(21) = {12, 6, -10, -2}; \n"], ... %! ["Ruled Surface(22) = {21}; \n"], ... %! ["Line Loop(23) = {11, -7, -10, 3}; \n"], ... %! ["Ruled Surface(24) = {23}; \n"], ... %! ["Surface Loop(25) = {18, 20, 22, 16, 24, 14}; \n"], ... %! ["Volume(26) = {25}; \n"]]; %! fname = tmpnam (); %! [fid, msg] = fopen (strcat (fname, ".geo"), "w"); %! if (fid < 0); error (msg); endif %! fputs (fid, gmsh_input); %! fclose (fid); %! msh = bim3c_mesh_properties (msh3m_gmsh (fname, "clscale", ".25")); %! x = msh.p (1, :).'; %! u = x; %! bnd = bim3c_unknowns_on_faces (msh, [14, 16]); %! int = setdiff (1:columns (msh.p), bnd); %! Mosc = bim3a_osc_advection_diffusion (msh, 1, msh.p(1,:)'*0); %! Mgal = bim3a_advection_diffusion (msh, 1, msh.p(1,:)'*0); %! f = bim3a_rhs (msh, 10, 1); %! u(int) = Mosc(int, int) \ (f(int) - Mosc(int, bnd) * u(bnd)); %! uosc = u; %! u(int) = Mgal(int, int) \ (f(int) - Mgal(int, bnd) * u(bnd)); %! ugal = u; %! fname_out = tmpnam (); %! printf ("saving results to %s \n", strcat (fname_out, ".vtu")); %! fpl_vtk_raw_write_field (fname_out, msh, {uosc, "u_osc"; ugal, "u_galerkin"}, {}); %! unlink (fname); bim-1.1.6/inst/bim3a_osc_laplacian.m000066400000000000000000000073101426641764600173050ustar00rootroot00000000000000## Copyright (C) 2012 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## -*- texinfo -*- ## @deftypefn {Function File} @ ## {@var{A}} = bim3a_osc_laplacian (@var{mesh}, @var{epsilon}) ## ## Build the osc finite element stiffness matrix for a diffusion ## problem. ## ## For details on the Orthogonal Subdomain Collocation (OSC) method ## see: M.Putti and C.Cordes, SIAM J.SCI.COMPUT. Vol.19(4), pp.1154-1168, 1998. ## ## The equation taken into account is: ## ## - div (@var{epsilon} grad (u)) = f ## ## where @var{epsilon} is an element-wise constant scalar function. ## ## @seealso{bim3a_rhs, bim3a_reaction, bim2a_laplacian, bim3a_laplacian} ## @end deftypefn function M = bim3a_osc_laplacian (msh, epsilon) ## Check input if (nargin != 2) print_usage (); elseif (! (isstruct (msh) && isfield (msh, "p") && isfield (msh, "t") && isfield (msh, "e"))) error (["bim3a_osc_laplacian: first input ", ... "is not a valid msh structure"]); endif nnodes = columns (msh.p); nelem = columns (msh.t); ## Turn scalar input to a vector of appropriate size if (isscalar (epsilon)) epsilon = epsilon * ones (nelem, 1); endif if (! isvector (epsilon)) error ("bim3a_osc_laplacian: coefficient is not a vector"); elseif (numel (epsilon) != nelem) error (["bim3a_osc_laplacian: length of epsilon is ", ... "not equal to the number of mesh elements"]); endif ## Avoid warnings for broadcasting warning ("off", "Octave:broadcast", "local") ## Local contributions Lloc = __osc_local_laplacian__ (msh.p, msh.t, msh.shg, epsilon, msh.area, nnodes, nelem); ## Assembly for inode = 1:4 for jnode = 1:4 ginode(inode, jnode,:) = msh.t(inode, :); gjnode(inode, jnode,:) = msh.t(jnode, :); endfor endfor M = sparse (ginode(:), gjnode(:), Lloc(:), nnodes, nnodes); endfunction %!shared msh, epsilon, M, nnodes, nelem, x, y, z %!test %! msh = bim3c_mesh_properties (msh3m_structured_mesh (0:5, 0:5, 0:5, 1, 1:6)); %! x = msh.p (1, :).'; %! y = msh.p (2, :).'; %! z = msh.p (3, :).'; %! u = ones (size (x)); %! M = bim3a_osc_laplacian (msh, 1); %! assert (M * u, zeros (size (u)), eps * 100) %!test %! u = x; %! bnd = bim3c_unknowns_on_faces (msh, [1, 2]); %! int = setdiff (1:columns (msh.p), bnd); %! assert (M(int, int) * u(int), -M(int, bnd) * u(bnd), 100 * eps) %!test %! u = y; %! bnd = bim3c_unknowns_on_faces (msh, [3, 4]); %! int = setdiff (1:columns (msh.p), bnd); %! assert (M(int, int) * u(int), -M(int, bnd) * u(bnd), 100 * eps) %!test %! u = z; %! bnd = bim3c_unknowns_on_faces (msh, [5, 6]); %! int = setdiff (1:columns (msh.p), bnd); %! assert (M(int, int) * u(int), -M(int, bnd) * u(bnd), 100 * eps) %!test %! u = z; %! bnd = bim3c_unknowns_on_faces (msh, [5, 6]); %! int = setdiff (1:columns (msh.p), bnd); %! M = bim3a_osc_laplacian (msh, pi); %! assert (M(int, int) * u(int), -M(int, bnd) * u(bnd), 100 * eps) bim-1.1.6/inst/bim3a_reaction.m000066400000000000000000000062401426641764600163220ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} @ ## {[@var{C}]} = bim3a_reaction (@var{mesh},@var{delta},@var{zeta}) ## ## Build the lumped finite element mass matrix for a diffusion ## problem. ## ## The equation taken into account is: ## ## @var{delta} * @var{zeta} * u = f ## ## where @var{delta} is an element-wise constant scalar function, while ## @var{zeta} is a piecewise linear conforming scalar function. ## ## @seealso{bim3a_rhs, bim3a_laplacian, bim2a_reaction, bim3a_reaction} ## @end deftypefn function [C] = bim3a_reaction (mesh,delta,zeta); ## Check input if nargin != 3 error("bim3a_reaction: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim3a_reaction: first input is not a valid mesh structure."); endif nnodes = columns (mesh.p); nelem = columns (mesh.t); ## Turn scalar input to a vector of appropriate size if isscalar(delta) delta = delta*ones(nelem,1); endif if isscalar(zeta) zeta = zeta*ones(nnodes,1); endif if !( isvector(delta) && isvector(zeta) ) error("bim3a_reaction: coefficients are not valid vectors."); elseif (numel (delta) != nelem) error("bim3a_: length of alpha is not equal to the number of elements."); elseif (numel (zeta) != nnodes) error("bim3a_: length of gamma is not equal to the number of nodes."); endif Cloc = zeros(4,nelem); coeff = zeta(mesh.t(1:4,:)); coeffe = delta; wjacdet = mesh.wjacdet; for inode = 1:4 Cloc(inode,:) = coeffe'.*coeff(inode,:).*wjacdet(inode,:); endfor gnode = (mesh.t(1:4,:)); ## Global matrix C = sparse(gnode(:),gnode(:),Cloc(:)); endfunction %!shared mesh,delta,zeta,nnodes,nelem % x = y = z = linspace(0,1,4); % [mesh] = msh3m_structured_mesh(x,y,z,1,1:6); % [mesh] = bim3c_mesh_properties(mesh); % nnodes = columns(mesh.p); % nelem = columns(mesh.t); % delta = ones(columns(mesh.t),1); % zeta = ones(columns(mesh.p),1); %!test % [C] = bim3a_reaction(mesh,delta,zeta); % assert(size(C),[nnodes, nnodes]); %!test % [C1] = bim3a_reaction(mesh,3*delta,zeta); % [C2] = bim3a_reaction(mesh,delta,3*zeta); % assert(C1,C2); %!test % [C1] = bim2a_reaction(mesh,3*delta,zeta); % [C2] = bim2a_reaction(mesh,3,1); % assert(C1,C2); bim-1.1.6/inst/bim3a_rhs.m000066400000000000000000000060401426641764600153100ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{b}]} = @ ## bim3a_rhs (@var{mesh}, @var{f}, @var{g}) ## ## Build the finite element right-hand side of a diffusion problem ## employing mass-lumping. ## ## The equation taken into account is: ## ## @var{delta} * u = @var{f} * @var{g} ## ## where @var{f} is an element-wise constant scalar function, while ## @var{g} is a piecewise linear conforming scalar function. ## ## @seealso{bim3a_reaction, bim3_laplacian, bim1a_reaction, ## bim2a_reaction} ## @end deftypefn function [b] = bim3a_rhs (mesh,f,g); ## Check input if nargin != 3 error("bim3a_rhs: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim3a_rhs: first input is not a valid mesh structure."); endif nnodes = columns (mesh.p); nelem = columns (mesh.t); ## Turn scalar input to a vector of appropriate size if isscalar(f) f = f*ones(nelem,1); endif if isscalar(g) g = g*ones(nnodes,1); endif if !( isvector(f) && isvector(g) ) error("bim3a_rhs: coefficients are not valid vectors."); elseif (numel (f) != nelem) error("bim3a_rhs: length of f is not equal to the number of elements."); elseif (numel (g) != nnodes) error("bim3a_rhs: length of g is not equal to the number of nodes."); endif bloc = zeros(4,nelem); coeff = g(mesh.t(1:4,:)); coeffe = f; wjacdet = mesh.wjacdet; for inode = 1:4 bloc(inode,:) = coeffe'.*coeff(inode,:).*wjacdet(inode,:); endfor gnode = (mesh.t(1:4,:)); ## Global matrix b = sparse(gnode(:),1,bloc(:)); endfunction %!shared mesh,f,g,nnodes,nelem % x = y = z = linspace(0,1,4); % [mesh] = msh3m_structured_mesh(x,y,z,1,1:6); % [mesh] = bim3c_mesh_properties(mesh); % nnodes = columns(mesh.p); % nelem = columns(mesh.t); % g = ones(columns(mesh.t),1); % f = ones(columns(mesh.p),1); %!test % [b] = bim3a_rhs(mesh,f,g); % assert(size(b),[nnodes, 1]); %!test % [b1] = bim3a_rhs(mesh,3*f,g); % [b2] = bim3a_rhs(mesh,f,3*g); % assert(b1,b2); %!test % [b1] = bim2a_rhs(mesh,3*f,g); % [b2] = bim2a_rhs(mesh,3,1); % assert(b1,b2); bim-1.1.6/inst/bim3c_global_flux.m000066400000000000000000000101371426641764600170160ustar00rootroot00000000000000## Copyright (C) 2012 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{F}]} = @ ## bim3c_global_flux (@var{mesh}, @var{u}, @var{alpha}, @var{v}) ## ## Compute the flux associated with the Scharfetter-Gummel approximation ## of the scalar field @var{u}. ## ## The vector field is defined as: ## ## F =- @var{alpha} ( grad (u) - grad (@var{v}) u ) ## ## where @var{v} is a piecewise linear continuous scalar ## functions and @var{alpha} is a piecewise constant scalar function. ## ## @seealso{bim3a_rhs, bim3a_reaction, bim3a_laplacian, bim3c_mesh_properties} ## @end deftypefn function F = bim3c_global_flux (mesh, u, acoeff, v) t = mesh.t; nelem = columns (mesh.t); F = zeros (3, nelem); ## Local contributions Lloc = zeros (4,4,nelem); epsilonareak = reshape (acoeff .* mesh.area', 1, 1, nelem); shg = mesh.shg(:,:,:); ## Computation for inode = 1:4 for jnode = 1:4 ginode(inode,jnode,:) = t(inode,:); gjnode(inode,jnode,:) = t(jnode,:); Lloc(inode,jnode,:) = sum (shg(:,inode,:) .* shg(:,jnode,:), 1) ... .* epsilonareak; endfor endfor uloc = u(t(1:4, :)); vloc = v(t(1:4, :)); [bp12,bm12] = bimu_bernoulli (vloc(2,:)-vloc(1,:)); [bp13,bm13] = bimu_bernoulli (vloc(3,:)-vloc(1,:)); [bp14,bm14] = bimu_bernoulli (vloc(4,:)-vloc(1,:)); [bp23,bm23] = bimu_bernoulli (vloc(3,:)-vloc(2,:)); [bp24,bm24] = bimu_bernoulli (vloc(4,:)-vloc(2,:)); [bp34,bm34] = bimu_bernoulli (vloc(4,:)-vloc(3,:)); bp12 = reshape (bp12, 1, 1, nelem) .* Lloc(1,2,:); bm12 = reshape (bm12, 1, 1, nelem) .* Lloc(1,2,:); bp13 = reshape (bp13, 1, 1, nelem) .* Lloc(1,3,:); bm13 = reshape (bm13, 1, 1, nelem) .* Lloc(1,3,:); bp14 = reshape (bp14, 1, 1, nelem) .* Lloc(1,4,:); bm14 = reshape (bm14, 1, 1, nelem) .* Lloc(1,4,:); bp23 = reshape (bp23, 1, 1, nelem) .* Lloc(2,3,:); bm23 = reshape (bm23, 1, 1, nelem) .* Lloc(2,3,:); bp24 = reshape (bp24, 1, 1, nelem) .* Lloc(2,4,:); bm24 = reshape (bm24, 1, 1, nelem) .* Lloc(2,4,:); bp34 = reshape (bp34, 1, 1, nelem) .* Lloc(3,4,:); bm34 = reshape (bm34, 1, 1, nelem) .* Lloc(3,4,:); ## SGloc=[... ## -bm12-bm13-bm14,bp12 ,bp13 ,bp14 ## bm12 ,-bp12-bm23-bm24 ,bp23 ,bp24 ## bm13 ,bm23 ,-bp13-bp23-bm34,bp34 ## bm14 ,bm24 ,bm34 ,-bp14-bp24-bp34 ## ]; Sloc(1,1,:) = -bm12-bm13-bm14; Sloc(1,2,:) = bp12; Sloc(1,3,:) = bp13; Sloc(1,4,:) = bp14; Sloc(2,1,:) = bm12; Sloc(2,2,:) = -bp12-bm23-bm24; Sloc(2,3,:) = bp23; Sloc(2,4,:) = bp24; Sloc(3,1,:) = bm13; Sloc(3,2,:) = bm23; Sloc(3,3,:) = -bp13-bp23-bm34; Sloc(3,4,:) = bp34; Sloc(4,1,:) = bm14; Sloc(4,2,:) = bm24; Sloc(4,3,:) = bm34; Sloc(4,4,:) = -bp14-bp24-bp34; r = zeros (4, nelem); f = zeros (3, nelem); for iel = 1:nelem r(:,iel) = Sloc(:,:,iel) * uloc(:,iel); f(:,iel) = Lloc(1:3, 1:3, iel) \ r(1:3, iel); F(:,iel) = shg(:,1:3, iel) * f(:, iel); endfor endfunction %!test %! N = 10; pp = linspace (0, 1, N); msh = bim3c_mesh_properties (msh3m_structured_mesh (pp, pp, pp, 1, 1:6)); %! u = ones (N^3, 1); %! v = ones (N^3, 1); %! alpha = ones (columns (msh.t), 1); %! F = bim3c_global_flux (msh, u, alpha, v); %! assert (norm (F(:), inf), 0, 100*eps); bim-1.1.6/inst/bim3c_intrp.m000066400000000000000000000042271426641764600156570ustar00rootroot00000000000000## Copyright (C) 2011, 2012 Carlo de Falco ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 3 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, see ## . ## -*- texinfo -*- ## ## @deftypefn {Function File} {@var{data}} = bim3c_intrp (@var{msh}, @var{n_data}, @var{e_data}, @var{points}) ## ## Compute interpolated values of node centered multicomponent node centered field @var{n_data} and ## cell centered field @var{n_data} at an arbitrary set of points whos coordinates are given in the ## n_by_3 matrix @var{points}. ## ## @end deftypefn ## Author: Carlo de Falco ## Created: 2012-10-01 function data = bim3c_intrp (msh, n_data, e_data, p) %% for each point, find the enclosing tetrahedron [t_list, b_list] = tsearchn (msh.p.', msh.t(1:4, :)', p); %% only keep points within tetrahedra invalid = isnan (t_list); t_list = t_list (! invalid); ntl = numel (t_list); b_list = b_list(! invalid, :); points(invalid,:) = []; data = []; if (! isempty (n_data)) data = cat (1, data, squeeze ( sum (reshape (n_data(msh.t(1:4, t_list), :), [4, ntl, (columns (n_data))]) .* repmat (b_list.', [1, 1, (columns (n_data))]), 1))); endif if (! isempty (e_data)) data = cat (1, data, e_data(t_list, :)); endif endfunction %!test %! msh = bim3c_mesh_properties (msh3m_structured_mesh (linspace (0, 1, 11), linspace (0, 1, 9), linspace (0, 1, 13), 1, 1:6)); %! x = y = z = linspace (0, 1, 100).'; %! u = msh.p(1, :).'; %! ui = bim3c_intrp (msh, u, [], [x, y, z]); %! assert (ui, linspace (0, 1, 100), 10*eps);bim-1.1.6/inst/bim3c_mesh_properties.m000066400000000000000000000041571426641764600177350ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{omesh}]} = @ ## bim3c_mesh_properties(@var{imesh}) ## ## Compute the properties of @var{imesh} needed by BIM method and append ## them to @var{omesh} as fields. ## ## @seealso{bim3a_reaction, bim3a_rhs, bim3a_laplacian} ## @end deftypefn function [omesh] = bim3c_mesh_properties (imesh) ## Check input if nargin != 1 error("bim3c_mesh_properties: wrong number of input parameters."); elseif (! isstruct (imesh) || any (! isfield (imesh, {"p", "e", "t"}))) error ("bim3c_mesh_properties: first input is not a valid mesh structure."); endif ## Compute properties omesh = imesh; [omesh.wjacdet,omesh.area,omesh.shg,omesh.shp] = ... msh3m_geometrical_properties (imesh, "wjacdet", "area", "shg", "shp"); endfunction %!shared mesh % x = y = z = linspace(0,1,4); % mesh = msh3m_structured_mesh(x,y,z,1,1:6); % mesh = bim3c_mesh_properties (mesh); %!test % tmp = msh3m_geometrical_properties (mesh, "wjacdet"); % assert(mesh.wjacdet,tmp); %!test % tmp = msh3m_geometrical_properties(mesh,"shg"); % assert(mesh.shg,tmp); %!test % tmp = msh3m_geometrical_properties(mesh,"shp"); % assert(mesh.shp,tmp); %!test % assert(mesh.area,sum(mesh.wjacdet,1)); bim-1.1.6/inst/bim3c_norm.m000066400000000000000000000115521426641764600154750ustar00rootroot00000000000000## Copyright (C) 2006-2013 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Matteo Porro ## -*- texinfo -*- ## ## @deftypefn {Function File} {[@var{norm_u}]} = @ ## bim3c_norm(@var{mesh},@var{u},@var{norm_type}) ## ## Compute the @var{norm_type}-norm of function @var{u} on the domain described ## by the tetrahedral grid @var{mesh}. ## ## The input function @var{u} can be either a piecewise linear conforming scalar ## function or an elementwise constant scalar or vector function. ## ## The string parameter @var{norm_type} can be one among 'L2', 'H1' and 'inf'. ## ## Should the input function be piecewise constant, the H1 norm will not be ## computed and the function will return an error message. ## ## For the numerical integration of the L2 norm the second order quadrature rule ## by Keast is used (ref. P. Keast, Moderate degree tetrahedral quadrature ## formulas, CMAME 55: 339-348 1986). ## ## @seealso{bim1c_norm, bim2c_norm} ## ## @end deftypefn function [norm_u] = bim3c_norm (m, u, norm_type) ## Check input if (nargin != 3) error ("bim3c_norm: wrong number of input parameters."); elseif (! (isstruct (m) && isfield (m,"p")) && isfield (m, "t") && isfield (m, "e")) error ("bim3c_norm: first input is not a valid mesh structure."); endif nnodes = columns (m.p); nel = columns (m.t); if (isequal (size (u), [3, nel])) u = u'; endif if ((numel (u) != nnodes) && (rows (u) != nel)) error ("bim3c_norm: length(u) != nnodes and rows(u) != nel."); endif if (! (strcmp (norm_type,'L2') || strcmp (norm_type,'inf') || strcmp (norm_type,'H1'))) error ("bim3c_norm: invalid norm type parameter."); endif if (strcmp (norm_type,'inf')) norm_u = max (abs (u(:))); else if (numel (u) == nnodes) M = __mass_matrix__ (m); if (strcmp (norm_type, 'H1')) A = bim3a_laplacian (m, 1, 1); M += A; endif norm_u = sqrt(u' * M * u); else if (strcmp (norm_type, 'H1')) error (["bim3c_norm: cannot compute the H1 norm", ... "of an elementwise constant function."]); endif norm_u = m.area * (norm (u', 2, 'cols').^2)'; norm_u = sqrt (norm_u); endif endif endfunction function M = __mass_matrix__ (mesh) t = mesh.t; nnodes = columns (mesh.p); nelem = columns (t); ## Local contributions a = (5 + 3 * sqrt (5)) / 20; b = (5 - sqrt (5)) / 20; l1 = (1 - 3*b)^2 + 3*(1 - 2*b - a)^2; l2 = (1 - 3*b)*b + (1 - 2*b - a)*(a + 2*b); Mref = 1/4 * [l1 l2 l2 l2; l2 l1 l2 l2; l2 l2 l1 l2; l2 l2 l2 l1]; area = reshape (mesh.area, 1, 1, nelem); ## Computation for inode = 1:4 for jnode = 1:4 ginode(inode,jnode,:) = t(inode,:); gjnode(inode,jnode,:) = t(jnode,:); endfor endfor Mloc = area .* Mref; ## assemble global matrix M = sparse (ginode(:), gjnode(:), Mloc(:), nnodes, nnodes); endfunction %!test %!shared L, V, x, y, z, m %! L = rand (1); V = rand (1); x = linspace (0,L,4); y = x; z = x; %! m = msh3m_structured_mesh (x,y,z,1,1:6); %! m.area = msh3m_geometrical_properties (m, 'area'); %! m.shg = msh3m_geometrical_properties (m, 'shg'); %! u = V * ones (columns(m.p),1); %! uinf = bim3c_norm (m, u, 'inf'); %! uL2 = bim3c_norm (m, u, 'L2'); %! uH1 = bim3c_norm (m, u, 'H1'); %! assert ([uinf, uL2, uH1], [V, V*sqrt(L^3), V*sqrt(L^3)], 1e-12); %!test %! u = V * (m.p(1,:) + 2*m.p(2,:) + 3*m.p(3,:))'; %! uinf = bim3c_norm (m, u, 'inf'); %! uL2 = bim3c_norm (m, u, 'L2'); %! uH1 = bim3c_norm (m, u, 'H1'); %! assert ([uinf, uL2, uH1], %! [6*L*V, V*sqrt(61/6*L^5), V*sqrt(61/6*L^5 + 14*L^3)], %! 1e-12); %!test %! u = V * ones (columns(m.t),1); %! uinf = bim3c_norm (m, u, 'inf'); %! uL2 = bim3c_norm (m, u, 'L2'); %! assert ([uinf, uL2], [V, V*sqrt(L^3)], 1e-12); %!test %! u = V * ones (columns(m.t),1); %! uvect = [u, 2*u, 3*u]; %! uinf = bim3c_norm (m, uvect, 'inf'); %! uL2 = bim3c_norm (m, uvect, 'L2'); %! assert ([uinf, uL2], [3*V, V*sqrt(14*L^3)], 1e-12); bim-1.1.6/inst/bim3c_pde_gradient.m000066400000000000000000000035261426641764600171510ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## ## @deftypefn {Function File} {[@var{gx}, @var{gy}, @var{gz}]} = @ ## bim3c_pde_gradient(@var{mesh},@var{u}) ## ## Compute the gradient of the piecewise linear conforming scalar ## function @var{u}. ## ## @seealso{bim3c_global_flux} ## @end deftypefn function [gx, gy, gz] = bim3c_pde_gradient (mesh, u) ## Check input if (nargin != 2) error("bim3c_pde_gradient: wrong number of input parameters."); elseif (! (isstruct (mesh) && isfield (mesh,"p")) && isfield (mesh, "t") && isfield(mesh, "e")) error ("bim3c_pde_gradient: first input is not a valid mesh structure."); endif nnodes = columns (mesh.p); if (numel (u) != nnodes) error ("bim3c_pde_gradient: length(u) != nnodes."); endif gx = sum (squeeze (mesh.shg(1,:,:)) .* u(mesh.t(1:4,:)), 1); gy = sum (squeeze (mesh.shg(2,:,:)) .* u(mesh.t(1:4,:)), 1); gz = sum (squeeze (mesh.shg(3,:,:)) .* u(mesh.t(1:4,:)), 1); endfunction bim-1.1.6/inst/bim3c_tri_to_nodes.m000066400000000000000000000052261426641764600172130ustar00rootroot00000000000000## Copyright (C) 2011, 2012 Carlo de Falco ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 3 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, see ## . ## -*- texinfo -*- ## ## @deftypefn {Function File} {@var{u_nod}} = bim3c_tri_to_nodes (@var{mesh}, @var{u_tri}) ## @deftypefnx {Function File} {@var{u_nod}} = bim3c_tri_to_nodes (@var{m_tri}, @var{u_tri}) ## @deftypefnx {Function File} {[@var{u_nod}, @var{m_tri}]} = bim3c_tri_to_nodes ( ... ) ## ## Compute interpolated values at triangle nodes @var{u_nod} given values at tetrahedral centers of mass @var{u_tri}. ## If called with more than one output, also return the interpolation matrix @var{m_tri} such that ## @code{u_nod = m_tri * u_tri}. ## If repeatedly performing interpolation on the same mesh the matrix @var{m_tri} obtained by a previous call ## to @code{bim2c_tri_to_nodes} may be passed as input to avoid unnecessary computations. ## ## @end deftypefn ## Author: Carlo de Falco ## Created: 2011-03-07 function [u_nod, m_tri] = bim3c_tri_to_nodes (m, u_tri) if (nargout > 1) if (isstruct (m)) nel = columns (m.t); nnod = columns (m.p); ii = m.t(1:4, :); jj = repmat (1:nel, 4, 1); vv = repmat (m.area(:)', 4, 1) / 4; m_tri = bim3a_reaction (m, 1, 1) \ sparse (ii, jj, vv, nnod, nel); elseif (ismatrix (m)) m_tri = m; else error ("bim3c_tri_to_nodes: first input parameter is of incorrect type"); endif u_nod = m_tri * u_tri; else if (isstruct (m)) rhs = bim3a_rhs (m, u_tri, 1); mass = bim3a_reaction (m, 1, 1); u_nod = full (mass \ rhs); elseif (ismatrix (m)) u_nod = m * u_tri; else error ("bim3c_tri_to_nodes: first input parameter is of incorrect type"); endif endif endfunction %!test %! msh = bim3c_mesh_properties (msh3m_structured_mesh (linspace (0, 1, 31), linspace (0, 1, 13), linspace (0, 1, 13), 1, 1:6)); %! nel = columns (msh.t); %! nnod = columns (msh.p); %! u_tri = randn (nel, 1); %! un1 = bim3c_tri_to_nodes (msh, u_tri); %! [un2, m] = bim3c_tri_to_nodes (msh, u_tri); %! assert (un1, un2, 1e-10) bim-1.1.6/inst/bim3c_unknowns_on_faces.m000066400000000000000000000040351426641764600202370ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{nodelist}]} = @ ## bim3c_unknowns_on_faces(@var{mesh},@var{facelist}) ## ## Return the list of the mesh nodes that lie on the geometrical faces ## specified in @var{facelist}. ## ## @seealso{bim3c_unknown_on_faces, bim2c_pde_gradient, ## bim2c_global_flux} ## ## @end deftypefn function [nodelist] = bim3c_unknowns_on_faces(mesh,facelist) ## Check input if nargin != 2 error("bim3c_unknowns_on_faces: wrong number of input parameters."); elseif !(isstruct(mesh) && isfield(mesh,"p") && isfield (mesh,"t") && isfield(mesh,"e")) error("bim3c_unknowns_on_faces: first input is not a valid mesh structure."); elseif !isnumeric(facelist) error("bim3c_unknowns_on_faces: second input is not a valid numeric vector."); endif [nodelist] = msh3m_nodes_on_faces(mesh,facelist); endfunction %!shared mesh % x = y = z = linspace(0,1,2); % [mesh] = msh3m_structured_mesh(x,y,z,1,1:6); %!test % assert( bim3c_unknowns_on_faces(mesh, 1),[1 2 5 6] ) %!test % assert( bim3c_unknowns_on_faces(mesh, 2),[3 4 7 8] ) %!test % assert( bim3c_unknowns_on_faces(mesh, [1 2]),1:8)bim-1.1.6/inst/bimu_bernoulli.m000066400000000000000000000041371426641764600164550ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {[@var{bp}, @var{bn}]} = bimu_bernoulli (@var{x}) ## ## Compute the values of the Bernoulli function corresponding to @var{x} ## and - @var{x} arguments. ## ## @seealso{bimu_logm} ## @end deftypefn function [bp,bn] = bimu_bernoulli(x) ## Check input if nargin != 1 error("bimu_bernoulli: wrong number of input parameters."); endif xlim= 1e-2; ax = abs(x); bp = zeros(size(x)); bn = bp; block1 = find(~ax); block21 = find((ax>80)&x>0); block22 = find((ax>80)&x<0); block3 = find((ax<=80)&(ax>xlim)); block4 = find((ax<=xlim)&(ax~=0)); ## X=0 bp(block1)=1.; bn(block1)=1.; ## ASYMPTOTICS bp(block21)=0.; bn(block21)=x(block21); bp(block22)=-x(block22); bn(block22)=0.; ## INTERMEDIATE VALUES bp(block3)=x(block3)./(exp(x(block3))-1); bn(block3)=x(block3)+bp(block3); ## SMALL VALUES if(any(block4))jj=1; fp=1.*ones(size(block4)); fn=fp; df=fp; segno=1.; while (norm(df,inf) > eps), jj=jj+1; segno=-segno; df=df.*x(block4)/jj; fp=fp+df; fn=fn+segno*df; endwhile; bp(block4)=1./fp; bn(block4)=1./fn; endif endfunction bim-1.1.6/inst/bimu_logm.m000066400000000000000000000031211426641764600154100ustar00rootroot00000000000000## Copyright (C) 2006,2007,2008,2009,2010 Carlo de Falco, Massimiliano Culpo ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## author: Massimiliano Culpo ## -*- texinfo -*- ## @deftypefn {Function File} @ ## {[@var{T}]} = bimu_logm (@var{t1},@var{t2}) ## ## Input: ## @itemize @minus ## @item @var{t1}: ## @item @var{t2}: ## @end itemize ## ## Output: ## @itemize @minus ## @item @var{T}: ## @end itemize ## ## @seealso{bimu_bern} ## @end deftypefn function [T] = bimu_logm(t1,t2) ## Check input if nargin != 2 error("bimu_logm: wrong number of input parameters."); elseif size(t1) != size(t2) error("bimu_logm: t1 and t2 are of different size."); endif T = zeros(size(t2)); sing = abs(t2-t1)< 100*eps ; T(sing) = (t2(sing)+t1(sing))/2; T(~sing) = (t2(~sing)-t1(~sing))./log(t2(~sing)./t1(~sing)); endfunction bim-1.1.6/inst/private/000077500000000000000000000000001426641764600147355ustar00rootroot00000000000000bim-1.1.6/inst/private/__osc_local_laplacian__.m000066400000000000000000000100451426641764600216510ustar00rootroot00000000000000## Copyright (C) 2012 Carlo de Falco ## ## This file is part of: ## BIM - Diffusion Advection Reaction PDE Solver ## ## BIM is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## BIM is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with BIM; If not, see . ## ## author: Carlo de Falco ## -*- texinfo -*- ## ## @deftypefn {Function File} @ ## {@var{Lloc}} = __osc_local_laplacian__ @ ## (@var{p}, @var{t}, @var{shg}, @var{epsilon}, @var{area}, @var{nnodes}, @var{nelem}) ## ## Unocumented private function. ## ## @end deftypefn function Lloc = __osc_local_laplacian__ (p, t, shg, epsilon, area, nnodes, nelem) Lloc = zeros (4, 4, nelem); epsilonbyareak = epsilon(:) ./ abs (area(:)) / 48; A = zeros (3, 4, nelem); ## Computation for inode = 1:4 A(:, inode, :) = 3 * abs (area) .* squeeze (shg (:, inode, :)); endfor Ann = squeeze (sum (A .^ 2, 1)); r12 = p(:, t (2, :)) - p(:, t (1, :)); r13 = p(:, t (3, :)) - p(:, t (1, :)); r14 = p(:, t (4, :)) - p(:, t (1, :)); r23 = p(:, t (3, :)) - p(:, t (2, :)); r24 = p(:, t (4, :)) - p(:, t (2, :)); r34 = p(:, t (4, :)) - p(:, t (3, :)); s12 = - epsilonbyareak .* (2 * (dot (r13, r23, 1) .* dot (r14, r24, 1))(:) + squeeze (dot (A(:, 3, :), A(:, 4, :), 1)) .* (dot ( r13, r23, 1) .^ 2 ./ Ann(4, :) + dot ( r14, r24, 1).^ 2 ./ Ann(3, :))(:)); s13 = - epsilonbyareak .* (2 * (dot (r12, -r23, 1) .* dot (r14, r34, 1))(:) + squeeze (dot (A(:, 2, :), A(:, 4, :), 1)) .* (dot ( r12, -r23, 1) .^ 2 ./ Ann(4, :) + dot ( r14, r34, 1).^ 2 ./ Ann(2, :))(:)); s14 = - epsilonbyareak .* (2 * (dot ( r12, -r24, 1) .* dot ( r13, -r34, 1))(:) + squeeze (dot (A(:, 2, :), A(:, 3, :), 1)) .* (dot ( r12, -r24, 1) .^ 2 ./ Ann(3, :) + dot ( r13, -r34, 1).^ 2 ./ Ann(2, :))(:)); s23 = - epsilonbyareak .* (2 * (dot (-r12, -r13, 1) .* dot ( r24, r34, 1))(:) + squeeze (dot (A(:, 1, :), A(:, 4, :), 1)) .* (dot (-r12, -r13, 1) .^ 2 ./ Ann(4, :) + dot ( r24, r34, 1).^ 2 ./ Ann(1, :))(:)); s24 = - epsilonbyareak .* (2 * (dot (-r12, -r14, 1) .* dot ( r23, -r34, 1))(:) + squeeze (dot (A(:, 1, :), A(:, 3, :), 1)) .* (dot (-r12, -r14, 1) .^ 2 ./ Ann(3, :) + dot ( r23, -r34, 1).^ 2 ./ Ann(1, :))(:)); s34 = - epsilonbyareak .* (2 * (dot (-r13, -r14, 1) .* dot (-r23, -r24, 1))(:) + squeeze (dot (A(:, 1, :), A(:, 2, :), 1)) .* (dot (-r13, -r14, 1) .^ 2 ./ Ann(2, :) + dot (-r23, -r24, 1).^ 2 ./ Ann(1, :))(:)); Lloc(1, 2, :) = s12; Lloc(2, 1, :) = s12; Lloc(1, 3, :) = s13; Lloc(3, 1, :) = s13; Lloc(1, 4, :) = s14; Lloc(4, 1, :) = s14; Lloc(1, 1, :) = -(s12+s13+s14); Lloc(2, 3, :) = s23; Lloc(3, 2, :) = s23; Lloc(2, 4, :) = s24; Lloc(4, 2, :) = s24; Lloc(2, 2, :) = -(s12+s23+s24); Lloc(3, 4, :) = s34; Lloc(4, 3, :) = s34; Lloc(3, 3, :) = -(s13+s23+s34); Lloc(4, 4, :) = -(s14+s24+s34); endfunction