How do I set up Code::Blocks build options to run plplot? - codeblocks

I cannot find any documentation on how to setup Code::Blocks to run plplot. I have installed plplot version 5.14.0 in /home/myname/plplot. I followed instructions on to compile at https://sourceforge.net/p/plplot/wiki/Linux/. That went smoothly, and I have a folder for build_directory and install_directory in plplot folder. I am running Ubuntu 18.04.
When I go to compile an example program in Code::Blocks, I do not know which files or folders need to be added into Project->Build Options. The error message reads: Fatal Error: Can't open module file ‘plplot.mod’ for reading at (1): No such file or directory
Here is the example I am trying to compile:
program ft_x00f
! This is a modified version of x00f.f90
! which was written by Alan Irwin
use plplot
implicit none
integer, parameter :: NSIZE = 100
real(kind=plflt), dimension(0:NSIZE) :: x, y
real(kind=plflt) :: xmin = 0.0_plflt, &
xmax = 1.0_plflt, &
ymin = 0.0_plflt, &
ymax = 100.0_plflt
integer :: i
! Prepare data to be plotted.
! x = .00, .01, .02, ..., .99, 1.00
x = [(i, i=0,NSIZE)] / real(NSIZE)
y = ymax * x**2
! Parse and process command line arguments
call plparseopts( PL_PARSE_FULL )
! Initialize plplot
call plinit( )
! Create a labelled box to hold the plot.
call plenv( xmin, xmax, ymin, ymax, just=0, axis=0 )
call pllab( "x", "y=100 x#u2#d", &
"Simple PLplot demo of a 2D line plot" )
! Plot the data that was prepared above.
call plline( x, y )
! Close PLplot library
call plend( )
end program ft_x00f

This one was painful, but I have found out the answer. First, I could not compile the above code even without Code::Blocks. I suggest copying a file directly from the plplot /examples directory and opening that in Code::Blocks. The best way to find out what commands are needed is to look at what plplot does when you issue the make command to compile the examples when installing plplot.
I then needed to add following to Code::Blocks -
1) Project -> Build Options -> Linker Settings -> Other Linker Options -lplplotfortran -lplfortrandemolib (Note, I knew these from the make command discussed above)
2) Project -> Build Options -> Search Directories -> Compiler /install_directory/lib/fortran/modules/plplot
3) Project -> Build Options -> Search Directories -> Linker /install_directory/lib
This will at least compile and run an example. I am sure more fun awaits for creating my own programs. Good luck to all.

Related

"ApplicationError: Solver (ipopt) did not exit normally" after compiling and installing ipopt by myself

I've been trying to install/compile ipopt in my Windows computer and so far I am not having any progress. I am mostly following the instructions listed here: https://coin-or.github.io/Ipopt/INSTALL. I downloaded MSYS2 MinGW specifically for this purpose.
As dependencies I have downloaded the following
OpenBLAS;
blis;
ThirdParty-ASL;
ThirdParty-HSL (with the HSL library solvers which I need);
ThirdParty-Mumps;
In the end, I get ipopt.exe in the C:\msys64\home\me\Ipopt\src\Apps\AmplSolver folder. But there is also another ipopt.exe in the C:\msys64\home\me\Ipopt\src\Apps\AmplSolver\.libs folder, so I tried to make use of both.
I am using Anaconda Navigator and Pyomo to see whether my ipopt.exe works, so I put the resulting ipopt.exe in the Anaconda\envs\myenv\Library\bin folder. However, the ipopt executable that I got gives me errors. One of them gives me an error related to libipoptamplinterface-3.dll. When I run the below Python script using Spyder:
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
model = pyo.ConcreteModel()
model.nVars = pyo.Param(initialize=4)
model.N = pyo.RangeSet(model.nVars)
model.x = pyo.Var(model.N, within=pyo.Binary)
model.obj = pyo.Objective(expr=pyo.summation(model.x))
model.cuts = pyo.ConstraintList()
opt = SolverFactory('ipopt')
opt.solve(model)
# Iterate, adding a cut to exclude the previously found solution
for i in range(5):
expr = 0
for j in model.x:
if pyo.value(model.x[j]) < 0.5:
expr += model.x[j]
else:
expr += (1 - model.x[j])
model.cuts.add( expr >= 1 )
results = opt.solve(model)
print ("\n===== iteration",i)
model.display()
I get errors such as this:
ERROR: Solver (ipopt) returned non-zero return code (127)
ApplicationError: Solver (ipopt) did not exit normally
I don't know how to solve this.
Your model is a MILP type of optimization however the ipopt solver is not for this purpose use glpk solver instead of that

How to make a change to the source code in Haskell using GHCi?

I am new to Haskell and am using GHCi to edit and run Haskell files. For some reason, I am not able to edit the source code of the file. The behavior I am getting is extremely odd.
Below is a screenshot of what is happening. I am loading the file lec3.hs and am attempting to edit this file to add the following function: myfun = \w -> not w. For some reason, this function successfully runs when I call it immediately after: myfun False. I do not need to reload the file.
It is clear that the function is not being added to the source code. When I reload the file, I am getting an error stating that myfun does not exist.
Could someone help me understand why GHCi is behaving this way, and how to fix such behaviour? I have already spent an hour trying to figure this out. I would sincerely appreciate any help.
Typing things into GHCi isn't supposed to add them to the source. But if you have loaded a file into GHCi you can edit it using the :e command, and when you close the editor it will be automatically reloaded.
You can use :e filename.hs if you are dealing with more than one file and you need to specify.
Generally it's easier to work in a separate editor and just reload into GHCi with :r, but :e is occasionally useful.
To answer
Is it possible to edit a .hs file from GHCi?
It technically speaking is possible, since Haskell has like any other language file-IO operations. Concretely, appendFile allows you to add content to a file.
$ cat >> Foo.hs # Creating a simple Haskell file
foo :: Int
foo = 3
$ ghci Foo.hs
GHCi, version 8.2.2: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/sagemuej/.ghc/ghci.conf
Loaded GHCi configuration from /home/sagemuej/.ghci
[1 of 1] Compiling Main ( Foo.hs, interpreted )
Ok, one module loaded.
*Main> foo
3
*Main> appendFile "Foo.hs" $ "myfun = \\w -> not w"
*Main> :r
[1 of 1] Compiling Main ( Foo.hs, interpreted )
Ok, one module loaded.
*Main> myfun False
True
But this really is not a good way to edit files. It makes much more sense to have two windows open, one with a text editor and one with the REPL, i.e. with GHCi. It can be either two completely separate OS windows, or two sub-windows of your IDE or whatever, but in either case GHCi will be used only for evaluation and one-line prototyping, not for actually adding/editing code. (The one-line prototypes can be copy&pasted into the editor, if it helps.)

bash: cannot execute binary file exec format error fortran

When I run the code given below, it always gives me the following error:
bash: cannot execute binary file exec format error fortran
Also, the file "file" is not being created at the location mentioned in the code. I've a 64-bit processor and 64-bit version of Ubuntu 16.04, so that does not appear to be the issue. Can someone please point out where I'm wrong?
program sandpile_model
implicit none
integer, parameter :: len = 20
integer, dimension(len,len) :: square
!real, dimension(len,len) :: blah
!open(unit=1,file="\\home\\sandpile\\fortran\\file")
!dummy variables
integer :: i,j,d
do i=1,len
do j=1,len
square(i,j)=2
end do
end do
do d=1,10000
square((len/2)-1,(len/2)-1)=square((len/2)-1,(len/2)-1)+1
if(square((len/2)-1,(len/2)-1)>3) then
call toppling((len/2)-1,(len/2)-1)
end if
end do
!open(unit=1,file="\\home\\sandpile\\fortran\\file")
do i=1,len
do j=1,len
write(1,*), i,'\t',j,'\t',square(i,j)
end do
print*, '\n'
end do
end program sandpile_model
!This subroutine specifies the evolution rules of the model
recursive subroutine toppling(x,y)
!implicit none
integer, parameter :: len = 20
integer, dimension(len,len) :: square
!real, dimension(len,len) :: blah
integer, intent(in) :: x,y
square(x,y)=square(x,y)-4
square(x+1,y)=square(x+1,y)+1
if(square(x+1,y)>3) then
call toppling(x+1,y)
end if
square(x-1,y)=square(x-1,y)+1
if(square(x-1,y)>3) then
call toppling(x-1,y)
end if
square(x,y+1)=square(x,y+1)+1
if(square(x,y+1)>3) then
call toppling(x,y+1)
end if
square(x,y-1)=square(x,y-1)+1
if(square(x,y-1)>3) then
call toppling(x,y-1)
end if
end subroutine toppling
The problem appears here that an attempt is made to run an object file and not an executable.
Very small / limited instructions:
To create an object file: gfortran -c <fortran file>
To create an executable: gfortran <fortran file>
When using multiple source files:
Create objects from the individual files and link the together by means of gfortran <object files>
Create executable directly from source files gfortran <fortran files>
Note:
order of the files might be important
It might be necessary to link libraries as well into the executable (`-l option)
Name of the output file can be specified by means of the -ooption
Further initial reading:
compiler documentation
man gfortran
man make for automation in more complex situations.

Erlang: Read from an input stream in a efficient way

I'm writing a program that reads from an input stream, i.e.
erl -run p main -noshell -s erlang halt < input
The problem is that it takes a lot of time to read it (the input stream is huge) using this read function:
read_input(L) ->
case io:get_line("") of
eof ->
lists:reverse(L);
E0 ->
read_input([E0|L])
end.
I have been looking for more efficient alternatives, but I have found nothing. I have tried to read the file using
{ok, Binary} = file:read_file("input")
This is by far much more efficient. The problem is that I have to run this program in a platform where the name is unknown so I'd need some alternative to do so. additionally, I can't select the flags used when running, e.g. flag -noinput cannot be added to the command line.
Whatever help you can give will be welcomed.
You can use open_port/2 to open stdin and read binaries from it. For example:
-module(p).
-export([start/0]).
start() ->
process_flag(trap_exit, true),
P = open_port({fd,0,1}, [in, binary]),
Bin = read(P,<<>>),
io:format("received ~p\n", [Bin]),
halt(0).
read(P, Bin) ->
receive
{P, {data, Data}} ->
read(P, <<Bin/binary, Data/binary>>);
{'EXIT',P,_} ->
Bin
end.
The code has to trap exits so it knows to exit its reading loop when the port closes. This example reads everything into a single binary returned from the read/2 function and then prints it out and exits, but obviously you can perform further operations on the binary in your actual application.
You can run this like this:
erl -noinput -s p < input
Although Steve's solution is fastest known to me solution there can be used file module solution with quite good performance:
-module(p).
-export([start/0]).
-define(BLK_SIZE, 16384).
start() ->
do(),
halt().
do() ->
Bin = read(),
io:format("~p~n", [byte_size(Bin)]).
read() ->
ok = io:setopts(standard_io, [binary]),
read(<<>>).
read(Acc) ->
case file:read(standard_io, ?BLK_SIZE) of
{ok, Data} ->
read(<<Acc/bytes, Data/bytes>>);
eof ->
Acc
end.
It works with invocation like:
erl -noshell -s p < input
Note both approaches could be used for line-oriented input using {line, Max_Line_Size} option for port or file:read_line/1 for file module solution. Since version 17 (if I recall correctly) there is fixed performance bug in file:read_line/1 I found so it is good now. Anyway, you should not expect performance and comfort of Perl.

Haskell Noob In Need of Assistance

This is a bit long, so bear with me!
I'm having a bit of trouble working with a Haskell program, that I have to use as part of a uni project. For reference, it's Casper.
So, you're supposed to execute a script, which is actually a Bash script to invoke Hugs interpreter like this:
exec $HUGSBIN/hugs $HUGSARGS +p"Casper> " $FILES
Where $FILES points to a Main.lhs file.
After this, I need to invoke a function "compile" with a path to a file, in the interpreter.
I need to perform the above in a scripted manner. I need this automated because I'm writing a program that will call on Casper in the background.
So I compiled the .lhs file. Now I want to execute the "compile" function but I have no idea how this is done. I try:
./Main compile <a path>
from the command line but it returns me an error about a file "test" not found. Upon investigation, I see these lines in the Main.lhs file:
>main :: String -> IO()
>main = compile "test"
>compile :: String -> IO()
>compile s = catch (compile0 False s) handler
[...snipped]
The 2nd line solves this question. Now my question is, how do I invoke the "compile" function and pass a path to it after I have compiled main.lhs? From the interpreter, I just type "compile " and it works, but I can't get the same to work after compiling the main.lhs and executing from the command line? Any ideas why? Is there any way I can script Hugs if all else fails?
Thank you for any assistance!
You may access the command-line arguments passed to a Haskell program via getArgs. For example, it sounds like you want a main function that does something like this:
>main = do
> args <- getArgs
> case args of
> [] -> putStrLn "What file did you want me to compile?"
> [filename] -> compile filename
> _ -> putStrLn "I only compile one file at a time."
Modify to taste.
Replace main with
main = getArgs >>= \(arg1:_) -> compile arg1
This will pass the first command line argument (arg1) to compile instead of "test", and ignore the rest (_). You may need to add
import System
or
import System.Environment
I can't remember what is needed in hugs for this.

Resources