├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── LICENSE ├── README.md ├── mshint ├── bucket.c ├── hash.c ├── inout.c ├── locelt.c ├── mi_calls.c ├── mi_calls.h ├── mshint.c ├── mshint.h └── mshint1.c └── mshmet ├── hash.c ├── inout.c ├── ms_calls.c ├── ms_calls.h ├── mshmet.c ├── mshmet.h ├── mshmet1_2d.c └── mshmet1_3d.c /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeFiles/* 2 | Makefile 3 | cmake_install.cmake 4 | CMakeCache.txt 5 | build/* 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c++ 2 | compiler: 3 | - gcc 4 | - clang 5 | os: 6 | - linux 7 | - osx 8 | before_script: 9 | - git clone https://github.com/ICStoolbox/Commons.git 10 | - mkdir Commons/build 11 | - cd Commons/build 12 | - cmake .. 13 | - make 14 | - make install 15 | - cd - 16 | script: 17 | - mkdir build 18 | - cd build 19 | - cmake .. 20 | - make 21 | - cd - 22 | notifications: 23 | email: false 24 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | set(CMAKE_BUILD_TYPE "Release") 3 | INCLUDE_DIRECTORIES( "$ENV{HOME}/include") 4 | LINK_DIRECTORIES( "$ENV{HOME}/lib") 5 | SET(CMAKE_CXX_FLAGS "-Wunused-result -Wno-dev -g -O3") 6 | set(CMAKE_MACOSX_RPATH 1) 7 | set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) 8 | 9 | #MshInt 10 | project(_mshint) 11 | #library 12 | file( GLOB_RECURSE source_files mshint/*) 13 | file( GLOB_RECURSE header_files mshint/*.h) 14 | ADD_LIBRARY( MshInt_a SHARED ${source_files}) 15 | find_library( Commons NAMES Commons HINTS "$ENV{HOME}/lib" 16 | DOC "Commons library (https://github.com/ISCDtoolbox/Commons)") 17 | target_link_libraries( MshInt_a ${Commons}) 18 | INSTALL( TARGETS MshInt_a LIBRARY DESTINATION "$ENV{HOME}/lib") 19 | INSTALL( FILES ${header_files} DESTINATION "$ENV{HOME}/include") 20 | #Executable 21 | add_executable( mshint mshint/mshint.c) 22 | target_link_libraries( mshint Commons MshInt_a) 23 | INSTALL( TARGETS mshint RUNTIME DESTINATION "$ENV{HOME}/bin") 24 | set_property(TARGET mshint PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE) 25 | 26 | #MshMet 27 | project(_mshmet) 28 | #library 29 | file( GLOB_RECURSE source_files mshmet/*) 30 | file( GLOB_RECURSE header_files mshmet/*.h) 31 | ADD_LIBRARY( MshMet_a SHARED ${source_files}) 32 | find_library( Commons NAMES Commons HINTS "$ENV{HOME}/lib") 33 | target_link_libraries( MshMet_a ${Commons}) 34 | INSTALL( TARGETS MshMet_a LIBRARY DESTINATION "$ENV{HOME}/lib") 35 | INSTALL( FILES ${header_files} DESTINATION "$ENV{HOME}/include") 36 | #Executable 37 | add_executable( mshmet mshmet/mshmet.c) 38 | target_link_libraries( mshmet Commons MshMet_a) 39 | INSTALL( TARGETS mshmet RUNTIME DESTINATION "$ENV{HOME}/bin") 40 | set_property(TARGET mshmet PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE) 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AdaptTools [![Build Status](https://travis-ci.org/ISCDtoolbox/AdaptTools.svg?branch=master)](https://travis-ci.org/ISCDtoolbox/AdaptTools) 2 | 3 | This directory contains a set of tools for mesh adaptation intwo and three dimensions. They can be used and combined in a Linux/Unix/Posix-compliant shell script wrapper or called as functions from C/C++/F programs. 4 | 5 | #### Mesh adaptation 6 | Mesh adaptation strategy has proved to be very efficient in numerical solutions of PDEs. The ability to adapt the elements geometric properties (size, shape and orientation) according to specific quantities related to the data greatly improves the accuracy of the solution and the convergence rate of numerical schemes. 7 | 8 | The approach pionneered in our group (F. Hecht et al.) relies on the construction of nearly uniform simplicial meshes in a metric space. The underlying metric is related to the (approximate) Hessian of the solution of the problem. 9 | ![aniso](https://cloud.githubusercontent.com/assets/11386916/13727601/ecdd2862-e8f8-11e5-97ed-1e436c98a851.png) 10 | 11 | #### Installation 12 | 1. Install the [ISCD Commons Library](https://github.com/ISCDtoolbox/Commons) on your system. 13 | Please refer to the instructions provided on the ICS Commons Library page in order to install this library. 14 | 15 | 2. download the zip archive of AdaptTools or clone this repository: 16 | 17 | ` git clone https://github.com/ISCDtoolbox/AdaptTools.git ` 18 | 19 | navigate to the downloaded directory: 20 | 21 | ` cd AdaptTools ` 22 | 23 | create a build directory and compile with cmake 24 | ``` 25 | mkdir build 26 | cd build 27 | cmake .. 28 | make 29 | make install 30 | ``` 31 | 32 | #### Remark on mshmet software 33 | * The `master` branch provides a library mode but, for now, 3D meshes aren't handled; 34 | * the `olderVersionThatWorks` branch provides metric computation for both 2D, 3D volume and 3D surface meshes but doesn't provides the library mode. 35 | 36 | #### Authors & contributors 37 | * these tools have been developed and contributed over the years by several contributors. Main developers are Charles Dapogny (Université J. Fourier), Cécile Dobrzynski (Université de Bordeaux, INRIA) and Pascal Frey (Université Pierre et Marie Curie). 38 | * Contributors to this project are warmly welcomed. 39 | 40 | #### License 41 | AdaptTools is given under the [terms of the GNU Lesser General Public License] (LICENSE.md). 42 | 43 | -------------------------------------------------------------------------------- /mshint/bucket.c: -------------------------------------------------------------------------------- 1 | #include "mshint.h" 2 | 3 | 4 | /* create and manage bucket structure */ 5 | Bucket *bucket_3d(Mesh *mesh,int nmax) { 6 | pPoint ppt; 7 | Bucket *bck; 8 | double dx,dy,dz; 9 | int k,c,i,j,l; 10 | 11 | /* memory alloc */ 12 | bck = (Bucket*)calloc(1,sizeof(Bucket)); 13 | assert(bck); 14 | bck->size = nmax; 15 | bck->cell = (int*)calloc(nmax*nmax*nmax,sizeof(int)); 16 | assert(bck->cell); 17 | 18 | /* insert vertices */ 19 | dx = (nmax-1) / (mesh->max[0] - mesh->min[0]); 20 | dy = (nmax-1) / (mesh->max[1] - mesh->min[1]); 21 | dz = (nmax-1) / (mesh->max[2] - mesh->min[2]); 22 | for (k=1; k<=mesh->np; k++) { 23 | ppt = &mesh->point[k]; 24 | i = (int)(dx * (ppt->c[0] - mesh->min[0])); 25 | j = (int)(dy * (ppt->c[1] - mesh->min[1])); 26 | l = (int)(dz * (ppt->c[2] - mesh->min[2])); 27 | c = (l*bck->size +j)*bck->size + i; 28 | 29 | if ( !bck->cell[c] ) bck->cell[c] = ppt->s; 30 | } 31 | 32 | return(bck); 33 | } 34 | 35 | 36 | /* create and manage bucket structure */ 37 | Bucket *bucket_2d(Mesh *mesh,int nmax) { 38 | pPoint ppt; 39 | Bucket *bck; 40 | double dx,dy; 41 | int k,c,i,j; 42 | 43 | /* memory alloc */ 44 | bck = (Bucket*)calloc(1,sizeof(Bucket)); 45 | assert(bck); 46 | bck->size = nmax; 47 | bck->cell = (int*)calloc(nmax*nmax,sizeof(int)); 48 | assert(bck->cell); 49 | 50 | /* insert vertices */ 51 | dx = (bck->size-1) / (mesh->max[0] - mesh->min[0]); 52 | dy = (bck->size-1) / (mesh->max[1] - mesh->min[1]); 53 | for (k=1; k<=mesh->np; k++) { 54 | ppt = &mesh->point[k]; 55 | i = (int)(dx * (ppt->c[0] - mesh->min[0])); 56 | j = (int)(dy * (ppt->c[1] - mesh->min[1])); 57 | c = j*bck->size + i; 58 | 59 | if ( !bck->cell[c] ) bck->cell[c] = ppt->s; 60 | } 61 | 62 | return(bck); 63 | } 64 | 65 | 66 | /* return simplex in bucket cell close to c */ 67 | int buckin_3d(Mesh *mesh,Bucket *bck,double *c) { 68 | double dx,dy,dz; 69 | int k,i,j,ii,jj,ll,b,d; 70 | 71 | dx = (bck->size-1) / (mesh->max[0] - mesh->min[0]); 72 | dy = (bck->size-1) / (mesh->max[1] - mesh->min[1]); 73 | dz = (bck->size-1) / (mesh->max[2] - mesh->min[2]); 74 | ii = (int)(dx * (c[0] - mesh->min[0])); 75 | jj = (int)(dy * (c[1] - mesh->min[1])); 76 | ll = (int)(dz * (c[2] - mesh->min[2])); 77 | b = (ll*bck->size +jj)*bck->size + ii; 78 | 79 | /* check current cell */ 80 | if ( bck->cell[b] ) 81 | return(bck->cell[b]); 82 | /* explore neighboring cells */ 83 | else { 84 | d = 1; 85 | do { 86 | for (k=MI_MAX(0,ll-d); ksize,ll+d); k++) { 87 | for (j=MI_MAX(0,jj-d); jsize,jj+d); j++) { 88 | for (i=MI_MAX(0,ii-d); isize,ii+d); i++) { 89 | b = (k*bck->size +j)*bck->size + i; 90 | if ( bck->cell[b] ) return(bck->cell[b]); 91 | } 92 | } 93 | } 94 | } 95 | while ( ++d < bck->size/8 ); 96 | } 97 | 98 | return(0); 99 | } 100 | 101 | 102 | /* return simplex in bucket cell close to c */ 103 | int buckin_2d(Mesh *mesh,Bucket *bck,double *c) { 104 | double dx,dy; 105 | int k,i,j,ii,jj,b,d; 106 | 107 | dx = (bck->size-1) / (mesh->max[0] - mesh->min[0]); 108 | dy = (bck->size-1) / (mesh->max[1] - mesh->min[1]); 109 | ii = (int)(dx * (c[0] - mesh->min[0])); 110 | jj = (int)(dy * (c[1] - mesh->min[1])); 111 | b = jj*bck->size + ii; 112 | 113 | /* check current cell */ 114 | if ( bck->cell[b] ) 115 | return(bck->cell[b]); 116 | /* explore neighboring cells */ 117 | else { 118 | d = 1; 119 | do { 120 | for (j=MI_MAX(0,jj-d); jsize,jj+d); j++) { 121 | for (i=MI_MAX(0,ii-d); isize,ii+d); i++) { 122 | b = j*bck->size + i; 123 | if ( bck->cell[b] ) return(bck->cell[b]); 124 | } 125 | } 126 | } 127 | while ( ++d < bck->size/8 ); 128 | } 129 | 130 | return(0); 131 | } 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /mshint/hash.c: -------------------------------------------------------------------------------- 1 | #include "mshint.h" 2 | 3 | 4 | /* data structures */ 5 | typedef struct { 6 | int min,max,nxt,elt,ind; 7 | } Cell; 8 | 9 | typedef struct { 10 | Cell *cell; 11 | int nmax,hsiz,hnxt; 12 | } Htab; 13 | 14 | 15 | static int hcode_3d(Tetra *tetra,Htab *ht,int a,int b,int c,int k,int i) { 16 | Cell *pc; 17 | pTetra pt,pt1; 18 | int adj,sum,min,max; 19 | 20 | sum = a+b+c; 21 | if ( sum >= ht->nmax ) return(0); 22 | 23 | /* check if edge ab stored */ 24 | sum = sum % ht->hsiz; 25 | pc = &ht->cell[sum]; 26 | min = MI_MIN3(a,b,c); 27 | max = MI_MAX3(a,b,c); 28 | if ( !pc->min ) { 29 | pc->min = min; 30 | pc->max = max; 31 | pc->elt = k; 32 | pc->ind = i; 33 | return(1); 34 | } 35 | 36 | /* analyze linked list */ 37 | pt = &tetra[k]; 38 | do { 39 | pt1 = &tetra[pc->elt]; 40 | if ( pc->min == min && pc->max == max ) { 41 | adj = pt1->adj[pc->ind]; 42 | if ( !adj ) { 43 | pt->adj[i] = 4*pc->elt+pc->ind; 44 | pt1->adj[pc->ind] = 4*k+i; 45 | } 46 | return(1); 47 | } 48 | else if ( !pc->nxt ) { 49 | pc->nxt = ht->hnxt; 50 | pc = &ht->cell[ht->hnxt]; 51 | if ( !pc ) return(0); 52 | pc->min = min; 53 | pc->max = max; 54 | pc->elt = k; 55 | pc->ind = i; 56 | ht->hnxt = pc->nxt; 57 | pc->nxt = 0; 58 | 59 | /* check for size overflow */ 60 | if ( !ht->hnxt ) return(0); 61 | return(1); 62 | } 63 | pc = &ht->cell[pc->nxt]; 64 | } while (1); 65 | 66 | return(0); 67 | } 68 | 69 | 70 | /* insert edge a,b and update adjacent triangle */ 71 | static int hcode_2d(Tria *tria,Htab *ht,int a,int b,int k,int i) { 72 | Cell *pc; 73 | pTria pt,pt1; 74 | int abmin,adj,sum; 75 | 76 | sum = a+b; 77 | if ( sum >= ht->nmax ) return(0); 78 | 79 | /* check if edge ab stored */ 80 | pc = &ht->cell[sum]; 81 | abmin = MI_MIN(a,b); 82 | if ( !pc->min ) { 83 | pc->min = abmin; 84 | pc->elt = k; 85 | pc->ind = i; 86 | return(1); 87 | } 88 | 89 | /* analyze linked list */ 90 | pt = &tria[k]; 91 | do { 92 | pt1 = &tria[pc->elt]; 93 | if ( pc->min == abmin ) { 94 | adj = pt1->adj[pc->ind]; 95 | if ( !adj ) { 96 | pt->adj[i] = 3*pc->elt+pc->ind; 97 | pt1->adj[pc->ind] = 3*k+i; 98 | } 99 | return(1); 100 | } 101 | else if ( !pc->nxt ) { 102 | pc->nxt = ht->hnxt; 103 | pc = &ht->cell[ht->hnxt]; 104 | if ( !pc ) return(0); 105 | pc->min = abmin; 106 | pc->elt = k; 107 | pc->ind = i; 108 | ht->hnxt = pc->nxt; 109 | pc->nxt = 0; 110 | 111 | /* check for size overflow */ 112 | if ( !ht->hnxt ) return(0); 113 | return(1); 114 | } 115 | pc = &ht->cell[pc->nxt]; 116 | } while (1); 117 | 118 | return(0); 119 | } 120 | 121 | 122 | /* hash mesh edges for creating P2 nodes */ 123 | int hashel_3d(MIst *mist) { 124 | Htab ht; 125 | pTetra pt; 126 | pPoint ppt; 127 | int k,nt; 128 | char i,i1,i2,i3; 129 | 130 | if ( mist->info.verb != '0' ) fprintf(stdout," Adjacency table: "); 131 | 132 | /* alloc hash */ 133 | ht.nmax = (int)(12.71 * mist->msrc.np); 134 | ht.cell = (Cell*)calloc(ht.nmax+2,sizeof(Cell)); 135 | assert(ht.cell); 136 | 137 | ht.hsiz = mist->msrc.np; 138 | ht.hnxt = ht.hsiz; 139 | for (k=ht.hsiz; kmsrc.ne; k++) { 145 | pt = &mist->msrc.tetra[k]; 146 | for (i=0; i<4; i++) { 147 | i1 = (i+1) % 4; 148 | i2 = (i+2) % 4; 149 | i3 = (i+3) % 4; 150 | if ( !hcode_3d(mist->msrc.tetra,&ht,pt->v[i1],pt->v[i2],pt->v[i3],k,i) ) return(0); 151 | nt++; 152 | } 153 | } 154 | 155 | /* add seed with point */ 156 | for (k=1; k<=mist->msrc.ne; k++) { 157 | pt = &mist->msrc.tetra[k]; 158 | for (i=0; i<4; i++) { 159 | if ( !pt->adj[i] ) mist->msrc.point[pt->v[i]].s = k; 160 | } 161 | } 162 | for (k=1; k<=mist->msrc.ne; k++) { 163 | pt = &mist->msrc.tetra[k]; 164 | for (i=0; i<4; i++) { 165 | ppt = &mist->msrc.point[pt->v[i]]; 166 | if ( !ppt->s ) ppt->s = k; 167 | } 168 | } 169 | free(ht.cell); 170 | 171 | if ( mist->info.verb != '0' ) fprintf(stdout," %d updated\n",nt); 172 | 173 | return(1); 174 | } 175 | 176 | 177 | /* build adjacency table */ 178 | int hashel_2d(MIst *mist) { 179 | Htab ht; 180 | pTria pt; 181 | pPoint ppt; 182 | int k,na; 183 | char i,i1,i2; 184 | 185 | if ( mist->info.verb == '+' ) fprintf(stdout," Adjacency table: "); 186 | 187 | /* alloc hash */ 188 | ht.nmax = (int)(3.71 * mist->msrc.np); 189 | ht.cell = (Cell*)calloc(ht.nmax+2,sizeof(Cell)); 190 | assert(ht.cell); 191 | 192 | ht.hsiz = 2 * mist->msrc.np; 193 | ht.hnxt = ht.hsiz; 194 | for (k=ht.hsiz; kmsrc.nt; k++) { 200 | pt = &mist->msrc.tria[k]; 201 | for (i=0; i<3; i++) { 202 | i1 = (i+1) % 3; 203 | i2 = (i+2) % 3; 204 | if ( !hcode_2d(mist->msrc.tria,&ht,pt->v[i1],pt->v[i2],k,i) ) return(0); 205 | na++; 206 | } 207 | } 208 | 209 | /* add seed with point */ 210 | for (k=1; k<=mist->msrc.nt; k++) { 211 | pt = &mist->msrc.tria[k]; 212 | for (i=0; i<3; i++) { 213 | if ( !pt->adj[i] ) mist->msrc.point[pt->v[(i+1)%3]].s = k; 214 | } 215 | } 216 | for (k=1; k<=mist->msrc.nt; k++) { 217 | pt = &mist->msrc.tria[k]; 218 | for (i=0; i<3; i++) { 219 | ppt = &mist->msrc.point[pt->v[i]]; 220 | if ( !ppt->s ) ppt->s = k; 221 | } 222 | } 223 | free(ht.cell); 224 | 225 | if ( mist->info.verb == '+' ) fprintf(stdout," %d updated\n",na); 226 | 227 | return(1); 228 | } -------------------------------------------------------------------------------- /mshint/inout.c: -------------------------------------------------------------------------------- 1 | #include "mshint.h" 2 | #include "mi_calls.h" 3 | 4 | 5 | /* read mesh */ 6 | int loadMesh(Mesh *mesh,char verb) { 7 | pPoint ppt; 8 | pTetra pt; 9 | pTria pt1; 10 | double dd; 11 | float fp1,fp2,fp3; 12 | int inm,i,k,ref; 13 | char *ptr,data[256]; 14 | 15 | strcpy(data,mesh->name); 16 | ptr = strstr(data,".mesh"); 17 | if ( !ptr ) { 18 | strcat(data,".meshb"); 19 | if ( !(inm = GmfOpenMesh(data,GmfRead,&mesh->ver,&mesh->dim)) ) { 20 | ptr = strstr(data,".mesh"); 21 | *ptr = '\0'; 22 | strcat(data,".mesh"); 23 | if ( !(inm = GmfOpenMesh(data,GmfRead,&mesh->ver,&mesh->dim)) ) { 24 | if ( verb != '0' ) fprintf(stderr," # %s: file not found.\n",data); 25 | return(0); 26 | } 27 | } 28 | } 29 | else if ( !(inm = GmfOpenMesh(data,GmfRead,&mesh->ver,&mesh->dim)) ) { 30 | if ( verb != '0' ) fprintf(stderr," # %s: file not found.\n",data); 31 | return(0); 32 | } 33 | 34 | if ( verb != '0' ) fprintf(stdout," %s:",data); 35 | 36 | mesh->np = GmfStatKwd(inm,GmfVertices); 37 | mesh->nt = GmfStatKwd(inm,GmfTriangles); 38 | mesh->ne = GmfStatKwd(inm,GmfTetrahedra); 39 | if ( !mesh->np ) { 40 | if ( verb != '0' ) fprintf(stdout,"\n # missing data\n"); 41 | return(0); 42 | } 43 | 44 | /* memory allocation */ 45 | mesh->point = (Point*)calloc(mesh->np+1,sizeof(Point)); 46 | assert(mesh->point); 47 | /* read mesh vertices */ 48 | GmfGotoKwd(inm,GmfVertices); 49 | if ( mesh->dim == 2 ) { 50 | /* 2d mesh */ 51 | mesh->min[0] = mesh->min[1] = FLT_MAX; 52 | mesh->max[0] = mesh->max[1] = -FLT_MAX; 53 | for (k=1; k<=mesh->np; k++) { 54 | ppt = &mesh->point[k]; 55 | if ( mesh->ver == GmfFloat ) { 56 | GmfGetLin(inm,GmfVertices,&fp1,&fp2,&ref); 57 | ppt->c[0] = fp1; 58 | ppt->c[1] = fp2; 59 | } 60 | else 61 | GmfGetLin(inm,GmfVertices,&ppt->c[0],&ppt->c[1],&ref); 62 | for (i=0; i<2; i++) { 63 | mesh->min[i] = MI_MIN(ppt->c[i],mesh->min[i]); 64 | mesh->max[i] = MI_MAX(ppt->c[i],mesh->max[i]); 65 | } 66 | } 67 | } 68 | else { 69 | /* 3d mesh */ 70 | mesh->min[0] = mesh->min[1] = mesh->min[2] = FLT_MAX; 71 | mesh->max[0] = mesh->max[1] = mesh->max[2] = -FLT_MAX; 72 | for (k=1; k<=mesh->np; k++) { 73 | ppt = &mesh->point[k]; 74 | if ( mesh->ver == GmfFloat ) { 75 | GmfGetLin(inm,GmfVertices,&fp1,&fp2,&fp3,&ref); 76 | ppt->c[0] = fp1; 77 | ppt->c[1] = fp2; 78 | ppt->c[2] = fp3; 79 | } 80 | else 81 | GmfGetLin(inm,GmfVertices,&ppt->c[0],&ppt->c[1],&ppt->c[2],&ref); 82 | for (i=0; i<3; i++) { 83 | mesh->min[i] = MI_MIN(ppt->c[i],mesh->min[i]); 84 | mesh->max[i] = MI_MAX(ppt->c[i],mesh->max[i]); 85 | } 86 | } 87 | } 88 | /* read mesh triangles */ 89 | if ( mesh->nt > 0 ) { 90 | mesh->tria = (Tria*)calloc(mesh->nt+1,sizeof(Tria)); 91 | assert(mesh->tria); 92 | GmfGotoKwd(inm,GmfTriangles); 93 | for (k=1; k<=mesh->nt; k++) { 94 | pt1 = &mesh->tria[k]; 95 | GmfGetLin(inm,GmfTriangles,&pt1->v[0],&pt1->v[1],&pt1->v[2],&ref); 96 | } 97 | } 98 | if ( mesh->ne > 0 ) { 99 | mesh->tetra = (Tetra*)calloc(mesh->ne+1,sizeof(Tetra)); 100 | assert(mesh->tetra); 101 | GmfGotoKwd(inm,GmfTetrahedra); 102 | for (k=1; k<=mesh->ne; k++) { 103 | pt = &mesh->tetra[k]; 104 | GmfGetLin(inm,GmfTetrahedra,&pt->v[0],&pt->v[1],&pt->v[2],&pt->v[3],&ref); 105 | } 106 | } 107 | GmfCloseMesh(inm); 108 | 109 | if ( verb != '0' ) { 110 | fprintf(stdout," %d vertices",mesh->np); 111 | if ( mesh->nt ) fprintf(stdout,", %d triangles",mesh->nt); 112 | if ( mesh->ne ) fprintf(stdout,", %d tetrahedra",mesh->ne); 113 | fprintf(stdout,"\n"); 114 | } 115 | 116 | return(1); 117 | } 118 | 119 | 120 | /* load solution */ 121 | int loadSol(Sol *sol,char verb) { 122 | double dbuf[GmfMaxTyp]; 123 | float fbuf[GmfMaxTyp]; 124 | int k,i,inm,keyword; 125 | char *ptr,data[128]; 126 | 127 | if ( !sol->name ) { 128 | if ( verb != '0' ) fprintf(stderr," # %s: file not found.\n",sol->name); 129 | return(-1); 130 | } 131 | strcpy(data,sol->name); 132 | 133 | /* remove .mesh extension */ 134 | ptr = strstr(data,".mesh"); 135 | if ( ptr ) *ptr = '\0'; 136 | 137 | /* look for data file */ 138 | ptr = strstr(data,".sol"); 139 | if ( ptr ) { 140 | inm = GmfOpenMesh(data,GmfRead,&sol->ver,&sol->dim); 141 | } 142 | else { 143 | /* first try to read binary file */ 144 | strcat(data,".solb"); 145 | inm = GmfOpenMesh(data,GmfRead,&sol->ver,&sol->dim); 146 | if ( !inm ) { 147 | ptr = strstr(data,".solb"); 148 | *ptr = '\0'; 149 | strcat(data,".sol"); 150 | inm = GmfOpenMesh(data,GmfRead,&sol->ver,&sol->dim); 151 | } 152 | } 153 | if ( !inm ) { 154 | if ( verb != '0' ) fprintf(stderr," # %s: file not found.\n",data); 155 | return(-1); 156 | } 157 | 158 | sol->np = GmfStatKwd(inm,GmfSolAtVertices,sol->type,sol->size,sol->typtab[0]); 159 | if ( !sol->np ) { 160 | if ( verb != '0' ) fprintf(stderr," # %s: no points.\n",sol->name); 161 | return(-1); 162 | } 163 | 164 | if ( verb != '0' ) fprintf(stdout," %s:",data); 165 | 166 | sol->u = (double*)calloc(sol->np*sol->size[0],sizeof(double)); 167 | assert(sol->u); 168 | 169 | GmfGotoKwd(inm,GmfSolAtVertices); 170 | for (k=0; knp; k++) { 171 | if ( sol->ver == GmfFloat ) { 172 | GmfGetLin(inm,GmfSolAtVertices,fbuf); 173 | for (i=0; isize[0]; i++) 174 | sol->u[k*sol->size[0]+i] = fbuf[i]; 175 | } 176 | else { 177 | GmfGetLin(inm,GmfSolAtVertices,dbuf); 178 | for (i=0; isize[0]; i++) 179 | sol->u[k*sol->size[0]+i] = dbuf[i]; 180 | } 181 | } 182 | 183 | if ( GmfStatKwd(inm,GmfIterations) ) { 184 | GmfGotoKwd(inm,GmfIterations); 185 | GmfGetLin(inm,GmfIterations,&sol->it); 186 | } 187 | if ( GmfStatKwd(inm,GmfTime) ) { 188 | GmfGotoKwd(inm,GmfTime); 189 | if ( sol->ver == GmfFloat ) { 190 | GmfGetLin(inm,GmfTime,fbuf); 191 | sol->time = (double)fbuf[0]; 192 | } 193 | else { 194 | GmfGetLin(inm,GmfTime,&sol->time); 195 | } 196 | } 197 | GmfCloseMesh(inm); 198 | 199 | if ( verb != '0' ) { 200 | fprintf(stdout," %d data\n",sol->np); 201 | } 202 | 203 | return(1); 204 | } 205 | 206 | 207 | /* save solution */ 208 | int saveSol(Sol *sol,char verb) { 209 | double dbuf[GmfMaxTyp]; 210 | float fbuf[GmfMaxTyp]; 211 | int k,i,ia,outm,keyword; 212 | char *ptr,data[128]; 213 | 214 | strcpy(data,sol->name); 215 | ptr = strstr(data,".mesh"); 216 | if ( ptr ) { 217 | *ptr = '\0'; 218 | strcat(data,".sol"); 219 | } 220 | else { 221 | ptr = strstr(data,".sol"); 222 | if ( !ptr ) strcat(data,".solb"); 223 | } 224 | 225 | if ( !(outm = GmfOpenMesh(data,GmfWrite,sol->ver,sol->dim)) ) { 226 | if ( verb != '0' ) fprintf(stderr," # unable to open %s\n",data); 227 | return(0); 228 | } 229 | if ( verb != '0' ) fprintf(stdout," %s:",data); 230 | 231 | /* write sol */ 232 | GmfSetKwd(outm,GmfSolAtVertices,sol->np,sol->type[0],sol->typtab[0]); 233 | for (k=0; knp; k++) { 234 | if ( sol->ver == GmfFloat ) { 235 | for (i=0; isize[0]; i++) 236 | fbuf[i] = sol->u[k*sol->size[0]+i]; 237 | GmfSetLin(outm,GmfSolAtVertices,fbuf); 238 | } 239 | else { 240 | for (i=0; isize[0]; i++) 241 | dbuf[i] = sol->u[k*sol->size[0]+i]; 242 | GmfSetLin(outm,GmfSolAtVertices,dbuf); 243 | } 244 | } 245 | 246 | if ( sol->it > 0 ) { 247 | GmfSetKwd(outm,GmfIterations); 248 | GmfSetLin(outm,GmfIterations,sol->it); 249 | } 250 | if ( sol->time > 0.0 ) { 251 | GmfSetKwd(outm,GmfTime); 252 | if ( sol->ver == GmfFloat ) { 253 | fbuf[0] = sol->time; 254 | GmfSetLin(outm,GmfTime,fbuf[0]); 255 | } 256 | else 257 | GmfSetLin(outm,GmfTime,sol->time); 258 | } 259 | GmfCloseMesh(outm); 260 | 261 | if ( verb != '0' ) fprintf(stdout," %d data\n",sol->np); 262 | 263 | return(1); 264 | } 265 | 266 | -------------------------------------------------------------------------------- /mshint/locelt.c: -------------------------------------------------------------------------------- 1 | #include "mshint.h" 2 | 3 | 4 | /* return closest point of p on mesh1 */ 5 | int closept_3d(Mesh *mesh,double *c) { 6 | pPoint p1; 7 | double ux,uy,uz,dd,dmin; 8 | int i,ipmin; 9 | 10 | dmin = MI_TGV; 11 | ipmin = 0; 12 | for (i=1; i<=mesh->np; i++) { 13 | p1 = &mesh->point[i]; 14 | ux = p1->c[0] - c[0]; 15 | uy = p1->c[1] - c[1]; 16 | uz = p1->c[2] - c[2]; 17 | dd = ux*ux + uy*uy + uz*uz; 18 | if ( dd < dmin) { 19 | ipmin = i; 20 | dmin = dd; 21 | } 22 | } 23 | 24 | return(ipmin); 25 | } 26 | 27 | 28 | int closept_2d(pMesh mesh,double *c) { 29 | pPoint p1; 30 | double ux,uy,dd,dmin; 31 | int i,ipmin; 32 | 33 | dmin = MI_TGV; 34 | ipmin = 0; 35 | for (i=1; i<=mesh->np; i++) { 36 | p1 = &mesh->point[i]; 37 | ux = p1->c[0] - c[0]; 38 | uy = p1->c[1] - c[1]; 39 | dd = ux*ux + uy*uy; 40 | if ( dd < dmin) { 41 | ipmin = i; 42 | dmin = dd; 43 | } 44 | } 45 | 46 | return(ipmin); 47 | } 48 | 49 | 50 | /* find element containing c, starting from nsdep, return baryc. coord */ 51 | int locelt_3d(pMesh mesh,int nsd,double *c,double *cb) { 52 | pTetra pt; 53 | pPoint p0,p1,p2,p3; 54 | double bx,by,bz,cx,cy,cz,dx,dy,dz,vx,vy,vz,apx,apy,apz; 55 | double eps,vto,vol1,vol2,vol3,vol4,dd; 56 | int i,nsf,nsp; 57 | 58 | nsf = nsd; 59 | nsp = nsd; 60 | ++mesh->mark; 61 | while ( nsf > 0 ) { 62 | pt = &mesh->tetra[nsf]; 63 | if ( pt->mark == mesh->mark ) return(nsp); 64 | pt->mark = mesh->mark; 65 | 66 | /* measure of element */ 67 | p0 = &mesh->point[pt->v[0]]; 68 | p1 = &mesh->point[pt->v[1]]; 69 | p2 = &mesh->point[pt->v[2]]; 70 | p3 = &mesh->point[pt->v[3]]; 71 | 72 | /* barycentric and volume */ 73 | bx = p1->c[0] - p0->c[0]; 74 | by = p1->c[1] - p0->c[1]; 75 | bz = p1->c[2] - p0->c[2]; 76 | cx = p2->c[0] - p0->c[0]; 77 | cy = p2->c[1] - p0->c[1]; 78 | cz = p2->c[2] - p0->c[2]; 79 | dx = p3->c[0] - p0->c[0]; 80 | dy = p3->c[1] - p0->c[1]; 81 | dz = p3->c[2] - p0->c[2]; 82 | 83 | /* test volume */ 84 | vx = cy*dz - cz*dy; 85 | vy = cz*dx - cx*dz; 86 | vz = cx*dy - cy*dx; 87 | vto = bx*vx + by*vy + bz*vz; 88 | eps = MI_EPS*vto; 89 | 90 | /* barycentric */ 91 | apx = c[0] - p0->c[0]; 92 | apy = c[1] - p0->c[1]; 93 | apz = c[2] - p0->c[2]; 94 | 95 | /* p in 2 */ 96 | vol2 = apx*vx + apy*vy + apz*vz; 97 | if ( vol2 < eps ) { 98 | nsp = nsf; 99 | nsf = pt->adj[1] / 4; 100 | if ( !nsf ) { 101 | cb[1] = 0.0; 102 | nsf = nsp; 103 | } 104 | else 105 | continue; 106 | } 107 | /* p in 3 */ 108 | vx = by*apz - bz*apy; 109 | vy = bz*apx - bx*apz; 110 | vz = bx*apy - by*apx; 111 | vol3 = dx*vx + dy*vy + dz*vz; 112 | if ( vol3 < eps ) { 113 | nsp = nsf; 114 | nsf = pt->adj[2] / 4; 115 | if ( !nsf ) { 116 | cb[2] = 0.0; 117 | nsf = nsp; 118 | } 119 | else 120 | continue; 121 | } 122 | /* p in 4 */ 123 | vol4 = -cx*vx - cy*vy - cz*vz; 124 | if ( vol4 < eps ) { 125 | nsp = nsf; 126 | nsf = pt->adj[3] / 4; 127 | if ( !nsf ) { 128 | cb[3] = 0.0; 129 | nsf = nsp; 130 | } 131 | else 132 | continue; 133 | } 134 | /* p in 1 */ 135 | vol1 = vto - vol2 - vol3 - vol4; 136 | if ( vol1 < eps ) { 137 | nsp = nsf; 138 | nsf = pt->adj[0] / 4; 139 | if ( !nsf ) { 140 | cb[0] = 0.0; 141 | nsf = nsp; 142 | } 143 | else 144 | continue; 145 | } 146 | dd = fabs(vol1+vol2+vol3+vol4); 147 | if ( dd > MI_EPSD ) { 148 | dd = 1.0 / dd; 149 | cb[0] = vol1 * dd; 150 | cb[1] = vol2 * dd; 151 | cb[2] = vol3 * dd; 152 | cb[3] = vol4 * dd; 153 | } 154 | if ( (cb[0]>=0.0) && (cb[1]>=0.0) && (cb[2]>=0.0) && (cb[3]>=0.0) ) 155 | return(nsf); 156 | else 157 | return(-nsf); 158 | } 159 | 160 | return(nsp); 161 | } 162 | 163 | 164 | /* find simplex containing c, starting from nsd, return baryc. coord */ 165 | int locelt_2d(Mesh *mesh,int nsd,double *c,double *cb) { 166 | pTria pt; 167 | pPoint p0,p1,p2; 168 | double ax,ay,bx,by,cx,cy,eps; 169 | double aire1,aire2,aire3,dd; 170 | int i,sgn,nsf,nsp; 171 | 172 | nsf = nsd; 173 | nsp = 0; 174 | mesh->mark = ++mesh->mark; 175 | while ( nsf > 0 ) { 176 | pt = &mesh->tria[nsf]; 177 | if ( pt->mark == mesh->mark ) return(nsp); 178 | pt->mark = mesh->mark; 179 | 180 | /* area of triangle */ 181 | p0 = &mesh->point[pt->v[0]]; 182 | p1 = &mesh->point[pt->v[1]]; 183 | p2 = &mesh->point[pt->v[2]]; 184 | ax = p1->c[0] - p0->c[0]; 185 | ay = p1->c[1] - p0->c[1]; 186 | bx = p2->c[0] - p0->c[0]; 187 | by = p2->c[1] - p0->c[1]; 188 | dd = ax*by - ay*bx; 189 | sgn = dd > 0.0 ? 1 : -1; 190 | eps = sgn == 1 ? -MI_EPS*dd : MI_EPS*dd; 191 | /* barycentric */ 192 | bx = p1->c[0] - c[0]; 193 | by = p1->c[1] - c[1]; 194 | cx = p2->c[0] - c[0]; 195 | cy = p2->c[1] - c[1]; 196 | 197 | /* p in half-plane lambda_0 > 0 */ 198 | aire1 = sgn*(bx*cy - by*cx); 199 | if ( aire1 < eps ) { 200 | nsp = nsf; 201 | nsf = pt->adj[0] / 3; 202 | if ( !nsf ) { 203 | cb[0] = 0.0; 204 | nsf = nsp; 205 | } 206 | else 207 | continue; 208 | } 209 | ax = p0->c[0] - c[0]; 210 | ay = p0->c[1] - c[1]; 211 | aire2 = sgn*(cx*ay - cy*ax); 212 | if ( aire2 < eps ) { 213 | nsp = nsf; 214 | nsf = pt->adj[1] / 3; 215 | if ( !nsf ) { 216 | cb[1] = 0.0; 217 | nsf = nsp; 218 | } 219 | else 220 | continue; 221 | } 222 | aire3 = sgn*dd - aire1 - aire2; 223 | if ( aire3 < eps ) { 224 | nsp = nsf; 225 | nsf = pt->adj[2] / 3; 226 | if ( !nsf ) { 227 | cb[2] = 0.0; 228 | nsf = nsp; 229 | } 230 | else 231 | continue; 232 | } 233 | dd = fabs(aire1+aire2+aire3); 234 | if ( dd > MI_EPSD ) { 235 | dd = 1.0 / dd; 236 | cb[0] = aire1 * dd; 237 | cb[1] = aire2 * dd; 238 | cb[2] = aire3 * dd; 239 | } 240 | if ( (cb[0]>=0.0) && (cb[1]>=0.0) && (cb[2]>=0.0) ) 241 | return(nsf); 242 | else 243 | return(-nsf); 244 | } 245 | 246 | return(nsp); 247 | } 248 | 249 | 250 | 251 | 252 | -------------------------------------------------------------------------------- /mshint/mi_calls.c: -------------------------------------------------------------------------------- 1 | #include "mshint.h" 2 | #include "mi_calls.h" 3 | 4 | 5 | MIst *MI_init(int dim,int ver) { 6 | MIst *mist; 7 | 8 | /* default values */ 9 | mist = (MIst*)calloc(1,sizeof(MIst)); 10 | memset(&mist->msrc,0,sizeof(Mesh)); 11 | memset(&mist->mtgt,0,sizeof(Mesh)); 12 | memset(&mist->ssrc,0,sizeof(Sol)); 13 | memset(&mist->stgt,0,sizeof(Sol)); 14 | 15 | mist->info.verb = '1'; 16 | 17 | /* init timer */ 18 | tminit(mist->info.ctim,TIMEMAX); 19 | chrono(ON,&mist->info.ctim[0]); 20 | 21 | return(mist); 22 | } 23 | 24 | 25 | /* free global data structure */ 26 | int MI_stop(MIst *mist) { 27 | char stim[32]; 28 | 29 | /* release memory */ 30 | free(mist->msrc.point); 31 | if ( mist->msrc.nt ) free(mist->msrc.tria); 32 | if ( mist->msrc.ne ) free(mist->msrc.tetra); 33 | free(mist->mtgt.point); 34 | if ( mist->mtgt.nt ) free(mist->mtgt.tria); 35 | if ( mist->mtgt.ne ) free(mist->mtgt.tetra); 36 | if ( mist->ssrc.u ) free(mist->ssrc.u); 37 | 38 | chrono(OFF,&mist->info.ctim[0]); 39 | if ( mist->info.verb != '0' ) { 40 | printim(mist->info.ctim[0].gdif,stim); 41 | fprintf(stdout,"\n ** Cumulative time: %s sec.\n",stim); 42 | } 43 | 44 | return(1); 45 | } 46 | 47 | 48 | int MI_mshint(MIst *mist) { 49 | int ier; 50 | 51 | if ( mist->msrc.dim == 2 ) 52 | ier = mshint1_2d(mist); 53 | else 54 | ier = mshint1_3d(mist); 55 | 56 | return(ier); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /mshint/mi_calls.h: -------------------------------------------------------------------------------- 1 | #ifndef __MI_CALLS_H 2 | #define __MI_CALLS_H 3 | 4 | /* data structure */ 5 | typedef struct _MIst MIst; 6 | 7 | /* prototypes */ 8 | MIst *MI_init(int dim, int ver); 9 | int MI_stop(MIst *mist); 10 | 11 | int MI_mshint(MIst *mist); 12 | 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /mshint/mshint.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main program file for mshint 3 | * (C) Copyright 1997 - , ICS-SU 4 | * 5 | * This file is part of mshint. 6 | * 7 | * mshint is free software: you can redistribute it and/or modify it under 8 | * the terms of the GNU Lesser General Public License as published by the 9 | * Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * mshint is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with AdaptTools. If not, see . 19 | */ 20 | #include "mshint.h" 21 | #include "mi_calls.h" 22 | 23 | 24 | static void excfun(int sigid) { 25 | fprintf(stdout,"\n # unexpected error:"); fflush(stdout); 26 | switch(sigid) { 27 | case SIGABRT: 28 | fprintf(stdout," abnormal stop\n"); break; 29 | case SIGBUS: 30 | fprintf(stdout," code error...\n"); break; 31 | case SIGFPE: 32 | fprintf(stdout," floating-point exception\n"); break; 33 | case SIGILL: 34 | fprintf(stdout," illegal instruction\n"); break; 35 | case SIGSEGV: 36 | fprintf(stdout," segmentation fault.\n"); break; 37 | case SIGTERM: 38 | case SIGINT: 39 | fprintf(stdout," programm killed.\n"); break; 40 | } 41 | fprintf(stdout," # no data file saved.\n"); 42 | exit(1); 43 | } 44 | 45 | 46 | static void usage(char *prog) { 47 | fprintf(stdout,"\n usage: %s [+/-v | -h | -e] source[.mesh] [-s data[.sol]] target[.mesh] [-o output[.sol]]\n",prog); 48 | 49 | fprintf(stdout,"\nOptions and flags:\n\ 50 | --help show the syntax and exit.\n\ 51 | --version show the version and date of release and exit.\n\n\ 52 | -v suppress any message (for use with function call).\n\ 53 | +v increase the verbosity level for output.\n\n\ 54 | source.mesh name of the source mesh\n\ 55 | target.mesh name of the target mesh\n\ 56 | data.sol name of file containing the solution\n\ 57 | output.sol name of the output file\n"); 58 | exit(1); 59 | } 60 | 61 | 62 | /* parsing arguments on command line */ 63 | static int parsar(int argc,char *argv[],MIst *mist) { 64 | int i; 65 | char *ptr,*data; 66 | 67 | i = 1; 68 | while ( i < argc ) { 69 | if ( (*argv[i] == '-') || (*argv[i ]== '+') ) { 70 | switch(argv[i][1]) { 71 | case '-': /* on-line help */ 72 | if ( !strcmp(argv[i],"--help") ) 73 | usage(argv[0]); 74 | else if ( !strcmp(argv[i],"--version") ) { 75 | fprintf(stdout,"%s: version: %s release: %s\n",argv[0],MI_VER,MI_REL); 76 | exit(1); 77 | } 78 | break; 79 | case 'h': /* on-line help */ 80 | case '?': 81 | usage(argv[0]); 82 | break; 83 | case 'v': 84 | if ( !strcmp(argv[i],"-v") ) 85 | mist->info.verb = '0'; 86 | else if ( !strcmp(argv[i],"+v") ) 87 | mist->info.verb = '+'; 88 | else { 89 | fprintf(stderr,"%s: illegal option %s\n",argv[0],argv[i]); 90 | usage(argv[0]); 91 | } 92 | break; 93 | case 's': 94 | if ( (!strcmp(argv[i],"-s")) || (!strcmp(argv[i],"-sol")) ) { 95 | if ( ++i < argc && isascii(argv[i][0]) && argv[i][0]!='-' ) { 96 | data = (char*)calloc(strlen(argv[i])+10,sizeof(char)); 97 | assert ( data ); 98 | strcpy(data,argv[i]); 99 | ptr = strstr(data,".solb"); 100 | 101 | char ext[8]; 102 | /* Binary sol file */ 103 | if ( ptr ) { 104 | strcpy(ext, ".solb"); 105 | } 106 | else { 107 | /* Ascii sol file */ 108 | strcpy(ext, ".sol"); 109 | } 110 | 111 | ptr = strstr(data,".sol"); 112 | if ( ptr ) { *ptr = '\0'; } 113 | strcat(data,ext); 114 | mist->ssrc.name = data; 115 | 116 | } 117 | else { 118 | fprintf(stderr,"%s: -s option: please provide a valid string.\n",argv[0]); 119 | exit(EXIT_FAILURE); 120 | } 121 | } 122 | else { 123 | fprintf(stderr,"%s: illegal option %s\n",argv[0],argv[i]); 124 | usage(argv[0]); 125 | } 126 | break; 127 | 128 | default: 129 | fprintf(stderr,"%s: illegal option %s\n",argv[0],argv[i]); 130 | usage(argv[0]); 131 | } 132 | } 133 | else { 134 | if ( mist->msrc.name == NULL ) { 135 | data = (char*)calloc(strlen(argv[i])+10,sizeof(char)); 136 | strcpy(data,argv[i]); 137 | ptr = strstr(data,".mesh"); 138 | if ( !ptr ) strcat(data,".mesh"); 139 | mist->msrc.name = data; 140 | } 141 | else if ( mist->mtgt.name == NULL ) { 142 | data = (char*)calloc(strlen(argv[i])+10,sizeof(char)); 143 | strcpy(data,argv[i]); 144 | ptr = strstr(data,".mesh"); 145 | if ( !ptr ) strcat(data,".mesh"); 146 | mist->mtgt.name = data; 147 | } 148 | else { 149 | fprintf(stdout,"%s: illegal option %s\n",argv[0],argv[i]); 150 | usage(argv[0]); 151 | } 152 | } 153 | i++; 154 | } 155 | 156 | /* check params */ 157 | if ( (mist->msrc.name == NULL) || (mist->mtgt.name == NULL) ) { 158 | if ( mist->info.verb != '0' ) fprintf(stderr,"%s: missing argument\n",argv[0]); 159 | usage(argv[0]); 160 | } 161 | if ( mist->ssrc.name == NULL) { 162 | data = (char*)calloc(strlen(mist->msrc.name)+10,sizeof(char)); 163 | strcpy(data,mist->msrc.name); 164 | ptr = strstr(data,".mesh"); 165 | if ( ptr ) *ptr = '\0'; 166 | strcat(data,".sol"); 167 | mist->ssrc.name = data; 168 | } 169 | 170 | return(1); 171 | } 172 | 173 | 174 | int main(int argc,char **argv) { 175 | MIst mist; 176 | int ier; 177 | char stim[32]; 178 | 179 | memset(&mist,0,sizeof(MIst)); 180 | tminit(mist.info.ctim,TIMEMAX); 181 | chrono(ON,&mist.info.ctim[0]); 182 | 183 | /* trap exceptions */ 184 | signal(SIGABRT,excfun); 185 | signal(SIGFPE,excfun); 186 | signal(SIGILL,excfun); 187 | signal(SIGSEGV,excfun); 188 | signal(SIGTERM,excfun); 189 | signal(SIGINT,excfun); 190 | signal(SIGBUS,excfun); 191 | 192 | /* init structure */ 193 | memset(&mist.msrc,0,sizeof(Mesh)); 194 | memset(&mist.mtgt,0,sizeof(Mesh)); 195 | memset(&mist.ssrc,0,sizeof(Sol)); 196 | memset(&mist.stgt,0,sizeof(Sol)); 197 | 198 | /* default values */ 199 | mist.info.verb = '1'; 200 | 201 | /* parse command line */ 202 | if ( !parsar(argc,argv,&mist) ) return(1); 203 | 204 | /* loading data */ 205 | chrono(ON,&mist.info.ctim[1]); 206 | 207 | if ( mist.info.verb != '0' ) { 208 | fprintf(stdout," - MSHINT, Release %s, %s\n %s\n\n",MI_VER,MI_REL,MI_CPY); 209 | fprintf(stdout," - LOADING DATA\n"); 210 | } 211 | 212 | /* loading meshes and solution */ 213 | ier = loadMesh(&mist.msrc,mist.info.verb); 214 | if ( ier <= 0 ) return(1); 215 | ier = loadMesh(&mist.mtgt,mist.info.verb); 216 | if ( ier <= 0 ) return(1); 217 | ier = loadSol(&mist.ssrc,mist.info.verb); 218 | if ( ier <= 0 ) return(1); 219 | 220 | /* allocating memory */ 221 | mist.stgt.dim = mist.ssrc.dim; 222 | mist.stgt.ver = mist.ssrc.ver; 223 | mist.stgt.np = mist.mtgt.np; 224 | mist.stgt.it = mist.ssrc.it; 225 | mist.stgt.time = mist.ssrc.time; 226 | memcpy(mist.stgt.type,mist.ssrc.type,2*sizeof(int)); 227 | memcpy(mist.stgt.size,mist.ssrc.size,2*sizeof(int)); 228 | memcpy(&mist.stgt.typtab[0],&mist.ssrc.typtab[0],mist.ssrc.type[0]*sizeof(int)); 229 | memcpy(&mist.stgt.typtab[1],&mist.ssrc.typtab[1],mist.ssrc.type[1]*sizeof(int)); 230 | if ( mist.stgt.np > 0 ) { 231 | mist.stgt.u = (double*)calloc(mist.stgt.np,mist.stgt.size[0]*sizeof(double)); 232 | assert(mist.stgt.u); 233 | } 234 | 235 | /* set up adjacencies */ 236 | if ( mist.msrc.dim == 2 ) 237 | hashel_2d(&mist); 238 | else 239 | hashel_3d(&mist); 240 | 241 | chrono(OFF,&mist.info.ctim[1]); 242 | printim(mist.info.ctim[1].gdif,stim); 243 | if ( mist.info.verb != '0' ) fprintf(stdout," - COMPLETED: %s\n",stim); 244 | 245 | if ( !mist.stgt.name ) { 246 | mist.stgt.name = (char *)calloc(128,sizeof(char)); 247 | assert(mist.stgt.name); 248 | strcpy(mist.stgt.name,mist.mtgt.name); 249 | } 250 | 251 | /* L2 projection */ 252 | chrono(ON,&mist.info.ctim[2]); 253 | if ( mist.info.verb != '0' ) fprintf(stdout,"\n ** MODULE MSHINT: %s\n",MI_VER); 254 | ier = MI_mshint(&mist); 255 | chrono(OFF,&mist.info.ctim[2]); 256 | if ( mist.info.verb != '0' ) { 257 | printim(mist.info.ctim[2].gdif,stim); 258 | if ( ier > 0 ) 259 | fprintf(stdout," ** COMPLETED: %s\n\n",stim); 260 | else 261 | fprintf(stdout," ** NOT COMPLETED!: %s\n\n",stim); 262 | } 263 | 264 | /* save file */ 265 | if ( mist.info.verb != '0' ) fprintf(stdout," - WRITING DATA\n"); 266 | chrono(ON,&mist.info.ctim[3]); 267 | 268 | ier = saveSol(&mist.stgt,0); 269 | if ( !ier ) return(1); 270 | chrono(OFF,&mist.info.ctim[3]); 271 | if ( mist.info.verb != '0' ) { 272 | printim(mist.info.ctim[3].gdif,stim); 273 | fprintf(stdout," - COMPLETED: %s\n",stim); 274 | } 275 | 276 | /* free mem */ 277 | free(mist.msrc.point); 278 | if ( mist.msrc.nt ) free(mist.msrc.tria); 279 | if ( mist.msrc.ne ) free(mist.msrc.tetra); 280 | free(mist.mtgt.point); 281 | if ( mist.mtgt.nt ) free(mist.mtgt.tria); 282 | if ( mist.mtgt.ne ) free(mist.mtgt.tetra); 283 | if ( mist.ssrc.u ) free(mist.ssrc.u); 284 | 285 | chrono(OFF,&mist.info.ctim[0]); 286 | if ( mist.info.verb != '0' ) { 287 | printim(mist.info.ctim[0].gdif,stim); 288 | fprintf(stdout,"\n ** Cumulative time: %s.\n",stim); 289 | } 290 | 291 | return(0); 292 | } 293 | 294 | 295 | -------------------------------------------------------------------------------- /mshint/mshint.h: -------------------------------------------------------------------------------- 1 | #ifndef _MSHINT_H 2 | #define _MSHINT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "chrono.h" 15 | #include "libmesh5.h" 16 | #include "mi_calls.h" 17 | 18 | 19 | #define MI_VER "4.1a" 20 | #define MI_REL "Mar. 9, 2016" 21 | #define MI_CPY "(C) Copyright 1997- , ICS-SU" 22 | 23 | #define MI_MAX(a,b) (((a) > (b)) ? (a) : (b)) 24 | #define MI_MIN(a,b) (((a) < (b)) ? (a) : (b)) 25 | #define MI_MIN3(a,b,c) ( (a) < (b) ? ((a)<(c) ? (a) : (c)) : ((b)<(c) ? (b) : (c)) ) 26 | #define MI_MAX3(a,b,c) ( (a) > (b) ? ((a)>(c) ? (a) : (c)) : ((b)>(c) ? (b) : (c)) ) 27 | 28 | #define BUCKSIZ 32 29 | #define MI_EPS 1.e-6 30 | #define MI_EPSD 1.e-200 31 | #define MI_TGV 1.e30 32 | 33 | 34 | typedef struct { 35 | double c[3]; 36 | int s; 37 | } Point; 38 | typedef Point * pPoint; 39 | 40 | typedef struct { 41 | int v[3],adj[3],mark; 42 | } Tria; 43 | typedef Tria * pTria; 44 | 45 | typedef struct { 46 | int v[4],adj[4],mark; 47 | } Tetra; 48 | typedef Tetra * pTetra; 49 | 50 | typedef struct { 51 | char verb; 52 | mytime ctim[TIMEMAX]; 53 | } Info; 54 | 55 | typedef struct { 56 | double min[3],max[3]; 57 | int dim,ver,np,nt,ne,mark; 58 | char *name; 59 | pPoint point; 60 | pTria tria; 61 | pTetra tetra; 62 | } Mesh; 63 | typedef Mesh * pMesh; 64 | 65 | typedef struct { 66 | int dim,ver,np,it,type[2],size[2],typtab[2][GmfMaxTyp]; 67 | double *u,time; 68 | char *name; 69 | } Sol; 70 | typedef Sol * pSol; 71 | 72 | struct _MIst { 73 | Mesh msrc,mtgt; 74 | Sol ssrc,stgt; 75 | Info info; 76 | }; 77 | 78 | typedef struct { 79 | int *cell,size; 80 | } Bucket; 81 | 82 | 83 | /* prototypes */ 84 | int loadMesh(Mesh *mesh,char verb); 85 | int loadSol(Sol *sol,char verb); 86 | int saveSol(Sol *sol,char verb); 87 | int hashel_2d(MIst *mist); 88 | int hashel_3d(MIst *mist); 89 | int mshint1_2d(MIst *mist); 90 | int mshint1_3d(MIst *mist); 91 | Bucket *bucket_2d(Mesh *mesh,int nmax); 92 | Bucket *bucket_3d(Mesh *mesh,int nmax); 93 | int buckin_2d(Mesh *mesh,Bucket *bck,double *c); 94 | int buckin_3d(Mesh *mesh,Bucket *bck,double *c); 95 | int closept_2d(pMesh mesh,double *c); 96 | int closept_3d(pMesh mesh,double *c); 97 | int locelt_2d(pMesh mesh,int nsd,double *c,double *cb); 98 | int locelt_3d(pMesh mesh,int nsd,double *c,double *cb); 99 | 100 | 101 | #endif 102 | -------------------------------------------------------------------------------- /mshint/mshint1.c: -------------------------------------------------------------------------------- 1 | #include "mshint.h" 2 | 3 | 4 | int mshint1_3d(MIst *mist) { 5 | Bucket *bck; 6 | pPoint ppt; 7 | pTetra pt; 8 | double *u,*ua,*ub,*uc,*ud,cb[4]; 9 | int i,k,iel,ip,nb,np; 10 | 11 | /* interpolation */ 12 | bck = bucket_3d(&mist->msrc,BUCKSIZ); 13 | if ( !bck ) return(0); 14 | 15 | if ( mist->info.verb != '0' ) { 16 | fprintf(stdout," Nodal interpolation: "); 17 | fflush(stdout); 18 | } 19 | 20 | nb = np = 0; 21 | for (k=1; k<=mist->mtgt.np; k++) { 22 | ppt = &mist->mtgt.point[k]; 23 | /* get seed element */ 24 | iel = buckin_3d(&mist->msrc,bck,ppt->c); 25 | if ( iel ) { 26 | iel = locelt_3d(&mist->msrc,iel,ppt->c,cb); 27 | if ( iel > 0 ) { 28 | pt = &mist->msrc.tetra[iel]; 29 | u = &mist->stgt.u[(k-1)*mist->stgt.size[0]]; 30 | ua = &mist->ssrc.u[(pt->v[0]-1)*mist->ssrc.size[0]]; 31 | ub = &mist->ssrc.u[(pt->v[1]-1)*mist->ssrc.size[0]]; 32 | uc = &mist->ssrc.u[(pt->v[2]-1)*mist->ssrc.size[0]]; 33 | ud = &mist->ssrc.u[(pt->v[3]-1)*mist->ssrc.size[0]]; 34 | 35 | for (i=0; istgt.size[0]; i++) 36 | u[i] = cb[0]*ua[i] + cb[1]*ub[i] + cb[2]*uc[i] + cb[3]*uc[i]; 37 | } 38 | } 39 | if ( iel < 1 ) { 40 | /* exhaustive search */ 41 | ip = closept_3d(&mist->msrc,ppt->c); 42 | if ( ip ) { 43 | ua = &mist->ssrc.u[(ip-1)*mist->ssrc.size[0]]; 44 | u = &mist->stgt.u[(k-1)*mist->stgt.size[0]]; 45 | memcpy(u,ua,mist->stgt.size[0]*sizeof(double)); 46 | np++; 47 | } 48 | else 49 | nb++; 50 | } 51 | } 52 | if ( mist->info.verb != '0' ) { 53 | fprintf(stdout,"%d, %d prox.\n",mist->mtgt.np,np); 54 | if ( nb > 0 ) fprintf(stdout," # Warning: %d nodes failed.\n",nb); 55 | } 56 | 57 | free(bck->cell); 58 | free(bck); 59 | 60 | return(nb == 0); 61 | } 62 | 63 | 64 | int mshint1_2d(MIst *mist) { 65 | Bucket *bck; 66 | pPoint ppt; 67 | pTria pt; 68 | double *u,*ua,*ub,*uc,cb[3]; 69 | int k,i,iel,ip,nb; 70 | 71 | /* interpolation */ 72 | bck = bucket_2d(&mist->msrc,BUCKSIZ); 73 | if ( !bck ) return(0); 74 | 75 | if ( mist->info.verb != '0' ) { 76 | fprintf(stdout," Nodal interpolation: "); 77 | fflush(stdout); 78 | } 79 | puts("debut\n----------------------\n"); 80 | nb = 0; 81 | for (k=1; k<=mist->mtgt.np; k++) { 82 | ppt = &mist->mtgt.point[k]; 83 | /* get seed element */ 84 | iel = buckin_2d(&mist->msrc,bck,ppt->c); 85 | if ( iel > 0 ) { 86 | iel = locelt_2d(&mist->msrc,iel,ppt->c,cb); 87 | if ( iel > 0 ) { 88 | pt = &mist->msrc.tria[iel]; 89 | u = &mist->stgt.u[(k-1)*mist->stgt.size[0]]; 90 | ua = &mist->ssrc.u[(pt->v[0]-1)*mist->ssrc.size[0]]; 91 | ub = &mist->ssrc.u[(pt->v[1]-1)*mist->ssrc.size[0]]; 92 | uc = &mist->ssrc.u[(pt->v[2]-1)*mist->ssrc.size[0]]; 93 | for (i=0; istgt.size[0]; i++) 94 | u[i] = cb[0]*ua[i] + cb[1]*ub[i] + cb[2]*uc[i]; 95 | } 96 | } 97 | if ( iel < 1 ) { 98 | ip = closept_2d(&mist->msrc,ppt->c); 99 | if ( ip > 0 ) { 100 | ua = &mist->ssrc.u[(ip-1)*mist->ssrc.size[0]]; 101 | u = &mist->stgt.u[(k-1)*mist->stgt.size[0]]; 102 | memcpy(u,ua,mist->stgt.size[0]*sizeof(double)); 103 | } 104 | else 105 | nb++; 106 | } 107 | } 108 | if ( mist->info.verb != '0' ) { 109 | fprintf(stdout,"%d\n",mist->mtgt.np); 110 | if ( nb > 0 ) fprintf(stdout," # Warning: %d nodes failed.\n",nb); 111 | } 112 | 113 | free(bck->cell); 114 | free(bck); 115 | 116 | return(nb == 0); 117 | } 118 | -------------------------------------------------------------------------------- /mshmet/hash.c: -------------------------------------------------------------------------------- 1 | #include "mshmet.h" 2 | 3 | 4 | /* data structures */ 5 | typedef struct { 6 | int min,max,nxt,elt,ind; 7 | } Cell; 8 | 9 | typedef struct { 10 | Cell *cell; 11 | int nmax,hsiz,hnxt; 12 | } Htab; 13 | 14 | 15 | static int hcode_3d(Tetra *tetra,Htab *ht,int a,int b,int c,int k,int i) { 16 | Cell *pc; 17 | pTetra pt,pt1; 18 | int adj,sum,min,max; 19 | 20 | sum = a+b+c; 21 | if ( sum >= ht->nmax ) return(0); 22 | 23 | /* check if edge ab stored */ 24 | sum = sum % ht->hsiz; 25 | pc = &ht->cell[sum]; 26 | min = MS_MIN3(a,b,c); 27 | max = MS_MAX3(a,b,c); 28 | if ( !pc->min ) { 29 | pc->min = min; 30 | pc->max = max; 31 | pc->elt = k; 32 | pc->ind = i; 33 | return(1); 34 | } 35 | 36 | /* analyze linked list */ 37 | pt = &tetra[k]; 38 | do { 39 | pt1 = &tetra[pc->elt]; 40 | if ( pc->min == min && pc->max == max ) { 41 | adj = pt1->adj[pc->ind]; 42 | if ( !adj ) { 43 | pt->adj[i] = 4*pc->elt+pc->ind; 44 | pt1->adj[pc->ind] = 4*k+i; 45 | } 46 | return(1); 47 | } 48 | else if ( !pc->nxt ) { 49 | pc->nxt = ht->hnxt; 50 | pc = &ht->cell[ht->hnxt]; 51 | if ( !pc ) return(0); 52 | pc->min = min; 53 | pc->max = max; 54 | pc->elt = k; 55 | pc->ind = i; 56 | ht->hnxt = pc->nxt; 57 | pc->nxt = 0; 58 | 59 | /* check for size overflow */ 60 | if ( !ht->hnxt ) return(0); 61 | return(1); 62 | } 63 | pc = &ht->cell[pc->nxt]; 64 | } while (1); 65 | 66 | return(0); 67 | } 68 | 69 | 70 | /* insert edge a,b and update adjacent triangle */ 71 | static int hcode_2d(Tria *tria,Htab *ht,int a,int b,int k,int i) { 72 | Cell *pc; 73 | pTria pt,pt1; 74 | int abmin,adj,sum; 75 | 76 | sum = a+b; 77 | if ( sum >= ht->nmax ) return(0); 78 | 79 | /* check if edge ab stored */ 80 | pc = &ht->cell[sum]; 81 | abmin = MS_MIN(a,b); 82 | if ( !pc->min ) { 83 | pc->min = abmin; 84 | pc->elt = k; 85 | pc->ind = i; 86 | return(1); 87 | } 88 | 89 | /* analyze linked list */ 90 | pt = &tria[k]; 91 | do { 92 | pt1 = &tria[pc->elt]; 93 | if ( pc->min == abmin ) { 94 | adj = pt1->adj[pc->ind]; 95 | if ( !adj ) { 96 | pt->adj[i] = 3*pc->elt+pc->ind; 97 | pt1->adj[pc->ind] = 3*k+i; 98 | } 99 | return(1); 100 | } 101 | else if ( !pc->nxt ) { 102 | pc->nxt = ht->hnxt; 103 | pc = &ht->cell[ht->hnxt]; 104 | if ( !pc ) return(0); 105 | pc->min = abmin; 106 | pc->elt = k; 107 | pc->ind = i; 108 | ht->hnxt = pc->nxt; 109 | pc->nxt = 0; 110 | 111 | /* check for size overflow */ 112 | if ( !ht->hnxt ) return(0); 113 | return(1); 114 | } 115 | pc = &ht->cell[pc->nxt]; 116 | } while (1); 117 | 118 | return(0); 119 | } 120 | 121 | 122 | /* hash mesh edges for creating P2 nodes */ 123 | int hashel_3d(MSst *msst) { 124 | Htab ht; 125 | pTetra pt; 126 | pPoint ppt; 127 | int k,nt; 128 | char i,i1,i2,i3; 129 | 130 | if ( msst->info.verb != '0' ) fprintf(stdout," Adjacency table: "); 131 | 132 | /* alloc hash */ 133 | ht.nmax = (int)(12.71 * msst->info.np); 134 | ht.cell = (Cell*)calloc(ht.nmax+2,sizeof(Cell)); 135 | assert(ht.cell); 136 | 137 | ht.hsiz = msst->info.np; 138 | ht.hnxt = ht.hsiz; 139 | for (k=ht.hsiz; kinfo.ne; k++) { 145 | pt = &msst->mesh.tetra[k]; 146 | for (i=0; i<4; i++) { 147 | i1 = (i+1) % 4; 148 | i2 = (i+2) % 4; 149 | i3 = (i+3) % 4; 150 | if ( !hcode_3d(msst->mesh.tetra,&ht,pt->v[i1],pt->v[i2],pt->v[i3],k,i) ) return(0); 151 | nt++; 152 | } 153 | } 154 | 155 | /* add seed with point */ 156 | for (k=1; k<=msst->info.ne; k++) { 157 | pt = &msst->mesh.tetra[k]; 158 | for (i=0; i<4; i++) { 159 | if ( !pt->adj[i] ) msst->mesh.point[pt->v[i]].s = k; 160 | } 161 | } 162 | for (k=1; k<=msst->info.ne; k++) { 163 | pt = &msst->mesh.tetra[k]; 164 | for (i=0; i<4; i++) { 165 | ppt = &msst->mesh.point[pt->v[i]]; 166 | if ( !ppt->s ) ppt->s = k; 167 | } 168 | } 169 | free(ht.cell); 170 | 171 | if ( msst->info.verb != '0' ) fprintf(stdout," %d updated\n",nt); 172 | 173 | return(1); 174 | } 175 | 176 | 177 | /* build adjacency table */ 178 | int hashel_2d(MSst *msst) { 179 | Htab ht; 180 | pTria pt; 181 | pPoint ppt; 182 | int k,na; 183 | char i,i1,i2; 184 | 185 | if ( msst->info.verb == '+' ) fprintf(stdout," Adjacency table: "); 186 | 187 | /* alloc hash */ 188 | ht.nmax = (int)(3.71 * msst->info.np); 189 | ht.cell = (Cell*)calloc(ht.nmax+2,sizeof(Cell)); 190 | assert(ht.cell); 191 | 192 | ht.hsiz = 2 * msst->info.np; 193 | ht.hnxt = ht.hsiz; 194 | for (k=ht.hsiz; kinfo.nt; k++) { 200 | pt = &msst->mesh.tria[k]; 201 | for (i=0; i<3; i++) { 202 | i1 = (i+1) % 3; 203 | i2 = (i+2) % 3; 204 | if ( !hcode_2d(msst->mesh.tria,&ht,pt->v[i1],pt->v[i2],k,i) ) return(0); 205 | na++; 206 | } 207 | } 208 | 209 | /* add seed with point */ 210 | for (k=1; k<=msst->info.nt; k++) { 211 | pt = &msst->mesh.tria[k]; 212 | for (i=0; i<3; i++) { 213 | if ( !pt->adj[i] ) msst->mesh.point[pt->v[(i+1)%3]].s = k; 214 | } 215 | } 216 | for (k=1; k<=msst->info.nt; k++) { 217 | pt = &msst->mesh.tria[k]; 218 | for (i=0; i<3; i++) { 219 | ppt = &msst->mesh.point[pt->v[i]]; 220 | if ( !ppt->s ) ppt->s = k; 221 | } 222 | } 223 | free(ht.cell); 224 | 225 | if ( msst->info.verb == '+' ) fprintf(stdout," %d updated\n",na); 226 | 227 | return(1); 228 | } -------------------------------------------------------------------------------- /mshmet/inout.c: -------------------------------------------------------------------------------- 1 | #include "mshmet.h" 2 | 3 | 4 | /* read mesh data */ 5 | int loadMesh(MSst *msst) { 6 | pPoint ppt; 7 | pTria pt1; 8 | pTetra pt; 9 | float fp1,fp2,fp3; 10 | int i,k,inm,ref; 11 | char *ptr,data[256]; 12 | 13 | strcpy(data,msst->mesh.name); 14 | ptr = strstr(data,".mesh"); 15 | if ( !ptr ) { 16 | strcat(data,".meshb"); 17 | if( !(inm = GmfOpenMesh(data,GmfRead,&msst->info.ver,&msst->info.dim)) ) { 18 | ptr = strstr(data,".mesh"); 19 | *ptr = '\0'; 20 | strcat(data,".mesh"); 21 | if ( !(inm = GmfOpenMesh(data,GmfRead,&msst->info.ver,&msst->info.dim)) ) { 22 | fprintf(stderr," # %s: file not found.\n",data); 23 | return(0); 24 | } 25 | } 26 | } 27 | else if ( !(inm = GmfOpenMesh(data,GmfRead,&msst->info.ver,&msst->info.dim)) ) { 28 | fprintf(stderr," # %s: file not found.\n",data); 29 | return(0); 30 | } 31 | 32 | if ( msst->info.verb != '0' ) fprintf(stdout," %s:",data); 33 | 34 | msst->info.np = GmfStatKwd(inm,GmfVertices); 35 | msst->info.nt = GmfStatKwd(inm,GmfTriangles); 36 | msst->info.ne = GmfStatKwd(inm,GmfTetrahedra); 37 | if ( !msst->info.np ) { 38 | if ( msst->info.verb != '0' ) fprintf(stdout,"\n # missing data\n"); 39 | return(0); 40 | } 41 | 42 | /* memory allocation */ 43 | msst->mesh.point = (pPoint)calloc(msst->info.np+1,sizeof(Point)); 44 | assert(msst->mesh.point); 45 | GmfGotoKwd(inm,GmfVertices); 46 | if ( msst->info.dim == 2 ) { 47 | /* 2d mesh */ 48 | for (k=1; k<=msst->info.np; k++) { 49 | ppt = &msst->mesh.point[k]; 50 | if ( msst->info.ver == GmfFloat ) { 51 | GmfGetLin(inm,GmfVertices,&fp1,&fp2,&ref); 52 | ppt->c[0] = fp1; 53 | ppt->c[1] = fp2; 54 | } 55 | else 56 | GmfGetLin(inm,GmfVertices,&ppt->c[0],&ppt->c[1],&ref); 57 | } 58 | } 59 | else { 60 | /* 3d mesh */ 61 | for (k=1; k<=msst->info.np; k++) { 62 | ppt = &msst->mesh.point[k]; 63 | if ( msst->info.ver == GmfFloat ) { 64 | GmfGetLin(inm,GmfVertices,&fp1,&fp2,&fp3,&ref); 65 | ppt->c[0] = fp1; 66 | ppt->c[1] = fp2; 67 | ppt->c[2] = fp3; 68 | } 69 | else 70 | GmfGetLin(inm,GmfVertices,&ppt->c[0],&ppt->c[1],&ppt->c[2],&ref); 71 | } 72 | } 73 | if ( msst->info.nt > 0 ) { 74 | msst->mesh.tria = (pTria)calloc(msst->info.nt+1,sizeof(Tria)); 75 | assert(msst->mesh.tria); 76 | /* read mesh triangles */ 77 | GmfGotoKwd(inm,GmfTriangles); 78 | for (k=1; k<=msst->info.nt; k++) { 79 | pt1 = &msst->mesh.tria[k]; 80 | GmfGetLin(inm,GmfTriangles,&pt1->v[0],&pt1->v[1],&pt1->v[2],&ref); 81 | } 82 | } 83 | if ( msst->info.ne > 0 ) { 84 | msst->mesh.tetra = (pTetra)calloc(msst->info.ne+1,sizeof(Tetra)); 85 | assert(msst->mesh.tetra); 86 | /* read tetrahedra */ 87 | GmfGotoKwd(inm,GmfTetrahedra); 88 | for (k=1; k<=msst->info.ne; k++) { 89 | pt = &msst->mesh.tetra[k]; 90 | GmfGetLin(inm,GmfTetrahedra,&pt->v[0],&pt->v[1],&pt->v[2],&pt->v[3],&ref); 91 | } 92 | } 93 | GmfCloseMesh(inm); 94 | 95 | if ( msst->info.verb != '0' ) { 96 | fprintf(stdout," %d vertices",msst->info.np); 97 | if ( msst->info.nt ) fprintf(stdout,", %d triangles",msst->info.nt); 98 | if ( msst->info.ne ) fprintf(stdout,", %d tetrahedra",msst->info.ne); 99 | fprintf(stdout,"\n"); 100 | } 101 | 102 | return(1); 103 | } 104 | 105 | 106 | /* load one solution */ 107 | int loadSol(MSst *msst) { 108 | double dbuf[GmfMaxTyp]; 109 | float fbuf[GmfMaxTyp]; 110 | int k,i,inm,np,dim,ver,type,size,typtab[GmfMaxTyp]; 111 | char *ptr,data[128]; 112 | 113 | if ( !msst->sol.name ) return(-1); 114 | strcpy(data,msst->sol.name); 115 | 116 | /* remove .mesh extension */ 117 | ptr = strstr(data,".mesh"); 118 | if ( ptr ) *ptr = '\0'; 119 | 120 | /* look for data file */ 121 | ptr = strstr(data,".sol"); 122 | if ( ptr ) { 123 | inm = GmfOpenMesh(data,GmfRead,&ver,&dim); 124 | } 125 | else { 126 | /* first try to read binary file */ 127 | strcat(data,".solb"); 128 | inm = GmfOpenMesh(data,GmfRead,&ver,&dim); 129 | if ( !inm ) { 130 | ptr = strstr(data,".solb"); 131 | *ptr = '\0'; 132 | strcat(data,".sol"); 133 | inm = GmfOpenMesh(data,GmfRead,&ver,&dim); 134 | } 135 | } 136 | if ( !inm ) return(-1); 137 | 138 | if ( dim != msst->info.dim ) return(-1); 139 | np = GmfStatKwd(inm,GmfSolAtVertices,&type,&size,&typtab); 140 | if ( !np || np != msst->info.np ) return(-1); 141 | 142 | if ( msst->info.verb != '0' ) fprintf(stdout," %s:",data); 143 | 144 | /* memory allocation */ 145 | msst->sol.u = (double*)calloc(msst->info.np,sizeof(double)); 146 | assert(msst->sol.u); 147 | 148 | /* look at first solution only (for now) */ 149 | GmfGotoKwd(inm,GmfSolAtVertices); 150 | msst->sol.umax = -FLT_MAX; 151 | switch(typtab[0]) { 152 | case GmfSca: 153 | /* read scalar field */ 154 | for (k=0; kinfo.np; k++) { 155 | if ( ver == GmfFloat ) { 156 | GmfGetLin(inm,GmfSolAtVertices,fbuf); 157 | msst->sol.u[k] = fbuf[0]; 158 | } 159 | else { 160 | GmfGetLin(inm,GmfSolAtVertices,dbuf); 161 | msst->sol.u[k] = dbuf[0]; 162 | } 163 | msst->sol.umax = MS_MAX(msst->sol.umax,msst->sol.u[k]); 164 | } 165 | break; 166 | case GmfVec: 167 | /* read vector field */ 168 | for (k=0; kinfo.np; k++) { 169 | if ( ver == GmfFloat ) { 170 | GmfGetLin(inm,GmfSolAtVertices,fbuf); 171 | msst->sol.u[k] = sqrt(fbuf[0]*fbuf[0] + fbuf[1]*fbuf[1]); 172 | } 173 | else { 174 | GmfGetLin(inm,GmfSolAtVertices,dbuf); 175 | msst->sol.u[k] = sqrt(dbuf[0]*dbuf[0] + dbuf[1]*dbuf[1]); 176 | } 177 | msst->sol.umax = MS_MAX(msst->sol.umax,msst->sol.u[k]); 178 | } 179 | break; 180 | case GmfSymMat: 181 | /* read tensor field */ 182 | for (k=0; kinfo.np; k++) { 183 | msst->sol.umax = MS_MAX(msst->sol.umax,msst->sol.u[k]); 184 | } 185 | break; 186 | default: 187 | break; 188 | } 189 | GmfCloseMesh(inm); 190 | 191 | if ( msst->info.verb != '0' ) { 192 | if ( typtab[0] == GmfSca) 193 | fprintf(stdout," %d scalar data\n",msst->info.np); 194 | else if ( typtab[0] == GmfVec ) 195 | fprintf(stdout," %d vector data\n",msst->info.np); 196 | else 197 | fprintf(stdout," %d tensor data\n",msst->info.np); 198 | } 199 | 200 | return(1); 201 | } 202 | 203 | 204 | /* save metric file */ 205 | int saveMet(MSst *msst) { 206 | double db[6]; 207 | int i,k,outm,type,typtab[GmfMaxTyp]; 208 | char *ptr,data[128]; 209 | 210 | strcpy(data,msst->sol.name); 211 | ptr = strstr(data,".mesh"); 212 | if ( ptr ) { 213 | *ptr = '\0'; 214 | strcat(data,".met.sol"); 215 | } 216 | else { 217 | ptr = strstr(data,".met.sol"); 218 | if ( !ptr ) strcat(data,".met.solb"); 219 | } 220 | 221 | msst->info.ver = GmfDouble; 222 | if ( !(outm = GmfOpenMesh(data,GmfWrite,msst->info.ver,msst->info.dim)) ) { 223 | if ( msst->info.verb != '0' ) fprintf(stderr," # unable to open %s\n",data); 224 | return(0); 225 | } 226 | if ( msst->info.verb != '0' ) fprintf(stdout," %s:",data); 227 | 228 | /* write metric file */ 229 | type = 1; 230 | if ( msst->info.iso ) { 231 | typtab[0] = GmfSca; 232 | GmfSetKwd(outm,GmfSolAtVertices,msst->info.np,type,typtab); 233 | for (k=0; kinfo.np; k++) { 234 | GmfSetLin(outm,GmfSolAtVertices,&msst->sol.m[k]); 235 | } 236 | } 237 | else { 238 | typtab[0] = GmfSymMat; 239 | GmfSetKwd(outm,GmfSolAtVertices,msst->info.np,type,typtab); 240 | if ( msst->info.dim == 2 ) { 241 | for (k=0; kinfo.np; k++) { 242 | for (i=0; i<3; i++) db[i] = msst->sol.m[3*k+i]; 243 | GmfSetLin(outm,GmfSolAtVertices,db); 244 | } 245 | } 246 | } 247 | GmfCloseMesh(outm); 248 | 249 | if ( msst->info.verb != '0' ) 250 | fprintf(stdout," %d %s data\n",msst->info.np,msst->info.iso ? "scalar" : "tensor"); 251 | 252 | return(1); 253 | } 254 | 255 | 256 | -------------------------------------------------------------------------------- /mshmet/ms_calls.c: -------------------------------------------------------------------------------- 1 | #include "mshmet.h" 2 | #include "ms_calls.h" 3 | 4 | 5 | MSst *MS_init(int dim,int ver) { 6 | MSst *msst; 7 | 8 | /* default values */ 9 | msst = (MSst *)calloc(1,sizeof(MSst)); 10 | memset(&msst->mesh,0,sizeof(Mesh)); 11 | memset(&msst->sol,0,sizeof(Sol)); 12 | 13 | msst->info.verb = '1'; 14 | msst->info.dim = dim; 15 | msst->info.ver = ver; 16 | 17 | /* init timer */ 18 | tminit(msst->info.ctim,TIMEMAX); 19 | chrono(ON,&msst->info.ctim[0]); 20 | 21 | return(msst); 22 | } 23 | 24 | 25 | int MS_stop(MSst *msst) { 26 | char stim[32]; 27 | 28 | free(msst->mesh.point); 29 | if ( msst->info.nt > 0 ) free(msst->mesh.tria); 30 | if ( msst->info.ne > 0 ) free(msst->mesh.tetra); 31 | free(msst->sol.u); 32 | free(msst->sol.m); 33 | 34 | chrono(OFF,&msst->info.ctim[0]); 35 | if ( msst->info.verb != '0' ) { 36 | printim(msst->info.ctim[0].gdif,stim); 37 | fprintf(stdout,"\n ** Cumulative time: %s sec.\n",stim); 38 | } 39 | 40 | return(1); 41 | } 42 | 43 | 44 | int MS_mshmet(MSst *msst) { 45 | int ier; 46 | 47 | if ( msst->info.dim == 2 ) 48 | ier = mshmet1_2d(msst); 49 | else 50 | ier = mshmet1_3d(msst); 51 | 52 | return(ier); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /mshmet/ms_calls.h: -------------------------------------------------------------------------------- 1 | #ifndef __MS_CALLS_H 2 | #define __MS_CALLS_H 3 | 4 | /* data structure */ 5 | typedef struct _MSst MSst; 6 | 7 | /* prototypes */ 8 | MSst *MS_init(int dim,int ver); 9 | int MS_stop(MSst *msst); 10 | int MS_mshmet(MSst *msst); 11 | 12 | 13 | #endif -------------------------------------------------------------------------------- /mshmet/mshmet.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main program file for mshmet 3 | * (C) Copyright 1997 - , ICS-SU 4 | * 5 | * This file is part of mshmet. 6 | * 7 | * mshmet is free software: you can redistribute it and/or modify it under 8 | * the terms of the GNU Lesser General Public License as published by the 9 | * Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * mshmet is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with AdaptTools. If not, see . 19 | */ 20 | #include "mshmet.h" 21 | #include "ms_calls.h" 22 | 23 | 24 | static void excfun(int sigid) { 25 | fprintf(stdout,"\n # unexpected error:"); fflush(stdout); 26 | switch(sigid) { 27 | case SIGABRT: 28 | fprintf(stdout," abnormal stop\n"); break; 29 | case SIGBUS: 30 | fprintf(stdout," code error...\n"); break; 31 | case SIGFPE: 32 | fprintf(stdout," floating-point exception\n"); break; 33 | case SIGILL: 34 | fprintf(stdout," illegal instruction\n"); break; 35 | case SIGSEGV: 36 | fprintf(stdout," segmentation fault.\n"); break; 37 | case SIGTERM: 38 | case SIGINT: 39 | fprintf(stdout," programm killed.\n"); break; 40 | } 41 | fprintf(stdout," # no data file saved.\n"); 42 | exit(1); 43 | } 44 | 45 | 46 | static void usage(char *prog) { 47 | fprintf(stdout,"\n usage: %s [+/-v | -h | -i | -l] source[.mesh] [-e err] [-hmin|-hmax|-hgrad val] [-n nrm] [-o output[].sol]] [-p param[.mhes]]\n",prog); 48 | 49 | fprintf(stdout,"\nOptions and flags:\n\ 50 | --help show the syntax and exit.\n\ 51 | --version show the version and date of release and exit.\n\n\ 52 | -e err approximation error\n\ 53 | -i isotropic metric (default is aniso)\n\ 54 | -l metric for level sets\n\ 55 | -n nrm metric normalization method (default=1)\n\ 56 | -v suppress any message (for use with function call).\n\ 57 | +v increase the verbosity level for output.\n\n\ 58 | source.mesh name of the source mesh\n\ 59 | metric.sol name of metric file\n\ 60 | param.mhes name of file containing metric specifications\n\ 61 | output.sol name of the output file\n"); 62 | exit(1); 63 | } 64 | 65 | 66 | static int parsar(int argc,char *argv[],MSst *msst) { 67 | int i; 68 | char *ptr,*data; 69 | 70 | i = 1; 71 | while ( i < argc ) { 72 | if ( (*argv[i] == '-') || (*argv[i ]== '+') ) { 73 | switch(argv[i][1]) { 74 | case '-': /* on-line help */ 75 | if ( !strcmp(argv[i],"--help") ) 76 | usage(argv[0]); 77 | else if ( !strcmp(argv[i],"--version") ) { 78 | fprintf(stdout,"%s: version: %s release: %s\n",argv[0],MS_VER,MS_REL); 79 | exit(1); 80 | } 81 | break; 82 | case 'h': 83 | case '?': 84 | if ( !strcmp(argv[i],"-hmin") ) { 85 | if ( ++i < argc && isdigit(argv[i][0]) ) 86 | msst->info.hmin = strtod(argv[i],NULL); 87 | } 88 | else if ( !strcmp(argv[i],"-hmax") ) { 89 | if ( ++i < argc && isdigit(argv[i][0]) ) 90 | msst->info.hmax = strtod(argv[i],NULL); 91 | } 92 | else if ( !strcmp(argv[i],"-hgrad") ) { 93 | if ( ++i < argc && isdigit(argv[i][0]) ) 94 | msst->info.hgrad = strtod(argv[i],NULL); 95 | } 96 | else 97 | usage(argv[0]); 98 | break; 99 | case 'e': 100 | if ( ++i < argc && isdigit(argv[i][0]) ) 101 | msst->info.err = strtod(argv[i],NULL); 102 | else { 103 | fprintf(stderr,"%s: missing argument option\n",argv[0]); 104 | usage(argv[0]); 105 | } 106 | break; 107 | case 'i': 108 | msst->info.iso = 1; 109 | break; 110 | case 'l': 111 | msst->info.ls = 1; 112 | break; 113 | case 'n': 114 | if ( ++i < argc && isdigit(argv[i][0]) ) { 115 | msst->info.nrm = atoi(argv[i]); 116 | if ( msst->info.nrm < 0 || msst->info.nrm > 2 ) { 117 | fprintf(stdout,"%s: wrong parameter value: set default\n",argv[0]); 118 | msst->info.nrm = 1; 119 | } 120 | } 121 | else { 122 | fprintf(stdout,"%s: missing parameter file\n", argv[0]); 123 | usage(argv[0]); 124 | } 125 | break; 126 | case 'p': 127 | if ( ++i < argc ) { 128 | msst->sol.namepar = argv[i]; 129 | ptr = strstr(msst->sol.namepar,".mhes"); 130 | if ( !ptr ) strcat(msst->sol.namepar,".mhes"); 131 | } 132 | else { 133 | fprintf(stdout,"%s: missing parameter file\n", argv[0]); 134 | usage(argv[0]); 135 | } 136 | break; 137 | case 'v': 138 | if ( !strcmp(argv[i],"-v") ) 139 | msst->info.verb = '0'; 140 | else if ( !strcmp(argv[i],"+v") ) 141 | msst->info.verb = '+'; 142 | else { 143 | fprintf(stderr,"%s: illegal option %s\n",argv[0],argv[i]); 144 | usage(argv[0]); 145 | } 146 | break; 147 | default: 148 | fprintf(stderr,"%s: illegal option %s\n",argv[0],argv[i]); 149 | usage(argv[0]); 150 | } 151 | } 152 | else { 153 | if ( msst->mesh.name == NULL ) { 154 | data = (char*)calloc(strlen(argv[i])+10,sizeof(char)); 155 | strcpy(data,argv[i]); 156 | ptr = strstr(data,".mesh"); 157 | if ( !ptr ) strcat(data,".mesh"); 158 | msst->mesh.name = data; 159 | } 160 | else { 161 | fprintf(stdout,"%s: illegal option %s\n",argv[0],argv[i]); 162 | usage(argv[0]); 163 | } 164 | } 165 | i++; 166 | } 167 | 168 | /* check params */ 169 | if ( msst->mesh.name == NULL ) { 170 | if ( msst->info.verb != '0' ) fprintf(stderr,"%s: missing argument\n",argv[0]); 171 | usage(argv[0]); 172 | } 173 | if ( !msst->sol.name ) { 174 | msst->sol.name = (char *)calloc(128,sizeof(char)); 175 | assert(msst->sol.name); 176 | strcpy(msst->sol.name,msst->mesh.name); 177 | } 178 | 179 | return(1); 180 | } 181 | 182 | 183 | int parsop(MSst *msst) { 184 | int i,ret; 185 | char *ptr,data[256]; 186 | FILE *in; 187 | 188 | /* check for parameter file */ 189 | if ( !msst->sol.namepar ) { 190 | strcpy(data,msst->mesh.name); 191 | ptr = strstr(data,".mesh"); 192 | if ( ptr ) *ptr = '\0'; 193 | strcat(data,".mhes"); 194 | in = fopen(data,"r"); 195 | if ( !in ) { 196 | sprintf(data,"%s","DEFAULT.mhes"); 197 | in = fopen(data,"r"); 198 | } 199 | } 200 | else { 201 | strcpy(data,msst->sol.namepar); 202 | ptr = strstr(data,".mhes"); 203 | if ( !ptr ) strcat(data,".mhes"); 204 | in = fopen(data,"r"); 205 | } 206 | if ( !in ) return(-1); 207 | if ( msst->info.verb != '0' ) fprintf(stdout," %s:",data); 208 | 209 | /* read metric specifications */ 210 | while ( !feof(in) ) { 211 | /* scan line */ 212 | ret = fscanf(in,"%s",data); 213 | if ( !ret || feof(in) ) break; 214 | for (i=0; iinfo.hmin); 219 | else if ( !strcmp(data,"hmax") ) 220 | fscanf(in,"%lf",&msst->info.hmax); 221 | else if ( !strcmp(data,"err") ) 222 | fscanf(in,"%lf",&msst->info.err); 223 | else if ( !strcmp(data,"iso") ) 224 | msst->info.iso = 1; 225 | else if ( data[0] == '#' ) { 226 | fgets(data,255,in); 227 | } 228 | } 229 | fclose(in); 230 | 231 | return(1); 232 | } 233 | 234 | 235 | int main(int argc,char **argv) { 236 | MSst msst; 237 | int ier; 238 | char stim[32]; 239 | 240 | memset(&msst,0,sizeof(MSst)); 241 | tminit(msst.info.ctim,TIMEMAX); 242 | chrono(ON,&msst.info.ctim[0]); 243 | 244 | /* trap exceptions */ 245 | signal(SIGABRT,excfun); 246 | signal(SIGFPE,excfun); 247 | signal(SIGILL,excfun); 248 | signal(SIGSEGV,excfun); 249 | signal(SIGTERM,excfun); 250 | signal(SIGINT,excfun); 251 | signal(SIGBUS,excfun); 252 | 253 | /* init structure */ 254 | memset(&msst.mesh,0,sizeof(Mesh)); 255 | memset(&msst.sol,0,sizeof(Sol)); 256 | 257 | /* default values */ 258 | msst.info.dim = 3; 259 | msst.info.err = 0.01; 260 | msst.info.hmin = 0.01; 261 | msst.info.hmax = 1.0; 262 | msst.info.iso = 0; 263 | msst.info.ls = 0; 264 | msst.info.nrm = 1; 265 | msst.info.ver = 1; 266 | msst.info.verb = '1'; 267 | 268 | /* parse command line */ 269 | if ( !parsar(argc,argv,&msst) ) return(1); 270 | 271 | /* loading data */ 272 | chrono(ON,&msst.info.ctim[1]); 273 | 274 | if ( msst.info.verb != '0' ) { 275 | fprintf(stdout," - MSHMET, Release %s, %s\n %s\n\n",MS_VER,MS_REL,MS_CPY); 276 | fprintf(stdout," - LOADING DATA\n"); 277 | } 278 | 279 | /* loading mesh and solution */ 280 | ier = loadMesh(&msst); 281 | if ( ier <= 0 ) return(1); 282 | ier = loadSol(&msst); 283 | if ( ier <= 0 ) return(1); 284 | if ( msst.sol.umax < MS_EPSD ) { 285 | if ( msst.info.verb != '0' ) fprintf(stdout," # Warning: solution modulus too small\n"); 286 | return(1); 287 | } 288 | if ( !parsop(&msst) ) return(1); 289 | 290 | /* setting adjacencies */ 291 | msst.info.dim == 2 ? hashel_2d(&msst) : hashel_3d(&msst); 292 | 293 | chrono(OFF,&msst.info.ctim[1]); 294 | printim(msst.info.ctim[1].gdif,stim); 295 | if ( msst.info.verb != '0' ) fprintf(stdout," - COMPLETED: %s\n",stim); 296 | 297 | /* build metric */ 298 | chrono(ON,&msst.info.ctim[2]); 299 | if ( msst.info.verb != '0' ) 300 | fprintf(stdout,"\n ** MODULE MSHMET: %s\n",MS_VER); 301 | ier = MS_mshmet(&msst); 302 | chrono(OFF,&msst.info.ctim[2]); 303 | if ( msst.info.verb != '0' ) { 304 | printim(msst.info.ctim[2].gdif,stim); 305 | if ( ier ) 306 | fprintf(stdout," ** COMPLETED: %s\n\n",stim); 307 | else 308 | fprintf(stdout," ** NOT COMPLETED!: %s\n\n",stim); 309 | } 310 | 311 | /* save file */ 312 | if ( msst.info.verb != '0' ) fprintf(stdout," - WRITING DATA\n"); 313 | chrono(ON,&msst.info.ctim[3]); 314 | ier = saveMet(&msst); 315 | if ( !ier ) return(1); 316 | chrono(OFF,&msst.info.ctim[3]); 317 | if ( msst.info.verb != '0' ) { 318 | printim(msst.info.ctim[3].gdif,stim); 319 | fprintf(stdout," - COMPLETED: %s\n",stim); 320 | } 321 | 322 | /* free mem */ 323 | free(msst.mesh.point); 324 | if ( msst.info.nt > 0 ) free(msst.mesh.tria); 325 | if ( msst.info.ne > 0 ) free(msst.mesh.tetra); 326 | free(msst.sol.u); 327 | free(msst.sol.m); 328 | 329 | chrono(OFF,&msst.info.ctim[0]); 330 | if ( msst.info.verb != '0' ) { 331 | printim(msst.info.ctim[0].gdif,stim); 332 | fprintf(stdout,"\n ** Cumulative time: %s.\n",stim); 333 | } 334 | 335 | return(0); 336 | } 337 | -------------------------------------------------------------------------------- /mshmet/mshmet.h: -------------------------------------------------------------------------------- 1 | #ifndef _MSHMET_H 2 | #define _MSHMET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "chrono.h" 15 | #include "libmesh5.h" 16 | #include "ms_calls.h" 17 | 18 | 19 | #define MS_VER "4.0a" 20 | #define MS_REL "Mar. 12, 2016" 21 | #define MS_CPY "(C) Copyright 2007- , ICS-SU" 22 | 23 | #define MS_EPS 1.e-6 24 | #define MS_EPSD 1.e-200 25 | #define LONMAX 4096 26 | 27 | #define CTE2D 2.0 / 9.0 28 | #define CTE3D 9.0 / 32.0 29 | 30 | #define MS_MAX(a,b) ( ((a) < (b)) ? (b) : (a) ) 31 | #define MS_MIN(a,b) ( ((a) < (b)) ? (a) : (b) ) 32 | #define MS_MIN3(a,b,c) ( (a) < (b) ? ((a)<(c) ? (a) : (c)) : ((b)<(c) ? (b) : (c)) ) 33 | #define MS_MAX3(a,b,c) ( (a) > (b) ? ((a)>(c) ? (a) : (c)) : ((b)>(c) ? (b) : (c)) ) 34 | 35 | 36 | typedef struct { 37 | double c[3]; 38 | int s; 39 | } Point; 40 | typedef Point * pPoint; 41 | 42 | typedef struct { 43 | int v[3],adj[3],mark; 44 | } Tria; 45 | typedef Tria * pTria; 46 | 47 | typedef struct { 48 | int v[4],adj[4],mark; 49 | } Tetra; 50 | typedef Tetra * pTetra; 51 | 52 | typedef struct { 53 | double hmin,hmax,hgrad,err; 54 | int np,nt,ne,dim,ver; 55 | char verb,iso,ls,nrm; 56 | mytime ctim[TIMEMAX]; 57 | } Info; 58 | 59 | typedef struct { 60 | int mark; 61 | char *name; 62 | pPoint point; 63 | pTria tria; 64 | pTetra tetra; 65 | } Mesh; 66 | typedef Mesh * pMesh; 67 | 68 | typedef struct { 69 | double *u,*g,*h,*m,umax; 70 | char *name,*namepar; 71 | } Sol; 72 | typedef Sol * pSol; 73 | 74 | struct _MSst { 75 | Mesh mesh; 76 | Sol sol; 77 | Info info; 78 | }; 79 | 80 | /* prototypes */ 81 | int loadMesh(MSst *msst); 82 | int loadSol(MSst *msst); 83 | int saveMet(MSst *msst); 84 | int hashel_2d(MSst *mist); 85 | int hashel_3d(MSst *mist); 86 | int mshmet1_2d(MSst *mist); 87 | int mshmet1_3d(MSst *mist); 88 | 89 | 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /mshmet/mshmet1_2d.c: -------------------------------------------------------------------------------- 1 | #include "mshmet.h" 2 | #include "eigen.h" 3 | 4 | #define CTE2D 2.0 / 9.0 5 | 6 | 7 | /* define 2d metric tensor field */ 8 | int defmet_2d(MSst *msst) { 9 | double *m,*h,l[2],vp[2][2],dd,hmin,hmax; 10 | int k; 11 | 12 | hmin = 1.0 / (msst->info.hmax*msst->info.hmax); 13 | hmax = 1.0 / (msst->info.hmin*msst->info.hmin); 14 | 15 | for (k=0; kinfo.np; k++) { 16 | h = &msst->sol.h[3*k]; 17 | 18 | if ( !eigen_2d(h,l,vp) ) { 19 | if ( msst->info.verb != '0' ) fprintf(stdout," # Error: eigenvalue problem.\n"); 20 | return(0); 21 | } 22 | /* truncation */ 23 | l[0] = MS_MIN(MS_MAX(fabs(l[0]),hmin),hmax); 24 | l[1] = MS_MIN(MS_MAX(fabs(l[1]),hmin),hmax); 25 | 26 | if ( msst->info.iso ) { 27 | msst->sol.m[k] = (l[0] < l[1]) ? 1.0 / sqrt(l[1]) : 1.0 / sqrt(l[0]); 28 | } 29 | else { 30 | /* compute Mat = B M Bt */ 31 | m = &msst->sol.m[3*k]; 32 | m[0] = l[0]*vp[0][0]*vp[0][0] + l[1]*vp[1][0]*vp[1][0]; 33 | m[1] = l[0]*vp[0][0]*vp[0][1] + l[1]*vp[1][0]*vp[1][1]; 34 | m[2] = l[0]*vp[0][1]*vp[0][1] + l[1]*vp[1][1]*vp[1][1]; 35 | } 36 | } 37 | 38 | return(1); 39 | } 40 | 41 | 42 | int nrmhes_2d(MSst *msst) { 43 | double u,err; 44 | int i,k; 45 | 46 | switch(msst->info.nrm) { 47 | case 0: /* no normalization */ 48 | for (k=0; kinfo.np; k++) { 49 | for (i=0; i<3; i++) msst->sol.h[3*k+i] *= CTE2D / msst->info.err; 50 | } 51 | break; 52 | case 1: /* relative value: M(u)= |H(u)| / err*||u||_inf */ 53 | default: 54 | for (k=0; kinfo.np; k++) { 55 | for (i=0; i<3; i++) msst->sol.h[3*k+i] *= CTE2D / (msst->info.err*msst->sol.umax); 56 | } 57 | break; 58 | case 2: /* local norm: M(u)= |H(u)| / err*|u| */ 59 | err = msst->sol.umax > 0.0 ? msst->sol.umax*0.01 : 0.01; 60 | for (k=0; kinfo.np; k++) { 61 | u = fabs(msst->sol.u[k]); 62 | for (i=0; i<3; i++) msst->sol.h[3*k+i] *= CTE2D / MS_MAX(err,u); 63 | } 64 | break; 65 | } 66 | 67 | return(1); 68 | } 69 | 70 | 71 | /* least square approximation of hessian */ 72 | int hessls_2d(MSst *msst) { 73 | pPoint p0,p1; 74 | pTria pt; 75 | double *ha,*hb,*ma,*mb,*g0,*h,a[6],u0,u1,dd; 76 | int k,i; 77 | 78 | /* memory allocation */ 79 | ha = (double*)calloc(msst->info.np*6,sizeof(double)); 80 | assert(ha); 81 | hb = (double*)calloc(msst->info.np*3,sizeof(double)); 82 | assert(hb); 83 | 84 | for (k=1; k<=msst->info.nt; k++) { 85 | pt = &msst->mesh.tria[k]; 86 | 87 | for (i=0; i<3; i++) { 88 | p0 = &msst->mesh.point[pt->v[i]]; 89 | u0 = msst->sol.u[pt->v[i]-1]; 90 | g0 = &msst->sol.g[msst->info.dim*(pt->v[i]-1)]; 91 | ma = &ha[6*(pt->v[i]-1)]; 92 | mb = &hb[3*(pt->v[i]-1)]; 93 | 94 | if ( pt->adj[(i+1)%3]/3 < k ) { 95 | memset(a,0,6*sizeof(double)); 96 | p1 = &msst->mesh.point[pt->v[(i+2)%3]]; 97 | u1 = msst->sol.u[pt->v[(i+2)%3]-1]; 98 | 99 | a[0] += 0.5 * (p1->c[0]-p0->c[0]) * (p1->c[0]-p0->c[0]); 100 | a[1] += (p1->c[0]-p0->c[0]) * (p1->c[1]-p0->c[1]); 101 | a[2] += 0.5 * (p1->c[1]-p0->c[1]) * (p1->c[1]-p0->c[1]); 102 | dd = (u1-u0) - (p1->c[0]-p0->c[0])*g0[0] - (p1->c[1]-p0->c[1])*g0[1]; 103 | 104 | /* M = At*A symmetric definite positive */ 105 | ma[0] += a[0]*a[0]; 106 | ma[1] += a[0]*a[1]; 107 | ma[2] += a[1]*a[1]; 108 | ma[3] += a[0]*a[2]; 109 | ma[4] += a[1]*a[2]; 110 | ma[5] += a[2]*a[2]; 111 | 112 | /* c = At*b */ 113 | mb[0] += a[0]*dd; 114 | mb[1] += a[1]*dd; 115 | mb[2] += a[2]*dd; 116 | } 117 | if ( pt->adj[(i+2)%3]/3 < k ) { 118 | memset(a,0,6*sizeof(double)); 119 | p1 = &msst->mesh.point[pt->v[(i+1)%3]]; 120 | u1 = msst->sol.u[pt->v[(i+1)%3]-1]; 121 | 122 | a[0] += 0.5 * (p1->c[0]-p0->c[0]) * (p1->c[0]-p0->c[0]); 123 | a[1] += (p1->c[0]-p0->c[0]) * (p1->c[1]-p0->c[1]); 124 | a[2] += 0.5 * (p1->c[1]-p0->c[1]) * (p1->c[1]-p0->c[1]); 125 | dd = (u1-u0) - (p1->c[0]-p0->c[0])*g0[0] - (p1->c[1]-p0->c[1])*g0[1]; 126 | 127 | /* M = At*A symmetric definite positive */ 128 | ma[0] += a[0]*a[0]; 129 | ma[1] += a[0]*a[1]; 130 | ma[2] += a[1]*a[1]; 131 | ma[3] += a[0]*a[2]; 132 | ma[4] += a[1]*a[2]; 133 | ma[5] += a[2]*a[2]; 134 | 135 | /* c = At*b */ 136 | mb[0] += a[0]*dd; 137 | mb[1] += a[1]*dd; 138 | mb[2] += a[2]*dd; 139 | } 140 | } 141 | } 142 | 143 | /* hessian evaluation */ 144 | for (k=0; kinfo.np; k++) { 145 | ma = &ha[6*k]; 146 | mb = &hb[3*k]; 147 | 148 | /* direct solving */ 149 | a[0] = ma[2]*ma[5] - ma[4]*ma[4]; 150 | a[1] = ma[4]*ma[3] - ma[1]*ma[5]; 151 | a[2] = ma[1]*ma[4] - ma[2]*ma[3]; 152 | dd = ma[0]*a[0] + ma[1]*a[1] + ma[3]*a[2]; 153 | 154 | /* singular matrix */ 155 | if ( fabs(dd) > MS_EPSD ) { 156 | h = &msst->sol.h[3*k]; 157 | 158 | a[3] = ma[0]*ma[5] - ma[3]*ma[3]; 159 | a[4] = ma[1]*ma[3] - ma[0]*ma[4]; 160 | a[5] = ma[0]*ma[2] - ma[1]*ma[1]; 161 | h[0] = (mb[0]*a[0] + mb[1]*a[1] + mb[2]*a[2]) / dd; 162 | h[1] = (mb[0]*a[1] + mb[1]*a[3] + mb[2]*a[4]) / dd; 163 | h[2] = (mb[0]*a[2] + mb[1]*a[4] + mb[2]*a[5]) / dd; 164 | } 165 | else { 166 | printf("Pt %d : SINGULAR hessian\n",k); 167 | } 168 | } 169 | free(ha); 170 | free(hb); 171 | 172 | return(1); 173 | } 174 | 175 | 176 | /* compute gradients at mesh vertices (least-square approx.) */ 177 | int gradls_2d(MSst *msst) { 178 | pTria pt; 179 | pPoint p0,p1; 180 | double *ga,*gb,*a,*b,*g,u0,u1,dd; 181 | int k,i; 182 | 183 | /* memory allocation */ 184 | ga = (double*)calloc(msst->info.np*3,sizeof(double)); 185 | assert(ga); 186 | gb = (double*)calloc(msst->info.np*2,sizeof(double)); 187 | assert(gb); 188 | 189 | /* triangle contribution */ 190 | for (k=1; k<=msst->info.nt; k++) { 191 | pt = &msst->mesh.tria[k]; 192 | 193 | for (i=0; i<3; i++) { 194 | p0 = &msst->mesh.point[pt->v[i]]; 195 | u0 = msst->sol.u[pt->v[i]-1]; 196 | a = &ga[3*(pt->v[i]-1)]; 197 | b = &gb[2*(pt->v[i]-1)]; 198 | if ( pt->adj[(i+1)%3]/3 < k ) { 199 | p1 = &msst->mesh.point[pt->v[(i+2)%3]]; 200 | u1 = msst->sol.u[pt->v[(i+2)%3]-1]; 201 | /* M = At*A symmetric definite positive */ 202 | a[0] += (p1->c[0]-p0->c[0]) * (p1->c[0]-p0->c[0]); 203 | a[1] += (p1->c[0]-p0->c[0]) * (p1->c[1]-p0->c[1]); 204 | a[2] += (p1->c[1]-p0->c[1]) * (p1->c[1]-p0->c[1]); 205 | //printf(" tria %d: point %d: %e %e %e\n",k,pt->v[i],a[0],a[1],a[2]); 206 | /* b = A^t*du */ 207 | b[0] += (p1->c[0]-p0->c[0]) * (u1-u0); 208 | b[1] += (p1->c[1]-p0->c[1]) * (u1-u0); 209 | } 210 | if ( pt->adj[(i+2)%3]/3 < k ) { 211 | p1 = &msst->mesh.point[pt->v[(i+1)%3]]; 212 | u1 = msst->sol.u[pt->v[(i+1)%3]-1]; 213 | 214 | /* M = At*A symmetric definite positive */ 215 | a[0] += (p1->c[0]-p0->c[0]) * (p1->c[0]-p0->c[0]); 216 | a[1] += (p1->c[0]-p0->c[0]) * (p1->c[1]-p0->c[1]); 217 | a[2] += (p1->c[1]-p0->c[1]) * (p1->c[1]-p0->c[1]); 218 | 219 | /* b = A^t*du */ 220 | b[0] += (p1->c[0]-p0->c[0]) * (u1-u0); 221 | b[1] += (p1->c[1]-p0->c[1]) * (u1-u0); 222 | } 223 | } 224 | } 225 | 226 | /* gradient evaluation */ 227 | for (k=0; kinfo.np; k++) { 228 | /* solution of A(2,2)*grad(1,2) = b(1,2) */ 229 | a = &ga[3*k]; 230 | b = &gb[2*k]; 231 | dd = a[0]*a[2] - a[1]*a[1]; 232 | if ( fabs(dd) > MS_EPSD ) { 233 | g = &msst->sol.g[msst->info.dim*k]; 234 | g[0] = (a[2]*b[0] - a[1]*b[1]) / dd; 235 | g[1] = (a[0]*b[1] - a[1]*b[0]) / dd; 236 | } 237 | else { 238 | printf("Singular grad pt %d\n",k); 239 | } 240 | } 241 | free(ga); 242 | free(gb); 243 | 244 | return(1); 245 | } 246 | 247 | 248 | int mshmet1_2d(MSst *msst) { 249 | int k,ier; 250 | 251 | /* compute nodal gradients */ 252 | msst->sol.g = (double*)calloc(msst->info.np,msst->info.dim*sizeof(double)); 253 | assert(msst->sol.g); 254 | ier = gradls_2d(msst); 255 | if ( !ier ) { 256 | if ( msst->info.verb != '0' ) fprintf(stdout," # Error: unable to evaluate gradients\n"); 257 | free(msst->sol.g); 258 | return(0); 259 | } 260 | 261 | /* compute nodal hessian matrix */ 262 | msst->sol.h = (double*)calloc(msst->info.np,3*sizeof(double)); 263 | assert(msst->sol.h); 264 | ier = hessls_2d(msst); 265 | free(msst->sol.g); 266 | if ( !ier ) { 267 | if ( msst->info.verb != '0' ) fprintf(stdout," # Error: unable to evaluate hessian\n"); 268 | free(msst->sol.h); 269 | return(0); 270 | } 271 | 272 | /* normalize hessian */ 273 | ier = nrmhes_2d(msst); 274 | if ( !ier ) { 275 | if ( msst->info.verb != '0' ) fprintf(stdout," # Error: unable to normalize hessian\n"); 276 | free(msst->sol.h); 277 | return(0); 278 | } 279 | 280 | /* compute metric */ 281 | if ( msst->info.iso ) 282 | msst->sol.m = (double*)calloc(msst->info.np,sizeof(double)); 283 | else 284 | msst->sol.m = (double*)calloc(msst->info.np,3*sizeof(double)); 285 | assert(msst->sol.m); 286 | ier = defmet_2d(msst); 287 | free(msst->sol.h); 288 | if ( !ier ) { 289 | if ( msst->info.verb != '0' ) fprintf(stdout," # Error: unable to define metric\n"); 290 | return(0); 291 | } 292 | 293 | return(1); 294 | } 295 | 296 | -------------------------------------------------------------------------------- /mshmet/mshmet1_3d.c: -------------------------------------------------------------------------------- 1 | #include "mshmet.h" 2 | 3 | 4 | int mshmet1_3d(MSst *msst) { 5 | return(1); 6 | } --------------------------------------------------------------------------------