Commit d2995e69 authored by Pierre-Chanteloup Edwin's avatar Pierre-Chanteloup Edwin
Browse files

Update.

parent bf229a16
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.0)
project(GaussSeidel Fortran)
enable_language(Fortran)
add_executable(GaussSeidel main.f gauss_seidel.f tril.f90 triu.f90)
\ No newline at end of file
add_executable(GaussSeidel main.f90 matop.f90)
FC = gfortran
OPT = -g
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.15
gss: main.f gauss_seidel.o
$(FC) $(OPT) main.f gauss_seidel.o -o main
# Default target executed when no arguments are given to make.
default_target: all
gauss_seidel.o: gauss_seidel.f
$(FC) $(OPT) gauss_seidel.f -c
.PHONY : default_target
# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:
#=============================================================================
# Special targets provided by cmake.
# Disable implicit rules so canonical targets will work.
.SUFFIXES:
# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =
.SUFFIXES: .hpux_make_needs_suffix_list
# Suppress display of executed commands.
$(VERBOSE).SILENT:
# A target that is always out of date.
cmake_force:
.PHONY : cmake_force
#=============================================================================
# Set environment variables for the build.
# The shell in which to execute make rules.
SHELL = /bin/sh
# The CMake executable.
CMAKE_COMMAND = /home/epierre01/Documents/Programs/clion-2019.2.3/bin/cmake/linux/bin/cmake
# The command to remove a file.
RM = /home/epierre01/Documents/Programs/clion-2019.2.3/bin/cmake/linux/bin/cmake -E remove -f
# Escaping for special characters.
EQUALS = =
# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/epierre01/Documents/GM3/Projet/gauss-seidel
# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/epierre01/Documents/GM3/Projet/gauss-seidel
#=============================================================================
# Targets provided globally by CMake.
# Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
/home/epierre01/Documents/Programs/clion-2019.2.3/bin/cmake/linux/bin/cmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache
# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast
# Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..."
/home/epierre01/Documents/Programs/clion-2019.2.3/bin/cmake/linux/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available.
.PHONY : edit_cache
# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/epierre01/Documents/GM3/Projet/gauss-seidel/CMakeFiles /home/epierre01/Documents/GM3/Projet/gauss-seidel/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/epierre01/Documents/GM3/Projet/gauss-seidel/CMakeFiles 0
.PHONY : all
# The main clean target
clean:
rm main *.o *.mod *.exe
\ No newline at end of file
$(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean
# The main clean target
clean/fast: clean
.PHONY : clean/fast
# Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall
# Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast
# clear depends
depend:
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend
#=============================================================================
# Target rules for targets named GaussSeidel
# Build rule for target.
GaussSeidel: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 GaussSeidel
.PHONY : GaussSeidel
# fast build rule for target.
GaussSeidel/fast:
$(MAKE) -f CMakeFiles/GaussSeidel.dir/build.make CMakeFiles/GaussSeidel.dir/build
.PHONY : GaussSeidel/fast
main.o: main.f90.o
.PHONY : main.o
# target to build an object file
main.f90.o:
$(MAKE) -f CMakeFiles/GaussSeidel.dir/build.make CMakeFiles/GaussSeidel.dir/main.f90.o
.PHONY : main.f90.o
main.i: main.f90.i
.PHONY : main.i
# target to preprocess a source file
main.f90.i:
$(MAKE) -f CMakeFiles/GaussSeidel.dir/build.make CMakeFiles/GaussSeidel.dir/main.f90.i
.PHONY : main.f90.i
main.s: main.f90.s
.PHONY : main.s
# target to generate assembly for a file
main.f90.s:
$(MAKE) -f CMakeFiles/GaussSeidel.dir/build.make CMakeFiles/GaussSeidel.dir/main.f90.s
.PHONY : main.f90.s
matop.o: matop.f90.o
.PHONY : matop.o
# target to build an object file
matop.f90.o:
$(MAKE) -f CMakeFiles/GaussSeidel.dir/build.make CMakeFiles/GaussSeidel.dir/matop.f90.o
.PHONY : matop.f90.o
matop.i: matop.f90.i
.PHONY : matop.i
# target to preprocess a source file
matop.f90.i:
$(MAKE) -f CMakeFiles/GaussSeidel.dir/build.make CMakeFiles/GaussSeidel.dir/matop.f90.i
.PHONY : matop.f90.i
matop.s: matop.f90.s
.PHONY : matop.s
# target to generate assembly for a file
matop.f90.s:
$(MAKE) -f CMakeFiles/GaussSeidel.dir/build.make CMakeFiles/GaussSeidel.dir/matop.f90.s
.PHONY : matop.f90.s
# Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
@echo "... all (the default if no target is provided)"
@echo "... clean"
@echo "... depend"
@echo "... rebuild_cache"
@echo "... GaussSeidel"
@echo "... edit_cache"
@echo "... main.o"
@echo "... main.i"
@echo "... main.s"
@echo "... matop.o"
@echo "... matop.i"
@echo "... matop.s"
.PHONY : help
#=============================================================================
# Special targets to cleanup operation of make.
# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system
2
0.000001
3 1 2 4
3 3
1 1
\ No newline at end of file
subroutine gauss_seidel(A, n, nmax, b, x)
implicit none
integer :: n, nmax, i, j
double precision :: A(nmax,nmax), b(nmax), x(nmax), tmp
do i=1,n(1)
tmp = 0
do j=1,n(1)
if (j /= i) then
tmp = tmp + A(i,j) * x(j)
endif
enddo
x(i) = (b(i) - tmp) / A(i, i)
enddo
end
\ No newline at end of file
function inv_trig_inf(L, nmax, n)
integer :: nmax, n, i, j
real :: L(nmax, nmax), inv_trig_inf(nmax, nmax), diag
inv_trig_inf = 0
! inv(1,1) = 1/L(1,1)
! inv(1,2) =
! inv(2,1) = 0
diag = 1
do i = 1,n
inv_trig_inf(i,i) = 1/L(i,i)
diag = diag * L(i,i)
x(l) = b(l)
do j = 1, n
x(l) = x(l) - L(l, c) * x(c)
enddo
x(l) = x(l) / L(l, l)
enddo
end
\ No newline at end of file
program main
implicit none
integer, parameter :: nmax=5
integer i, j, n
real :: A(nmax,nmax), F(nmax,nmax), G(nmax,nmax), D1(nmax,nmax), L(nmax,nmax), LI(nmax,nmax), U(nmax,nmax), Id(nmax,nmax), b(nmax), x(nmax)
real :: prec,res
read (*,*) n, prec
read (*,*) A
read (*,*) b
read (*,*) x
D1 = 0
Id = 0
do i=1,n
D1(i,i) = 1.0/A(i,i)
Id(i,i) = 1
enddo
do i=1,n
do j=1,n
L(i, j) = A
enddo
enddo
L = tril(matmul(D1, A), nmax, n) - Id
U = triu(matmul(D1, A), nmax, n) - Id
LI = inv(L+Id)
F = -matmul(LI, U)
G = matmul(matmul(LI, D1), b)
do i=1,nmax
call solve_system(A, b, x)
res = norm2(matmul(A, x) - b)
write(*,*) i, res
if ( res < prec) then
exit
endif
enddo
end
program main
use matop
implicit none
integer i, j, n, nmax
real, allocatable, dimension(:,:) :: A, F, D1, L, LI, U, Eye, tmp
real, allocatable, dimension(:) :: b, x, G
real :: prec, res
read (*, *) n, prec, nmax
allocate(A(n,n))
allocate(F(n,n))
allocate(D1(n,n))
allocate(L(n,n))
allocate(LI(n,n))
allocate(U(n,n))
allocate(Eye(n,n))
allocate(tmp(n,n))
allocate(b(n))
allocate(x(n))
allocate(G(n))
read (*, *) A
read (*, *) b
read (*, *) x
D1 = 0
Eye = 0
do i = 1, n
D1(i, i) = 1.0 / A(i, i)
Eye(i, i) = 1
enddo
tmp = matmul(D1, A)
call tril(tmp, L)
call triu(tmp, U)
U = U - Eye
call inv_trig_inf(L, LI)
F = -matmul(LI, U)
G = matmul(matmul(LI, D1), b)
deallocate(D1)
deallocate(L)
deallocate(LI)
deallocate(U)
deallocate(Eye)
deallocate(tmp)
do i = 1, nmax
x = matmul(F, x) + G
res = norm2(matmul(A, x) - b)
write(*, *) i, res
if (res < prec) then
exit
endif
enddo
write (*,*) x, matmul(A, x)
deallocate(F)
deallocate(G)
deallocate(x)
deallocate(A)
deallocate(b)
end
\ No newline at end of file
module matop
implicit none
contains
subroutine tril(M, L)
implicit none
integer :: n, i
real, dimension(:,:) :: M, L
n = size(M, 1)
L = 0
do i=1,n
L(i,1:i) = M(i,1:i)
end do
end subroutine tril
subroutine triu(M, U)
implicit none
integer :: n, p, i
real :: M(:,:), U(:,:)
n = size(M, 1)
p = size(M, 2)
U = 0
do i=1,n
U(i,i:p) = M(i,i:p)
end do
end subroutine triu
subroutine inv_trig_inf(L, inv)
implicit none
integer :: i, j, n
real, allocatable, dimension(:,:) :: L(:,:), inv
real, dimension(size(L,1),size(L,2)) :: Eye
n = size(L, 1)
inv = 0;
Eye = 0
do i=1,n
Eye(i,i) = 1
enddo
do i=1,n
inv(i,:) = (Eye(i,:) - matmul(L(i,1:i-1),inv(1:i-1,:))) / L(i,i)
enddo
return
end subroutine inv_trig_inf
end module matop
\ No newline at end of file
FC = gfortran
OPT = -g
gss : main.f gauss_seidel.o
$(FC) $(OPT) main.f gauss_seidel.o -o gss
gauss_seidel.o: gauss_seidel.f
$(FC) $(OPT) gauss_seidel.f -c
clean:
rm gss *.o *.mod *.exe
2 1.e-6 100
3 1 2 4
3 3
1 1
n, prec,nmax
A
b
x
module gauss_seidel
implicit none
private
public :: solve_system
interface solve_system
procedure solve_system_double, solve_system_real
endinterface
contains
subroutine solve_system_double(A, b, x)
implicit none
integer :: An(2), n(1), i, j
double precision :: A(:,:), b(:), x(:), tmp
n = shape(b)
do i=1,n(1)
tmp = 0
do j=1,n(1)
if (j /= i) then
tmp = tmp + A(i,j) * x(j)
endif
enddo
x(i) = (b(i) - tmp) / A(i, i)
enddo
end subroutine solve_system_double
subroutine solve_system_real(A, b, x)
implicit none
integer :: n(1), i, j
real :: A(:,:), b(:), x(:), tmp
n = shape(b)
do i=1,n(1)
tmp = 0
do j=1,n(1)
if (j /= i) then
tmp = tmp + A(i,j) * x(j)
endif
enddo
x(i) = (b(i) - tmp) / A(i, i)
enddo
end subroutine solve_system_real
end module
\ No newline at end of file
%A = [1 2 3 4 5 ; 5 1 2 3 4 ; 4 5 1 2 3 ; 3 4 5 1 2 ; 2 3 4 5 1];
%Abis = [1 -1 -200 0 ; 0 1 100 -100 ; 0 1 101 -101 ; 0 0 0 100];
%b = [1 ; 1 ; 3 ; 4 ; 2];
%bbis = [1 ; 1 ; 2 ; 4];
%x = [0 ; 0 ; 0 ; 0 ; 0];
%xbis = [0 ; 0 ; 0 ; 0];
tol = 1e-10;
cont = 0;
delta = 10;
A = [3 1;2 4];
b = [3;3];
x = [1;1];
n = size(b);
while delta > tol & cont < 200
ref = x;
for i = 1:n(1)
tmp = 0;
for j = 1:n(1)
if j != i
tmp = tmp + A(i,j)*x(j);
end
x(i) = (b(i) - tmp) / A(i,i);
end
end
delta = norm(ref - x);
cont = cont + 1;
end
fprintf('%f\n%f\n%f\n%f\n en %d boucles',x, cont);
\ No newline at end of file
File deleted
program main
use gauss_seidel
implicit none
integer i, n,nmax
real, allocatable :: A(:,:), b(:), x(:)
real :: prec,res
read (*,*) n, prec,nmax
allocate(A(n,n))
allocate(b(n))
allocate(x(n))
read (*,*) A
read (*,*) b
read (*,*) x
do i=1,nmax
call solve_system(A, b, x)
res = norm2(matmul(A, x) - b)
write(*,*) i, res
if ( res < prec) then
exit
endif
enddo
end
function tril(M, nmax, n)
integer :: nmax, n
real M(nmax,nmax), tril(nmax, nmax)
tril = 0
do i=1,n
do j=1,i
tril(i,j) = M(i,j)
end do
end do
end function tril
\ No newline at end of file
function triu(M, nmax, n)
integer :: nmax, n
real M(nmax,nmax), triu(nmax, nmax)
triu = 0
do i=1,n
do j=i,n
triu(i,j) = M(i,j)
end do
end do
end function triu
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment