MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/datfiles.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso, Raul Durand                   *
00004  * Copyright (C) 2009 Sergio Galindo                                    *
00005  *                                                                      *
00006  * This program is free software: you can redistribute it and/or modify *
00007  * it under the terms of the GNU General Public License as published by *
00008  * the Free Software Foundation, either version 3 of the License, or    *
00009  * any later version.                                                   *
00010  *                                                                      *
00011  * This program is distributed in the hope that it will be useful,      *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00014  * GNU General Public License for more details.                         *
00015  *                                                                      *
00016  * You should have received a copy of the GNU General Public License    *
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>  *
00018  ************************************************************************/
00019 
00020 #ifndef MECHSYS_DATFILES_H
00021 #define MECHSYS_DATFILES_H
00022 
00023 // STL
00024 #include <cmath>   // for ceil and floor
00025 #include <cfloat>  // for DBL_EPSILON
00026 
00027 // MechSys
00028 #include <mechsys/linalg/matvec.h>
00029 #include <mechsys/util/fatal.h>
00030 #include <mechsys/util/array.h>
00031 #include <mechsys/util/maps.h>
00032 #ifdef USE_WXWIDGETS
00033   #include <mechsys/gui/plotxy.h>
00034   #include <mechsys/gui/common.h>
00035 #endif
00036 
00037 #ifdef USE_WXWIDGETS
00038 class DatFiles : public wxWindow
00039 #else
00040 class DatFiles
00041 #endif
00042 {
00043 public:
00044     // Constructor
00045 #ifdef USE_WXWIDGETS
00046      DatFiles (wxFrame * Parent);
00047     ~DatFiles () { Aui.UnInit(); }
00048 #else
00049     DatFiles () {}
00050 #endif
00051 
00052     // Methods
00053 #ifdef USE_WXWIDGETS
00054     void Read (wxArrayString const & FNames, wxArrayString const & ShortNames); 
00055 #else
00056     void Read (Array<String> const & FNames); 
00057 #endif
00058 
00059     // Data
00060     Array<Array<Vec_t> >  sig;                  
00061     Array<Array<Vec_t> >  eps;                  
00062     Array<Array<double> > ed, ev;               
00063     Array<Array<double> > p, q, t, lp, qp, mqp; 
00064 
00065     // Data corresponding to simulations
00066     Array<Array<double> > ED, EV;      
00067     Array<Array<double> > LP, QP, MQP; 
00068 
00069 #ifdef USE_WXWIDGETS
00070     // Methods
00071     void Sync    () { TransferDataFromWindow(); } 
00072     void ReBuild ();                              
00073 
00074     // Data
00075     wxAuiManager   Aui;    
00076     GUI::PlotXY  * qped;   
00077     GUI::PlotXY  * qpev;   
00078     GUI::PlotXY  * eved;   
00079     GUI::PlotXY  * evlp;   
00080     bool           Multq;  
00081     bool           PltAll; 
00082     wxComboBox   * CbxFNs; 
00083     wxString       LstDir; 
00084 
00085     // Events
00086     void OnLoad    (wxCommandEvent & Event);
00087     void OnReBuild (wxCommandEvent & Event) { ReBuild (); }
00088     DECLARE_EVENT_TABLE()
00089 #endif
00090 };
00091 
00092 
00094 
00095 
00096 #ifdef USE_WXWIDGETS
00097 inline void DatFiles::Read (wxArrayString const & FNames, wxArrayString const & ShortNames)
00098 #else
00099 inline void DatFiles::Read (Array<String> const & FNames)
00100 #endif
00101 {
00102     // resize data arrays
00103     size_t nfiles = FNames.size();
00104     sig.Resize (nfiles);
00105     eps.Resize (nfiles);
00106     ed .Resize (nfiles);
00107     ev .Resize (nfiles);
00108     p  .Resize (nfiles);
00109     q  .Resize (nfiles);
00110     t  .Resize (nfiles);
00111     lp .Resize (nfiles);
00112     qp .Resize (nfiles);
00113     mqp.Resize (nfiles);
00114 
00115     // load data
00116     for (size_t i=0; i<FNames.size(); ++i)
00117     {
00118         // read tabular data
00119         Table dat;
00120         dat.Read (FNames[i].ToStdString().c_str());
00121         size_t nrow    = 0;
00122         bool   has_sxy = false;
00123         bool   has_syz = false;
00124         bool   has_szx = false;
00125         bool   has_exy = false;
00126         bool   has_eyz = false;
00127         bool   has_ezx = false;
00128         Array<String> skeys(6);
00129         Array<String> ekeys(6);
00130         double m = 1.0; // multiplier for sig
00131         double n = 1.0; // multiplier for eps to convert to %
00132         for (size_t j=0; j<dat.Keys.Size(); ++j)
00133         {
00134             if      (dat.Keys[j]=="Sx")  { skeys="Sx","Sy","Sz","Sxy","Syz","Szx"; ekeys="Ex","Ey","Ez","Exy","Eyz","Ezx";  m=-1.0;  nrow=dat("Sx").Size(); } // old_data_file_xyz
00135             else if (dat.Keys[j]=="Sa")  { skeys="Sr","St","Sa","Srt","Sat","Sar"; ekeys="Er","Et","Ea","Ert","Eat","Ear";  m=-1.0;  nrow=dat("Sa").Size(); } // old_data_file_art
00136             else if (dat.Keys[j]=="sx")  { skeys="sx","sy","sz","sxy","syz","szx"; ekeys="ex","ey","ez","exy","eyz","ezx";  n=100.0; nrow=dat("sx").Size(); } // new_data_file_xyz
00137             else if (dat.Keys[j]=="Sxy" || dat.Keys[j]=="Srt" || dat.Keys[j]=="sxy") has_sxy = true;
00138             else if (dat.Keys[j]=="Syz" || dat.Keys[j]=="Sat" || dat.Keys[j]=="syz") has_syz = true;
00139             else if (dat.Keys[j]=="Szx" || dat.Keys[j]=="Sar" || dat.Keys[j]=="szx") has_szx = true;
00140             else if (dat.Keys[j]=="Exy" || dat.Keys[j]=="Ert" || dat.Keys[j]=="exy") has_exy = true;
00141             else if (dat.Keys[j]=="Eyz" || dat.Keys[j]=="Eat" || dat.Keys[j]=="eyz") has_eyz = true;
00142             else if (dat.Keys[j]=="Ezx" || dat.Keys[j]=="Ear" || dat.Keys[j]=="ezx") has_ezx = true;
00143         }
00144         if (nrow==0) throw new Fatal("DatFiles::OnLoad: Could not find (sx,sy,sz,sxy) columns in file %s",FNames[i].ToStdString().c_str());
00145 
00146         // resize sub arrays
00147         sig[i].Resize (nrow);
00148         eps[i].Resize (nrow);
00149         ed [i].Resize (nrow);
00150         ev [i].Resize (nrow);
00151         p  [i].Resize (nrow);
00152         q  [i].Resize (nrow);
00153         t  [i].Resize (nrow);
00154         lp [i].Resize (nrow);
00155         qp [i].Resize (nrow);
00156         mqp[i].Resize (nrow);
00157 
00158         // calculate invariants
00159         for (size_t j=0; j<nrow; ++j)
00160         {
00161             sig[i][j].change_dim (6);
00162             sig[i][j] = m*dat(skeys[0])[j], m*dat(skeys[1])[j], m*dat(skeys[2])[j],
00163                         (has_sxy ? m*dat(skeys[3])[j]*sqrt(2.0) : 0.0),
00164                         (has_syz ? m*dat(skeys[4])[j]*sqrt(2.0) : 0.0),
00165                         (has_szx ? m*dat(skeys[5])[j]*sqrt(2.0) : 0.0);
00166 
00167             eps[i][j].change_dim (6);
00168             eps[i][j] = m*n*dat(ekeys[0])[j], m*n*dat(ekeys[1])[j], m*n*dat(ekeys[2])[j],
00169                         (has_exy ? m*n*dat(ekeys[3])[j]*sqrt(2.0) : 0.0),
00170                         (has_eyz ? m*n*dat(ekeys[4])[j]*sqrt(2.0) : 0.0),
00171                         (has_ezx ? m*n*dat(ekeys[5])[j]*sqrt(2.0) : 0.0);
00172 
00173             OctInvs (sig[i][j], p[i][j], q[i][j], t[i][j]);
00174             ev [i][j] = Calc_evoct (eps[i][j]);
00175             ed [i][j] = Calc_edoct (eps[i][j]);
00176             lp [i][j] = log(p[i][j]);
00177             qp [i][j] = q[i][j]/p[i][j];
00178             mqp[i][j] = (t[i][j]<0.0 ? -qp[i][j] : qp[i][j]);
00179         }
00180     }
00181 
00182 #ifdef USE_WXWIDGETS
00183     // refresh cbx => replot
00184     CbxFNs->Set          (ShortNames);
00185     CbxFNs->SetSelection (0);
00186 #endif
00187 }
00188 
00189 #ifdef USE_WXWIDGETS
00190 
00191 enum
00192 {
00193     ID_LOAD = wxID_HIGHEST+1,
00194     ID_SELDATA,
00195     ID_MULTQ,
00196     ID_PLTALL,
00197 };
00198 
00199 BEGIN_EVENT_TABLE(DatFiles, wxWindow)
00200     EVT_BUTTON   (ID_LOAD,    DatFiles::OnLoad)
00201     EVT_COMBOBOX (ID_SELDATA, DatFiles::OnReBuild)
00202     EVT_CHECKBOX (ID_MULTQ,   DatFiles::OnReBuild)
00203     EVT_CHECKBOX (ID_PLTALL,  DatFiles::OnReBuild)
00204 END_EVENT_TABLE()
00205 
00206 inline DatFiles::DatFiles (wxFrame * Parent)
00207     : wxWindow (Parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE),
00208       Multq  (true),
00209       PltAll (false)
00210 {
00211     // force validation of child controls
00212     SetExtraStyle (wxWS_EX_VALIDATE_RECURSIVELY);
00213 
00214     // tell wxAuiManager to manage this frame
00215     Aui.SetManagedWindow (this);
00216 
00217     // plots
00218     qped = new GUI::PlotXY (this, "Deviatoric stress/strains",             "ed [%]", "q/p");
00219     qpev = new GUI::PlotXY (this, "Deviatoric stress - volumetric strain", "ev [%]", "q/p");
00220     eved = new GUI::PlotXY (this, "Volumetric strain - deviatoric strain", "ed [%]", "ev [%]");
00221     evlp = new GUI::PlotXY (this, "Volumetric strain - natural log (p)",   "ln(p)",  "ev [%]");
00222     qped->ShowLastY = false;
00223     qpev->ShowLastY = false;
00224     eved->ShowLastY = false;
00225     evlp->ShowLastY = false;
00226 
00227     // control panel
00228     ADD_WXPANEL    (pnl, szt, sz, 1, 4);
00229     ADD_WXBUTTON   (pnl, sz, ID_LOAD,    c0,     "Load data");
00230     ADD_WXCOMBOBOX (pnl, sz, ID_SELDATA, CbxFNs, "Data files");
00231     ADD_WXCHECKBOX (pnl, sz, ID_MULTQ,   c2,     "Lode multiply q", Multq);
00232     ADD_WXCHECKBOX (pnl, sz, ID_PLTALL,  c3,     "Plot all",        PltAll);
00233     CbxFNs->SetMinSize (wxSize(200,20));
00234 
00235     // commit all changes to wxAuiManager
00236     Aui.AddPane (pnl,  wxAuiPaneInfo().Name("cpnl").Caption("Data").Top   ().MinSize(wxSize(100,50)) .DestroyOnClose(false).CaptionVisible(true) .CloseButton(false));
00237     Aui.AddPane (qped, wxAuiPaneInfo().Name("qped").Caption("qped").Centre().Position(0)             .DestroyOnClose(false).CaptionVisible(false).CloseButton(false));
00238     Aui.AddPane (eved, wxAuiPaneInfo().Name("eved").Caption("eved").Centre().Position(1)             .DestroyOnClose(false).CaptionVisible(false).CloseButton(false));
00239     Aui.AddPane (evlp, wxAuiPaneInfo().Name("evlp").Caption("evlp").Right ().MinSize(wxSize(200,100)).DestroyOnClose(false).CaptionVisible(false).CloseButton(false));
00240     Aui.AddPane (qpev, wxAuiPaneInfo().Name("qpev").Caption("qpev").Right ().MinSize(wxSize(200,100)).DestroyOnClose(false).CaptionVisible(false).CloseButton(false));
00241     Aui.Update  ();
00242 
00243 }
00244 
00245 inline void DatFiles::ReBuild ()
00246 {
00247     // update control's data
00248     Sync ();
00249 
00250     // filenames
00251     wxArrayString fnames = CbxFNs->GetStrings ();
00252 
00253     // disconnect plots
00254     qped->DelCurves ();
00255     qpev->DelCurves ();
00256     eved->DelCurves ();
00257     evlp->DelCurves ();
00258 
00259     // reconnect plots
00260     bool   mul = Multq;
00261     bool   all = PltAll;
00262     size_t ini = (all ? 0         : CbxFNs->GetSelection());
00263     size_t num = (all ? ed.Size() : ini+1);
00264     for (size_t i=ini; i<num; ++i)
00265     {
00266         // data
00267         GUI::CurveProps & c0 = qped->AddCurve (&ed[i], (mul ? &mqp[i] : &qp[i]), fnames[i].ToStdString().c_str());
00268         GUI::CurveProps & c1 = qpev->AddCurve (&ev[i], (mul ? &mqp[i] : &qp[i]), fnames[i].ToStdString().c_str());
00269         GUI::CurveProps & c2 = eved->AddCurve (&ed[i],                  &ev[i],  fnames[i].ToStdString().c_str());
00270         GUI::CurveProps & c3 = evlp->AddCurve (&lp[i],                  &ev[i],  fnames[i].ToStdString().c_str());
00271         c0.Typ=GUI::CT_BOTH;  c0.Psz=4;  c0.Pen.Set((all ? GUI::LinClr(i) : "black"), (i>5 ? "dash" : "solid"), 1);
00272         c1.Typ=GUI::CT_BOTH;  c1.Psz=4;  c1.Pen.Set((all ? GUI::LinClr(i) : "black"), (i>5 ? "dash" : "solid"), 1);
00273         c2.Typ=GUI::CT_BOTH;  c2.Psz=4;  c2.Pen.Set((all ? GUI::LinClr(i) : "black"), (i>5 ? "dash" : "solid"), 1);
00274         c3.Typ=GUI::CT_BOTH;  c3.Psz=4;  c3.Pen.Set((all ? GUI::LinClr(i) : "black"), (i>5 ? "dash" : "solid"), 1);
00275 
00276         // simulations
00277         if (ED.Size()>i)
00278         {
00279             wxString buf;  buf.Printf("sim:%s",fnames[i].ToStdString().c_str());
00280             GUI::CurveProps & C0 = qped->AddCurve (&ED[i], (mul ? &MQP[i] : &QP[i]), buf.ToStdString().c_str());
00281             GUI::CurveProps & C1 = qpev->AddCurve (&EV[i], (mul ? &MQP[i] : &QP[i]), buf.ToStdString().c_str());
00282             GUI::CurveProps & C2 = eved->AddCurve (&ED[i],                  &EV[i],  buf.ToStdString().c_str());
00283             GUI::CurveProps & C3 = evlp->AddCurve (&LP[i],                  &EV[i],  buf.ToStdString().c_str());
00284             C0.Typ=GUI::CT_LINES;  C0.Pen.Set((all ? GUI::LinClr(i) : "red"), (i>5 ? "dash" : "solid"), 1);
00285             C1.Typ=GUI::CT_LINES;  C1.Pen.Set((all ? GUI::LinClr(i) : "red"), (i>5 ? "dash" : "solid"), 1);
00286             C2.Typ=GUI::CT_LINES;  C2.Pen.Set((all ? GUI::LinClr(i) : "red"), (i>5 ? "dash" : "solid"), 1);
00287             C3.Typ=GUI::CT_LINES;  C3.Pen.Set((all ? GUI::LinClr(i) : "red"), (i>5 ? "dash" : "solid"), 1);
00288         }
00289     }
00290 
00291     // redraw
00292     qped->Redraw ();
00293     qpev->Redraw ();
00294     eved->Redraw ();
00295     evlp->Redraw ();
00296 }
00297 
00298 inline void DatFiles::OnLoad (wxCommandEvent & Event)
00299 {
00300     wxFileDialog fd(this, "Select data files (.dat)", LstDir, "", "*.dat", wxFD_MULTIPLE);
00301     if (fd.ShowModal()==wxID_OK)
00302     {
00303         // get filenames
00304         wxArrayString paths, fnames;
00305         fd.GetPaths     (paths);
00306         fd.GetFilenames (fnames);
00307 
00308         // disconnect plots
00309         qped->DelCurves ();
00310         qpev->DelCurves ();
00311         eved->DelCurves ();
00312         evlp->DelCurves ();
00313         CbxFNs->Clear   ();
00314 
00315         // load data
00316         LstDir = fd.GetDirectory ();
00317         try { Read (paths, fnames); }
00318         catch (Fatal * e) { WxError(e->Msg().CStr()); }
00319     }
00320 }
00321 
00322 #endif // USE_WXWIDGETS
00323 
00324 #endif // MECHSYS_DATFILES_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines