Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F65434454
main.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Jun 3, 19:01
Size
3 KB
Mime Type
text/x-c
Expires
Wed, Jun 5, 19:01 (2 d)
Engine
blob
Format
Raw Data
Handle
18071625
Attached To
R8929 Conjugate Gradient Solver
main.cpp
View Options
/**
* @file main.cpp
* @author Sergio Hernandez
* This file is part of the Conjugate Gradient Project
*
* Ths is the entry point of the project.
*
*/
#include <iostream>
#include<vector>
#include "CG_Serial.hpp"
#include "ConfigFileParser.hpp"
/**
* Constant to inform the user the correct usage of the program in case of a misuse.
*/
#define USAGE "Please enter correctly one argument (a config file). Example: ./CG_Parallel configFile.txt"
/**
* Constant which defines the correct number of arguments the program can receive.
*/
#define N_ARGS 2
int main(int argc, char* argv[]) {
//TODO Temporal - using Tridiagonal matrix
if(argc!=N_ARGS){
std::cout<<USAGE<< std::endl;
return 1;
}
try {
int N, max_iter, number_outputs;
double tol;
std::string type_input, type_output, outputFile;
std::vector<double> A;//Filled with 0.0
std::vector<double> b;//Filled with 1.0
std::string fName = argv[1];
ConfigFileParser parser = ConfigFileParser();
parser.parseFile(fName);
parser.verify();
std::cout << "Finished parsing the file " << std::endl;
//This could be an executer
std::map<std::string, std::string> data = parser.getData();
//Size of the matrix
std::string tempKey = "N";
N = stoi(data[tempKey]);
std::cout << " N = " << N << std::endl;
//Tolerance
tempKey = "tol";
tol = stof(data[tempKey]);
std::cout << " tol = " << tol << std::endl;
//max iterations
tempKey = "max_iter";
max_iter = stoi(data[tempKey]);
std::cout << " max_iter = " << max_iter << std::endl;
//type input
tempKey = "type_input";
type_input = data[tempKey];
//number_outputs
tempKey = "number_outputs";
number_outputs = stoi(data[tempKey]);
tempKey = "type_output";
type_output = data[tempKey];
tempKey = "outputFile";
outputFile = data[tempKey];
if (type_input=="tridiagonal") {
std::cout<<"Matrix A is: "<<type_input<< std::endl;
//TODO Temporal - using Tridiagonal matrix
A.resize(N*N);//Fills it with 0s.
b.resize(N,1.0);//Fills it with 0s.
std::fill(b.begin(), b.end(), 1);//Fills it with 1s.
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (j == i+1){
if (i < N-1 ) {
A[i + N * j] = 1.0; //Upper
}
}
if (j == i-1){
if (i > 0 ){
A[i+N*j] = 1.0; //Downer
}
}
if (j == i){
A[i+N*j] = -2.0; //Main diagonal
}
}
}
}
std::vector<double> x_0 (N,0.0);
CG_Serial solver = CG_Serial(A,b,tol,max_iter);
std::tuple<std::vector<double>,int> result = solver.computeCG(x_0);
std::vector<double> x = std::get<0>(result);
int k = std::get<1>(result);
std::cout << "Solution was found after : "<<k<<" iterations.\n";
std::cout<<"Solution x: \n";
for(int i=0;i<x.size();i++){
std::cout<< x[i]<<"\n";
}
}
catch(const std::runtime_error &e){
std::cout << e.what() <<std::endl;
return 1;
}
return 0;
}
Event Timeline
Log In to Comment