/* Laplace problem in Picat. From the CLP(R) laplace example: """ ************************************* CLP(R) Version 1.1 - Example Programs ************************************* Solve the Dirichlet problem for Laplace's equation using Leibman's five-point finit-differenc approximation. The goal ?- go1 is a normal example, while the goal ?- go2 shows output constraints for a small region where the boundary conditions are not specified. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import mip. main => go. go ?=> data(1,R,C,Z,M,X), laplace(R,C,Z,M, X), foreach(I in 1..R+1) foreach(J in 1..C+1) printf("%6.2f ", X[I,J]) end, nl end, nl. go => true. % % generate % go2 => N = 30, Z = 0.0, M = 100.0, generate(N,Z,M, X), laplace(N,N,Z,M, X), print_matrix(X), nl. print_matrix(X) => N = X.len, foreach(I in 1..N) foreach(J in 1..N) printf("%6.2f ", X[I,J]) end, nl end, nl. laplace(R,C,Z,M, X) => X.vars :: Z..M, foreach(I in 2..R, J in 2..C) 4.0*X[I,J] #= X[I-1,J] + X[I,J-1] + X[I+1,J] + X[I,J+1] end, solve(X.vars). % % Generate a NxN Laplace matrix % generate(N, Z, M, X) => X = new_array(N+1,N+1), X :: Z..M, foreach(I in 2..N+1) foreach(C in [1,N+1] ) X[I,C] #= M end end, foreach(J in 1..N+1) X[1,J] #= Z, X[N+1,J] #= M end. data(1,R,C,Z,M,X) => R = 10, C = 10, Z = 0.0, M = 100.0, X = { {Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z}, {M,_,_,_,_,_,_,_,_,_,M}, {M,_,_,_,_,_,_,_,_,_,M}, {M,_,_,_,_,_,_,_,_,_,M}, {M,_,_,_,_,_,_,_,_,_,M}, {M,_,_,_,_,_,_,_,_,_,M}, {M,_,_,_,_,_,_,_,_,_,M}, {M,_,_,_,_,_,_,_,_,_,M}, {M,_,_,_,_,_,_,_,_,_,M}, {M,_,_,_,_,_,_,_,_,_,M}, {M,M,M,M,M,M,M,M,M,M,M} }. %% This problem instance is from MiniZinc's tutorial/laplace.mzn data(2,R,C,Z,M,X) => Z = 0.0, M = 100.0, R = 4, C = 4, X = { {Z,M,M,M,Z}, {Z,_,_,_,Z}, {Z,_,_,_,Z}, {Z,_,_,_,Z}, {Z,Z,Z,Z,Z} }.