1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkFEMSolverHyperbolic.h"
#include "itkFEMSpatialObjectReader.h"
#include "itkFEMLinearSystemWrapperDenseVNL.h"
#include "itkFEMLinearSystemWrapperItpack.h"
typedef itk::fem::SolverHyperbolic<2> FEMSolverType;
// Print K - the global stiffness matrix
void PrintK(FEMSolverType *S)
{
itk::fem::LinearSystemWrapper::Pointer lsw = S->GetLinearSystemWrapper();
std::cout << std::endl << "k" << "=[";
for( unsigned int j = 0; j < lsw->GetSystemOrder(); j++ )
{
std::cout << " [";
for( unsigned int k = 0; k < lsw->GetSystemOrder(); k++ )
{
std::cout << lsw->GetMatrixValue(j, k);
if( (k + 1) < lsw->GetSystemOrder() )
{
std::cout << ", ";
}
}
if( j < lsw->GetSystemOrder() - 1 )
{
std::cout << " ]," << std::endl;
}
else
{
std::cout << "]";
}
}
std::cout << "];" << std::endl;
}
// Print F - the global load vector
void PrintF(FEMSolverType *S)
{
itk::fem::LinearSystemWrapper::Pointer lsw = S->GetLinearSystemWrapper();
std::cout << std::endl << "f" << "=[";
for( unsigned int j = 0; j < lsw->GetSystemOrder(); j++ )
{
if( j > 0 )
{
std::cout << ", ";
}
std::cout << lsw->GetVectorValue(j);
}
std::cout << "];" << std::endl;
}
void PrintNodalCoordinates(FEMSolverType *S)
// Print the nodal coordinates
{
std::cout << std::endl << "Nodal coordinates: " << std::endl;
std::cout << "xyz" << "=[";
int numberOfNodes = S->GetInput()->GetNumberOfNodes();
for( int i = 0; i < numberOfNodes; i++ )
{
std::cout << " [";
std::cout << S->GetInput()->GetNode(i)->GetCoordinates();
std::cout << "]";
}
std::cout << "];" << std::endl;
}
// Useful for display purposes - lets you draw each element
// individually, instead of just a stream of nodes
void PrintElementCoordinates(FEMSolverType *S )
{
std::cout << std::endl << "Element coordinates: " << std::endl;
int ct = 1;
const unsigned int invalidID = itk::fem::Element::InvalidDegreeOfFreedomID;
int numberOfElements = S->GetInput()->GetNumberOfElements();
for(int i = 0; i < numberOfElements; i++ )
{
std::cout << "e(" << ct << ",:,:)=[";
for (unsigned int n=0; n < S->GetInput()->GetElement(i)->GetNumberOfNodes(); n++)
{
itk::fem::Element::VectorType nc = S->GetInput()->GetElement(i)->GetNodeCoordinates(n);
for (unsigned int d=0, dof; ( dof = S->GetInput()->GetElement(i)->GetNode(n)->GetDegreeOfFreedom(d) ) != invalidID; d++)
{
nc[d] += S->GetSolution( dof );
}
std::cout << nc << std::endl;
}
std::cout << "];" << std::endl;
ct++;
}
}
// Useful for display purposes - lets you draw each element
// individually, instead of just a stream of nodes
void PrintSolution(FEMSolverType *S )
{
std::cout << std::endl << "Solution: " << std::endl;
const unsigned int invalidID = itk::fem::Element::InvalidDegreeOfFreedomID;
int numberOfNodes = S->GetInput()->GetNumberOfNodes();
for (int i = 0; i < numberOfNodes; i++ )
{
std::cout << "Solution Node " << i << ":";
for (unsigned int d=0, dof; ( dof = S->GetInput()->GetNode(i)->GetDegreeOfFreedom(d) ) != invalidID; d++)
{
std::cout << " " << S->GetSolution( dof );
}
std::cout << std::endl;
}
}
int itkFEMSolverHyperbolicTest(int ac, char* av[])
{
if (ac < 4)
{
std::cout << "Usage: " << av[0];
std::cout << " input-file iterations lsw (0=VNL, 1=Dense VNL, 2=Itpack)";
std::cout << std::endl;
return EXIT_FAILURE;
}
itk::FEMFactoryBase::GetFactory()->RegisterDefaultTypes();
unsigned int niter = atoi ( av[2] );
unsigned int w = atoi( av[3] );
std::vector<double> solution;
if (ac > 4)
{
solution.resize( ac - 4 );
for (int i=4;i<ac;i++)
{
solution[i-4] = atof(av[i]);
}
}
typedef itk::FEMSpatialObjectReader<2> FEMSpatialObjectReaderType;
typedef FEMSpatialObjectReaderType::Pointer FEMSpatialObjectReaderPointer;
FEMSpatialObjectReaderPointer SpatialReader = FEMSpatialObjectReaderType::New();
SpatialReader->SetFileName( av[1] );
try
{
SpatialReader->Update();
}
catch (::itk::fem::FEMException & e)
{
std::cout<<"Error reading FEM problem: "<< av[1] <<"!\n";
e.Print(std::cout);
return EXIT_FAILURE;
}
typedef itk::FEMObjectSpatialObject<2> FEMObjectSpatialObjectType;
FEMObjectSpatialObjectType::ChildrenListType* children = SpatialReader->GetGroup()->GetChildren();
FEMObjectSpatialObjectType::Pointer femSO =
dynamic_cast<FEMObjectSpatialObjectType *>( (*(children->begin() ) ).GetPointer() );
if (!femSO)
{
std::cout << " dynamic_cast [FAILED]" << std::endl;
return EXIT_FAILURE;
}
delete children;
femSO->GetFEMObject()->FinalizeMesh();
/**
* Third, create the FEM solver object and generate the solution
*/
FEMSolverType::Pointer SH = FEMSolverType::New();
SH->SetInput( femSO->GetFEMObject() );
SH->SetTimeStep( .5 );
SH->SetNumberOfIterations( niter );
itk::fem::LinearSystemWrapperDenseVNL lsw_dvnl;
itk::fem::LinearSystemWrapperItpack lsw_itpack;
itk::fem::LinearSystemWrapperVNL lsw_vnl;
switch (w)
{
case 0:
// VNL
std::cout << std::endl << ">>>>>Using LinearSystemWrapperVNL" << std::endl;
SH->SetLinearSystemWrapper(&lsw_vnl);
break;
case 1:
// Dense VNL
std::cout << std::endl << ">>>>>Using LinearSystemWrapperDenseVNL" << std::endl;
SH->SetLinearSystemWrapper(&lsw_dvnl);
break;
case 2:
// IT Pack
std::cout << std::endl << ">>>>>Using LinearSystemWrapperItpack" << std::endl;
SH->SetLinearSystemWrapper(&lsw_itpack);
break;
default:
// Sparse VNL - default
std::cout << std::endl << ">>>>>Using LinearSystemWrapperVNL" << std::endl;
SH->SetLinearSystemWrapper(&lsw_vnl);
break;
}
try
{
SH->Update();
}
catch (itk::ExceptionObject &err)
{
std::cerr << "ITK exception detected: " << err;
return EXIT_FAILURE;
}
PrintK( SH );
PrintF( SH );
PrintNodalCoordinates( SH );
PrintSolution( SH );
if (ac > 4)
{
int numberOfNodes = SH->GetInput()->GetNumberOfNodes();
const unsigned int invalidID = itk::fem::Element::InvalidDegreeOfFreedomID;
for (int i = 0; i < numberOfNodes; i++ )
{
for (unsigned int d=0, dof; ( dof = SH->GetInput()->GetNode(i)->GetDegreeOfFreedom(d) ) != invalidID; d++)
{
double result = SH->GetSolution( dof );
if (fabs(result-solution[dof]) > 1.0e-5)
{
std::cerr << "Error: Solution outside the expected range: " << result << ", " << dof << std::endl;
return EXIT_FAILURE;
}
}
}
}
return EXIT_SUCCESS;
}
|