From 57a091f4618b926937993f4282d66a89c469b23f Mon Sep 17 00:00:00 2001 From: cristiansinaloa Date: Tue, 16 Mar 2021 18:56:36 -0700 Subject: [PATCH] Programming Excercises 1 & 2 --- prgm_01_01.f03 | 4 ++- prgm_01_02.f03 | 76 ++++++++++++++++++++++++++++++++++++++++++ prgm_01_03.f03 | 82 ++++++++++++++++++++++++++++++++++++++++++++++ prgm_02_01.f03 | 57 ++++++++++++++++++++++++++++++++ prgm_02_02.f03 | 72 ++++++++++++++++++++++++++++++++++++++++ prgm_02_03.f03 | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 379 insertions(+), 1 deletion(-) create mode 100644 prgm_01_02.f03 create mode 100644 prgm_01_03.f03 create mode 100644 prgm_02_01.f03 create mode 100644 prgm_02_02.f03 create mode 100644 prgm_02_03.f03 diff --git a/prgm_01_01.f03 b/prgm_01_01.f03 index 291c1f7..2ee6b52 100644 --- a/prgm_01_01.f03 +++ b/prgm_01_01.f03 @@ -53,7 +53,9 @@ Subroutine PrintMatrix3x3(matrix) ! write(*,*)' Printing Matrix' ! - ADD CODE HERE + Do i = 1,3 + write(*,1000) matrix(i,1), matrix(i,2), matrix(i,3) + End Do ! ! return diff --git a/prgm_01_02.f03 b/prgm_01_02.f03 new file mode 100644 index 0000000..8ba74ee --- /dev/null +++ b/prgm_01_02.f03 @@ -0,0 +1,76 @@ + Program prgm_01_02 +! +! This program reads a 3x3 matrix from a user-provided input file. After the +! file is opened and read, it is closed and then printed. +! +! + implicit none + integer,parameter::inFileUnitA=10,inFileUnitB=11 + integer::errorFlag,i + real,dimension(3,3)::matrixInA,matrixInB + character(len=128)::fileNameA,fileNameB +! +! +! Start by asking the user for the name of the data file. +! + write(*,*)' What is the name of the input data file?' + read(*,*) fileNameA + write(*,*)' What is the name of the input data file?' + read(*,*) fileNameB +! +! Open the data file and read matrixInA from that file. +! + open(unit=inFileUnitA,file=TRIM(fileNameA),status='old', & + iostat=errorFlag) + if(errorFlag.ne.0) then + write(*,*)' There was a problem opening the input file.' + goto 999 + endIf + do i = 1,3 + read(inFileUnitA,*) matrixInA(1,i),matrixInA(2,i),matrixInA(3,i) + endDo + close(inFileUnitA) +! + open(unit=inFileUnitB,file=TRIM(fileNameB),status='old', & + iostat=errorFlag) + if(errorFlag.ne.0) then + write(*,*)' There was a problem opening the input file.' + goto 999 + endIf + do i = 1,3 + read(inFileUnitB,*) matrixInB(1,i),matrixInB(2,i),matrixInB(3,i) + endDo + close(inFileUnitB) +! +! Call the subroutine PrintMatrix to print matrixInA. +! + call PrintMatrix3x3(matrixInA) + call PrintMatrix3x3(matrixInB) +! + 999 continue + End Program prgm_01_02 + + + Subroutine PrintMatrix3x3(matrix) +! +! This subroutine prints a 3x3 real matrix. The output is written to StdOut. +! + implicit none + real,dimension(3,3),intent(in)::matrix + integer::i +! +! Format statements. +! + 1000 format(3(2x,f5.1)) +! +! Do the printing job. +! + write(*,*)' Printing Matrix' +! + Do i = 1,3 + write(*,1000) matrix(i,1), matrix(i,2), matrix(i,3) + End Do +! +! + return + End Subroutine PrintMatrix3x3 diff --git a/prgm_01_03.f03 b/prgm_01_03.f03 new file mode 100644 index 0000000..7b71393 --- /dev/null +++ b/prgm_01_03.f03 @@ -0,0 +1,82 @@ + Program prgm_01_03 +! +! This program reads a 3x3 matrix from a user-provided input file. After the +! file is opened and read, it is closed and then printed. +! +! + implicit none + integer,parameter::inFileUnitA=10,inFileUnitB=11 + integer::errorFlag,i + real,dimension(3,3)::matrixInA,matrixInB,matrixProduct + character(len=128)::fileNameA,fileNameB +! +! +! Start by asking the user for the name of the data file. +! + write(*,*)' What is the name of the input data file?' + read(*,*) fileNameA + write(*,*)' What is the name of the input data file?' + read(*,*) fileNameB +! +! Open the data file and read matrixInA from that file. +! + open(unit=inFileUnitA,file=TRIM(fileNameA),status='old', & + iostat=errorFlag) + if(errorFlag.ne.0) then + write(*,*)' There was a problem opening the input file.' + goto 999 + endIf + do i = 1,3 + read(inFileUnitA,*) matrixInA(1,i),matrixInA(2,i),matrixInA(3,i) + endDo + close(inFileUnitA) +! + open(unit=inFileUnitB,file=TRIM(fileNameB),status='old', & + iostat=errorFlag) + if(errorFlag.ne.0) then + write(*,*)' There was a problem opening the input file.' + goto 999 + endIf + do i = 1,3 + read(inFileUnitB,*) matrixInB(1,i),matrixInB(2,i),matrixInB(3,i) + endDo + close(inFileUnitB) +! +! Call the subroutine PrintMatrix to print matrixInA. +! + call PrintMatrix3x3(matrixInA) + call PrintMatrix3x3(matrixInB) +! +! Form matrixProduct using the intrinsic function MatMul. Then, print the +! result. +! + matrixProduct = MatMul(matrixInA,matrixInB) + call PrintMatrix3x3(matrixProduct) +! + 999 continue + End Program prgm_01_03 + + + Subroutine PrintMatrix3x3(matrix) +! +! This subroutine prints a 3x3 real matrix. The output is written to StdOut. +! + implicit none + real,dimension(3,3),intent(in)::matrix + integer::i +! +! Format statements. +! + 1000 format(3(2x,f5.1)) +! +! Do the printing job. +! + write(*,*)' Printing Matrix' +! + Do i = 1,3 + write(*,1000) matrix(i,1), matrix(i,2), matrix(i,3) + End Do +! +! + return + End Subroutine PrintMatrix3x3 diff --git a/prgm_02_01.f03 b/prgm_02_01.f03 new file mode 100644 index 0000000..09fa26a --- /dev/null +++ b/prgm_02_01.f03 @@ -0,0 +1,57 @@ + Program prgm_02_01 + + implicit none + real:: m,l + integer:: n1,n2 + integer:: ierror=1 + real,external:: PIB_1D_T_Element + + write(*,*)' What is the mass of the particle M? (must be an integer)' + read(*,*) m + if (m <= 0) then + write(*,*)'The mass cannot be less than or equal to zero.' + stop + end if + + write(*,*)'What is the value of the lenght of the box L? (must be & + & an integer)' + read(*,*) l + + if (l <= 0.) then + write(*,*)'The box length cannot be less than or equal to zero.' + stop + end if + + write(*,*)'What is the value of the quantum number of the first & + & eigenstate n1? (must be an integer)' + read(*,*) n1 + + write(*,*)'What is the value of the quantum number of the second & + & eigenstate n2? (must be an integer)' + read(*,*) n2 + + 900 Format(1x, 'Kinetic Energy Matrix element', i5,',',i5,' is',f12.5,'.') +! +! Do the printing +! + If (n1 == n2) then + write(*,900) n1, n2, PIB_1D_T_Element(m,l,n1) + else + write(*,'(1x, a, i2, a, i2, a, a)') 'Kinetic Energy Matrix& + & element ', n1, ',', n2, ' is ', '0.' + end if +! + End Program prgm_02_01 +! +! (m,l,n1) = (a,b,c) +! + real function PIB_1D_T_Element(a,b,c) + implicit none + real:: a,b + real,parameter:: pi=4.*atan(1.0) + integer:: c + + PIB_1D_T_Element =((c**2)*(pi**2))/(2*a*(b**2)) + return +! + end function PIB_1D_T_Element diff --git a/prgm_02_02.f03 b/prgm_02_02.f03 new file mode 100644 index 0000000..48df37b --- /dev/null +++ b/prgm_02_02.f03 @@ -0,0 +1,72 @@ + Program prgm_02_02 + + implicit none + real:: m,l,b + integer:: n1,n2 + integer:: ierror=1 + real,external:: PIB_1D_T_Element + real,external:: PIB_1D_Modified_V_Element + + write(*,*)' What is the mass of the particle M? (must be an integer)' + read(*,*) m + if (m <= 0) then + write(*,*)'The mass cannot be less than or equal to zero.' + stop + end if + + write(*,*)'What is the value of the lenght of the box L? (must be & + & an integer)' + read(*,*) l + + if (l <= 0.) then + write(*,*)'The box length cannot be less than or equal to zero.' + stop + end if + + write(*,*)'What is the value of the quantum number of the first & + & eigenstate n1? (must be an integer)' + read(*,*) n1 + n2=n1 + + write(*,*)'What is the value of b for the potential V(x)=bx? & + &(must be a real)' + read(*,*) b + + 900 Format(1x, 'Kinetic Energy Matrix element', i5,',',i5,' is',f12.5,'.') + 1000 Format(1x, 'Potential Energy Matrix element', i5,',',i5,' is',f12.5,'.') +! +! Do the printing +! + If (n1 == n2) then + write(*,900) n1, n2, PIB_1D_T_Element(m,l,n1) + write(*,1000) n1, n2, PIB_1D_Modified_V_Element(b,l) + else + write(*,'(1x, a, i2, a, i2, a, a)') 'Kinetic Energy Matrix& + & element ', n1, ',', n2, ' is ', '0.' + write(*,'(1x, a, i2, a, i2, a, a)') 'Potential Energy Matrix& + & element ', n1, ',', n2, ' is ', '0.' + end if +! + End Program prgm_02_02 +! +! (m,l,n1) = (a,b,c) +! + real function PIB_1D_T_Element(a,b,c) + implicit none + real:: a,b + real,parameter:: pi=4.0*atan(1.0) + integer:: c + + PIB_1D_T_Element =((c**2)*(pi**2))/(2*a*(b**2)) + return +! + end function PIB_1D_T_Element +! + real function PIB_1D_Modified_V_Element(a,b) + implicit none + real:: a,b + + PIB_1D_Modified_V_Element = (a*b)/2.0 + return +! + end function PIB_1D_Modified_V_Element diff --git a/prgm_02_03.f03 b/prgm_02_03.f03 new file mode 100644 index 0000000..b22ce9b --- /dev/null +++ b/prgm_02_03.f03 @@ -0,0 +1,89 @@ + Program prgm_02_03 + + implicit none + real:: m,l,b + integer:: n1,n2 + integer:: ierror=1 + real,external:: PIB_1D_T_Element + real,external:: PIB_1D_Modified_V_Element + real,external:: PIB_1D_Modified_Hamiltonian_Element + + write(*,*)' What is the mass of the particle M? (must be an integer)' + read(*,*) m + if (m <= 0) then + write(*,*)'The mass cannot be less than or equal to zero.' + stop + end if + + write(*,*)'What is the value of the lenght of the box L? (must be & + & an integer)' + read(*,*) l + + if (l <= 0.) then + write(*,*)'The box length cannot be less than or equal to zero.' + stop + end if + + write(*,*)'What is the value of the quantum number of the first & + & eigenstate n1? (must be an integer)' + read(*,*) n1 + n2=n1 + + write(*,*)'What is the value of b for the potential V(x)=bx? & + &(must be a real)' + read(*,*) b + + 900 Format(1x, 'Kinetic Energy Matrix element', i5,',',i5,' is',f12.5,'.') + 1000 Format(1x, 'Potential Energy Matrix element', i5,',',i5,' is',f12.5,'.') + 1100 Format(1x, 'Hamiltonian Matrix element', i5,',',i5,' is',f12.5,'.') +! +! Do the printing +! + If (n1 == n2) then + write(*,900) n1, n2, PIB_1D_T_Element(m,l,n1) + write(*,1000) n1, n2, PIB_1D_Modified_V_Element(b,l) + write(*,1100) n1, n2, PIB_1D_Modified_Hamiltonian_Element& + & (m,l,n1,b) + + else + write(*,'(1x, a, i2, a, i2, a, a)') 'Kinetic Energy Matrix& + & element ', n1, ',', n2, ' is ', '0.' + write(*,'(1x, a, i2, a, i2, a, a)') 'Potential Energy Matrix& + & element ', n1, ',', n2, ' is ', '0.' + end if +! + End Program prgm_02_03 +! +! (m,l,n1) = (a,b,c) +! + real function PIB_1D_T_Element(a,b,c) + implicit none + real:: a,b + real,parameter:: pi=4.0*atan(1.0) + integer:: c + + PIB_1D_T_Element =((c**2)*(pi**2))/(2*a*(b**2)) + return +! + end function PIB_1D_T_Element +! + real function PIB_1D_Modified_V_Element(a,b) + implicit none + real:: a,b + PIB_1D_Modified_V_Element = (a*b)/2.0 + return +! + end function PIB_1D_Modified_V_Element + + real function PIB_1D_Modified_Hamiltonian_Element(m,l,n1,b) + implicit none + real:: m,l,b + integer:: n1 + real,external:: PIB_1D_T_Element + real,external:: PIB_1D_Modified_V_Element + + PIB_1D_Modified_Hamiltonian_Element = & + &PIB_1D_T_Element(m,l,n1) + PIB_1D_Modified_V_Element(b,l) + return +! + end function PIB_1D_Modified_Hamiltonian_Element