PhreeqcUsers Discussion Forum
Click here to donate to keep PhreeqcUsers open

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 

  • Forum Home
  • Login
  • Register

  • PhreeqcUsers Discussion Forum »
  • Conceptual Models »
  • Design of conceptual models »
  • Fortran interface PhreeqC
« previous next »
  • Print
Pages: [1]   Go Down

Author Topic: Fortran interface PhreeqC  (Read 343 times)

IliasBouchkira

  • Top Contributor
  • Posts: 39
Fortran interface PhreeqC
« on: May 23, 2023, 12:29:16 PM »
Dear all

I am interfacing Matlab and PhreeqC using the following code:
 
Code: [Select]
C0=[0.1:0.1:4.02];
for i=1:length(C0)
%Creation of the ActiveX server:
iphreeqc = actxserver('IPhreeqcCOM.Object');
%Specify the database file according to the location on your file system.
iphreeqc.LoadDatabase([pwd,'\pitzer.dat']);
%Please change this line if you installed this somewhere else
%Call PhreeqC functionality using separate input file
%Note that in this way dynamic assignment of values to variables is not possible.
%I assume that the input file for PhreeqC is located in the current working directory.
%iphreeqc.RunFile([pwd,'\inputPhreeqC.txt']);
OUTphreeqFILE = iphreeqc.GetSelectedOutputArray;
%Call PhreeqC functionality providing input as MATLAB multiline String

%Define input string as cell array of strings and combine them to a multiline string using the sprintf command.
mS =C0(i);
tem=20;

IPCstringCell= {'SOLUTION 1',  ...
    ['temp ' num2str(tem)],...
    '-pH    1 charge',...
    '-units mol/kgw ',  ...
    ['S(6) ' num2str(mS)],...
    'SAVE SOLUTION 1', ...
    'SELECTED_OUTPUT', ...
    '-molalities H+ HSO4- SO4-2', ...
    'soln false',  ...
    'pH false',  ...
    'sim false',  ...
    'state false',  ...
    'time false',  ...
    'step false',  ...
    'pe false',  ...
    'distance false' ...
     };

IPCstring = sprintf('%s\n', IPCstringCell{:})
%Use the PhreeqC object method RunString with the above defined multiline string.
iphreeqc.RunString( IPCstring );
OUTphreeqSTRING = iphreeqc.GetSelectedOutputArray
out1=OUTphreeqSTRING(2,:);
m_H_25(i)=out1{1};
m_HSO4_25(i)=out1{2};
m_SO4_25(i)=out1{3};

end

For some reasons I need to do the same but from Fortran

Is there any possibilities? is here any examples ?

Many thanks in advance,

Ilias.
Logged

dlparkhurst

  • Top Contributor
  • Posts: 3088
Re: Fortran interface PhreeqC
« Reply #1 on: May 23, 2023, 02:43:21 PM »
Matlab is using the IPhreeqcCOM version of phreeqc. There is a non-COM IPhreeqc that can be downloaded and compiled from https://www.usgs.gov/software/phreeqc-version-3. It can be used in C++, Fortran, or C programs to perform PHREEQC calculations. It should work similarly to the COM version. You can create a string with the PHREEQC input that you want and then run it with a RunString command.

There is a README file to help with compilation of the library, which you will need to link with your Fortran code. An example is provided.
Logged

IliasBouchkira

  • Top Contributor
  • Posts: 39
Re: Fortran interface PhreeqC
« Reply #2 on: May 31, 2023, 02:53:42 PM »
Dear Dr Parkhurst

Many thanks for the answer on how to use fortran to create a string with the PHREEQC input that I want and then run it with a RunString command.

 I have tried all my best to make the code but I still cannot manage this. Is there anyway to provide a very simple fortran code (for exemple that calculates water activity of any property) that I can use as a start and build more complex one? I will be very grateful.

many thanks in advance,

Kind regards;

Ilias.

Logged

dlparkhurst

  • Top Contributor
  • Posts: 3088
Re: Fortran interface PhreeqC
« Reply #3 on: May 31, 2023, 03:44:19 PM »
I assume you downloaded this link:

Windows (any processor): iphreeqc-3.7.3-15968.zip [13M] -Source with CMake, database files, examples, and documentation https://water.usgs.gov/water-resources/software/PHREEQC/iphreeqc-3.7.3-15968.zip

and that you followed the steps to build the IPhreeqc library.

There is a Fortran example in the directory examples/Fortran/advect with a README file on how to compile the library and the advect example.

When using CMake to build a Visual Studio solution (.sln file), enable the option IPHREEQC_FORTRAN_TESTING and there will be an option test_F90 that tests all of the methods of IPhreeqc. You can look at that code for examples.
Logged

IliasBouchkira

  • Top Contributor
  • Posts: 39
Re: Fortran interface PhreeqC
« Reply #4 on: June 02, 2023, 04:14:54 PM »
Dear Dr Parkhurst

Many thanks again for your reply. I have done everything correctly and managed all steps except the building of INSTALL. when i try to build it, i always get an error related to "setlocal" command

Is it related to the location where I am intalling the files ?

Many thanks in advance again

Ilias.
Logged

charlton

  • Top Contributor
  • Posts: 35
Re: Fortran interface PhreeqC
« Reply #5 on: June 02, 2023, 05:10:03 PM »
Hi Ilias

Since you're getting a setlocal error, I'm assuming you're building in Visual Studio on Windows. On Windows, CMake uses 'C:/Program Files (x86)/IPhreeqc' as the default install location (CMAKE_INSTALL_PREFIX).  In order to install here you need to have Administrator privileges (which would require you to run Visual Studio as Administrator).  A better option would be to change the CMAKE_INSTALL_PREFIX to a location that you have write access to.  Somewhere under your home directory (usually C:\Users\<username>) is probably best (ie C:\Users\<username>\iphreeqc).

Scott
Logged

IliasBouchkira

  • Top Contributor
  • Posts: 39
Re: Fortran interface PhreeqC
« Reply #6 on: June 05, 2023, 09:01:14 AM »
Dear Dr Charlton

Many thanks the problem is solved. I wonder please if there are any Fortran codes (same as advect.f90) that deal with the usual thermodynamic calculations example below (water activity; molalities..). I am trying to learn how to call PhreeqC from Fortran, and this would help so so much.

Code: [Select]
SOLUTION 1
    temp      25
    pH        7 charge
    pe        4
    redox     pe
    units     mmol/kgw
    density   1
    Cl        1 mol/kgw
    Na        1 mol/kgw
    Ca        1 mol/kgw 
    -water    1 # kg


SELECTED_OUTPUT 1
    -file                 selected_output_1.sel
    -ionic_strength       true
    -water                true
    -molalities           Na+  Ca+2



Many thanks in advance for your time.

Ilias.
Logged

dlparkhurst

  • Top Contributor
  • Posts: 3088
Re: Fortran interface PhreeqC
« Reply #7 on: June 05, 2023, 03:56:50 PM »
One way to make that calculation is to accumulate lines and run what you have accumulated:
Code: [Select]
  Ierr = AccumulateLine(Id, "SOLUTION 1")   
  Ierr = AccumulateLine(Id, " temp 25")
...
  Ierr = AccumulateLine(Id, "END")
  Ierr = RunAccumulated(Id)

Then you would process the selected output with the selected-output methods in a similar way to the subroutine ExtractWrite in advect.f90.
Logged

IliasBouchkira

  • Top Contributor
  • Posts: 39
Re: Fortran interface PhreeqC
« Reply #8 on: June 08, 2023, 09:52:27 AM »
Dear Dr. Many thanks for the answer. Everything is going great. I was always calling PhreeqC from Matlab and still a not very familiar with Fortran.  I Have followed every step correctly, my current issue is that I can't find/see the simulation results.  In Matlab, The results were displayed directly after running the code, but I don't know whether it is the same for fortran or not?

Many thanks in advance,Ilias.
Logged

dlparkhurst

  • Top Contributor
  • Posts: 3088
Re: Fortran interface PhreeqC
« Reply #9 on: June 08, 2023, 03:07:47 PM »
You can write the output results to a file or obtain them as a series of strings.

SetOutputFileName defines the name of the file where the results will be written.
SetOutputFileOn allows you to toggle writing results for calculations when you use the methods RunFile or RunString.
Logged

IliasBouchkira

  • Top Contributor
  • Posts: 39
Re: Fortran interface PhreeqC
« Reply #10 on: June 10, 2023, 06:44:38 PM »
Dear Dr Parkhurst

Many thanks for your answer, I have tried to save the results in a .txt file as I have been doing in Matlab. the fortran code is below:

Code: [Select]

module Subs
  integer   :: Id
  contains
 
  subroutine EHandler()
    use IPhreeqc
    IMPLICIT NONE
    call OutputErrorString(Id)
    stop
  end subroutine EHandler   
end module Subs

   
program File_PhreeqC
  use Subs
  use IPhreeqc
  IMPLICIT NONE
  integer :: Ierr

 
!Create module, load database, define initial conditions and selected output

  Id = CreateIPhreeqc()
  if (LoadDatabase(Id, "phreeqc.dat") .ne. 0) call EHandler()
  If (RunFile(Id, "ic") .ne. 0) call EHandler()



! RunString ----$
 
  Ierr = AccumulateLine(Id, "  SOLUTION 1")   
  Ierr = AccumulateLine(Id, "   temp     25" )
  Ierr = AccumulateLine(Id, "  pH        7 charge " )
  Ierr = AccumulateLine(Id, "  units     mmol/kgw" )
  Ierr = AccumulateLine(Id, "   Mg   0.1")
  Ierr = AccumulateLine(Id, "  -water    1 # kg")
  Ierr = AccumulateLine(Id, "  Ca        0.1 mol/kgw" )
  Ierr = AccumulateLine(Id, "  Mg        0.1 mol/kgw")
  Ierr = AccumulateLine(Id, "  Cl        0.1 mol/kgw")
  Ierr = AccumulateLine(Id, "SELECTED_OUTPUT 1")
  Ierr = AccumulateLine(Id, " -file                 V:\selected_output_1.txt")
  Ierr = AccumulateLine(Id, "-ionic_strength       true")
  Ierr = AccumulateLine(Id, "-water                true")
  Ierr = AccumulateLine(Id, " -molalities           Ca+2")
  Ierr = RunAccumulated(Id)
 
end program File_PhreeqC



However, I cannot find the txt file. Is there any way to do this? is it possible to save the results as I did ?

Many thanks in advance,

Ilias.
Logged

dlparkhurst

  • Top Contributor
  • Posts: 3088
Re: Fortran interface PhreeqC
« Reply #11 on: June 10, 2023, 07:27:30 PM »
Code: [Select]
  Ierr = SetSelectedOutputFileOn(Id, 1)
  Ierr = SetSelectedOutputFileName(Id, "myoutput.sel")
  Ierr = SetOutputFileOn(Id, 1)
  Ierr = SetOutputFileName(Id, "myoutput.out")
  Ierr = RunAccumulated(Id)
Logged

  • Print
Pages: [1]   Go Up
« previous next »
  • PhreeqcUsers Discussion Forum »
  • Conceptual Models »
  • Design of conceptual models »
  • Fortran interface PhreeqC
 

  • SMF 2.0.17 | SMF © 2019, Simple Machines | Terms and Policies
  • XHTML
  • RSS
  • WAP2