public static int StartAsBatchInConsole(string batpath, bool pause, params string[] commands) { bool delbat = false; if ((batpath == null) || batpath.Trim().Length == 0) { delbat = true; batpath = HFile.GetTempPath("bat"); } HFile.WriteAllLines(batpath, commands); if (pause) { HFile.AppendAllLines(batpath, new string[] { "pause" }); } System.Diagnostics.Process pdb2gmx = System.Diagnostics.Process.Start(batpath); pdb2gmx.WaitForExit(); int exitcode = pdb2gmx.ExitCode; Thread.Sleep(100); if (delbat) { HFile.Delete(batpath); } return(exitcode); }
public static void _Serialize(string filename, int?ver, object[] objs) { string lockname = "Serializer: " + filename.Replace("\\", "@"); using (new NamedLock(lockname)) { Stream stream = HFile.Open(filename, FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); { if (ver != null) { bFormatter.Serialize(stream, new Ver(ver.Value)); } } { System.Int32 count = objs.Length; bFormatter.Serialize(stream, count); for (int i = 0; i < count; i++) { bFormatter.Serialize(stream, objs[i]); } } stream.Flush(); stream.Close(); } }
public static TVector <double> GetVectorLarge(string name, bool bUseFile = false) { TVector <double> vector; try { Execute("htlib2_matlab_GetVector = " + name + ";"); int size = GetValueInt("length(htlib2_matlab_GetVector)"); if (size < TVector <double> .MaxBlockCapacity) { System.Array real = new double[size]; System.Array imag = new double[size]; matlab.GetFullMatrix("htlib2_matlab_GetVector", "base", ref real, ref imag); Execute("clear htlib2_matlab_GetVector;"); vector = new TVector <double>(size); for (int i = 0; i < size; i++) { vector[i] = (double)real.GetValue(i); } } else { HDebug.Assert(bUseFile, _path_temporary != null); string tmppath = HFile.GetTempPath(_path_temporary, ".dat"); Execute("htlib2_matlab_GetVectorLarge.vec = " + name + ";"); { Execute("htlib2_matlab_GetVectorLarge.fid=fopen('" + tmppath + "','w');"); Execute("htlib2_matlab_GetVectorLarge.vec=fwrite(htlib2_matlab_GetVectorLarge.fid,htlib2_matlab_GetVectorLarge.vec','double');"); Execute("fclose(htlib2_matlab_GetVectorLarge.fid);"); Execute("clear htlib2_matlab_GetVectorLarge;"); } { bool clear_var = false; if (clear_var) { Execute("clear " + name + ";"); } } vector = new TVector <double>(size); { System.IO.BinaryReader reader = new System.IO.BinaryReader(new System.IO.FileStream(tmppath, System.IO.FileMode.Open)); for (long i = 0; i < size; i++) { vector[i] = reader.ReadDouble(); } reader.Close(); } HFile.Delete(tmppath); return(vector); } } catch (System.Runtime.InteropServices.COMException) { HDebug.Assert(false); vector = null; } return(vector); }
public static void SerializeText2D <T>(string filename, Func <T, string> tostring, T[,] values) { string text = values.HToStringSeparated(tostring , "{ ", "\n, ", "\n}" , "{", ", ", "}" ); HFile.WriteAllText(filename, text); }
public static void CopyResourceTo <T>(string resname, string path) { HDebug.Assert(GetResourceNames <T>().Contains(resname)); var assm = typeof(T).Assembly; var stream = assm.GetManifestResourceStream(FindResourceName <T>(resname)); HFile.WriteStream(path, stream); stream.Close(); }
//public static FileStream Open(string path, FileMode mode) //{ // return System.IO.File.Open(path, mode); //} //public static FileStream Open(string path, FileMode mode, FileAccess access) //{ // return System.IO.File.Open(path, mode, access); //} //public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share) //{ // return System.IO.File.Open(path, mode, access, share); //} //public static StreamReader OpenText(string path) //{ // return System.IO.File.OpenText(path); //} //public static StreamWriter CreateText(string path) //{ // return System.IO.File.CreateText(path); //} //public static StreamWriter WriteText(string path) //{ // return new System.IO.StreamWriter(path); //} public static IEnumerable <string> HEnumAllLines(string path) { System.IO.StreamReader reader = HFile.OpenText(path); while (reader.EndOfStream == false) { string line = reader.ReadLine(); yield return(line); } reader.Close(); }
public static void Run(string scriptpath, bool waitForExit) { string currpath = HEnvironment.CurrentDirectory; HEnvironment.CurrentDirectory = HFile.GetFileInfo(scriptpath).Directory.FullName; { System.Diagnostics.Process mathm; string argument = "-script \"" + HFile.GetFileInfo(scriptpath).Name + "\""; mathm = System.Diagnostics.Process.Start(@"C:\Program Files\Wolfram Research\Mathematica\8.0\math.exe", argument); if (waitForExit) { mathm.WaitForExit(); } } HEnvironment.CurrentDirectory = currpath; }
public static void PutMatrix(string name, ref IMatrix <double> real, bool bUseFile, bool call_GC) { if ((bUseFile == false) || (_path_temporary == null)) { HDebug.Assert(real.RowSize * real.ColSize < 2000 * 2000); System.Array arr_real = real.ToArray(); System.Array arr_imag = new double[real.ColSize, real.RowSize]; matlab.PutFullMatrix("htlib2_matlab_PutMatrix", "base", arr_real, arr_imag); Execute(name + " = htlib2_matlab_PutMatrix;"); Execute("clear htlib2_matlab_PutMatrix;"); } else { HDebug.Assert(bUseFile, _path_temporary != null); string tmppath = HFile.GetTempPath(_path_temporary, ".dat"); int colsize = real.ColSize; int rowsize = real.RowSize; { System.IO.BinaryWriter writer = new System.IO.BinaryWriter(new System.IO.FileStream(tmppath, System.IO.FileMode.CreateNew)); for (int c = 0; c < colsize; c++) { for (int r = 0; r < rowsize; r++) { writer.Write(real[c, r]); } } writer.Flush(); writer.Close(); } if (call_GC) { real = null; System.GC.Collect(); } { Execute("htlib2_matlab_PutMatrix.fid=fopen('" + tmppath + "','r');"); Execute("htlib2_matlab_PutMatrix.mat=fread(htlib2_matlab_PutMatrix.fid,[" + rowsize + "," + colsize + "],'*double')';"); Execute("fclose(htlib2_matlab_PutMatrix.fid);"); Execute(name + " = htlib2_matlab_PutMatrix.mat;"); Execute("clear htlib2_matlab_PutMatrix;"); } HFile.Delete(tmppath); } }
public static void DeserializeText <T>(string filename, Func <string, T> parse, out T[] values) { string text = HFile.ReadAllText(filename).Trim(); HDebug.Assert(text.StartsWith("{")); text = text.Substring(1); HDebug.Assert(text.EndsWith("}")); text = text.Substring(0, text.Length - 1); text = text.Trim(); if (text.Length == 0) { values = new T[0]; return; } string[] token = text.Split(','); values = new T[token.Length]; for (int i = 0; i < token.Length; i++) { T value = parse(token[i]); values[i] = value; } }
public static bool _Deserialize(string filename, int?ver, out object[] objs) { string lockname = "Serializer: " + filename.Replace("\\", "@"); using (new NamedLock(lockname)) { Stream stream = HFile.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryFormatter bFormatter = new BinaryFormatter(); { if (ver != null) { try { Ver sver = (Ver)bFormatter.Deserialize(stream); if (sver.ver != ver.Value) { stream.Close(); objs = null; return(false); } } catch (Exception) { stream.Close(); objs = null; return(false); } } } { System.Int32 count = (System.Int32)bFormatter.Deserialize(stream); objs = new object[count]; for (int i = 0; i < count; i++) { objs[i] = bFormatter.Deserialize(stream); } } stream.Close(); } return(true); }
public static IMATRIX GetMatrix <IMATRIX>(string name, Func <int, int, IMATRIX> Zeros, bool bUseFile, bool clear_var) where IMATRIX : IMatrix <double> { if ((bUseFile == false) || (_path_temporary == null)) { System.Array real; System.Array imag; Execute("htlib2_matlab_GetGetMatrix = " + name + ";"); int colsize = GetValueInt("size(htlib2_matlab_GetGetMatrix, 1)"); int rowsize = GetValueInt("size(htlib2_matlab_GetGetMatrix, 2)"); HDebug.Assert(colsize * rowsize < 2000 * 2000); real = new double[colsize, rowsize]; imag = new double[colsize, rowsize]; matlab.GetFullMatrix("htlib2_matlab_GetGetMatrix", "base", ref real, ref imag); Execute("clear htlib2_matlab_GetGetMatrix;"); IMATRIX matrix = Zeros(colsize, rowsize); for (int c = 0; c < colsize; c++) { for (int r = 0; r < rowsize; r++) { matrix[c, r] = (double)real.GetValue(c, r); } } real = null; imag = null; return(matrix); } else { HDebug.Assert(bUseFile, _path_temporary != null); string tmppath = HFile.GetTempPath(_path_temporary, ".dat"); Execute("htlib2_matlab_GetGetMatrix.test = 0;"); Execute("htlib2_matlab_GetGetMatrix.mat = " + name + ";"); int colsize = GetValueInt("size(htlib2_matlab_GetGetMatrix.mat, 1)"); int rowsize = GetValueInt("size(htlib2_matlab_GetGetMatrix.mat, 2)"); { Execute("htlib2_matlab_GetGetMatrix.fid=fopen('" + tmppath + "','w');"); Execute("htlib2_matlab_GetGetMatrix.mat=fwrite(htlib2_matlab_GetGetMatrix.fid,htlib2_matlab_GetGetMatrix.mat','double');"); Execute("fclose(htlib2_matlab_GetGetMatrix.fid);"); Execute("clear htlib2_matlab_GetGetMatrix;"); } if (clear_var) { Execute("clear " + name + ";"); } IMATRIX matrix = Zeros(colsize, rowsize); { System.IO.BinaryReader reader = new System.IO.BinaryReader(new System.IO.FileStream(tmppath, System.IO.FileMode.Open)); for (int c = 0; c < colsize; c++) { for (int r = 0; r < rowsize; r++) { matrix[c, r] = reader.ReadDouble(); } } reader.Close(); } HFile.Delete(tmppath); return(matrix); } }
/// Matlab.PutSparseMatrix("H", H.GetMatrixSparse(), 3, 3); //public static void PutSparseMatrix<MATRIX>(string name, MatrixSparse<MATRIX> real, int elemColSize, int elemRowSize) //public static void PutSparseMatrix<MATRIX>(string name, IMatrixSparse<MATRIX> real, int elemColSize, int elemRowSize) // where MATRIX : Matrix public static void PutSparseMatrix(string name, IMatrixSparse <MatrixByArr> real, int elemColSize, int elemRowSize, string opt = null) { /// http://www.mathworks.com/help/matlab/ref/sparse.html /// S = sparse(i,j,s,m,n) /// * create m-by-n sparse matrix /// * where S(i(k),j(k)) = s(k) /// * Vectors i, j, and s are all the same length. /// * Any elements of s that are zero are ignored. /// * Any elementsof s that have duplicate values of i and j are added together. if (opt == null) { int m = real.ColSize * elemColSize; int n = real.RowSize * elemRowSize; //if(opt == null) { List <int> i = new List <int>(); List <int> j = new List <int>(); List <double> s = new List <double>(); foreach (var c_r_val in real.EnumElements()) { int c = c_r_val.Item1; int r = c_r_val.Item2; Matrix hesscr = c_r_val.Item3; HDebug.Assert(hesscr != null); HDebug.Assert(hesscr.ColSize == elemColSize, hesscr.RowSize == elemRowSize); for (int dc = 0; dc < elemColSize; dc++) { for (int dr = 0; dr < elemRowSize; dr++) { i.Add(c * elemColSize + dc); j.Add(r * elemRowSize + dr); s.Add(hesscr[dc, dr]); } } } //for(int ii=0; ii<i.Count; ii++) //{ // if(i[ii] == j[ii]) // HDebug.Assert(s[ii] != 0); //} PutVector("htlib2_matlab_PutSparseMatrix.i", i.ToArray()); PutVector("htlib2_matlab_PutSparseMatrix.j", j.ToArray()); PutVector("htlib2_matlab_PutSparseMatrix.s", s.ToArray()); } //else //{ // Execute("htlib2_matlab_PutSparseMatrix.i = [];"); // Execute("htlib2_matlab_PutSparseMatrix.j = [];"); // Execute("htlib2_matlab_PutSparseMatrix.s = [];"); // // int maxleng = 10_000_000; // Count = 134217728 (maximum) // List<int > i = new List<int >(maxleng); // List<int > j = new List<int >(maxleng); // List<double> s = new List<double>(maxleng); // foreach(var c_r_val in real.EnumElements()) // { // int c = c_r_val.Item1; // int r = c_r_val.Item2; // Matrix hesscr = c_r_val.Item3; // HDebug.Assert(hesscr != null); // HDebug.Assert(hesscr.ColSize == elemColSize, hesscr.RowSize == elemRowSize); // for(int dc=0; dc<elemColSize; dc++) // for(int dr=0; dr<elemRowSize; dr++) // { // if(i.Count == maxleng) // { // PutVector("htlib2_matlab_PutSparseMatrix.ix", i.ToArray()); // PutVector("htlib2_matlab_PutSparseMatrix.jx", j.ToArray()); // PutVector("htlib2_matlab_PutSparseMatrix.sx", s.ToArray()); // Execute("htlib2_matlab_PutSparseMatrix.i = [htlib2_matlab_PutSparseMatrix.i; htlib2_matlab_PutSparseMatrix.ix];"); // Execute("htlib2_matlab_PutSparseMatrix.j = [htlib2_matlab_PutSparseMatrix.j; htlib2_matlab_PutSparseMatrix.jx];"); // Execute("htlib2_matlab_PutSparseMatrix.s = [htlib2_matlab_PutSparseMatrix.s; htlib2_matlab_PutSparseMatrix.sx];"); // Execute("clear htlib2_matlab_PutSparseMatrix.ix;"); // Execute("clear htlib2_matlab_PutSparseMatrix.jx;"); // Execute("clear htlib2_matlab_PutSparseMatrix.sx;"); // i.Clear(); // j.Clear(); // s.Clear(); // } // i.Add(c*elemColSize+dc); // j.Add(r*elemRowSize+dr); // s.Add(hesscr[dc, dr]); // } // } // if(i.Count != 0) // { // PutVector("htlib2_matlab_PutSparseMatrix.ix", i.ToArray()); // PutVector("htlib2_matlab_PutSparseMatrix.jx", j.ToArray()); // PutVector("htlib2_matlab_PutSparseMatrix.sx", s.ToArray()); // Execute("htlib2_matlab_PutSparseMatrix.i = [htlib2_matlab_PutSparseMatrix.i; htlib2_matlab_PutSparseMatrix.ix];"); // Execute("htlib2_matlab_PutSparseMatrix.j = [htlib2_matlab_PutSparseMatrix.j; htlib2_matlab_PutSparseMatrix.jx];"); // Execute("htlib2_matlab_PutSparseMatrix.s = [htlib2_matlab_PutSparseMatrix.s; htlib2_matlab_PutSparseMatrix.sx];"); // Execute("htlib2_matlab_PutSparseMatrix.ix = [];"); // Execute("htlib2_matlab_PutSparseMatrix.jx = [];"); // Execute("htlib2_matlab_PutSparseMatrix.sx = [];"); // i.Clear(); // j.Clear(); // s.Clear(); // } // HDebug.Assert(i.Count == 0); // HDebug.Assert(j.Count == 0); // HDebug.Assert(s.Count == 0); //} PutValue("htlib2_matlab_PutSparseMatrix.m", m); PutValue("htlib2_matlab_PutSparseMatrix.n", n); Execute("htlib2_matlab_PutSparseMatrix = sparse(htlib2_matlab_PutSparseMatrix.i+1, htlib2_matlab_PutSparseMatrix.j+1, htlib2_matlab_PutSparseMatrix.s, htlib2_matlab_PutSparseMatrix.m, htlib2_matlab_PutSparseMatrix.n);"); Execute(name + " = htlib2_matlab_PutSparseMatrix;"); Execute("clear htlib2_matlab_PutSparseMatrix;"); } else if (opt == "use file") { string i_path = HFile.GetTempPath(_path_temporary, ".dat"); string j_path = HFile.GetTempPath(_path_temporary, ".dat"); string s_path = HFile.GetTempPath(_path_temporary, ".dat"); ulong count = 0; { System.IO.BinaryWriter i_writer = new System.IO.BinaryWriter(new System.IO.FileStream(i_path, System.IO.FileMode.CreateNew)); System.IO.BinaryWriter j_writer = new System.IO.BinaryWriter(new System.IO.FileStream(j_path, System.IO.FileMode.CreateNew)); System.IO.BinaryWriter s_writer = new System.IO.BinaryWriter(new System.IO.FileStream(s_path, System.IO.FileMode.CreateNew)); foreach (var c_r_val in real.EnumElements()) { int c = c_r_val.Item1; int r = c_r_val.Item2; Matrix hesscr = c_r_val.Item3; HDebug.Assert(hesscr != null); HDebug.Assert(hesscr.ColSize == elemColSize, hesscr.RowSize == elemRowSize); for (int dc = 0; dc < elemColSize; dc++) { for (int dr = 0; dr < elemRowSize; dr++) { count++; double i = (c * elemColSize + dc); double j = (r * elemRowSize + dr); double s = (hesscr[dc, dr]); i_writer.Write(i); j_writer.Write(j); s_writer.Write(s); HDebug.Exception(count > 0); } } } i_writer.Flush(); i_writer.Close(); j_writer.Flush(); j_writer.Close(); s_writer.Flush(); s_writer.Close(); } { int m = real.ColSize * elemColSize; int n = real.RowSize * elemRowSize; PutValue("htlib2_matlab_PutSparseMatrix.m", m); PutValue("htlib2_matlab_PutSparseMatrix.n", n); Execute("htlib2_matlab_PutSparseMatrix.ifid=fopen('" + i_path + "','r');"); Execute("htlib2_matlab_PutSparseMatrix.jfid=fopen('" + j_path + "','r');"); Execute("htlib2_matlab_PutSparseMatrix.sfid=fopen('" + s_path + "','r');"); // A = fread(fileID) reads all the data in the file into a vector of class double. By default, fread reads a file 1 byte at a time, interprets each byte as an 8-bit unsigned integer (uint8), and returns a double array. Execute("htlib2_matlab_PutSparseMatrix.imat=fread(htlib2_matlab_PutSparseMatrix.ifid, [" + count + "],'*double')';"); Execute("htlib2_matlab_PutSparseMatrix.jmat=fread(htlib2_matlab_PutSparseMatrix.jfid, [" + count + "],'*double')';"); Execute("htlib2_matlab_PutSparseMatrix.smat=fread(htlib2_matlab_PutSparseMatrix.sfid, [" + count + "],'*double')';"); Execute("fclose(htlib2_matlab_PutSparseMatrix.ifid);"); Execute("fclose(htlib2_matlab_PutSparseMatrix.jfid);"); Execute("fclose(htlib2_matlab_PutSparseMatrix.sfid);"); Execute("htlib2_matlab_PutSparseMatrix = sparse(htlib2_matlab_PutSparseMatrix.imat+1, htlib2_matlab_PutSparseMatrix.jmat+1, htlib2_matlab_PutSparseMatrix.smat, htlib2_matlab_PutSparseMatrix.m, htlib2_matlab_PutSparseMatrix.n);"); Execute(name + " = htlib2_matlab_PutSparseMatrix;"); Execute("clear htlib2_matlab_PutSparseMatrix;"); } HFile.Delete(i_path); HFile.Delete(j_path); HFile.Delete(s_path); } }
public void Release() { file.Close(); HFile.Delete(path); file = null; }
public static int StartAsBatchSilent(Tuple <string, IList <string>, IList <string> > env, List <string> lineStdout, List <string> lineStderr, params string[] commands) { string tempbase = null; IList <string> filesIn = null; IList <string> filesOut = null; if (env != null) { tempbase = env.Item1; HDebug.Assert(tempbase != null); HDebug.Assert(HDirectory.Exists(tempbase)); filesIn = env.Item2; filesOut = env.Item3; } string currdir = HEnvironment.CurrentDirectory; if (tempbase != null) { var dirinfo = HDirectory.CreateTempDirectory(tempbase); HEnvironment.CurrentDirectory = dirinfo.FullName; } if (filesIn != null) { foreach (string file in filesIn) { HFile.Copy(currdir + "\\" + file, file); } } int exitcode; { string tmpbatpath = HFile.GetTempPath("bat"); HFile.WriteAllLines(tmpbatpath, commands); { if (lineStderr == null) { lineStderr = new List <string>(); // collect into garbage storage if they are initially null. } if (lineStdout == null) { lineStdout = new List <string>(); // collect into garbage storage if they are initially null. } // capture error message in console System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = tmpbatpath; proc.StartInfo.RedirectStandardError = (lineStderr != null); proc.StartInfo.RedirectStandardOutput = (lineStdout != null); proc.StartInfo.UseShellExecute = false; proc.Start(); if (lineStderr != null) { lineStderr.Clear(); lineStderr.AddRange(proc.StandardError.ReadToEnd().Replace("\r", "").Split('\n')); } if (lineStdout != null) { lineStdout.Clear(); lineStdout.AddRange(proc.StandardOutput.ReadToEnd().Replace("\r", "").Split('\n')); } proc.WaitForExit(); exitcode = proc.ExitCode; } Thread.Sleep(100); HFile.Delete(tmpbatpath); } if (filesOut != null) { foreach (string file in filesOut) { if (HFile.Exists(file)) { HFile.Copy(file, currdir + "\\" + file); } } } if (HEnvironment.CurrentDirectory != currdir) { string delpath = HEnvironment.CurrentDirectory; HEnvironment.CurrentDirectory = currdir; try{ HDirectory.Delete(delpath, true); } catch (System.IO.IOException) { } } return(exitcode); }