From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Arne Babenhauserheide Newsgroups: gmane.lisp.guile.devel Subject: I wrote fluid advection code: How to make this more elegant? Date: Sat, 23 Jan 2016 11:00:17 +0100 Message-ID: <87io2kpqdq.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" X-Trace: ger.gmane.org 1453543250 27445 80.91.229.3 (23 Jan 2016 10:00:50 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 23 Jan 2016 10:00:50 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Jan 23 11:00:34 2016 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aMuzi-0000cP-2M for guile-devel@m.gmane.org; Sat, 23 Jan 2016 11:00:34 +0100 Original-Received: from localhost ([::1]:56705 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMuzh-0007DL-4h for guile-devel@m.gmane.org; Sat, 23 Jan 2016 05:00:33 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41845) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMuze-0007DF-2K for guile-devel@gnu.org; Sat, 23 Jan 2016 05:00:31 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aMuza-00081v-JC for guile-devel@gnu.org; Sat, 23 Jan 2016 05:00:29 -0500 Original-Received: from mout.web.de ([212.227.15.14]:56851) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMuza-00081R-9F for guile-devel@gnu.org; Sat, 23 Jan 2016 05:00:26 -0500 Original-Received: from fluss ([85.212.4.17]) by smtp.web.de (mrweb004) with ESMTPSA (Nemesis) id 0MXSg2-1aadyT2fWY-00WSkH; Sat, 23 Jan 2016 11:00:23 +0100 User-agent: mu4e 0.9.15; emacs 24.5.1 X-Provags-ID: V03:K0:BGjXUCTEtzelVb/Ul/3PF67tOsloicGwu5Thas2rDt5UjU0hkLO aS3Z3owTWMy+eAGVPt7ILOU95NUNnSm/6ddcqjYNMaKla+hcjg/nhmdAvn3zRLRNBT6aStp b2EJxAb2v15jenJlwHfpdEUHIUHCAmSX/j8X8IDmhzDB0+Acm4I6Dfz1iIxjGbZZEJzri+S Y0mF2XBIovBsiLKfzCnrQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:eWWWcP1oJww=:zkBFdWE04nclKxfDCK+ZDA eQCQbT05H8tyoA8yI95rmkhLz/K5fZkehHVzXo9UwGVjMKC/+dVoKdqFTK4imyfNq1ZdcS68X i0/w4pL/5C/qSJ7i+70JetMiMtY1BZ1F1Fe/tBOpB3B6ydmyPyHXRSCHDYkyHFidxD5Lfoy52 X/HHY7vlq5TYr6YWrvARyThe6dXFNcfYmNJMmOJvN+e8EiR/YHiU5plx3rBcu/vmJcbXIsvEh zd9UUQ5po9wYQDaStZ40WYd8zTQIK9+4OcgcXAzqUVSrYNZkuAdzZA6QOuOr2nm54thBkIWC3 5KfvTmKEcdn8ZL4XL2up2KT2v//Lf+BEWN6yMFahcij/zuQqMd5VSukIbqTL/AozVpOAM90y7 O4MXPpNh0WVGUy1H0TB3NRTmMrp5EltM5Iz2EjKxUwUlO4gI39zYZTGuyfWDDoU//byrQCRKE BBwsTT71BWWLYU3Zfb17U7VebRlHwPw0fAR4vDRfPzU+PY7DeOSuS9z6M7pQavDQTfQTUVlb4 Jv9j/AKYNfkoOx4BhVbYTXYtdV6pj65idZmIhF/EbNs1T11hJ7mfNpFbJfFt8gL1pTr8AIkdr fqIzqITNxqwGei7u1b7Fjxxkm93bCQPdtvAfYXekMibzRKHLu/cdhO7R9v/HWSwrNF8fYA11Q J8K4cuciCDcMd+7aidNSS9qxZ+4/NyNFgO61wtwsVwVseAJLGSMNrhhKnSWVdOzNoH2E7T2LD X3SCbCKSGuA/KV/++EWah35hSSXhKa5Gj8GSkuKPFPPGMMQjcC77sXBo5dU= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.15.14 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:18127 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi, I just recreated a fluid advection exercise in Guile Scheme and I=E2=80=99m= not quite happy with its readability. Can you help me improve it? My main gripe is that the math does not look instantly accessible. The original version was in Python: psi[i] - c1*(psi[i+1] - psi[i-1]) + c2*(psi[i+1] - 2.0*psi[i] + psi[i-1= ]) My port to Scheme looks like this: (let ((newvalue (+ (- (psir i) (* c1 (- (psir (+ i 1)) (psir (- i 1))))) (* c2 (+ (- (psir (+ i 1)) (* 2 (psir i))) (psir (- i 1))))))) (array-set! psinew newvalue i)) Liebe Gr=C3=BC=C3=9Fe, Arne Here=E2=80=99s the full code: #!/bin/sh # -*- scheme -*- exec guile -e '(@@ (advection) main)' -s "$0" "$@" !# ; Copyright (c) 2015 John Burkardt (original Python), 2016 Corinna ; Hoose (adaption) and 2016 Arne Babenhauserheide (pep8 + Scheme ; version). ; License: LGPL, built on the Python version from 2015 John Burkardt ; and Corinna Hoose. License LGPL. (define-module (advection) #:use-module (ice-9 optargs) ; define* #:use-module (srfi srfi-1) ; iota #:use-module (ice-9 format) #:use-module (ice-9 popen)) (define* (fd1d-advection-lax-wendroff #:key (nx 101) (nt 1000) (c 1)) (let* ((dx (/ 1 (- nx 1))) (x (iota nx 0 (/ 1 nx))) (dt (/ 1 nt)) (c1 (* #e0.5 (* c (/ dt dx)))) (c2 (* 0.5 (expt (* c (/ dt dx)) 2)))) (format #t "CFL condition: dt (~g) =E2=89=A4 (~g) dx/c\n" dt (/ dx c)) (let ((psi (make-array 0 nx)) (X (make-array 0 nx (+ nt 1))) (Y (make-array 0 nx (+ nt 1))) (Z (make-array 0 nx (+ nt 1)))) (let ((psinew (let ((pn (make-array 0 nx))) (let loop ((i 0)) (cond ((=3D i nx) pn) (else (let ((xi (list-ref x i))) (when (and (<=3D 0.4 xi) (<=3D xi 0.6)) (array-set! pn (* (expt (- (* 10 xi) 4) 2) (expt (- 6 (* 10 xi)) 2)) i)) (loop (+ 1 i))))))))) (define (psir i) (array-ref psi i)) (let loop ((j 0)) (cond ((> j nt) #t) ; done (else=20 (let ((t (/ j nt))) (when (>=3D j 1) (let ((newvalue (+ (- (psir 0) (* c1 (- (psir 1) (psir (- nx 1))))) (* c2 (+ (- (psir 1) (* 2 (psir 0))) (psir (- nx 1))))))) (array-set! psinew newvalue 0)) (let loop ((i 1)) (when (< i (- nx 1)) (let ((newvalue (+ (- (psir i) (* c1 (- (psir (+ i 1)) (psir (- = i 1))))) (* c2 (+ (- (psir (+ i 1)) (* 2 (psi= r i))) (psir (- i 1))))))) (array-set! psinew newvalue i)) (loop (+ i 1)))) (let ((newvalue (+ (- (psir (- nx 1)) (* c1 (- (psir 0) (psir (- nx 2))))) (* c2 (+ (- (psir 0) (* 2 (psir (- nx 1)= ))) (psir (- nx 2))))))) (array-set! psinew newvalue (- nx 1)))) (let loop ((i 0)) (when (< i nx) (array-set! psi (array-ref psinew i) i) (array-set! X (list-ref x i) i j) (array-set! Y t i j) (array-set! Z (psir i) i j) (loop (+ i 1))))) (loop (+ j 1))))) (list X Y Z))))) (define (main args) (display "Hello World!\n") (let ((res (fd1d-advection-lax-wendroff))) (let ((X (list-ref res 0)) (Y (list-ref res 1)) (Z (list-ref res 2))) ; (display Z) (let ((port (open-output-pipe "python"))) (format port "import pylab as pl\n") (format port "y =3D [[float(j) for j in i] for i in eval('~A'[2:].r= eplace(' ',', '))]\n" Z) (format port "pl.imshow(y)\n") (format port "pl.colorbar()\n") (format port "pl.show()\n") (close-pipe port)))) (newline)) =2D-=20 Unpolitisch sein hei=C3=9Ft politisch sein ohne es zu merken --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iJwEAQEIAAYFAlajTzYACgkQ3M8NswvBBUiAdgQAlDEQuCJ5trVz+0xcaEt3nKYG urHWIlSEm2e88VE9XU5Jx0JBTJf4+qb9z0FY06Wtx3cVJ3BT14bR1piBswTwWAq+ WWnVXYB1nq2uPC4l+91zG06FsT9QI6bqHoa3KulAM9IuYzZYTFBeR0m/z5jEWivT IvAYeT4PhmiE/Dw7j2M= =yMB/ -----END PGP SIGNATURE----- --=-=-=--