General Mechanical

General Mechanical

Topics related to Mechanical Enterprise, Motion, Additive Print and more.

How to write an array to text file ?

    • Md_Salem
      Subscriber

      Hi evey one

      I used the next code to extract the potential energy of harmonic analysis (10 steps) for some parts of structure simulated with element 185 . 

      *DIM,energy_array,ARRAY,10
      /POST1
      i=1
      *DO, step, 1,19, 2 
         SET,,,,,,,STEP       ! store results for set step
         ESEL,S,ENAME,,185
         ETABLE,ENERGY,SENE
         SSUM
         
         *GET,my_energy,SSUM,,ITEM,ENERGY
         energy_array(i)= my_energy
         
         *CFOPEN,ENERGIES,txt,C:\Users\MD\Desktop
        *VWRITE,energy_array
        %(F18.10)
        *CFCLOS
         
         ETABLE,CLEAR
         
         
         ALLSEL
         i=i+1
      *ENDDO

      Unfortunately the file ENERGIES.txt is created but empty !!!

      Also I need to set parameter "my_energy" to record the highst value of the harmonic steps rather than recording last step as this code do.

      would any one help me with that code ?

      Regards

    • Md_Salem
      Subscriber

      I solved the first problem by using *MWRITE command after the DO loop and it worked properly.

      Still the second problem for me : how to set parameter “my_energy” to the highest value of the steps :


      *DIM,energy_array,ARRAY,10
      /POST1
      i=1

      *DO, step, 1,19, 2
      set,,,,,,,step ! store results for set step
      ESEL,S,ENAME,,185
      etable,energy,sene
      ssum

      *get,my_energy,ssum,,ITEM,energy
      energy_array(i)= my_energy
      ETABLE,CLEAR
      ALLSEL
      i=i+1
      *ENDDO

      *MWRITE,energy_array,C:\Users\MD\Desktop\ENERGIES,TXT
      (F18.10)

      Regads

    • Chandra Sekaran
      Ansys Employee

      1) On issue 1 I think *VWRITE should also work if you use the starting index number like below. Also I am notsure what the % sign is on the format so I removed it.

         *CFOPEN,ENERGIES,txt,C:\Users\MD\Desktop
        *VWRITE,energy_array(1)
        (F18.10)
        *CFCLOS

      2) I am not sure what you mean by "the highst value of the harmonic steps rather than recording last step as this code do." Let us say you requested harmonic excitation frequency between 10 and 15 Hz and asked for 5 equally spaced steps. You will get 5 solutions at 11,12,13,14,15 Hz. Can you explain your question in this scenario?

       

      • Md_Salem
        Subscriber
        Hi Chandra,
         
        I apologise for any confusion.
         
        I mean that the parameter "my_energy" continues changing at each step in the displayed *DO loop. However, it retains the value of the last step of the SENE energy. I need the parameter "my_energy" to hold the maximum value over *DO loop steps.
         
        In other words, assuming the values of "my_energy" at the five steps you provided are 1.1, 1.2, 1.3, 1.1, and 0.9, respectively, the final value of "my_energy" in the supplied code should be 0.9. I require the maximum value, which is "1.3" I hope that makes sense.
        Regards
    • Claudio Pedrazzi
      Subscriber

      Concerning the question about getting the maximum value, if I understand correctly, the problem is that everytime you go through the loop, you overwrite my_energy.  I would simply use an additional storage variable like max_my_energy and initialise it to 0 before the loop begins, then in every step you should simply compare my_energy with the maximum until there, and if higher, substitute it.  

      *IF,my_energy,GT,max_my_energy,THEN

      max_my_energy=my_energy

      *ENDIF

    • Chandra Sekaran
      Ansys Employee

      Oh I see. I did not catch on to what you meant. Yes, the above should work.

    • Chandra Sekaran
      Ansys Employee

      If you have the array energy_array filled then you can also use (outside the loop) "*VSCFUN,maxval,MAX,energy_array(1) " . This will return the maximum value and store it in APDL parameter maxval.

Viewing 5 reply threads
  • The topic ‘How to write an array to text file ?’ is closed to new replies.