1
$\begingroup$

I am trying to integrate the solver for the set of nonlinear equations link written in Fortran language into mathematica . To do so i developed a function called netlib based on the explanation given in link

Needs["NETLink`"];
GFortran1[name_, inp_] := (
  Export[name <> ".f90", inp, "Text"];
  string1 = "!gfortran -c " <> name <> ".f90 ";
  string2 = 
   "!gfortran -s -shared -mrtd -o " <> name <> ".dll " <> name <> ".o";
  Column@ReadList[string1, String];
  Column@ReadList[string2, String])
netlink[name_, code_, fun_] := Block[{string1, string2},

  ReinstallNET["Force32Bit" -> True];
  ReinstallNET["Force32Bit" -> False];
  SetDirectory[NotebookDirectory[]];
  path = FileNameJoin[{Directory[], name <> ".dll"}];
  If[fun == 1,
   (Evaluate@ToExpression@name) = 
    DefineDLLFunction[name <> "_", path, 
     "double", {"double[]", "double*"}], (Evaluate@
      ToExpression@name) = 
    DefineDLLFunction[name <> "_", path, 
     "void", {"Double[]", "Double*", "Double[]"}]];
  DeleteFile[{name <> ".f90", name <> ".dll", name <> ".o", 
    name <> ".mod"}];
  GFortran1[name, code];
  ]

and it works like this

enter image description here

enter image description here

So now with a slight modification of the FsolveHybridTest subroutine (x0 is the initial condition and xout2 is the result) in the hybrid.f90 file (to save the results in a file called results.txt) and debug_hybrid.f90

enter image description here enter image description here

it is possible to call the netlink function to get the correct result (replace ... with a correct directory)

name = {"myutility", "hybrid", "debug_hybrid"};
Table[
  pr = Import[
    ".../Optimization-master/" <> name[[ix]] <> 
     ".f90", {"Text"}];
  Quiet@netlink[name[[ix]], pr, 2], {ix, Length@name}];
Run["gfortran myutility.o hybrid.f90  debug_hybrid.o -o resx.exe"];
Run["resx.exe"]
result = ReadList["...\\results.txt", Real]

enter image description here

So now my question is that how can we modify the debug_hybrid.f90 (main program that calls fsolvehybrid) file to act as a subroutine that gets the initial values and returns the result?

$\endgroup$
  • $\begingroup$ how can we convert mathematica file to fortran file? $\endgroup$ – ABCDEMMM Sep 4 '19 at 22:01
  • 1
    $\begingroup$ I understand this is not your question but have you tried FindRoot for you root finding? $\endgroup$ – user21 Sep 5 '19 at 4:31
  • $\begingroup$ @ABCDEMMM the other way around $\endgroup$ – user49047 Sep 5 '19 at 10:32
  • $\begingroup$ @user21 yes but i was wondering if this code can perform faster since it has some fancy jacobian update. $\endgroup$ – user49047 Sep 5 '19 at 10:37
  • $\begingroup$ Have you seen this? $\endgroup$ – user21 Sep 5 '19 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.