SyFi  0.3
code_gen.cpp File Reference
#include <SyFi.h>
#include <fstream>

Go to the source code of this file.

Functions

void compute_Poisson_element_matrix (FE &fe, Dof &dof, std::map< std::pair< unsigned int, unsigned int >, GiNaC::ex > &A)
void code_gen2D (FE &fe)
int main ()

Function Documentation

void code_gen2D ( FE fe)

Definition at line 34 of file code_gen.cpp.

References SyFi::FE::N(), SyFi::FE::nbf(), SyFi::x, and SyFi::y.

Referenced by main().

                       { 
    cout <<csrc; 
    for (unsigned int i=0; i< fe.nbf(); i++) {
        cout <<"double N"<<i<<"(double x, double y){"<<endl; 
        cout <<"  return "<<fe.N(i)<<";"<<endl; 
        cout <<"}"<<endl; 
    }

    for (unsigned int i=0; i< fe.nbf(); i++) {
        cout <<"double dN"<<i<<"dx(double x, double y){"<<endl; 
        cout <<"  return "<<diff(fe.N(i),x)<<";"<<endl; 
        cout <<"}"<<endl; 
    }

    for (unsigned int i=0; i< fe.nbf(); i++) {
        cout <<"double dN"<<i<<"dy(double x, double y){"<<endl; 
        cout <<"  return "<<diff(fe.N(i),y)<<";"<<endl; 
        cout <<"}"<<endl; 
    }

    cout <<"double N(int i, double x, double y){"<<endl; 
    cout <<"  switch(i) {"<<endl; 
    for (unsigned int i=0; i< fe.nbf(); i++) {
        cout <<"    case "<<i<<" :  return N"<<i<<"(x,y);"<<endl;  
    }
    cout <<"  }"<<endl; 
    cout <<"}"<<endl; 

    cout <<"double dNdx(int i, double x, double y){"<<endl; 
    cout <<"  switch(i) {"<<endl; 
    for (unsigned int i=0; i< fe.nbf(); i++) {
        cout <<"    case "<<i<<" :  return dN"<<i<<"dx(x,y);"<<endl;  
    }
    cout <<"  }"<<endl; 
    cout <<"}"<<endl; 

    cout <<"double dNdy(int i, double x, double y){"<<endl; 
    cout <<"  switch(i) {"<<endl; 
    for (unsigned int i=0; i< fe.nbf(); i++) {
        cout <<"    case "<<i<<" :  return dN"<<i<<"dy(x,y);"<<endl;  
    }
    cout <<"  }"<<endl; 
    cout <<"}"<<endl; 
}
void compute_Poisson_element_matrix ( FE fe,
Dof dof,
std::map< std::pair< unsigned int, unsigned int >, GiNaC::ex > &  A 
)

Definition at line 8 of file code_gen.cpp.

References SyFi::FE::dof(), SyFi::FE::get_polygon(), SyFi::Dof::glob_dof(), SyFi::grad(), SyFi::inner(), SyFi::Dof::insert_dof(), SyFi::Polygon::integrate(), SyFi::FE::N(), and SyFi::FE::nbf().

Referenced by check_CrouzeixRaviart(), and main().

{
    std::pair<unsigned int,unsigned int> index; 

    // Insert the local degrees of freedom into the global Dof
    for (unsigned int i=0; i< fe.nbf(); i++) {
        dof.insert_dof(1,i,fe.dof(i)); 
    }

    Polygon& domain = fe.get_polygon(); 

    // The term (grad u, grad v)  
    for (unsigned int i=0; i< fe.nbf(); i++) {
        index.first = dof.glob_dof(fe.dof(i));               // fetch the global dof for Ni 
        for (unsigned int j=0; j< fe.nbf(); j++) {
            index.second = dof.glob_dof(fe.dof(j));            // fetch the global dof for Nj 
            GiNaC::ex nabla = inner(grad(fe.N(i)),grad(fe.N(j))); 
            GiNaC::ex Aij = domain.integrate(nabla);   // compute the integral  
            A[index] += Aij;                           // add to global matrix 
        }
    }
}
int main ( )

Definition at line 79 of file code_gen.cpp.

References code_gen2D(), SyFi::compare_archives(), SyFi::Lagrange::compute_basis_functions(), compute_Poisson_element_matrix(), SyFi::initSyFi(), SyFi::istr(), SyFi::StandardFE::N(), SyFi::StandardFE::nbf(), print(), SyFi::StandardFE::set_order(), and SyFi::StandardFE::set_polygon().

           {

    initSyFi(2); 

    Triangle triangle(lst(0,0), lst(1,0), lst(0,1));   
    Lagrange fe; 
    fe.set_order(2); 
    fe.set_polygon(triangle); 
    fe.compute_basis_functions(); 


    code_gen2D(fe); 


    Dof dof; 
    std::map<std::pair<unsigned int,unsigned int>, ex> A; 
    ::compute_Poisson_element_matrix(fe, dof, A); 
    cout <<"C code format on output "<<endl; 
    cout <<csrc; 
    print(A); 


    archive ar; 
    for (unsigned int i=0; i<fe.nbf(); i++) {
      ar.archive_ex(fe.N(i), istr("N",i).c_str()); 

    }
    pair<unsigned int,unsigned int> index; 
    for (unsigned int i=0; i<fe.nbf(); i++) {
      index.first = i; 
      for (unsigned int j=0; j<fe.nbf(); j++) {
        index.second = j; 
        ar.archive_ex(A[index], istr("A",i,j).c_str()); 
      }

    }
    ofstream vfile("code_gen.gar.v"); 
    vfile << ar; vfile.close(); 
    if(!compare_archives("code_gen.gar.v", "code_gen.gar.r")) { 
      cerr << "Failure!" << endl;
      return -1;
    }

    return 0; 
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator