From b68207631c57aaf18116eafaaba77c0fddeac6d7 Mon Sep 17 00:00:00 2001 From: tTh Date: Wed, 30 Nov 2022 02:52:16 +0100 Subject: [PATCH] first real run, make a gif89a, need more tweaking --- GravityField/.gitignore | 4 ++ GravityField/Makefile | 5 ++- GravityField/essai.f90 | 89 ++++++++++++++++++++++++++++---------- GravityField/realfield.f90 | 20 ++++----- GravityField/runme.sh | 5 ++- 5 files changed, 86 insertions(+), 37 deletions(-) diff --git a/GravityField/.gitignore b/GravityField/.gitignore index 28b14b5..e121fb9 100644 --- a/GravityField/.gitignore +++ b/GravityField/.gitignore @@ -1,6 +1,10 @@ essai +WS/*.pgm + +*.gif + foo.pgm bar.pgm diff --git a/GravityField/Makefile b/GravityField/Makefile index 23e5236..5552fea 100644 --- a/GravityField/Makefile +++ b/GravityField/Makefile @@ -2,7 +2,8 @@ # Fortraneries by tTh - Gravity Field # -GFOPT = -Wall -Wextra -g -time +GFOPT = -Wall -Wextra -g -time -pg -I../Modules +MODOBJ = '../Modules/spitpgm.o' all: essai @@ -10,6 +11,6 @@ realfield.o: realfield.f90 Makefile gfortran $(GFOPT) -c $< essai: essai.f90 Makefile realfield.o - gfortran $(GFOPT) $< realfield.o -o $@ + gfortran $(GFOPT) $< realfield.o $(MODOBJ) -o $@ diff --git a/GravityField/essai.f90 b/GravityField/essai.f90 index 8dbe3e6..6e156b4 100644 --- a/GravityField/essai.f90 +++ b/GravityField/essai.f90 @@ -1,54 +1,97 @@ +!----------------------------------------------------------------------- ! ! project "gravity field" ! +!----------------------------------------------------------------------- program essai use realfield + use spitpgm ! XXX + implicit none type(massbody) :: planet integer :: foo - planet%posx = 1337.314 - planet%posy = 1664.666 - planet%mass = 1e4 + character (len=100) :: filename - do foo=1, 10 - call build_a_field(800, 600, planet) - planet%posy = planet%posy + 51.45 + planet%posx = 337.314 + planet%posy = 164.666 + planet%mass = 1e8 + planet%serial = 42 + + do foo=0, 48 + write (filename, "(a, i5.5, a)") 'WS/', foo, '.pgm' + call build_and_write_a_field(800, 600, planet, filename) + planet%posx = planet%posx + 6.99 + planet%posy = planet%posy + 2.55 enddo STOP 'YOLO' -contains !------------------------------------------ -! -subroutine build_a_field(szx, szy, moon) +contains +!----------------------------------------------------------------------- +!- +! compute a field with only one body; and write pic file +!- +subroutine build_and_write_a_field(szx, szy, moon, fname) integer, intent(in) :: szx, szy type(massbody), intent(in) :: moon + character (len=*), intent(in) :: fname - integer :: ix, iy - real :: fx, fy - real :: grav, maxi, mini + real :: maxi, mini integer :: errcode - real, dimension(:,:), allocatable :: field + real, dimension(:,:), allocatable :: field + integer, dimension(:,:), allocatable :: greymap allocate(field(szx, szy), stat=errcode) - - do ix=1, szx - fx = real(ix) - do iy=1, szy - fy = real(iy) - grav = compute_gravity(ix, iy, moon) / 2180800.0 - field(ix,iy) = grav - enddo - enddo + call compute_a_field(field, moon) maxi = maxval(field) mini = minval(field) print *, "field: ", mini, maxi, maxi-mini + allocate(greymap(szx, szy), stat=errcode) + greymap = 65535 + ! convert from real value to 16 bits int values + where (field < 65530.0) + greymap = int(field) + end where + + call spit_as_pgm_16(greymap, trim(fname)) + + ! make valgrind happy deallocate(field) + deallocate(greymap) end subroutine - !------------------------------------------ +!----------------------------------------------------------------------- +!- +! Compute the gravity field in a pre-allocated array relative +! to the massbody 'moon'. Nobody know where the magic number +! come from, sorry. +!- +subroutine compute_a_field(field, moon) + real, dimension(:,:), intent(out) :: field + type(massbody), intent(in) :: moon + + integer :: ix, iy + real :: fx, fy + real :: grav + + ! print *, "pic size ", ubound(field, 1), "W", ubound(field, 2), "H" + ! print *, "mass body ", moon + + do ix=1, ubound(field, 1) + fx = real(ix) + do iy=1, ubound(field, 2) + fy = real(iy) + grav = compute_gravity(fx, fy, moon) + field(ix,iy) = grav + enddo + enddo + +end subroutine +!----------------------------------------------------------------------- +!----------------------------------------------------------------------- end program diff --git a/GravityField/realfield.f90 b/GravityField/realfield.f90 index 27272a8..233a6ac 100644 --- a/GravityField/realfield.f90 +++ b/GravityField/realfield.f90 @@ -6,7 +6,8 @@ module realfield implicit none !----------------------------------------------------------------------- - +! definition of structures +! type massbody real :: posx, posy real :: mass = 1.0 @@ -17,23 +18,20 @@ end type contains !----------------------------------------------------------------------- -function compute_gravity(ix, iy, body) - - integer, intent(in) :: ix, iy +function compute_gravity(fx, fy, body) + real, intent(in) :: fx, fy type(massbody), intent(in) :: body real :: compute_gravity - real :: rx, ry, dist - - rx = real(ix) - body%posx - ry = real(iy) - body%posy + real :: rx, ry, dist + rx = fx - body%posx + ry = fy - body%posy dist = sqrt( (rx*rx) + (ry*ry) ) - if (dist .LT. 0.15) then + if (dist .LT. 4.50) then write (0, *) "dist too small ", dist compute_gravity = 0e0 endif - - compute_gravity = body%mass * (dist ** 2) + compute_gravity = body%mass / (dist ** 2) end function diff --git a/GravityField/runme.sh b/GravityField/runme.sh index 84eb42b..81e576e 100755 --- a/GravityField/runme.sh +++ b/GravityField/runme.sh @@ -4,4 +4,7 @@ set -e # stop on error make essai -time ./essai +./essai + +convert -delay 10 WS/*.pgm foo.gif +